Hello All,
I'm fairly new to Arduino but I do have at least some experience programming in general. That being said, I am working on a peak detection program based on the algorithm provided here. A search of this forum seems to indicate a similar library has already been created but I still would like to work on it from scratch. The actual issue that I'm currently dealing with is while going through the code to debug, I've noticed that some of the values the Arduino prints are incorrect. Specifically, when I'm trying to print the value of an array (to see what is actually inside) using the following code:
avgFilter[lag] = getMean(filteredY, lag);
stdFilter[lag] = getStdDev(filteredY, lag);
for (int x = 0; x <= lag; x++)
{
Serial.println(avgFilter[5]);
Serial.println(x);
Serial.println(avgFilter[x]);
}
The output is as follows:
If you notice on the last few lines of the output it is essentially saying that the value of the array avgFilter[5] is equal to 1, which is correct. It then goes on to say that the counter x is equal to 5 but for some reason, when that counter is 5, the value of avgFilter[x] is equal to 0???
I'm sure it is a simple error on my part, but I'm at my wits end trying to determine the cause. Any and all help is certainly appreciated. Also, here is the rest of my code for reference:
#include <Arduino.h>
float getMean(float val[], int arrayCount)
{
float total = 0;
for (int i = 0; i < arrayCount; i++)
{
total = total + val[i];
}
float avg = total/(float)arrayCount;
return avg;
}
float getStdDev(float val[], int arrayCount)
{
float avg = getMean(val, arrayCount);
float total = 0;
for (int i = 0; i < arrayCount; i++)
{
total = total + (val[i] - avg) * (val[i] - avg);
}
float variance = total/(float)arrayCount;
float stdDev = sqrt(variance);
return stdDev;
}
void setup()
{
Serial.begin(9600);
int sizeOfSignal = 74;
float y[sizeOfSignal] = {1,1,1.1,1,0.9,1,1,1.1,1,0.9,1,1.1,1,1,0.9,1,1,1.1,1,1,1,1,1.1,0.9,1,1.1,1,1,0.9,
1,1.1,1,1,1.1,1,0.8,0.9,1,1.2,0.9,1,1,1.1,1.2,1,1.5,1,3,2,5,3,2,1,1,1,0.9,1,1,3,
2.6,4,3,3.2,2,1,1,0.8,4,4,2,2.5,1,1,1};
int lag = 5;
float threshold = 3.5;
float influence = 0.5;
float signal[sizeOfSignal];
float filteredY[lag];
for (int i = 0; i <= lag; i = i + 1)
{
filteredY[i] = y[i];
}
float avgFilter[lag] = {0};
float stdFilter[lag] = {0};
avgFilter[lag] = getMean(filteredY, lag);
stdFilter[lag] = getStdDev(filteredY, lag);
for (int x = 0; x <= lag; x++)
{
Serial.println(avgFilter[5]);
Serial.println(x);
Serial.println(avgFilter[x]);
}
for (int x = lag+1; x <= sizeOfSignal; x = x + 1)
{
if(abs(y[x] - avgFilter[x-1]) > threshold*stdFilter[x-1])
{
if (y[x] > avgFilter[x-1])
{
signal[x] = 1;
}
else
{
signal[x] = -1;
}
}
else
{
signal[x] = 0;
filteredY[x] = y[x];
}
filteredY[x] = influence*y[x] + (1-influence)*filteredY[x-1];
float avgHolderArray[lag-1];
for (int k = 0; k <= lag-1; k = k + 1)
{
avgHolderArray[k] = filteredY[x-lag+1];
}
float stdHolderArray[lag-1];
for (int m = 0; m <= lag-1; m = m + 1)
{
stdHolderArray[m] = filteredY[m-lag+1];
}
avgFilter[x] = getMean(avgHolderArray,(lag-1));
stdFilter[x] = getStdDev(stdHolderArray,(lag-1));
}
}
void loop() {
// put your main code here, to run repeatedly:
}