3
$\begingroup$

I found this digital filter in code I am working on. It is a low pass filter. In the code it is called an "alpha filter", but it is not the same as the alpha filter mentioned here.

I post the relevant code below:

float alpha = 0.7;
float prev = 0.0;

float filter(float sample) {
    float filtered = (sample * alpha) + (prev * (1.0 - alpha));
    prev = sample;
    return filtered;
}

It looks like alpha should be in range $[0, 1]$. By increasing alpha, the filter tracks the measurement with less delay, but lets more noise through.

I am tasked with writing library functions, and I would like to give this thing its proper name, and hopefully link to a Wikipedia article in the doc tags.

$\endgroup$
  • 2
    $\begingroup$ Thanks Marcus. I did read the other question but I failed to catch that: out[i-1] + (alpha*(in[i] - out[i-1])); is mathematically the same as what I was dealing with. $\endgroup$ – Erik Van Hamme Jan 10 '18 at 12:55
  • $\begingroup$ You can also check Is there a technical term for this simple method of smoothing out a signal? with both ways for writing the equation $\endgroup$ – Laurent Duval Jan 10 '18 at 16:36
  • $\begingroup$ hay @MarcusMüller, take a look at Ced's answer. we got it wrong. it's not $y[n-1]$ that is fed back as "prev". it's $x[n-1]$. it's a two-sample FIR. $\endgroup$ – robert bristow-johnson Jan 11 '18 at 6:30
  • 1
    $\begingroup$ i am opposed to closing. we all ('cept for @cedrondawg) read the question wrong. the OP should uncheck @MattL. 's answer or Matt should correct it. $\endgroup$ – robert bristow-johnson Jan 11 '18 at 6:32
  • $\begingroup$ Oh, shoot, you're right! $\endgroup$ – Marcus Müller Jan 11 '18 at 8:03
9
$\begingroup$

I'm new, so I can't add this comment to Matt L.'s answer.

It is not an exponential filter, the equation is actually:

$$ y[n] \ = \ \alpha \, x[n] \ + \ ( 1 - \alpha ) \, x[n-1] $$

So it is a very short FIR filter, not an IIR filter. I'm not expert enough to know a specific name.

Ced

============================================= Followup:

I want to thank everyone for their upvotes. Yes, I've contributed to comp.dsp and I am a blogger at dsprelated.com.

Like all of you, I suspect that the intent of the function was to be an exponential filter and was coded erroneously.

If I were to name the filter as coded, I would call it a "Linear Interpolation Filter", or perhaps a "Subsamplesize Time Shift filter" when $\alpha$ is between zero and one. It would make more sense as such if the $\alpha$ and $(1-\alpha)$ coefficients were reversed.

$\endgroup$
  • $\begingroup$ hay Ced!! didn't we see you around comp.dsp or somewhere? $$ $$ welcome. this answer tells us that you read the problem better than either @MattL. or myself. i totally missed that. $\endgroup$ – robert bristow-johnson Jan 11 '18 at 6:25
  • $\begingroup$ You deserve additional votes to allow you comments $\endgroup$ – Laurent Duval Jan 11 '18 at 8:22
  • $\begingroup$ Indeed. And I should in fact be detracted these reputation points that you deserve! $\endgroup$ – Marcus Müller Jan 11 '18 at 9:05
  • 1
    $\begingroup$ I select this as the answer because this was the first correct identification of the filter, even though it is likely that an IIR filter was intended, but not implemented correctly. I also like Matt's answer. $\endgroup$ – Erik Van Hamme Jan 11 '18 at 16:16
3
$\begingroup$

This is a correction of my old answer (see below). It turns out that all of us misread the question except for Cedron Dawg in his answer.

Since prev is not the previous output sample but the previous input sample, this is no recursion and, consequently, not a 1-pole IIR filter, but a simple two-tap (finite impulse response) FIR filter:

$$y[n]=\alpha x[n]+(1-\alpha) x[n-1]$$

Since this is an FIR filter it is stable for any value of $\alpha$. For smoothing, a reasonable range for $\alpha$ is indeed $\alpha\in (0,1)$. Note that this filter doesn't really do that much, and it's much worse at smoothing than its IIR counterpart (see below).

Unlike the EWMA filter described below, this filter is no standard filter. It's just a two-tap FIR filter, and with $\alpha$ in the range $(0,1)$ it's a two-tap FIR smoothing filter. For $\alpha=0.5$ it would be a two-tap moving average filter.

I think what mislead most of us was the use of $\alpha$ in a convex combination of two samples. This is commonly done in a 1-pole IIR filter, but usually not in the FIR case. It could even be a bug, and that the author's intention actually was to code a 1-pole IIR filter.


OLD ANSWER

[I keep it because it's an explanation of a standard filter that might be a good alternative to the filter in the question.]

It's called exponential smoothing or exponentially weighted moving average (EWMA) filter.

The corresponding difference equation is

$$\begin{align}y[n]&=\alpha x[n]+(1-\alpha)y[n-1]\\&=\alpha (x[n]-y[n-1])+y[n-1]\end{align}\tag{1}$$

For stability, $\alpha$ must be in the range $\alpha\in (0,2)$. For this to be a smoothing filter, one must choose $\alpha\in (0,1)$.

$\endgroup$
  • 1
    $\begingroup$ a.k.a. a one-pole low-pass filter and the pole is $p = 1-\alpha$ . sometimes called "high-cut" in the audio domain. $\endgroup$ – robert bristow-johnson Jan 11 '18 at 6:22
  • $\begingroup$ @robertbristow-johnson: Corrected, finally ... $\endgroup$ – Matt L. Jan 11 '18 at 7:38
  • 1
    $\begingroup$ @MattL I think your bug theory is on the money here. Unfortunately, the old-timer who wrote this code is retired. Based on your comment that the filter doesn't do that much, I have coded both in a quick ruby script and compared the 2. The IIR is indeed much better. I have implemented the IIR in my library project and flagged a possible bug for the application code. $\endgroup$ – Erik Van Hamme Jan 11 '18 at 16:12
2
$\begingroup$

As other contributors, I did not read the question correctly, and kept the initial answer at the bottom. So it is, generally and in special cases:

  • a (stable) unit-sum 2-tap FIR filter
  • $\alpha = 0$: a lazy filter, that does nothing
  • $\alpha = 1$: a pure delay filter, with unit delay
  • $\alpha =1/2$: the two-tap averaging filter
  • $\alpha \in ]0,1[$: a low-pass filter
  • $\alpha = 2$: the 2-tap linear extrapolating filter.

Details are as follows:

$$ \hat{s}[n] = \alpha s[n] + (1-\alpha) s[n-1] $$

is a 2-tap FIR filter. When $\alpha =1$ or $0$, it reduces to a one-tap filter, producing either the same output as the input or with a 1-unit delay (flat spectrum). Since the sum of the coefficients is always $1$, it leaves constant components unchanged : the frequency response is constant, equal to one. When $\alpha = 2$, $[2 - 1]$ is the 2-tap linear extrapolating filter.

Several frequency responses are given below:

frequency responses for varying alphas

[OLD ANSWER, KEPT IN CASE THE DESIGN WERE RECURSIVE] As this structure is one of the simplest/shortest one can imagine with a recursive form, it has likely emerged in different context, and got different names. Some of the names identified are:

Other details, including historical notes on its emergence, can be retrieved at:

Its behavior, as answered by Matt L., is driven by $\alpha$. For smoothing, the closer $\alpha$ to one, the higher the relative weight of the latest samples with respect to "filtered data". So the result is less immediate (more delay), but the larger the smoothing effect, as you benefit from the averaving of longer stationary sequences, weighted by an exponential window. Conversely, shortest delays correspond to less noise reduction. There is no free lunch in filtering: shorter filters, poorer frequency selection.

This filter does not deal too well with non stationarity (like trends) and was subsequently extended to double or triple exponential filters, giving rise to Holt-Winters smoothers, see for instance Exponential Smoothing for Time Series Forecasting:

If you notice that your time series is not stationary, you’ll have to find something other than a simple EWMA to do your forecasting.

In the late 1950s, Charles Holt recognized the issue with the simple EWMA model with time series with trend. He modified the simple exponential smoothing model to account for a linear trend. This is known as Holt’s exponential smoothing.

$\endgroup$
  • 1
    $\begingroup$ You might also want to change your answer since we've all misread the question :) $\endgroup$ – Matt L. Jan 11 '18 at 7:40
  • 1
    $\begingroup$ Thank you for the frequency plots. Also, excellent info on the names of the IIR filter in the old answer! $\endgroup$ – Erik Van Hamme Jan 11 '18 at 16:23
  • 1
    $\begingroup$ For a variation of an exponential filter that I discovered and have not seen anywhere else, check out my blog article: "Exponential Smoothing with a Wrinkle" dsprelated.com/showarticle/896.php Yay, I can comment now. $\endgroup$ – Cedron Dawg Jan 11 '18 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.