33

The moving average filter (sometimes known colloquially as a boxcar filter) has a rectangular impulse response: $$ h[n] = \frac{1}{N}\sum_{k=0}^{N-1} \delta[n-k] $$ Or, stated differently: $$ h[n] = \begin{cases} \frac{1}{N}, && 0 \le n < N \\ 0, && \text{otherwise} \end{cases} $$ Remembering that a discrete-time system's frequency ...


17

If $N$ is the length of the moving average, then an approximate cut-off frequency $F_{co}$ (valid for $N >= 2$) in normalized frequency $F=f/fs$ is: $F_{co} = \frac {0.442947} {\sqrt{N^2-1}}$ The inverse of this is $N = \frac {\sqrt{0.196202 + F_{co}^2}}{F_{co}}$ This formula is asymptotically correct for large N, and has about 2% error for N=2, and ...


10

Consider a zero-phase moving average of length $N$: $$\text{y}[n] = \begin{cases} \displaystyle\frac{\text{x}[n] + \displaystyle\sum_{k=1}^{\frac{N-1}{2}}\left(\text{x}[n+k] + \text{x}[n-k]\right)}{N},&n\in\mathbb{Z}&\text{for }N\text{ odd}\\ \displaystyle\frac{\displaystyle\sum_{k=1}^{\frac{N}{2}}\left(\text{x}[n+(k-\frac{1}{2})] + \text{x}[n-(k-\...


9

A moving average can be implemented recursively, but for an exact computation of the moving average you have to remember the oldest input sample in the sum (i.e. the a in your example). For a length $N$ moving average you compute: $$y[n]=\frac{1}{N}\sum_{k=n-N+1}^nx[k]\tag{1}$$ where $y[n]$ is the output signal and $x[n]$ is the input signal. Eq. (1) can ...


8

In more standard DSP terms, you have the following filter: $$ y[n] = (1-a) x[n] + a y[n-1] $$ where $x[n]$ and $y[n]$ are the input and output signals at time $n$ respectively. The transfer function (which you didn't ask for) is: $$ H(z) = \frac{1-a}{1 - az^{-1}} $$ so here is your single pole, at $z=a$ in the complex plane. This filter is also known as ...


7

Convolution in linear time-invariant system is asociative. So to get the equivalent mask you just need to convolve the kernel with itself twice. This will then then give you a 7x7 kernel: octave:1> a = [ 1 1 1 ; 1 1 1 ; 1 1 1 ] a = 1 1 1 1 1 1 1 1 1 octave:2> conv2(a,conv2(a,a)) ans = 1 3 6 7 6 3 1 3 ...


7

What is wrong with a fading memory (exponential) moving average: ma_new = alpha * new_sample + (1-alpha) * ma_old


6

Let's compare the actual numerical errors for different approximations of the cutoff frequency. The error given in the table is calculated by subtracting the true (numerically solved) -3 dB cutoff frequency $\omega_c$ from the approximation. $$\begin{array}{rl} \text{a})&\frac{2.78311475650302030063992}{N}\\ \text{b})&\frac{2.78311475650302030063992}...


6

No you are not missing anything. A moving average with a period of $T = 1/60$ seconds will indeed have a notch at 60 Hz, since the frequency response is a Sinc function with the first null at 1/T. (So it is both a low pass filter with an magnitude envelope that goes down as 1/f combined with a notches at integers of 1/T not including 0). This is because a ...


6

Yes you can consider a zeroth (or first) order SG filter as a moving average filter. Below MATLAB / Octave code computes the impulse response of a SG filter of order $N$ and length $2M+1$ : % Savitzky-Golay Filter % clc; clear all; close all; N = 0; % a0,a1,...,aN : Nth order polynomial M = 3; % x[-M],...,x[M] : 2M +...


5

Is moving median always less sensitive to outliers? Sometimes. It will work if you have a very short spike (preferrably shorter than the median/average sample size). However, if you have a large spike, then taking the median won't help eliminate the spikes. Long spike I have illustrated this using a dataset (with sample size = 5, taking into account 2 left ...


4

Exponential averaging is performed using a first order IIR filter according to the difference equation: \begin{equation} y[n] = (1-\alpha)\;x[n]+\alpha \; y[n-1] \end{equation} where $x[n]$ and $y[n]$ are the input and output signals at time $n$ and the value of the single coefficient $\alpha$ is given by the equation: \begin{equation} \alpha = \exp( \frac{-...


4

Your normalized window is given by $$w(n)=\frac{2}{N+1}\frac{N-n}{N},\quad n=0,1,\ldots,N-1$$ The window satisfies $$\sum_{n=0}^{N-1}w(n)=1$$ which means that the gain of the corresponding moving average filter is 1 at DC. For determining the cut-off frequency, we need to compute the frequency response of the window: $$W(e^{j\theta})=\sum_{n=0}^{N-1}w(...


4

This isn't really an answer, but I thought I'd report what I'm seeing and ask for more information. I've loaded your test.wav file and I can see the signal plotted below. So what you're getting in the plots you show is not so much the median value, but is more like an envelope of the signal. The second issue is that the signal actually seems to be part of ...


4

up arrow from me, Olli. but for some reason, i think the answer is much simpler. normally i like to design acausal symmetric FIR filters, because they are zero phase, but usually i limit myself to an odd number of non-zero taps. to do this more generally, i might stick to the causal FIR moving average. let's say the number of taps is $N$. $$ y[n] = \...


4

OK, this is fun. I'm going to add my own thoughts and approximations, the first of which turns out to be identical to the one given by Massimo in this answer, and the one derived by Olli in this thread. I still include it here because its derivation is different. Then I'll show a better approximation, which has a maximum relative error of $0.002$ for $N=2$ (...


4

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 ...


4

(Update: I just realized that the first part of this covering CIC structures is basically what Hilmar has already answered-- I'll leave this up since it offers more graphics and details in case that his helpful to anyone but it is indeed the same answer) This may not be optimum and although a feedback structure is involved it is strictly not IIR, but want ...


4

This is not a full answer. It explores some basic approximations. For a moving average as long as 600 samples it is informative to look at impulse responses of continuous-time filters as approximations of those of the desired discrete-time filters. Let's have as the ideal desired impulse response a boxcar function normalized to a width of 1 spanning $x = 0\...


4

I can show you some low order IIR approximations to an FIR moving average filter. In the figure below you see $3$ (infinite) impulse responses that approximate a moving average of length $N=600$. The filter orders are $1$, $2$, and $5$, respectively, and they all approximate the desired response in a least squares sense. I used the equation error method to ...


4

That system you're referring to is a first-order recursive filter $$y[n]=\alpha x[n]+(1-\alpha)y[n-1],\qquad 0<\alpha<1\tag{1}$$ where $n$ is the (integer) time index, $y[n]$ is the output, and $x[n]$ is the input. You chose $\alpha=\frac12$. That filter is also called exponentially weighted moving average. It has theoretically infinite memory and ...


4

There is a way to do it, it is really similar to a moving average $N$ : number of samples per period $Z$ : Accumulator $x[n]$ : current sample $$Z[n] = Z[n-1]+ x[n]^2 - x[n-N]^2 $$ $$x_{RMS}[n] = \sqrt{\frac{1}{N}Z[n]}$$ Basically, you need a delay line to store the previous $x[n]$ samples or better yet the previous $x[n]^2$ samples and you need an ...


3

The formula $$ a_n = a_{n-1} \frac{n-1}{n} + \frac{x_n}{n} .$$ is a way to compute the average of $n$ samples, and update this average whenever a new sample is obtained, without storing all of the samples individually. Ignoring the part about online computation, I think your question is equivalent to "is there a way to estimate the mean of N identically ...


3

One way to see the equivalence is to first factor out the $0.5$ in your initial transfer function, i.e. $$ H(f) = 0.5(1 + e^{-i2\pi f}). $$ Then you can rewrite the $1$ and $e^{-i2\pi f}$ as follows: $$ H(f) = 0.5(e^{i\pi f}e^{-i\pi f} + e^{-i\pi f}e^{-i\pi f}) = 0.5(e^{i\pi f} + e^{-i\pi f})e^{-i\pi f}. $$ Your result then follows as a trivial ...


3

ok the solution was: (in R) emaweights<-function(m) { alpha<-2/(m+1) i<-1:m sm<-sum((alpha*(1-alpha)^(1-i))) return(((alpha*(1-alpha)^(1-i)))/sm) } Here the weights do add up to 1.


3

A simple and general method for filling in missing data, if you have runs of complete data, is to use Linear regression. Say you have 1000 runs of 5 in a row with none missing. Set up the 1000 x 1 vector y and 1000 x 4 matrix X: y X wt[0] wt[-2] wt[-1] wt[1] wt[2] --------------------------------- 68 67 70 70 68 ... Regression will ...


3

So when will you think you need moving average in an algorithm design? If you mean moving average filters; moving averages as the name suggest are computed as averages of samples say, $M-1$ previous samples (+ current sample) from input $x[n]$ to get an average output $y[n]$, repeating the process to get all $y$ samples. Computing their mean to get an ...


3

I provide another answer because this approach is completely different in the sense that I do not try to approximate the filter's amplitude function to compute an approximation of the cut-off frequency, but I use a pure data fitting approach given the exact cut-off frequencies, which were computed numerically (and which are also given for a set of filter ...


3

The frequency response of a causal length $N$ moving average filter is $$H(\omega)=\frac{\sin\left(\frac{N\omega}{2}\right)}{N\sin\left(\frac{\omega}{2}\right)}e^{-j\omega(N-1)/2}=A(\omega)e^{j\phi(\omega)}\tag{1}$$ Note that $A(\omega)$ is not the magnitude of $H(\omega)$, but it is a real-valued amplitude function, which takes on positive as well as ...


3

Is the ADC sample-and-hold fast enough to even capture any envelope peaks or even half cycles? If not, all bets are off. If your ADC does have a fast enough capture time, then randomizing your sample times might be a good bet at capturing a few near extrema. If you know, a priori, the approximate shape of your waveform, then Monte-Carlo sampling might ...


Only top voted, non community-wiki answers of a minimum length are eligible