# Moving Average of sinusoid

If you calculate a simple moving average over a window length that is equal to the period of the sinusoid, you get a straight line (=0 because the wave is symmetric around the X-axis): the wave has a period of 40, equal to the length of the window of the simple moving average: so the filter weights are 1/40, 1/40,... 40 times. This is kind of obvious because a sinusoid is like a reflection of the same shape around its axis.

Which non-trivial moving average weights would you need to find to get a filter that returns the same output as its input (so besides SMA(1) )? So I want to find the filter weights of some type of average over some window length, that when multiplied with their respective values of the sinusoid, while sliding the window over the sinusoid, add to a sum equal to the sinusoid.

ps: is it possible to answer using as little signal processing / engineering terminology as possible please? Like you're talking to a mentally challenged person.. :)

• if all the weights are equal to each other and you're averaging over exactly a period, there is no answer because the output is always zero and it's pretty hard to multiply something by zero and get something that is not zero. if the weights can be more general, then there is an infinite number of valid answers. all you need is for the gain at the frequency of the sinusoid to equal 1 (or 0 dB). doesn't matter what the filter gain is for other frequencies. – robert bristow-johnson Jul 8 '16 at 4:49
• Do you need to preserve the phase of the sinusoid, as in do you allow for example a cosine as the output? – Olli Niemitalo Jul 8 '16 at 6:15

The fundamental property of averaging filters (in swear words, a linear system) is that the output of a sine is a sine of the same frequency, albeit of zero amplitude. So:

• Which non-trivial moving average weights would you need to find to get a filter that returns the same output as its input?

An averaging in phase with the sine: take a filter full of zeroes, except $K$ values, pairwise distant by a multiple of $40$, with coefficient values $1/K$. This way, you will average $K$ sine value of the same amplitude, yielding a value with the same amplitude.

Hence, you have a bunch of non trivial filters. However, they are not very interesting in practice, because they shall be tuned to the period of a sine sampled with particular sampling.

Any moving average that is of a length different from that of the period of sinusoid (or integer multiple thereof) will output a sinusoid of that same frequency.

So you could just make your moving average 1 sample shorter (which would be the same as subtracting some phase of sma(1) from DC).

• I would say one sample more is easier to get your head around as then you can think of it as every sample up to the period cancels out, then you just have the one extra sample that equals the original amplitude of the signal one period later :-) – keith Jul 8 '16 at 8:25