Hey there,
I am trying to use your library implementation of the PeakDetection algorithm and I have to make some modifications for my needs. When trying to understand the code I stumbled upon your implementation of the getStd() method.
As far as I understand the algorithm this should just collect the sum of squared differences between stored values and the average value and then divide it by the amount of values to get the average squared difference of the values to the mean.
The square root of this is then returned as the average difference of the signal to the mean.
Other peoples code looks like this, and is easily understandable for me:
https://stackoverflow.com/questions/22583391/peak-signal-detection-in-realtime-timeseries-data/54507140#54507140 and I can understand it really well...
float stddev(float data[], int len) {
float the_mean = mean(data, len);
float standardDeviation = 0.0;
int i;
for(i=0; i<len; ++i) {
standardDeviation += pow(data[i] - the_mean, 2);
}
return sqrt(standardDeviation/len);
}
Your implementation goes like this:
double PeakDetection::getStd(int start, int len) {
double x1 = getAvg(start, len);
double x2 = getPoint(start, len);
double powx1 = x1 * x1;
double std = x2 - powx1;
if (std > -EPSILON && std < EPSILON)
return 0.0;
else
return sqrt(x2 - powx1);
}
and I really dont understand it. What is this getPoint() method doing?
double PeakDetection::getPoint(int start, int len) {
double xi = 0.0;
for (int i = 0; i < len; ++i)
xi += data[(start + i) % lag] * data[(start + i) % lag];
return xi / len;
}
If i understand correctly it returns the average squared values of all collected values. I don't get why this is necessary. Can you help me to understand this?
thank you for any help on this,
paul
__ EDIT:
Sorry, I just recalculated it with some test data and I am surprised your function gave the right result. Seems like a mathematical trick I would like to understand...
This is some test scenario with caluclations as I assume them:
testdata = [ 1, 7, 4, 2]
avg -> 14/4 = 3,5
stdDeviation -> -2.5, 3,5, 0.5, -1,5 -> 6.25 + 12,25 + 0,25 + 2,25 = 5,25
root(5,25) => 2,29128784747792
Your code results in :
x1 = 3,5
x2 -> 1 + 49 + 16 + 4 -> 17,5
powx1 = 12,25
std -> 17,5 - 12,25 = 5,25
root(5,25) => 2,29128784747792