Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Industries Academia Support User Community Company

 

Product Support

1704 - How Can I Use the Signal Processing Toolbox to Filter My Data?


Assume Y is a vector containing your data. Recall, too, that half the sampling frequency is the highest frequency that can be fully reproduced in your signal, according to the Sampling Theorem. A sample vector used in this Technical Note is:

% Sampling frequency:
Fs = 1024; 
		
% The maximum frequency that % can be shown in the spectrum is % always half the sampling % frequency: Fn = Fs/2;
% Time vector, 1 second long, % sampled at Fs Hz: t = 0:1/Fs:1;
% A sine wave of 200 Hz Y = sin(2*pi*t*200);
% Look at the sound in the % frequency domain: PlotFFT(Y,Fs);

PlotFFT is a short M-file that displays the magnitude of the FFT of a signal.

Save this file somewhere on your MATLAB path, as it will be used throughout this Technical Note.

Now, add some random noise to the signal:

% Ynoisy is our original
% sound, plus some random
% noise
Ynoisy = Y + randn(size(t));

% Plot the FFT of the noise % signal PlotFFT(Ynoisy,Fs);

If you look at the FFT of the noisy signal, you can clearly see the location of the 200 Hz sine wave, but you can also see the extra frequency components contributed by the noise.

Now design a simple filter to eliminate the noise. You want this filter to pass the frequencies around 200 Hz, our sine wave, and eliminate all the rest.

The important thing to remember when designing a filter is that the frequency vector that you pass to the filter design functions must be normalized to the Nyquist frequency. 0.0 corresponds to 0 Hz, and 1.0 corresponds to the Nyquist frequency. So, our normalized frequency is given by the equation

Fnorm = Fc/Fnyquist.

For more information about frequency normalization, see the chapter on Filter Requirements and Specification, of the Signal Processing Toolbox User's Guide.

The passband should be around 200 Hz, so 200/500 = .4

So when designing the filter, you need to pass frequencies around .4, so pass BUTTER the vector [.35 .45]. BUTTER will generate a filter that passes this band of frequencies.

% Generate a band-pass
% filter
[b,a] = butter(4, [0.35 0.45]);

Now look at the frequency response of our filter, and decide if it is good enough.

freqz(b,a)   % Plot the response with the FREQZ function
      

At this point, all that is really important is the magnitude. The filter already has a pretty good cutoff, but it could be sharper. One way to do this is to increase the filter order, and tighten the passband a little:

[b,a] = butter(10, [0.385 0.415]);	
freqz(b,a)
      

Now, the cutoff is pretty good and the passband is tight, so go ahead and apply the filter to the noisy signal with the FILTER function:

% Apply the filter
Yfiltered = filter(b,a, Ynoisy);

% Look at the filtered % spectrum: PlotFFT(Yfiltered,Fs);

You can see that most of the noise is gone. There is still a little left around 200 Hz, because we passed that frequency band. However, the filtered signal is pretty close to the original signal.

Another interesting thing to look at is the group delay incurred by the filtering operation. The group delay is the amount of time the filtered signal is delayed by. To see the group delay, try the following command:

grpdelay(b,a);
      

You should see that at Fnorm = .4, where we've centered the passband, that the group delay is around 150 samples. Now, have a look at the very beginning of the filtered signal, compared to the original signal:

plot(Yfiltered(1:200))
hold on
plot(Y(1:200),'r');
      

You should see that the filtered signal is the same as the original signal, but it doesn't reach steady-state until after approximately 150 samples. This is the effect that group delay has on a signal. It delays a certain frequency by a specific number of samples.

Contact support
E-mail this page
Print this page