private void ProcessPacket(OSCPacket packet) { foreach (OSCPacket p in packet.Data) { if (p.IsBundle()) { ProcessPacket(p); } else { OSCMessage msg = (OSCMessage)p; if ((EyesStatus)msg.Extra != EyesStatus.NONE) { Status = (EyesStatus)msg.Extra; } if (msg.Address == "/muse/elements/alpha_absolute") { if (!Training || (Training && alphaIgnore == 0 && keepTrainingData)) { if (alphaSet) { alpha2 = (float)msg.Data[0]; alphaSet = false; if (prevStatus == Status) { TrainingValue trainingValue = new TrainingValue((int)Status, 2); trainingValue.Features[0] = alpha1; trainingValue.Features[1] = alpha2; AlphaTrainingValues.Add(trainingValue); } } else { alpha1 = (float)msg.Data[0]; alphaSet = true; } } else if (Training && alphaIgnore != 0) { alphaIgnore--; } prevStatus = Status; } else if (msg.Address == "/muse/eeg") { tp9.Add((float)msg.Data[0]); af7.Add((float)msg.Data[1]); af8.Add((float)msg.Data[2]); tp10.Add((float)msg.Data[3]); } } } }
private void Predict() { TrainingValue tmp = new TrainingValue(); tmp.Features = new double[FEATURE_COUNT]; FillTrainingValue(ref tmp, fftResults); int result = trainer.Predict(tmp); predictedMuscleState = (MuscleState)result; }
private void Train() { if (skipsRemaining > 0) { skipsRemaining--; return; } TrainingValue value = new TrainingValue((int)currentMuscleState, FEATURE_COUNT); FillTrainingValue(ref value, fftResults); trainingData.Add(value); }
public static void FillTrainingValue(ref TrainingValue value, List <Complex> fftResults) { float freqRange = HIGHER_FREQ - LOWER_FREQ; float freqStep = freqRange / FEATURE_COUNT; float lower = LOWER_FREQ; float higher = lower + freqStep; for (int i = 0; i < FEATURE_COUNT; i++) { value.Features[i] = PSD(fftResults, EMGPacket.SAMPLE_RATE, lower, higher); lower += freqStep; higher += freqStep; } }
internal static List <TrainingValue> GetTrainingValues(List <EMGPacket> packets, bool enableSkip) { List <TrainingValue> values = new List <TrainingValue>(EMGProcessor.FFT_SAMPLE_SIZE); int skipsRemaining = 0; for (int i = 0; i < packets.Count / EMGProcessor.FFT_SAMPLE_SIZE; i++) { if (enableSkip && skipsRemaining > 0) { skipsRemaining--; continue; } Complex[] data = new Complex[EMGProcessor.FFT_SAMPLE_SIZE]; int start = i * EMGProcessor.FFT_SAMPLE_SIZE; int end = start + EMGProcessor.FFT_SAMPLE_SIZE; MuscleState startMuscleState = packets[start].muscleStateHint; for (int j = start; j < end; j++) { EMGPacket packet = packets[j]; if (packet.muscleStateHint != startMuscleState) { skipsRemaining += EMGProcessor.SKIPS_AFTER_TRANSITION; break; } data[j - start] = new Complex(EMGProcessor.ValueFromPacket(packet), 0); } if (enableSkip && skipsRemaining > 0) { continue; } FourierTransform.FFT(data, FourierTransform.Direction.Forward); List <Complex> fftResults = new List <Complex>(data); TrainingValue trainingValue = new TrainingValue((int)packets[start].muscleStateHint, EMGProcessor.FEATURE_COUNT); EMGProcessor.FillTrainingValue(ref trainingValue, fftResults); values.Add(trainingValue); } return(values); }
private void RunFFT() { readingsMean.Clear(); for (int i = 0; i < af7.Count; i++) { readingsMean.Add(af7[i] + tp9[i] + tp10[i] + af8[i] / FEATURE_COUNT); } CalculateFFT(readingsMean, FFTResults); CalculateFFT(af7, AF7FFT); CalculateFFT(af8, AF8FFT); CalculateFFT(tp9, TP9FFT); CalculateFFT(tp10, TP10FFT); if (!Training || (Training && ignore == 0)) { TrainingValue trainingValue = new TrainingValue((int)Status, FEATURE_COUNT); trainingValue.Features[0] = PSD(TP9FFT, FREQ_STEP); trainingValue.Features[1] = PSD(AF7FFT, FREQ_STEP); trainingValue.Features[2] = PSD(AF8FFT, FREQ_STEP); trainingValue.Features[3] = PSD(TP10FFT, FREQ_STEP); if (!Training && trainer != null && trainer.Trained) { Status = (EyesStatus)trainer.Predict(trainingValue); } if (training || keepTrainingData) { TrainingValues.Add(trainingValue); } } else if (Training && ignore != 0) { ignore--; } }
public int Predict(TrainingValue val) { if (!Trained) { throw new Exception("Train must be called first!"); } switch (type) { case ClassifierType.DecisionTree: return(tree.Decide(val.Features)); case ClassifierType.LDA: return(pipeline.Decide(val.Features)); case ClassifierType.SVM: return(Convert.ToInt32(svm.Decide(val.Features))); case ClassifierType.Bayes: return(bayes.Decide(val.Features)); } return(-1); }
private string csvLineFromTrainingValue(TrainingValue val) { return(string.Format("{0},{1},{2}", val.Features[0], val.Features[1], (int)val.State)); }