As there are many ways to do averaging, and many potential results, what I’m not understanding is what you mean by “incorrectly”: What is your code currently doing, and what do you want it to be doing instead? How do you know it is wrong, and how will you know when it is working?
Let me explain you what I think I am doing incorrectly -
let say you have arrays of same size … a1=[ …], a2=[ …], a3=[ …], a4=[ …], a5=[ …] … aN=[ …]
this is correct
Aavg = [] = (a1+a2+a3+a4+a5…aN)/N = ( [a1x1+a2x1+a3x1…], [aNx1, a1x2+a2x2+a3x2+…aNx2 ], … );
but for this you need to know all arrays beforehand …
To do real time signal averaging which I am doing is -
((a1+a2)/2+a3)/2 …and so on…
why? because in real time you don’t know next sample so you have to average current and pevious sample… then average it with next sample…
One approach: You’ll still need a window of samples, a0 to aN. It is just a rolling window. The bigger the window, the smoother the result. A window size of 2 is really jumpy. A window size of 100 might be too smooth.
If you want to use the previous average value as a smoothing effect on the output, do that in output only, then throw it away – do not roll it into the next average calculation, just use the window.
let avg = myavg(signalBuffer); // don't use prevavg in this calc
let smoothamt = 0.2
let smoothavg = lerp(avg, prevavg, smoothamt);
// ... display with smoothavg
prevavg = avg; // save for next loop
Now avg is a real value on real data. If you compound avg it becomes an artifact.