Why my butterworth low pass filter doesn't work?

조회 수: 9 (최근 30일)
Betsey
Betsey 2012년 4월 17일
I want to do a low pass filter, in order to test if my filter work properly, I first use a simple data to test. I compose a data with two frequency,one is 100Hz and the other is 300Hz in a cosine function. I make a low pass filter for a cut off frequceny of 300Hz, but the result seems to be wrong,the 300Hz signal still exist in my filtered data, anybody can help me to find the problem? Many thanks! here is my code:
clear;
clc;
fs=1000; % samping frequency
fc=300; % cutoff frequecny
t=0:1/fs:0.6;
xc=length(t);
f1=100; % frequency for composing my data
f2=300; % frequency for composing my data
x=cos(2*pi*f1*t)+cos(2*pi*f2*t); % raw data
n=numel(x);
f=(transpose(((0:n-1)*(fs/n)))); %Frequency
v=fft(x,n); %Discrete Fourier Transform
plot(f,real(v),'r');
grid on
Wn=2*fc/fs; % the normalized cutoff frequency for butter function
nn=6; % the order
[b,a] = butter(nn,Wn,'low');
nv=filtfilt(b,a,v);
vv=ifft(nv);
subplot(2,1,1)
plot(f,nv)
subplot(2,1,2)
plot(t,real(vv),'r')

채택된 답변

Honglei Chen
Honglei Chen 2012년 4월 17일
I don't understand. I modified your code a bit to do
  • Use filter operation in time domain
  • Use 200Hz as cutoff frequency
  • Examine magnitude of spectrum
The result looks pretty good
clear;
clc;
fs=1000; % samping frequency
fc=200; % cutoff frequecny
t=0:1/fs:0.6;
xc=length(t);
f1=100; % frequency for composing my data
f2=300; % frequency for composing my data
x=cos(2*pi*f1*t)+cos(2*pi*f2*t); % raw data
n=numel(x);
f=(transpose(((0:n-1)*(fs/n)))); %Frequency
v=fft(x,n); %Discrete Fourier Transform
plot(f,abs(v),'r');
grid on
Wn=2*fc/fs; % the normalized cutoff frequency for butter function
nn=6; % the order
[b,a] = butter(nn,Wn,'low');
nv=filter(b,a,x);
vv=fft(nv);
subplot(2,1,1)
plot(f,abs(vv));
subplot(2,1,2)
plot(t,real(nv),'r')
  댓글 수: 1
Betsey
Betsey 2012년 4월 17일
Yes, it looks just like what I want to do. I will try it on my real data. Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Honglei Chen
Honglei Chen 2012년 4월 17일
Please format your code. But just a quick note, if you set cutoff frequency at 300Hz, the 300Hz component will not be eliminated, only reduced by half.
  댓글 수: 4
Betsey
Betsey 2012년 4월 17일
But the 100Hz signal is also reduced. I'm confused, how does the low pass mean then? Do you have any good suggestions for low pass filtering? thanks!
Honglei Chen
Honglei Chen 2012년 4월 17일
I looked at your code, why are you filtering v, which is the spectrum of the signal x? It should be just nv = filtfilt(b,a,x).
I'm not sure if you intentionally chose filtfilt, but you could just use filter
Finally you may want to plot abs(v) instead of real(v). After you get nv, you also want to plot abs(fft(nv)) to examine the spectrum of the output signal

댓글을 달려면 로그인하십시오.


Betsey
Betsey 2012년 4월 17일
I wamt to find a low frequcncy signal in my data, but my data is a little noisy and the low frequency signal maybe not so strong, so I want to do fft for the signal first to convert it into frequency domian, so I do fft first and do filter for the fft of my original data.
  댓글 수: 1
Honglei Chen
Honglei Chen 2012년 4월 17일
Then the operation in frequency domain should be multiply, not convolution and after all, it is the same as the convolution in time domain

댓글을 달려면 로그인하십시오.


Betsey
Betsey 2012년 4월 17일
Yes, you are right. I made fft for the butterworth filter too in another code, but the result seems to be the same.

카테고리

Help CenterFile Exchange에서 Single-Rate Filters에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by