xcoder123 / MAX30100

Driver for MAX30100 using arduino
MIT License
105 stars 32 forks source link

Problem with sum getting undefined value for the DiffMean filter #8

Open bbogdanmircea opened 4 years ago

bbogdanmircea commented 4 years ago

First congrats for the tutorial and the explanations, I think your explanation and implementation is the best relating Oximeter functionality. I still don't understand fully all the filters and signal processing that you used but with time maybe I will get it.

I forked your code in order to use it with my 30102 board and an ESP32 based board, I kind of massacred your driver part to make it work with my 30102 board. As a suggestion it would have been much nicer if you could have split the driver and the DSP functionality, so that different drivers could be used to get the values from the sensor and then the DSP could process this independently. With you permission and copyright I will try to make this so I can use some driver that is better written with your processing part.

I had a big problem when debugging, as the sum inside the meanDiff filter and the following butterworth filter was showing inf values when debugging, and I couldn't understand why, as the raw values from the sensor were OK.

After a lot of head banging, I found the cause to be that before the array is filled for the first time, you subtract from the sum the first 15 values, but only the first value is initialized, so for the other values is subtracting some random memory value which is huge and messes up the whole calculation. I think I fixed this by simply not subtracting until the buffer is filled for the first time:

So this is not an issue as I already fixed it:

`float MAX30100::meanDiff(float M, meanDiffFilter_t* filterValues) { float avg = 0;

Serial.print("sum_before = "); Serial.print(filterValues->sum); if (filterValues->count == MEAN_FILTER_SIZE) { filterValues->sum -= filterValues->values[filterValues->index]; } Serial.print(" sum- = "); Serial.print(filterValues->sum); filterValues->values[filterValues->index] = (int32_t)M; Serial.print(" value = "); Serial.print(filterValues->values[filterValues->index]);
filterValues->sum += filterValues->values[filterValues->index]; Serial.print(" sum+ = "); Serial.println(filterValues->sum);

filterValues->index++; filterValues->index = filterValues->index % MEAN_FILTER_SIZE;

if(filterValues->count < MEAN_FILTER_SIZE) filterValues->count++;

avg = (float)filterValues->sum / filterValues->count;

ifdef FLEXIPLOT_DRIVER

Serial.print("{P3|meanDiffValue|0,0,255|"); Serial.print(filterValues->values[filterValues->index]); Serial.print("|meanDiffIndex|255,0,0|"); Serial.print(filterValues->index); Serial.print("|meanDiffCount|255,0,0|"); Serial.print(filterValues->count); Serial.print("|meanDiffSum|255,0,0|"); Serial.print(filterValues->sum); Serial.print("|meanDiffAverage|255,0,0|"); Serial.print(avg); Serial.print("|meanDiffReturn|255,0,0|"); Serial.print(avg-M); Serial.println("}");

endif

return avg - M; }`