- Learn to use different types of filters for signal processing
- Arduino IDE
When we take readings from a sensor, it is common to find some noise mixed in with the data. In such a case, noise can be defined simply as unwanted information. Since it’s unwanted, it needs to be removed, so that we can use the data which we need.
To remove noise, we use filters. By principle, filters block unwanted information, and only let the required data pass through it.
We will be dealing mainly with three types of filters: Low-pass, High-pass, and Band-pass.
Terminology associated with filters
The three filters we are discussing work on a common assumption that the frequency of the data signal is significantly different from the noise signals. Let’s discuss a few terms related to filters. Consider the following diagram of an ideal low-pass filter. You will learn more about it later in this unit.
A filter also has the capability of amplifying a signal. This is signified by a factor called gain. A gain of 1 means that the signal amplitude will have the same amplitude as before. A gain more than 1 signifies that the signal is being amplified (amplitude is being increased). Conversely, a gain of less than 1 signifies that the signal is being attenuated (amplitude is being reduced).
A gain of zero means that the signal is attenuated completely. This essentially means that the signal is blocked. So it becomes obvious that we need to have a gain of 1 for the data signal and a gain of 0 for the noise signal.
A pass band denotes the range of frequencies that are allowed to pass through the filter. A filter usually has a gain of 1 for the pass band.
The stop band denotes the range of frequencies that will be attenuated (blocked) by the filter. This band has a gain of zero.
Cutoff frequency (fc)
This is the frequency which determines the separation of the pass band and the stop band. It decides which frequencies are data and which frequencies are noise. It is called “cutoff” frequency since it decides which part of the signal is noise and hence needs to be blocked.
Low pass filter
As the name suggests, this filter allows frequencies lower than the cutoff frequency to pass through, and it blocks frequencies higher than the cutoff frequency. An ideal low pass filter looks like the figure above, where the difference between the pass band and stop band is clear. However, a realistic low pass filter will look like follows:
To implement a low pass filter in Arduino code, we make use of a concept called the difference equation.
When taking sensor readings, we don’t get a continuous stream of data. Instead we get samples of data at regular intervals of time. So the simplest way to implement a low pass filter is as follows:
y(n) = α*x(n) +(1-α)*x(n-1)
Here, α (alpha) is a constant. y(n) is the nth output sample. x(n) and x(n-1) are the nth and (n-1)th input samples respectively.
To find the value of alpha, a few calculations are required:
sampling rate = number of samples/second
If we use a delay of 10 ms between sensor readings, this will be 100 samples/second.
dt = 1/sampling rate = 1/100 = 0.01
fc = cutoff frequency. Let’s assume it to be 10 initially.
The following calculations need to be made:
RC = 1/(2*pi*fc)
alpha = dt/(dt+RC)
This gives alpha = 0.38 for the assumed values. As we reduce the cutoff frequency, value of alpha also goes down. We can determine the optimum value of alpha using trial and error from here on. In the following code, we have chosen to use a value of alpha = 0.2.
High pass filter
As the name suggests, this filter allows frequencies higher than the cutoff frequency to pass through, and it blocks frequencies lower than the cutoff frequency. An ideal high pass filter looks like the figure above. However, a realistic low pass filter will look like follows:
Band pass filter
This filter only lets a particular range of frequencies pass through it. The range is decided by two cutoff frequencies, fc1 and fc2. All frequencies lower than fc1 or higher than fc2 are cut off. Frequencies higher than fc1 and lower than fc2 are allowed to pass through.
A realistic band pass filter looks like follows: