You are on page 1of 5

Arduino Tutorial: Simple High-pass, Band-pass and Band-

stop Filtering
March 10, 2016 Mads Aasvik Arduino Tutorials, Popular Posts

In this post well show you how to implement very simple high-pass, band-pass and band-stop lters on an Arduino.

It is highly recommended that you read our previous post about potentiometers and EMA (Exponential Moving Average) ltering as well
as the one about plotting multiple values in the Arduino IDE before continuing since we use similar circuitry, ltering method and plotting
method in this tutorial. We use a simple potentiometer and the ADC to create a signal we run through the lters.

Di erent kinds of lters. X-axis is frequency and Y-axis is gain which is how much signal is let through (or sometimes even ampli ed). (Source: Wikipedia)

High-pass Filtering
High-pass ltering is the opposite of low-pass ltering. Instead of smoothing out a signal, youre left with all the noise and rapid changes.
When the original signal stabilizes around any steady value, the high-passed signal goes to zero.

[] run a low-pass lter and subtract the result from the original signal.

There are problaby more correct and e cient ways to implement high-pass lters, but the way we like to do it is to run a low-pass lter and
subtract the result from the original signal. That way youre left with only the high frequencies of the original signal.

This is the way we implemented it on an Arduino Leonardo:

1 //Global Variables
2 int sensorPin = 0; //pin number to use the ADC
3 int sensorValue = 0; //initialization of sensor variable, equivalent to EMA Y
4 float EMA_a = 0.3; //initialization of EMA alpha
5 int EMA_S = 0; //initialization of EMA S
6 int highpass = 0;
7
8 void setup(){
9 Serial.begin(115200); //setup of Serial module, 115200 bits/second
10 EMA_S = analogRead(sensorPin); //set EMA S for t=1
11 }
12
13 void loop(){
14 sensorValue = analogRead(sensorPin); //read the sensor value using ADC
15 EMA_S = (EMA_a*sensorValue) + ((1-EMA_a)*EMA_S); //run the EMA
16 highpass = sensorValue - EMA_S; //calculate the high-pass signal
17
18 Serial.println(highpass);
19
20 delay(20); //20ms delay
21 }

Band-pass Filtering
Imagine you have a high-pass ltered signal which is too noisy. Then a band-pass lter might be for you. A band-pass ltered signal is
basically a smooth high-pass ltered signal.

[] run two seperate EMA lters with di erent cuto frequencies.

Our idea behind the implementation of a band-pass lter is that we run two seperate EMA lters with di erent cuto frequencies. We then
subtract the ltered signal with the lowest cuto frequency from the ltered signal with the highest cuto frequency. That way were left
with the frequencies between the two cuto frequencies, which both are crucial to get the performance you want from the lter.

Its easy to see the di erence between the high-passed signal (blue) and the band-passed signal (orange). The latter is in practice a low-pass ltered high-pass
signal.

Our implementation:

1 //Global Variables
2 int sensorPin = 0; //pin number to use the ADC
3 int sensorValue = 0; //initialization of sensor variable, equivalent to EMA Y
4
5 float EMA_a_low = 0.3; //initialization of EMA alpha
6 float EMA_a_high = 0.5;
7
8 int EMA_S_low = 0; //initialization of EMA S
9 int EMA_S_high = 0;
10
11 int highpass = 0;
12 int bandpass = 0;
13
14 void setup(){
15 Serial.begin(115200); //setup of Serial module, 115200 bits/second
16
17 EMA_S_low = analogRead(sensorPin); //set EMA S for t=1
18 EMA_S_high = analogRead(sensorPin);
19 }
20
21 void loop(){
22 sensorValue = analogRead(sensorPin); //read the sensor value using ADC
23
24 EMA_S_low = (EMA_a_low*sensorValue) + ((1-EMA_a_low)*EMA_S_low); //run the EMA
25 EMA_S_high = (EMA_a_high*sensorValue) + ((1-EMA_a_high)*EMA_S_high);
26
27 highpass = sensorValue - EMA_S_low; //find the high-pass as before (for comparison)
28 bandpass = EMA_S_high - EMA_S_low; //find the band-pass
29
30 Serial.print(highpass);
31 Serial.print(" ");
32 Serial.println(bandpass);
33
34 delay(20); //20ms delay
35 }

Band-stop Filtering
This is the one that might be the most di cult one to wrap your head around. This lter only let the lowest and the highest frequencies
through. This means that it di erentiates itself quite a bit from the high-pass and the band-pass since it doesnt return to zero all the time,
but instead follows the steady value of the signal (like a low-pass lter). However, unlike a low-pass lter, it also includes high frequencies
such as noise and rapid changes.

[] subtract the band-pass ltered signal from the original signal.

Our idea behind this implementation is to subtract the band-pass ltered signal from the original signal. This ends up being an inverse
band-pass lter. The two cuto -frequencies are just as important here as in the band-pass lter.

In this graph you can see that at low frequencies the band-stopped signal (red) behaves like the low-passed signal (orange), while at higher frequencies it
behaves more like the original signal (blue).

An interesting phenomenon where the band-stopped signal on a (near) step-response initially follows the original signal before, after a transient, behaves like
the low-passed signal

Our implementation:

1 //Global Variables
2 int sensorPin = 0; //pin number to use the ADC
3 int sensorValue = 0; //initialization of sensor variable, equivalent to EMA Y
4
5 float EMA_a_low = 0.05; //initialization of EMA alpha (cutoff-frequency)
6 float EMA_a_high = 0.4;
7
8 int EMA_S_low = 0; //initialization of EMA S
9 int EMA_S_high = 0;
10
11 int highpass = 0;
12 int bandpass = 0;
13 int bandstop = 0;
14
15 void setup(){
16 Serial.begin(115200); //setup of Serial module, 115200 bits/second
17
18 EMA_S_low = analogRead(sensorPin); //set EMA S for t=1
19 EMA_S_high = analogRead(sensorPin);
20
21
}

22 void loop(){
23 sensorValue = analogRead(sensorPin); //read the sensor value using ADC
24
25 EMA_S_low = (EMA_a_low*sensorValue) + ((1-EMA_a_low)*EMA_S_low); //run the EMA
26 EMA_S_high = (EMA_a_high*sensorValue) + ((1-EMA_a_high)*EMA_S_high);
27
28 bandpass = EMA_S_high - EMA_S_low; //find the band-pass as before
29
30 bandstop = sensorValue - bandpass; //find the band-stop signal
31
32 Serial.print(sensorValue);
33 Serial.print(" ");
34 Serial.print(EMA_S_low);
35 Serial.print(" ");
36 Serial.println(bandstop);
37
38 delay(20); //20ms delay
39 }

The Small Chapter at the Bottom


As previously mentioned this is probably not the most e cient or most correct way to implement these kinds of lters. However, if you need
some quick and dirty lter implementations, these ones might not be too shabby. Remember that the delay at the bottom of the loops in
these examples are crucial for both the plotting rate and the lter characteristics.

Here is a nice guide on how to nd the actual cuto -frequency for EMA lters. This is applicable on these other lters as well, since we use
EMA as a basis for all of them.

Try them out for yourself, play with the cuto -frequencies and have fun!

Related Posts

Getting Started with Programming part 2: Sous-Vide Some technical insights into Making a Plotter for Trondheim Maker
Conditional Statements this magical food preparation method Faire Part 5: Finale
February 18, 2016 October 1, 2015 November 26, 2014
6 Comments Norwegian Creations
1 Login

Sort by Best
Recommend Share

Join the discussion

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Tommy Johnson 2 months ago


How can we estimate the cuttof frequencies to better tune the band-pass filter?
Reply Share

Norwegian Creations Mod > Tommy Johnson 2 months ago

That's a good question!

Maybe this can help you?


https://dsp.stackexchange.c...

The question stated here is for a low-pass filter, but you can probably get an estimation for the band-pass
frequencies as well by doing the suggested calculation twice (once for each frequency).

Hope this helps!


Reply Share

radyp09 5 months ago


Its really cool.
Thank you
Reply Share

Norwegian Creations Mod > radyp09 5 months ago


Thanks =)
Reply Share

azerimaker 2 years ago


Excellent write up. Keep it up NC team.
Reply Share

Norwegian Creations Mod > azerimaker 2 years ago

Thank you! :)
Reply Share

ALSO ON NORWEGIAN CREATIONS

An open source CNC Dust Shoe Designing and Building an Interactive LED Lamp
21 comments 3 years ago Technical Insights
AvatarNorwegian Creations We would love to do an in depth 6 comments 2 years ago
testing of various printers if we had them in house :P AvatarNorwegian Creations Cool to hear
ReformedConservative =D Take pictures!

PID Seesaw part 1: The Design Do It Yourself: Bose QC15 / QC25 Bluetooth Module
2 comments a year ago 2 comments 10 months ago

AvatarMarcelo Andrade Hello, do you have the 3D models so I AvatarNorwegian Creations Thanks, Kim.Yeah, those found on
can try to print these parts? Thanks! eBay and similar doesn't have much battery time. In the
specs on the one you linked to it says

Subscribe d Add Disqus to your siteAdd DisqusAdd Privacy

You might also like