2
$\begingroup$

I found some snippets of code on the internet that are really simple signal filters. Unfortunately there wasn't much info to back it up. Using Google I've found similar code, but none of it backed up by research or documentation. I've tried it and it works well enough for my situation.

The low pass filter looks like this:

float RC = 1.0/(CUTOFF*2*3.14);
float dt = 1.0/SAMPLE_RATE;
float alpha = dt/(RC+dt);
float out[numSamples];
out[0] = in[0];
for(i=1; i<numSamples; i++){
    out[i] = out[i-1] + (alpha*(in[i] - out[i-1]));
}

And the high pass filter looks like this:

float RC = 1.0/(CUTOFF*2*3.14);
float dt = 1.0/SAMPLE_RATE;
float alpha = RC/(RC + dt);
float out[numSamples];
out[0] = in[0];
for (i = 1; i<numSamples; i++){
    out[i] = alpha * (out[i-1] + in[i] - in[i-1]);
}

I'd like to know if there is a name for these algorithms or who invented them so I can find more information about them.

$\endgroup$
  • $\begingroup$ Frankly, why not take the other route and read up on simple filters, build such and then compare them to the snippets? sounds more goal-oriented to me :) $\endgroup$ – Marcus Müller Dec 20 '16 at 23:27
  • $\begingroup$ And: Filters are really typically designed to purpose, not "picked from a website"; of course, both your filter types are extremely common, at least in hardware, where you'd want to filter something with extremely limited ressources. For other usages, they're far less common; so maybe you'd really want to ask "what filter would I do to achieve XYZ on hardware ABC under the constraint that UVW?" in another question $\endgroup$ – Marcus Müller Dec 20 '16 at 23:47
  • 2
    $\begingroup$ dunno that anyone gets credit, but these are the basic First-order LPF and HPF with passband gain of 0 dB. i dunno who invented multiplication or addition of exponents either. $\endgroup$ – robert bristow-johnson Dec 21 '16 at 2:33
3
$\begingroup$

In addition to @MarcusMüller answer, you can find other names and historical details on this type of filter in Is there a technical term for this simple method of smoothing out a signal?.

I will not rewrite the other answer, just give here a few aspects:

This filter is so useful and common that its has several names, for instance first order exponential averaging low-pass filter, exponentially weighted moving average (EWMA). History also knows it as Brown's Simple (linear) Exponential Smoothing (sometimes called SES). The above link provides mentions of this filter in 1956, and possibly dating back to 1944.

$\endgroup$
3
$\begingroup$
 out[i] = out[i-1] + (alpha*(in[i] - out[i-1]));

From Wikipedia's article on Low pass filters:

That is, this discrete-time implementation of a simple RC low-pass filter is the exponentially weighted moving average

[…]

The loop that calculates each of the n outputs can be refactored into the equivalent:

for i from 1 to n
     y[i] := y[i-1] + α * (x[i] - y[i-1])

So, yes, this is a "exponentially weighted moving average". You can call it that, a single-pole IIR low pass with a decay of $\alpha$, or just a single-tap recursive low pass.

I'm happy I could help you with this, it wasn't very hard!

$\endgroup$
  • $\begingroup$ Was it necessary to state how easy that was? $\endgroup$ – Octopus Dec 21 '16 at 17:37
  • 2
    $\begingroup$ well, no, not really, but on the other hand, the formula was directly on the wikipedia site on low pass filters. $\endgroup$ – Marcus Müller Dec 21 '16 at 18:06
  • $\begingroup$ I agree with Marcus on the information being readily accessible on Wikipedia. I read his statement on how "easy" it was as a comment on his effort (i.e. copy from Wiki), rather than a critique of you @Octopus or your knowledge. We're all here to learn, sir. $\endgroup$ – ruoho ruotsi Dec 21 '16 at 18:21
  • $\begingroup$ @Octopus algorithmic implementations sometimes differ. What is important in the learning process is to progressively increase your ability in detecting "patterns", that help you find other patterns, and seize the nature of the filter from a few lines of code. $\endgroup$ – Laurent Duval Dec 21 '16 at 19:10

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.