Posted on

Learn MATLAB Episode #16: Low Pass Filters

 

In this tutorial we’re going to talk about low pass filters, and how to apply those to an audio stream. So, to first recap and kind of give you a different perspective on what we’ve been doing, we’re just going to listen to a sound wave at different sampling frequencies. So, I’m going to create my x-axis (0, 50*pi,10000) samples; I’m going to say y = sin(x); So, this sound is only at one frequency, so you’re it’s going to sound kind of like a buzz. So, I’m going to play the sound at a common frequency that’s used for recording mp3’s and waves 44100. So, let’s listen to this. Ok, and so now what I’m going to do is I’m going to play the same sound wave but at a lower frequency, or sorry a lower sampling rate, and that should give me what sounds like a lower frequency even though it’s the same wave, the same matrix of values. And so it indeed sounds lower pitched, and so now what we’re going to do is we’re going to create a low-pass filter. So, what that does is say we have multiple different sine waves playing, which is essentially what the Fourier transform is giving us, we’re going to filter the signal so that only the low frequency components can be heard. So, if we look at the typical shape of a low-pass filter you generally see things like this, and remember that this is only the left half of the plots that we’ve been looking at. When we’re in the frequency domain convolution turns into multiplication. So, right here zero decibels is actually equal to 1 because it’s a log scale, so we’re multiplying 1 by the signal at these low frequencies, but then as we go down here we’re multiplying a very small number by those higher frequencies. And so after we’ve multiplied one signal by the other one of them being this low pass filter, those high frequencies are going to be silenced. So, if you’ve never studied about low-pass filters before you may want to go and look up the different kinds, what’s their function representation, and the time domain and the frequency domain. We’re going to use a very simple version of the low-pass filter called the moving average. So, matlab already has a function for us that can create filters, we just need to pass in the b and the a which is essentially the different multipliers on the original signal. So, I’m going to set b = ones (40,1)/40; I’m going to call…I’m going to load my data first. [d,fs] equals audioread(‘helloworld.wav’); So, I’m going to say dlp = filter(b, 1, d); so I want 1 as the numerator and then b as the denominator, pass in the data. So, now I want to play the original hello world for reference “hello world” and I’m going to play the new low pass filtered version to compare “hello world” So, you can see the, or you can hear, the low-pass filtered version sounds very muffled, sounds like you’re speaking through a pillow or something, and that’s because high frequencies tend to not go through different medium, so low frequencies can go through a wall. So, if you hear your neighbors talking for instance it will sound a little muffled, and that’s the same thing that’s happening your wall is acting like a low-pass filter. So, let’s do another way of comparing these, I’m going to plot them on the same axis. It’s convenient because they’re the same size, ok. So, now if I zoom in a little bit..so you can see all the sudden changes in the dark green signal don’t happen in the light green signal which is the low-pass filtered version. That’s because when you see sudden changes that’s actually you can think of it as a very fast sine wave that’s super positioned on a low-frequency signal, and so we’re essentially just getting rid of those. Now yet another way to compare these two signals is to plot them in the frequency domain. So, for this we want to use subplots so that they do not overlap each other. Let me just do it all in one line…ok. So, remember that the original signal is on the top and the filtered version is on the bottom. So, now you can see that we’re keeping the low frequencies, but the high frequencies which seemed to have some components that are nonzero are all damping or they’re set to zero in the filtered version. Now if you’re interested in this what I would recommend is an exercise is to try to implement a high-pass filter. As a hint you’re going to want to use the same function filter, and everything else is going to stay the same.