Search code examples
matlabfilterfftlowpass-filterbandpass-filter

Unable to get filtered bandpass signal in matlab


I'm trying to extract the signal with 15hz(bandpass filter) component but not getting answer.However I could get the 10hz(lowpass filter).

I have 3 signals of three different frequencies 10 15 and 30hz respectively.

I've designed them as follows:

x=0:1/fs:1;
f1=10;
f2=15;
f3=30;
s1=sin(2*pi*f1*x);
s2=sin(2*pi*f2*x);
s3=sin(2*pi*f3*x);

Now I've added all of them...

s=s1+s2+s3;

I've got correct fourier transform:

Combined signal Fourier transform

Butter command is not working here

This is what I've tried:

[b,a]=butter(10,[12 18]/500); % 500 is fs/2
filtered=filter(b,a,s);

I'm getting the following for filtered:

Filtered Original

Its fourier transform is this :

Fourier transform of filtered signal

I'm not even able to guess why I'm getting a peak at 0

If it is lowpass I'm getting a perfect output:

[b,a]=butter(10,10/500);
filtered=filter(b,a,s);

Low pass output

What is reason behind this ? Please tell me whether there is another approach to solve this..Thankyou :)

EDIT:

This is complete code :

clc;
close all;
fs=1000;
x=0:1/fs:1;
f1=10;
f2=15;
f3=30;
s1=sin(2*pi*f1*x);
s2=sin(2*pi*f2*x);
s3=sin(2*pi*f3*x);
s=s1+s2+s3;
figure
x_axis=linspace(-fs/2,fs/2,numel(x));
plot(s)
fourier=fft(s);
answer=fftshift(fourier);
plot(x_axis,abs(answer));
figure
[b,a]=butter(10,10/500);
filtered=filter(b,a,s);
plot(filtered);
figure
plot(x_axis,abs(fftshift(fft(filtered))));

Solution

  • A tenth order Butterworth is wayyy too big here (and in general). If you look at the filter coefficients b, you’ll see they’re on the order of machine precision (i.e., ~1e-16). Start with a second or third-order Butterworth filter! See if butter(2, 10/500) gives sufficient attenuation and carefully increase the filter order from there.