How to filter noisy sine sine signal using FIR filter

16 visualizzazioni (ultimi 30 giorni)
I want to apply FIR 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 for the design of Lowpass FIR filter using Hanning window
rp=input('Enter the passband ripple= ');
rs=input('Enter the stopband ripple= ');
fp=input('Enter the passband frequency= ');
fs=input('Enter the stopband frequency= ');
f=input('Enter the sampling frequency= ');
% Normalizing the frequencies
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
%Adjusting the filter order. The order of window must be an odd number and the order of filter must be one less than that of the window
if (rem(n,2)~=0)
n1=n;
n=n-1;
end
disp('Filter ordern n= ');n
w=hann(n1);
%Calculation of filter coefficients
b=fir1(n,wp,w);
%Filtering
z=filtfilt(b,1,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 (filter)');
figure, plot(b), title('Hanning lowpass FIR filter coefficient');
%Plott the filter response
figure, freqz(b,1,500,f);
title('Magnitude and Phase response');
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 passband ripple= 0.08
Enter the stopband ripple= 0.01
Enter the passband frequency= 2000
Enter the stopband frequency= 4000
Enter the sampling frequency= 10000
Filter ordern n=
n =
6

Risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by