1

I need to implement moving average digital filter for post processing of some recorded oscilloscope waveforms in Scilab. I have prepared a script with below given code (the recursive implementation with averaging window containing 256 samples)

// number of samples
N = 350000;
// vector of voltage samples
voltage = M(1:N, 2)';

// filtered values
filt_voltage = zeros(1:N);
// window length
L = 256;
// sum of the samples in the averaging window
sum = 0

for i = 1:N_01
    // averaging window full?
    if i > L
        // remove the oldest sample in the averaging window
        sum = sum - voltage(i - L);
    end
    // add the newest sample into the averaging window
    sum = sum + voltage(i);
    // average of the samples in the averaging window
    filt_voltage(i) = sum/L; 
end

The script output is following (blue waveform - the recorded data, red waveform - filtered data) enter image description here

The problem is that I am not sure whether me moving average implementation is correct (I have found a lot of implementations mostly based on convolution). The output seems to be somehow filtered but it would be helpful for me if anybody can confirm to me that it is correct. Thanks in advance.

Steve
  • 805
  • 7
  • 27
  • That looks OK. What seems to be the problem? – NickJH Nov 06 '17 at 10:37
  • Thanks NickJH for your reaction. The problem is that I'm not sure whether my implementation is correct (every implementation I have found until now was different from my implementation). Based on this finding I expected that there is something wrong. – Steve Nov 06 '17 at 11:02
  • You can reassure yourself (or find a bug!) with synthetic test cases, for instance a single impulse of height 10L should produce a pulse of width L and height 10; a constant input should be unchanged (after the initial ramp up from zero). If those both work there's not much that can go wrong. – NickJH Nov 06 '17 at 13:41
  • NickJH thank you for good tip for testing. – Steve Nov 06 '17 at 13:55

1 Answers1

1

There is nothing wrong with your implementation. In fact, it is a common implementation of the recursive formulation given on stats.stackexchange.com and on wikipedia:

    enter image description here

SleuthEye
  • 14,379
  • 2
  • 32
  • 61