I am using particle photon to read the data from GSR sensor. I want to add a median filter and a mean filter in my code to remove the frequency noise and the rapid-transient noise. Then use the data with less noise to find the suddenly changed gap. However, I am new in particle photon and Arduino. I found some example code about the median filter and mean filter, but I don’t know how to combine them in my code. Does anyone know how to do that? or if there is a library to do that? Thank you in advance.
The following is the example code of the median filter and mean filter. (Arduino code)
int Filter_Value; // median filter
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
Filter_Value = Filter();
Serial.println(Filter_Value);
delay(50);
}
int Get_AD() {
return random(295, 305);
}
#define FILTER_N 101
int Filter() {
int filter_buf[FILTER_N];
int i, j;
int filter_temp;
for(i = 0; i < FILTER_N; i++) {
filter_buf[i] = Get_AD();
delay(1);
}
for(j = 0; j < FILTER_N - 1; j++) {
for(i = 0; i < FILTER_N - 1 - j; i++) {
if(filter_buf[i] > filter_buf[i + 1]) {
filter_temp = filter_buf[i];
filter_buf[i] = filter_buf[i + 1];
filter_buf[i + 1] = filter_temp;
}
}
}
return filter_buf[(FILTER_N - 1) / 2];
}
int Filter_Value; // mean filter
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
Filter_Value = Filter();
Serial.println(Filter_Value);
delay(50);
}
int Get_AD() {
return random(295, 305);
}
#define FILTER_N 12
int Filter() {
int i;
int filter_sum = 0;
for(i = 0; i < FILTER_N; i++) {
filter_sum += Get_AD();
delay(1);
}
return (int)(filter_sum / FILTER_N);
}
The following part is my current code:
int gsr = A2;
int gsrV, gsrV2;
void setup(){
pinMode(gsr , INPUT);
}
void loop(){
gsrV = analogRead(gsr);
delay(1000);
gsrV2 = analogRead(gsr);
int temp = abs(gsrV - gsrV2);
}
In my code, “gsrV” and “gsrV2” are the raw data. Is that possible to create a function with median filter and mean filter to remove the noise in raw data first, then trasnfer these value to “gsrV” and “gsrV2” ?