# Should I use window or any filtering to average my data?

I have 80 seconds of data and I have to score my data by taking the average or median (or some other method) every 10 seconds. What's the best way to do this ? Should I just use a regular rectangular window or should I go into other FIR windowing? Should my window be overlapping/non-overlapping?

Any suggestions?

• Can I please ask if this was resolved? – A_A Feb 15 '18 at 11:25

I have 80 seconds of data and I have to score my data by taking the average or median (or some other method) every 10 seconds. What's the best way to do this ?

Should I just use a regular rectangular window or should I go into other FIR windowing?

The "window" in "Moving Average Window" has nothing to do with the "window" in "Window function".

Should my window be overlapping/non-overlapping?

By definition, the moving average filter is applied in an overlapping fashion to the signal.

Say for example that your original signal is in some $x[n]$ with $n \in [0..N-1]$ and $N$ being the length of the signal. A 4 tap moving average filter is like taking the mean of 4 sequential values and sliding it over the whole duration of $x[n]$.

To obtain the arithmetic mean of a signal in this way you would do:

$$y[n]=\frac{1}{4}\sum_{k=0}^{3} x[n+k]$$

To produce $y[0]$ you work on the window $0..3$ over $x$

To produce $y[1]$ you work on the window $1..4$ over $x$

To produce $y[2]$ you work on the window $2..5$ over $x$

And so on...

Since the signal $x[n]$ is defined between $0..N-1$, the valid values for $y[n]$ are between $0..N-1-4$ (or $0..N-5$). It is possible to extend $y[n]$ to the same length as $x[n]$ by assuming that $x[n]$ is $0$ outside of the observation (that is, outside of the 80 seconds of signal that you have the signal drops to zero). But in that case, those last 4 values would be unreliable because we do not really know what was actually hapenning to the system that the signal is obtained from.

A moving average filter (specifically) can be implemented via convolution by observing that all that we do by obtaining the arithmetic mean over a sliding window is to multiply those windows of $x[n]$ with 4 identical numbers ($\frac{1}{4}$) and sum the result which is exactly the way convolution is defined.

To generalise, an M-tap moving average filter would look like $h = [\frac{1}{M},\frac{1}{M},...,\frac{1}{M}]$ and the simplest way to apply it in a MATLAB-like language is y = conv(x, 1./ones(1,M)). (Try it with something like y = conv(x, [0.25, 0.25, 0.25, 0.25]).

To transfer it to your specific setting, you have 80 seconds of data to be averaged over a 10 second interval. If you divide your signal in 8 non-overlapping windows (0-9, 10-19, 20-29, 30-39, ...70-79), you will get 8 instances of what the value of the average of the signal. You would then know that in the first 10 seconds the average was some value and in the next 10 seconds it jumped to another value but you would have lost what happened in between. Did it jump? Did it vary progressively? If you apply a moving average filter (again, with a 10 second time constant) you would obtain a smoothly varying view of the evolution of the mean.

The arithmetic mean though is by definition a linear multiply-and-accumulate kind of operation and it was possible to be implemented with convolution.

If you wanted to compute some other expression in a sliding window fashion over $x$, you would probably have to do it manually. For example:

for n=0:N-1-M
y(n) = someFunction(x(n:n+M-1))


And within someFunction you can have whatever processing you like over the subsegment of $x$ between the $n$ and $n+M-1$ samples.

That processing might require that you modify the overlapping of the windows and / or a window function.

For example when computing a Spectrogram you apply a Discrete Fourier Transform in overlapping sliding windows over some $x$.

Now, when referring specifically to the DFT, in addition to these overlapping $x[n]$ segments you might also have to decide on a window function to reduce spectral leakage.

Hope this helps.

• "The 'window' in 'Moving Average Window' has nothing to do with the 'window' in 'Window function'."  i dunno that i would agree with that claim. – robert bristow-johnson Jan 21 '18 at 2:46
• From a mathematical point of view, the way to select things out of a set (whatever the set flavour) is with an index. To define a window function you use $N$. $N$ is the size of the index set. Therefore, selection of the specific values is one thing ("window" as a buffer) and shaping of those values is another ("window" as "spectral window"). You can use a boxcar to select parts of a signal but you cannot use a Blackman-Harris to select and "shape". – A_A Jan 21 '18 at 11:11
• @robertbristow-johnson ...for a "demo" of selecting parts of a signal by crafting boxcars (or constructing index sets), please see this, section "Detect the presence of the face...". Also, I did not add your username in my previous comment so you might want to check that first. – A_A Jan 21 '18 at 11:16
• So I decided to do moving average using the following piece of code in matlab. B = 1/10*ones(10,1) filter(B,1,input) I am having some issues. I have 80 samples of data which is all above 0.9. When I did the moving average, my first ~10 values out are below 0.9. Can someone explain to me what I did wrong here? – Baid Jan 30 '18 at 18:07
• @Baid Can you please amend your question with what you mention in this comment? It will then be visible to everyone in DSP.SE. As a simple comment to my answer I am the only one who gets the notification about it. – A_A Jan 31 '18 at 12:55