Something wrong with method
in
Programming Questions
•
3 years ago
I wrote a method to filter some data:
- void filterData(float[] filteredData, float[] yVals, int filterThreshold) {
- for (int i=1; i<yVals.length-1; i++) {
- float average = 0.0;
- float runningTotal = 0.0;
- int sampleCount = 0;
- while (abs(yVals[i]-yVals[i-1])<filterThreshold) {
- runningTotal += yVals[i];
- sampleCount++;
- }
- average = runningTotal/sampleCount;
- for (int j=i; j<sampleCount; j--) {
- filteredData[j] = average;
- }
- }
- }
What I am trying to do can be illustrated by this figure:
If the difference between the next value and current value is greater than threshold, calculate the average value in the current segment and assign it as filtered data.
I am sure something is wrong with the code as it crashes when I run it!
1