20

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


12

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


10

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

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


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


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


6

If a first-order IIR will do, modify that slightly, and you're done. So the usual first-order low-pass filter can be defined as $y_n = h(\theta_n)$ such that $y_n = y_{n-1} + a(\theta_n - y_{n-1})$. This works great for $\theta_n \in \mathbb{R}$. You want a low-pass filter that's defined on an interval that spans $360^\circ$. For reasons that will become ...


5

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


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


5

Impulse Response is basically the FIR coefficients of the system. Namely, a system $ H $ with an impulse response given by $ f [n] $ and a Filter $ F $ with an FIR representation of $ {f[0], f[1], \cdots, f[n]} $ are equivalent. Now, systems with Feedback are equivalent of both FIR and IIR (AR) filters. But given infinite length of FIR model any LTI ...


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

The frequency response of the moving average is called the asinc or psinc, the aliased sinc or periodic sinc (sinc for cardinal sine), or the Dirichlet function. Since the sum of the moving average filter coefficients is equal to one, it preserves constant signal, hence is somewhat lowpass. When the length $N=2$, the shape in the frequency domain is shaped ...


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


4

Median filtering is non-linear, and pretty awesome about removing outliers. You just need to adjust the length of the filter based on the estimate of the frequency of the errored samples.


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

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

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

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


3

A centered moving average filter is a finite impulse response (FIR) filter that affects the same weight to all the samples in the window. If you only care about time domain properties, and do not care about its relatively poor performance in the spectral domain, for a signal $s$ that is quite stationary across the window, you can use it. It has extremely ...


3

Yes sure they are LTI. Let $A$ be the $(L-1)\times (L-1)$ shift matrix $$ A := \begin{pmatrix}0 & 1 & 0 && \dots & 0\\0 & 0 & 1 & 0 &\dots &0\\\vdots &&& \ddots && 0\\0&&\dots&&1&0\\0 &&\dots&&0&1\\0&0&\dots&0&0&0\end{pmatrix} $$ and $$ B ...


3

I am not entirely sure what matlab's LASSO routine does so I started with Ordinary Least Squares (OLS) and worked backwards. From an OLS perspective X1 as you have it won't work. You've got a regressor that is as all ones, but your parameter inputs for you example data (r) doesn't contain an offset. Essentially your model doesn't fit you're data well, ...


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