Digital filtering is a widely used technique that is common in many fields of science and engineering. Filters remove unwanted signals and noise from a desired signal. There are many different kinds of filters, including low pass, high pass, band pass and band stop filters. In just the category of low pass filters, there is a large collection of filters that famous engineers and mathematicians have invented, including Hanning, Hamming, Blackman, Kaiser and Tukey windows. In this post, I will show you how to use Matlab’s filter function to remove a high frequency signal from a desired signal.
In the following example, the filter function is used to remove high frequency interference from a lower frequency signal. The most important lines in the code are as follows:
%Simple Low-Pass Filter b = 1; a = [1 -1]; %Apply Filter s3_f = filter(b,a,s3);
A simple low pass filter with a pole at +1 is used with the filter function. This filter has a transfer function ofMore sophisticated filters, which are presented on this Wiki, can be used by setting the a and b parameters as follows:
%Hanning N = 20; n = 0:N-1; b = 0.5*(1 - cos(2*pi*n/(N-1))); a = 1;
That example uses a Hanning Window, but any filter can be used by setting the b parameter to a different value. For a more detailed explanation of how the filter function uses the a and b parameters, see the Mathworks webpage.
Here are the figures that the code presented below generates. The first figure shows the original signal that we wish to retain. The second figure shows the original signal combined with interference at a frequency that is ten times higher. The third figure shows the signal after filtering with the simple low-pass filter. Note that the high frequency interference is gone, but the original signal has been distorted. Keeping the information in the desired signal intact is one of the main challenges for engineers using filters. The fourth and fifth figures show the frequency responses of the signals from the combined signal before and after filtering. These figures show the Fourier transforms of the second and third figures. Note that the signal at 10 Hz is greatly attenuated after filtering, while the signal at 1 Hz is almost the same as before filtering.
The following is the rest of the code in this example. This code should provide a good template for using the filter function with any type of filter and evaluating the results with the fft function.
%Example of How to Use the Filter Function %Parameters Fs = 100; tmax = 5; Nsamps = tmax*Fs; %Create Initial Signals t = 1/Fs:1/Fs:tmax; s1 = 10*cos(2*pi*t); s2 = 2*cos(20*pi*t + pi/4); s3 = s1 + s2; %Plot in Time Domain %Original figure plot(t,s1) xlabel('Time (s)') ylabel('Amplitude (V)') title('Original Signal') ylim([-15 15]) %Original + High Freq figure plot(t,s3) xlabel('Time (s)') ylabel('Amplitude (V)') title('Original Signal Combined With High Frequency Signal') ylim([-15 15]) %Filter Signals %Simple Low-Pass Filter b = 1; a = [1 -1]; %Apply Filter s3_f = filter(b,a,s3); %Scale Output s3_f = s3_f/15; %Plot Filtered Signal figure plot(t,s3_f) xlabel('Time (s)') ylabel('Amplitude (V)') title('Filtered Signal') ylim([-15 15]) %Frequency Domain f = Fs*(0:Nsamps/2-1)/Nsamps; %Prepare freq data for plot %Original + High Freq s3_fft = abs(fft(s3)); s3_fft = s3_fft(1:Nsamps/2); %Discard Half of Points figure plot(f, s3_fft) xlabel('Frequency (Hz)') ylabel('Amplitude') title('Frequency Response of Combined Signal Before Filtering') ylim([0 3000]) %Filter Signals s3_f_fft = abs(fft(s3_f)); s3_f_fft = s3_f_fft(1:Nsamps/2); %Discard Half of Points figure plot(f, s3_f_fft) xlabel('Frequency (Hz)') ylabel('Amplitude') title('Frequency Response of Combined Signal After Filtering') ylim([0 3000])