How to filter noisy signal by using IIR filter
Show older comments
I want to apply IIR filter to noisy sine signal but I am not sure if my programming is correct because the filtered signal that I got is not that smooth. Can somebody help me on this? Thank you in advanced!
% Sine signal with noise
Fs = input ('Enter the sampling frequency of the sine signal (Hz): ');
amp = input ('Enter the amplitude of the sine signal: ');
f = input('Enter the input frequency of the sine signal (Hz): ');
phase = input('Enter the phase of the sine signal (rad): ');
Ts = 1/Fs;
t = 0:Ts:10;
randn('state',0);
y = amp*sin((2*3.14*f*t) + phase) + 0.5*randn(size(t));
%Program to design a Butterworth Highpass filter
fp=input('Enter the pass band frequency fp = ');
fs=input('Enter the stop band frequency fs = ');
rp=input('Enter the pass band attenuation rp = ');
rs=input('Enter the stop band attenuation rs = ');
f=input ('Enter the sampling frequency f = ');
%Normalized the frequencies
wp=2*fp/f;
ws=2*fs/f;
%Calculate the filter order
[n,wn]=buttord(wp,ws,rp,rs);
disp('Filter order n= ');n
%Calculate the filter coefficient
[b,a]=butter(n,wn,'high');
% Filtering
z=filtfilt(b,a,y);
%Plot the signal
subplot(2,1,1), plot(y), title('Sine signal with noise');
subplot(2,1,2), plot(z), title('Filtered sine signal');
figure, plot([b,a]),title('Butterworth Highpass IIR Filter Coefficient');
%Plot the filter response
figure, freqz(b,a,500,f);
title ('Magnitude and phase response of the IIR butterworth filter');
As an example;
Enter the sampling frequency of the sine signal (Hz): 100
Enter the amplitude of the sine signal: 2
Enter the input frequency of the sine signal (Hz): 1
Enter the phase of the sine signal (rad): 0
Enter the pass band frequency fp = 2000
Enter the stop band frequency fs = 4000
Enter the pass band attenuation rp = 0.8
Enter the stop band attenuation rs = 45
Enter the sampling frequency f = 10000
Filter order n=
n =
5

2 Comments
Star Strider
on 1 May 2015
I did not run your code, but I see some problems that you need to resolve:
In this line:
f = input('Enter the input frequency of the sine signal (Hz): ');
you then overwrite ‘f’ with this line:
f=input ('Enter the sampling frequency f = ');
so you need to name them differently and then use them appropriately in your code.
Also, you have to normalise by the Nyquist frequency (sampling frequency/2):
wp=2*fp/f;
ws=2*fs/f;
not the sampling frequency, and check to be sure that ‘fp’ and ‘fs’ are less than f/2.
Here, you probably want a lowpass filter instead:
[b,a]=butter(n,wn,'high');
Experiment with these and see if these changes improve your code.
(I’m not listing this as an Answer because it isn’t one.)
Nur Fauzira Saidin
on 2 May 2015
Edited: Nur Fauzira Saidin
on 2 May 2015
Accepted Answer
More Answers (0)
Categories
Find more on Digital Filter Design in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!