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])
Very good article. I certainly love this website. Keep writing!
Hi, I am a life science person and not so familiar with matlab or signal processing. I can just read codes and understand what is happening, but not write codes. Still learning Matlab. My question is, I want to isolate a biomedical signal of a certain frequency range. First, the raw signal I have with me is an averaged waveform obtained after sampling for 500 trials. To obtain that waveform, the filter range used was 30-2500Hz. For my experiment, I want to isolate frequency between 450-750 Hz by using a Bartlett Hanning window. How should I go about it? Can somebody share the code.
Definitely consider that that you said. Your favorite reason appeared to be at the internet the easiest factor to take into accout of. I say to you, I certainly get annoyed while other folks think about worries that they just do not realize about. You managed to hit the nail upon the highest and also outlined out the whole thing without having side effect , other folks can take a signal. Will probably be again to get more. Thanks
sir i want to know how to design a simple digital filter by using matlab programming
Dear sir,
I want to know how to design FIR filter with signed power of two coefficient.I mean how to implement this in matlab
thank you so much
I have question.
Is there anyone who knows about this?
“In the Code”
%Scale Output
s3_f = s3_f/15;
Why “s3_f” should be divided by 15 ?
And where it derived from 15
I have question.
Is there anyone who knows about this?
“In the Code”
%Scale Output
s3_f = s3_f/15;
Why “s3_f” should be divided by 15 ?
And where it derived from 15
Thanks a lot, your post was very useful for my project.
Best regards my friend.
Thanks friend, your post help me a lot in my design.
Best regards.
i need to convert LPF TO LPF ,HPF,BPF,BSF can u give coding in matlab
hi, i want to remove a frequency and its sideband from the frf response, how can i do this?
Why do we need to scale resalt ?
%Scale Output
s3_f = s3_f/15;
How to retain voice and remove instruments from a song? Plz tell me the logic behind it. A matlab code will be more helpful.
Thank you
this information is really fruitful.. can you please explain or give an example(in matlab) of how to apply window or a filter to remove background music from a song.. thanks
Would you please send me the program for parks algoritm in matlab.
Thanks a lot
Can please give the code for LPF filter using window without using the inbuilt filter function
THANKS
THANKS A LOT..
IT REALLY WORKS
thanks
can i also get the codes for bandpass?
thank you sir
WOW…Welldone guys..ths has helped me immensely..Thank you for this great work.
thank you, this was so well explained! im sure it will help a lot of beginners with matlab. it really helped me!
hola necesito urgente un programa de filtros digitales en matlab para presentarlo en un informe por favor si alguien que sabe de esto me puede ayudar se lo agradeceria toda la vida.
Dear Sri,
Thanks so much for your comments. I really appreciate them. We plan to do more posts on DSP and communications in the future.
-Eric
Hi guys you are doing a great job.Your posts are extremely useful especially to the beginning level students of signal processing in understanding the basics of filtering in matlab. It would be nice if you also explain advanced concepts in signal processing like wavelets, fractal,adaptive filtering techniques, denoising non stationary signals.
Any work in this direction would be greatly appreciated.