13 votes

How to implement a moving average in C without a buffer?

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 ...
  • 80.9k
12 votes
Accepted

3dB-Cut off frequency of moving average

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}...
10 votes

How to implement a moving average in C without a buffer?

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

How does this "simple filter" work?

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 ...
  • 4,751
7 votes
Accepted

Moving Average for Notch filtering

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. (...
  • 38.3k
6 votes

3dB-Cut off frequency of moving average

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 ...
6 votes
Accepted

Is it correct to call a Savitsky-Golay filter of degree 0 a simple moving average?

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$ : ...
  • 27.1k
6 votes

Filtering Angular Measurements

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})$. ...
  • 9,116
5 votes

Moving average vs. Moving median

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 ...
  • 23k
5 votes

Moving average vs. Moving median

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,...
5 votes

What Is the Transfer Function of a Moving Average (FIR Filter)?

Moving Average in its general form is basically an FIR Filter which means it can mimic any linear system you'd like by the choice of the length and coefficients. If you mean Moving Average by a ...
  • 42.4k
5 votes
Accepted

What Is the Transfer Function of a Moving Average (FIR Filter)?

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 ...
5 votes
Accepted

How to interpret power of a two-point average signal

$⟨x[n]x[n−1]⟩ $ represents a correlation between the sample $x[n]$ and the previous sample $x[n-1]$. The sum of a sample and it's previous divided by two is a two sample moving average. A moving ...
  • 38.3k
4 votes

3dB-Cut off frequency of moving average

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

3dB-Cut off frequency of moving average

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 ...
  • 80.9k
4 votes
Accepted

Moving Average of sinusoid

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

What Is the Transfer Function of a Moving Average (FIR Filter)?

The following figure is borrowed from Frequency Response of the Running Average Filter: This is the gain applied to different frequencies (normalized to interval 0-$\pi$), for running averages of ...
4 votes

Optimal $ n $ -th Order IIR /AR Approximation of a Moving Average Filter

(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 ...
  • 38.3k
4 votes

Optimal $ n $ -th Order IIR /AR Approximation of a Moving Average Filter

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 ...
4 votes
Accepted

Optimal $ n $ -th Order IIR /AR Approximation of a Moving Average Filter

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 ...
  • 80.9k
4 votes
Accepted

A general conceptual understanding of moving average

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, ...
  • 80.9k
4 votes
Accepted

How to approximate a moving RMS without iterating over N samples for each required output?

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] =...
  • 3,660
4 votes

Filtering Angular Measurements

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.
  • 244
3 votes

Envelop detection with low sample rate?

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 ...
  • 34.1k
3 votes
Accepted

Phase response of moving average filter -- how to interpret?

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(\...
  • 80.9k
3 votes

3dB-Cut off frequency of moving average

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 ...
  • 80.9k
3 votes

Difference between Gaussian and moving average filters for peak detection and doppler shift detection?

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 ...
3 votes
Accepted

$N$ point moving average filters in state space

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 &&...
  • 748
3 votes
Accepted

How to Apply Least Squares Estimation for Sparse Coefficient Estimation?

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 ...
3 votes
Accepted

MATLAB: Implementing Least Squares Estimator for a Given Model

The equation you're trying to solve is $$ \mathbf{y}=\mathbf{X}\mathbf{h}, $$ where $\mathbf{h}$ is your unknown. The matrix $\mathbf{X}$ is going to have a time-shifted structure that reflects the ...
  • 2,761

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