I wrote this code for smoothing of a curve . It takes 5 points next to a point and adds them and averages it .
/* Smoothing */
void smoothing(vector
The current-value of the point is used twice: once because you use +=
and once if y==0
. So you are building the sum of eg 6 points but only dividing by 5. This problem is in both the IF and ELSE case. Also: you should check that the vector is long enough otherwise your ELSE-case will read at negative indices.
Following is not a problem in itself but just a thought: Have you considered to use an algorithm that only touches every point twice?: You can store a temporary x-y-value (initialized to be identical to the first point), then as you visit each point you just add the new point in and subtract the very-oldest point if it is further than your NEIGHBOURS
back. You keep this "running sum" updated for every point and store this value divided by the NEIGHBOURS
-number into the new point.