Reading EMG signals from analogRead

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.

Just by way of information I am using an Arduino UNO attached to backyard brains muscle SpikerShield Pro - Muscle SpikerShield Pro

The SpikerShield Pro has pins that the website states can be used to control robots exc, I have the pin from the SpikerShield going to my arduino Pro micro. The arduino pro micro is the one that I am trying to do these calculations with.

The spiker shield outputs a raw EMG signal. You need the EMG envelope to control a servo. To do the conversion, you'll need to first remove the DC offset from the signal, then rectify and integrate it.

The spiker documentation should provide the DC offset value. Simply convert this to ADC values (adcOffset = offset_voltage / 5V * 1023) and subtract that from the value you get from analogRead().

emgNoOffset = analogRead(sensorPin) - adcOffset;

To rectify it, get the absolute value.

emgRectified = abs(emgNoOffset);

The last part is the tricky part, you need to send your signal through a integration (aka low pass filter
or smoothing) algorithm. Although not a perfect example, you could look at the Arduino Smoothing example to do this. This example however only averages the signal instead of applying a filter that you could fine tune but it should still get you a signal that should be more manageable for servo control.

Alternatively, our MyoWare sensor has all this built in and outputs the EMG envelope which you can use to control a servo without all this extra processing.

Actually, it looks like the spiker pro can output the EMG envelope now.

Are your switches in the Envelope position?
Are you using the Arduino code they provide to get the EMG envelope?
Or the servo tutorial? Experiment: Controlling the Claw