I am currently trying to process EMG signals in order to power a servo motor. The problem is since EMG signals are so all over the place I need to be able to make calculations with the most recent data that is coming in. What I need to do is create an array from the data that is coming in and then make calculations using that data and send signals to the servo accordingly. I anticipate the best way of doing this is by taking the standard deviation of the data.
In short I need a real time updating array of data from which I can get updated values of the standard deviation. I have found a few things online regarding how to make an array of data and also take the standard deviation but seem to be having a hard time putting it all together.
const unsigned int numReadings = 20;
unsigned int analogVals[numReadings];
unsigned int i = 0;
/*
* Get the mean from an array of ints
*/
float getMean(int * val, int numReadings) {
long total = 0;
for (int i = 0; i < numReadings; i++) {
total = total + val[i];
}
float avg = total/(float)numReadings;
return avg;
}
/*
* Get the standard deviation from an array of ints
*/
float getStdDev(int * val, int numReadings) {
float avg = getMean(val, numReadings);
long total = 0;
for (int i = 0; i < numReadings; i++) {
total = total + (val[i] - avg) * (val[i] - avg);
}
float variance = total/(float)numReadings;
float stdDev = sqrt(variance);
return stdDev;
}
void setup()
{
}
void loop()
{
analogVals[i] = analogRead(A0);
i++;
if (i>=numReadings)
{
i=0;
}
float std = getStdDev(analogVals, numReadings);
Serial.print("Standard deviation: ");
Serial.println(std);
}
this code above is honestly me just copying and pasting things I found online and trying to put it all together correctly. This is also my first time dealing with arrays.
I appreciate any help I can get. I have a program to run this in Matlab but I am trying to miniaturize what I have.