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): 100Enter the amplitude of the sine signal: 2Enter the input frequency of the sine signal (Hz): 1Enter the phase of the sine signal (rad): 0Enter the pass band frequency fp = 2000Enter the stop band frequency fs = 4000Enter the pass band attenuation rp = 0.8Enter the stop band attenuation rs = 45Enter the sampling frequency f = 10000Filter order n= n = 5
Best Answer