Search code examples
matlabsignalssystemlowpass-filter

Matlab - using low pass filter on a system


i have to following code:

clc
clear all
close all

Fs = 6000;                    % Sampling frequency
T = 1/Fs;                     % Sample time
N=512;                        % Length of signal
t = (0:N-1)*T;                % Time vector


% Sum of a 1kHz sinusoid and a 2.5kHz sinusoid
x1=4*sin(2*pi*1000*t);
x2=sin(2*pi*2500*t); 

figure   
subplot(3,1,3)
stem(t,x1);

figure
subplot(2,1,1)
stem(t,x2);

x=x1+x2;
y=fft(x);
fx=(-N/2:(N/2-1))*(Fs/N);
figure, plot(fx,abs(fftshift(y))); 

i want to see the low part of the signal which means one impulse in the frequency domain, i am using this filter:

fc = 1500;
Wn = (2/Fs)*fc;
b = fir1(20,Wn,'low',kaiser(21,3));

fvtool(b,1,'Fs',Fs)


z = filter(b,1,y);


plot(t(1:100),abs(z(1:100)))

xlabel('Time (s)')
ylabel('Amplitude')

but i am seeing a periodic signal , how can i see the one impulse that is in the low frequencies?


Solution

  • You should use z = filter(b,1,x);,

    Fs = 6000;                   
    T = 1/Fs;              
    N=512;                       
    t = (0:N-1)*T;               
    x1 = 4*sin(2*pi*1000*t);
    x2 = sin(2*pi*2500*t); 
    x = x1 + x2;
    NFFT = 2^nextpow2(N);
    y = fft(x,NFFT)/N;
    fx = Fs/2*linspace(0,1,NFFT/2+1);
    subplot(211)
    plot(fx,2*abs(y(1:NFFT/2+1))) 
    
    fc = 1500; 
    Wn = (2/Fs)*fc;
    b = fir1(20,Wn,'low',kaiser(21,3));
    z = filter(b,1,x);
    Z = fft(z,NFFT)/N;
    subplot(212)
    plot(fx,2*abs(Z(1:NFFT/2+1))) 
    

    enter image description here

    It's more common to plot only the positive frequencies.

    Your N is already a power of 2 but I defined NFFT in case N changes.

    BTW you don't need to use figure before each plot.