Designing Digital Filters with MATLAB
From the series: Perception
Digital Filters are a fundamental component of digital signal processing. As demonstrated by Mark Schwab, MATLAB® and Simulink® provide an app to design filters for digital signal processing called the Filter Designer app. It allows users to interactively design, quantize, and analyze single- and multi-rate digital filters.
Mark uses the Filter Designer app to build a filter to filter a signal collected by a hydrophone from a RoboNation competition that can be implemented in both MATLAB and Simulink. This tool is very useful for student competition teams because it allows you to quickly realize Simulink models or generate MATLAB code for digital filters designed from within the app environment without having to author additional code. There are also options available to generate HDL code, C header files, and .COE files for the Xilinx® FPGAs.
Published: 29 Jan 2018
Hello, everyone, and welcome to another episode of the MATLAB and Simulink Robotics Arena. In today's episode, we're going to talk about an extremely fundamental concept in digital signal processing, and that is filter design. And to join me today, I've got Mark Schwab from the Engineering Development Group at the MathWorks. Hello, Mark. Do you want to introduce yourself and tell us why you are the best person to talk to us about filter design?
All right, Connell. Thanks for having me, by the way. So I work as an Application Support Engineer for MathWorks, and my domain is specifically signal processing and communication systems. So a lot of the tools that we use in this video I use on a regular basis in many of my projects.
Cool. Well, welcome to the Robotics Arena.
Thank you.
So, moving on, today's agenda. I'm going to talk a little bit about the motivation as to why we decided to do this video in the first place, following which, Mark's going to do a little bit of background on filter design and go into the math involved with it. And then we're going to do a software demonstration.
He's going to show us how to design a filter, and then implement it in MATLAB and Simulink. And we're going to use the Filter Design tool for this. And then finally, we're going to do some key takeaways and resources to help you guys do better in the competitions.
So starting off with the motivation, marine robotics student competitions involve a lot of signal processing, especially in the hydrophone task at competitions like robot, and robot sub, and robotics. But before getting into time, direction of arrival, and all that kind of stuff, you need to start small. And you need to start with filter design.
By the way, we do have a video on finding the direction of arrival of a signal in MATLAB in the Robotics Arena. So you might want to take a look at that. So, without further ado, I'm going to hand it over to Mark and have him get into MATLAB and do some background and take it away.
OK. So--
So go for it, Mark.
--before we get into the meat of this topic, I'm going to go over a little bit of background just to cover the basic mathematics behind filter design. So when working with filters, we're working with linear time invariant systems. And we have an input signal that's passed through the LTI system that contains an impulse or transfer function, and then we receive an output signal. In time, the process that's being computed in the LTI system is called convolution, and this is denoted by the first equation.
Yeah. Convolution is not easy. [LAUGHS]
As you can see-- yeah, the equation is quite complicated. This definitely isn't ideal. So a common practice in signal processing is to convert everything into the frequency domain. And what this does is this converts the convolution process into multiplication, which is much simpler.
So start with how we convert a signal from the time domain to the frequency domain.
To convert a signal to the frequency domain, we use the Fourier transform.
OK. OK.
And in the software demonstration, I'll talk a little bit about the tools that MATLAB and MathWorks products provide to support this workflow.
Cool. OK.
So let's take a look at some of the fundamental filters. Perhaps the two most basic filters are the low pass and the high pass filter. As implied by the name, the purpose of the low pass filter is to preserve low-frequency components and to attenuate, or remove, high-frequency noise or unwanted high-frequency components. Conversely, the high pass filter preserves high-frequency components and attenuates low-frequency components.
That's pretty simple.
Two slightly more complicated filters would be the band pass and the band stop filter. The purpose of the band pass filter is to preserve frequency components within a specific frequency range.
OK.
And then conversely, the band stop filter attenuates frequency components within a specific range.
OK.
So--
So these are all pretty simple, pretty common filters in digital signal processing. Right?
Correct. These are considered ideal filters.
OK. OK.
So typically when we have ideal filters, they're not the best choice for application. They're more for analysis and--
And theory? [LAUGHS]
And theory, yeah. They're more for analysis and theory.
OK.
So, without further ado, let's go ahead and jump into the software demonstration.
All right.
All right. So the data that I chose to use here is actually data recorded from a submarine ping sensor from one of the robotics competitions.
OK.
So let's go ahead and load in this data real quick. So in the workspace, you can see what I'm loading in. y denotes the data that's-- y denotes the data that was recorded from the ping sensors, and t denotes a time vector. And then noise is the noise that we're going to add to the signal to filter out. And then duration is just the time of the signal.
And just to clarify, this noise is something that you're adding in just for the purpose of the demonstration, right?
Yeah. For the purpose of the demonstration, I pretty much-- I generated a random number generator that creates random noise.
OK. OK.
So there's the uncertainty involved, and we're going to talk--
Yep. Yep.
And in real world, it's hard to predict the noise that's going to happen. So this is supposed to mimic a real-world application.
OK. OK. Cool.
So let's go ahead and take a look at the recorded data in time. As you can see, there's three distinct pings that the hydrophone has the task of identifying.
Correct.
So next, we're going to add the noise that I mentioned to the signal, and then compare the original signal in time with the noisy signal.
Oh, yeah. I cannot recognize the signal anymore. It's just random noise at this point.
Yeah. So, as you mentioned, the signal is almost indecipherable. There's a little bit of correlation. But even where they look slightly similar, there is an amplitude offset.
Correct. Correct.
So, apart from converting convolution to multiplication, analyzing signals in the frequency domain is also beneficial because it can make plots like this a little more clear. So let's go ahead and convert this signal to the frequency domain, as I mentioned before, and see if we can get a better idea of how we can filter out some of this noise.
Just before you do that, this fft function, is that the Fourier Fast Transform that you were talking about that converts signals from time to frequency domain?
Yeah.
OK.
In MATLAB, fft denotes the Fast Fourier Transform and will complete the task of converting the signals to the frequency domain for us.
OK.
So we're going to go ahead and convert both the original signal and the noisy signal into the frequency domain, and then take a look at the two so we can compare. As you can see, analyzing the signal in the frequency domain gives us a really good idea of the significant frequency components from the original signal and where the noise is present. The pulses that we can see around 50khz and 125khz are the two main frequency components of the original signal, and then the bands of dense frequency components would denote the noise.
You may be wondering why there's two distinct bands where the noise are present. In robotics competitions, there are pretty common sources of noise. One obstacle that many competitors face is the noise generated by the motor of their own robots often distorts their information.
Correct. Yeah.
And this would be denoted by the low-frequency noise that lies within the audible range of 22,000khz.
Correct.
And then the high-frequency noise could be denoted by other robots, sonar, or other extraneous factors.
OK.
All right. So let's jump into designing a filter and open the Filter Designer tool. The Filter Designer tool allows you to design filters via radio boxes, dropdown menus, and text boxes. And all of these lie within specific components like the response type, the design method, the filter order, frequency specifications, et cetera.
OK. And just going back to when we were talking about the background, I can see it's got the low pass, high pass, band pass, band stop. So yeah, we actually mentioned those filters earlier in this presentation.
Yeah. In each one of these dropdowns, there's slightly more complicated methods to implement the low pass, high pass.
OK.
Because, as we mentioned, ideal filters are not useful in applications. So there's slightly more intelligent filters provided.
OK.
Also, this tool implements several algorithms. As you can see in the filter order, the minimum order box is checked. This implements an algorithm that will generate a filter of the minimum order based on your specifications.
This tool also provides another feature that allows us to observe several different response types for the filter. Some of these response types are the magnitude response, the phase response, group delay, phase delay, impulse response, step response, and the pole-zero response. There are several other options. And if you're interested in learning more about them, I'd recommend looking at the documentation for the Filter Designer tool.
So now let's see if we can go ahead and design a filter to attenuate some of the noise. As I mentioned earlier, our signal is being sampled at 300khz. So let's go ahead and put that in.
And then since we're using a low pass filter, we're going to try to preserve some of the low-frequency noise and attenuate out some of the higher frequency noise. So, taking a look at the plot of our noisy signal, we can see that there's noise somewhere above 125khz. So let's go ahead and set the passband to be up to 125khz.
And then in real filter application, square waves are not practical, because the filter needs some time to drop down to the stopband. So we're going to need to give the filter a little bit of space to reach the stopband. So let's give it around 2.5khz and set this at 127.5khz. And then we'll design our filter.
OK, so this looks like a great filter. Now that we have the filter, you may be wondering, how do we actually implement this in MATLAB? The Filter Designer tool provides the option for us to export the filter as an object to the workspace.
To do this, you navigate to the File tab and then go down to Export. A dialog box will pop up giving you a few options. For simplicity, we're going to export this as an object. We can go ahead and keep the default name.
Now, if you go to the MATLAB workspace, you can see we have an ID object. We have an ID filter object. The way to implement this filter is to use the Filter function. As I mentioned, we chose to export this as an object for simplicity, because then the Filter function only applies to the filter object as an argument and then the signal we are filtering.
OK.
And then for analysis, we're going to convert this filtered signal into the frequency domain. Let's compare the original signal in time to our filtered signal in time. So looking at this plot, maybe our choice of filter parameters were not the best, or maybe even our choice of filter was not the best. So let's look at the frequency domain and see where we might have gone wrong.
So, as you can see, we did a good job of attenuating the high-frequency components. But almost as expected, we still have that low-frequency noise. And then from the plot in time, we see that the noise is still enough to drastically distort the signal.
Correct.
So let's jump back into the Filter Designer tool and see if we can do a little bit better. So a low pass filter might not have been the best option because there's low-frequency noise that we cannot remove from the noisy signal. So let's see if we can try one of the slightly more complicated filters that we mentioned earlier. So let's take a look at the band pass filter.
Since we were able to attenuate the high-frequency noise successfully using the low pass filter, we're going to use the same parameters for the passband and the stopband on the higher edge of our filter. So let's pull up the frequency plot to see if we can identify parameters that will be successful in attenuating all of the noise. Looking at the noisy signal, we can see that there are significant frequency components from the original signal, slightly below 50khz, and that the noise is within the audible range. So it's probably somewhere less than 22khz.
So let's go ahead and set the stopband to be a little over 22khz. And again, let's give the filter some time to rise to the passband. So now we have a filter that looks like it'll preserve a specific band. So let's export this to the workspace and see if this yields the desired result.
So when exporting variables, if there's a variable of the same name, you're going to want to choose to overwrite the variable, or else you will receive an error. So let's export this and then jump back into our code and implement this new filter. All right. So looking at the signal in time, it looks like we did a good job.
Yeah.
So let's go ahead and confirm that by, again, looking at the frequency domain. In the frequency domain, this looks great. We have all the same significant frequency components from the original signal. But perhaps we could even do a little bit better. So let's go ahead and go back into the Filter Designer tool and take a look at some of those other analysis options that I mentioned earlier to see if this filter is actually optimal for our workflow.
So before we move on, what's the first place that you start when you want to look at making these filters better? What is the first plot that you go to?
So the pole-zero plot is a very useful plot, because--
OK.
We'll go ahead and take a look at it, and I'll describe that a little more. So the pole-zero plot, as mentioned in the name, provides the poles and zeros. And this gives an idea of what the filter-- this gives us an idea of what the order of the filter is. As you can see here, there's 304 poles. So that means that the order of filter is 300--
That's not too good. [LAUGHS]
Yeah. So that means the order of the filter is 304. And for implementation and hardware, that would use an unnecessary amount of memory. So let's go ahead and see if we can change our filter a little bit to reduce the order. As you can see, the option for FIR is chosen in the design method.
FIR filters are more-- are beneficial in the sense that they're theoretically simple. IIRs are slightly more complicated theoretically, but this provides a better result in terms of filter order. So let's choose the IIR and see if this improves our filter.
Oh, yeah.
OK, so--
[INTERPOSING VOICES]
--86 Yeah.
Yeah. So now we see that the filter order is around 172, since we have 172 zeros. So this is much better, but this would still utilize a very large amount of memory. So let's see if we can choose another type of IIR filter to improve our filter even more.
For more information on these types of filters, I'd recommend taking a look at our documentation. I'm going to go ahead and choose the elliptic filter as somewhat of an educated guess and then see what this does. So now we can see that the filter order is around the magnitude of 20, which is much, much better than the 304 we started with.
Yeah. So before we move on, you did mention hardware implementations. Is there a way that I could deploy this filter onto a hardware directly from MATLAB and Simulink? Or do I have to take these coefficients and then write code for it standalone?
Of course. Yeah. So you mentioned MATLAB and Simulink. I'm going to go ahead and start with MATLAB.
OK.
Within the Filter Designer tool, if you go to the Targets pane--
Oh, OK.
Yeah. There's options to generate code--
To generate code. Yeah.
--for several different hardware targets.
OK.
There's also, if we go back over to File, the option to generate MATLAB code. And--
Correct.
--from there, code generation could be used to implement to filters.
OK.
But we can also export to a Simulink model.
Yeah. I'm interested to see the Simulink implementation, because I know a lot of our viewers are actually using Simulink. So do you want to go ahead and show us how to do that?
Sure. Of course. So I'm going to go ahead and choose the Export to Simulink option, and then click Realize Model. If no model is currently open, what this option will do is open a blank model with the filter block. But I actually already have a model open that implements a similar workflow to the software demonstration we just covered.
OK.
So, as you can see, a filter block was added to the model I already had open. So before we go. I'm going to-- so I'm going to go ahead and connect this block. But before we move further, I'll talk a little bit about what this model does.
The model reads in an input signal, and then a noisy signal, and adds the two signals together. Then these signals are passed through a filter. And we can observe the output at all three stages through a scope.
Coming down from the scope, you can see a transform subsystem. I'm going to go ahead and click on that and take a little bit of time to discuss what's going on. So the Fast Fourier Transform is a process that's computed on a vector of information.
But Simulink runs on sampled time. So if we were to just simply implement the FFT block in Simulink, the transform would be computed for each sample time. So we would be taking the transform of scalars rather than vectors.
Correct. OK.
So what we do here is we first use a zero-order hold to ensure that our signal is in the discrete time domain, and then we use a buffer to store these signals into a vector--
To a vector. OK.
--so then we can use the Fourier Transform. So then, of course, like we did in MATLAB, we take the Fourier Transform and then take the absolute value to analyze the data. And then we use a special vector scope that's set to analyze the data in the frequency domain. Let's take a look at the results of the Simulink model to ensure that they're similar to the MATLAB code.
OK.
So first, we'll take a look at the signal in time through the scope. So, as you can see, it's clear that this is very similar to the signals that we had in MATLAB. So let's jump over to the signals in the frequency domain and ensure that that's also-- so since we have three different vector scopes, there's going to be three different vector plots. The vector scope does not support multiple inputs, so we cannot stack plots like we did in the scope block.
But as you can see again, the signal in the frequency domain also pretty strongly correlates the signals from MATLAB.
OK. Cool.
If you would like to follow the same workflow without using the MATLAB interface, the Digital Signal Processing Toolbox also has a block used to generate digital filters.
OK.
To find this block-- let's go ahead and go into the library.
OK.
To find this block, navigate to the DSP System Toolbox, choose Filtering, and then choose Filter Implementations, and there will be a Digital Filter Design block. So let's go ahead and drag this into the model. To use the Digital Filter Design block, double-click on the block, and a graphical user interface identical to the one we used in MATLAB will be presented.
OK.
In implementing this block, you do not have to worry about exporting to Simulink. When you click the Design Filter button, those filter parameters will automatically be uploaded into your Simulink model.
OK.
As I mentioned earlier, there's also the option to generate code for hardware in Simulink. To learn more about this, I'd advise taking a look at the documentation. All right, so now let's go ahead and jump back to the presentation to look at some key takeaways.
So the Filter Designer tool allows for an iterative design. And this is beneficial because in competitions, the competitors likely won't know the parameters that they want to use for their filters. So they're going to have to use a trial and error approach to really reach the best filter for their robot.
OK.
Implementation for filter design is available on both MATLAB and Simulink, and hardware implementation is also available on both platforms
OK. Well, thank you so much, Mark, for taking the time out to come and share this information with us. Before we wrap up with this video, I'm just going to go ahead and point you guys to some resources like we do always. Again, we've got a lot of video content available and a lot more to come. Please write to us and tell us what you think about it.
And you can get in touch with us either on our-- either at roboticsarena@mathworks.com, or on our Facebook group. And then also, don't forget about the software offer. We do offer free MATLAB and Simulink with around 70-odd products to student competition teams. So please take advantage of that offer. And you can get access to tools like the Filter Design tool and a bunch of other tools.
And finally, we're also filling up the Robotics section of the Racing Lounge blog, so don't forget to take a look at that as well. And thank you so much for watching another episode of the Robotics Arena. And we hope to see you again.