08-10-2015 02:30 AM
The scalar is just fine. In my application I get single values from the analog input continously and the moving average is building the running sum of it.
I do not understand why it has to be a filter for a 1 D array ?!
The gain is decimate*stages.
08-19-2015 04:19 AM
Well,
I am still not able to derivate the differential equation. The whole algorithm is building the sum of the last M inputs. Later on the host side the sum is divided by M.
It has to be something like: y[n] = x[n] + x[all previous values-1] - x[all previous values-M-1]
Hope someone can help solving the problem.
Kind regards
Slev1n
08-24-2015 06:57 AM
It has cost me some time and nerves but I think I finally got it. Here me solution.
In general a moving average of 5 points has the following differential equation:
y[4] = (x[0]+x[1]+x[2]+x[3]+x[4]) / 5; in the following we will neglect the factor 1/M; M = # points used for averaging, in the example M = 5.
y[5] = x[1]+x[2]+x[3]+x[4]+x[5] or in a more efficient form: y[5]= y[4] + x[5] - x[0] = y[n-1] + x[n] -x[n-M]
Now back to my code:
Usually a 5 point moving average takes 5 iterations to perform a valid result, as it needs 5 input values to compute the output. In my case, there is a delay of 1, due to the feedback node (we neglect the case where stages > 1). So after 6 iterations the first valid output is computed by the algorithm.
Example: Input is constantly 1 !
So after 6 iterations the feedback node 1 (FN1, is the node after the sum sign) contains the value 6 (=x[0]+x[1]+x[2]+x[3]+x[4]+x[5]) and FN2 (after the BRAM) contains the value 1 = x[0] and the output = y[4] = 5 = x[0]+x[1]+x[2]+x[3]+x[4]. (Due to delay caused by the feedback node y[4] and not y[5] is the result of the 6th iteration)
In the 7th iteration y[5] is calculated: y[5] = FN1 - FN2 = x[0]+x[1]+x[2]+x[3]+x[4]+x[5] - x[0] = x[1]+x[2]+x[3]+x[4]+x[5] = y[4] + x[5] - x[0] = y[n-1] + x[n] - x[n-M];
FN1 contains now: x[0]+x[1]+x[2]+x[3]+x[4]+x[5]+x[6]
FN2 contains now: x[0]+x[1]
In the 8th iteration y[6] is calculated: y[6] = FN1 - FN2 = x[0]+x[1]+x[2]+x[3]+x[4]+x[5]+x[6]-(x[0]+x[1]) = x[2]+x[3]+x[4]+x[5]+x[6] = y[5] + x[6] - x[1]
and so on...
The fact that only one addition and one substraction is used computing the output shows an efficient way to compute the moving average. The calculations show, that the algorithm fits to the well known formula of a recursive moving average: y[n] = y[n-1] + x[n] -x[n-M]
I also tested it with a series of: 1,2,3,4,5,6,7 as input and the results fit to my above explained assumption.
kind regards and thanks for all your help, maybe someone can have a look at my derivation but I think its fine.
Slev1n