Low pass filter with butter does not filter the signal and decreses scale

6 vues (au cours des 30 derniers jours)
Lukas Lohr
Lukas Lohr le 15 Déc 2020
Commenté : Star Strider le 15 Déc 2020
Hello
i need to create a filter with the following conditions:
"For data sampled at 1000 Hz, design a low-pass filter with lessthan 3 dB of ripple in the passband, defined from 0 to 40 Hz, andat least 40 dB of attenuation in the stopband, defined from 200 Hz to the Nyquist frequency. The filter order should be less than 6."
My Code so far looks like this:
clear;
clc;
fs = 1000; % sample frequency = 1000 Hz
ny = fs*0.5; % nyquist frequency
fc = ny*0.2; % cutoff frequency at 20% of nyquist ny*0.2
t = 0:1/fs:2; % sampling at 1000 Hz for 2 seconds
x = sin(2*pi*t); % 1 Hz signal
y = sin(2*pi*30*t); % 30 Hz noise
%add signal and noise
k = x+y;
%plot the inital signal without and with noise
figure(1);
subplot(3,1,1);
plot(x)
title('signal w/o noise');
subplot(3,1,2);
plot(k)
title('signal with noise');
% from documentation of the buttord function from MatLab
Rp = 3; % dB passband ripple
Rs = 40; % dB stopband ripple
Wp = 40/500; % passband defined from 0-40 Hz
Ws = 200/500; % stopband defined from 200-500 Hz
% get Order and corresponding cutoff frequency
[n, wn]= buttord(Wp,Ws,Rp,Rs);
% get coefficients of the filter
[b,a] = butter(n,wn*2*pi,'low');
% filter the original signal with the previous coefficients
z = filter(b,a,k, [], n);
% plot the filtered signal
subplot(3,1,3);
plot(z)
%axis ([0 2500 -2 2 ]);
title('filtered signal');
% plot what the filter is doing
figure(2)
freqz(b,a)
Unfortunately the filtered signal does not look as desired. It is in a very small scale in the y axis and the noise is still in there.
Help would be appreciated. Thank you.

Réponses (1)

Star Strider
Star Strider le 15 Déc 2020
Two problems that I see, just looking at your posted code:
First,
[b,a] = butter(n,wn,'low');
‘wn’ is already normalised, so multiplying it by 2*pi is not necessary, and results in a higher passband than you want.
Second, use filtfilt to do the actual filtering.
The filter is preforming as designed. The noise frequency is 30 Hz, and the filter passband is 40 Hz, so it is passing everything. If you increase the noise frequency to something beyond 40 Hz (perhaps 60 Hz) you can easily see the filter effect, with significantly attenuated noise.
  2 commentaires
Lukas Lohr
Lukas Lohr le 15 Déc 2020
Thank you very much. This solved the Problem :)

Connectez-vous pour commenter.

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by