MATLAB: Fourier Graphs of the lowpass and high pass filtered signals are looking weird

fourier transformsignal processing

Hello everyone,
I am currently doing a project in MATLAB and I am using a lowpass filter in a signal with a cutoff frequency of 30Hz and then I find the fourier transform of it and I graph it. I want to know if the actual graph is right or am I doing something wrong? I am going to attach the part of the code for the filters and plots.
This is the plot that I get for the low pass filter:
It shouldn't go to 0 after 30 Hz? or around 30 Hz?
And for the high pass filter:
I get it to be 0 around until like 190 Hz, but why is it not 270Hz or closer? I did use smaller frequencies for the HPF and they were more accurate until when they stayed 0.
%LPF 30Hz
y_LPF_30 = lowpass(y,30,Fs);
yf_LPF_30 = fft(y_LPF_30);
% 30Hz
%set up

%% Compute the two-sided spectrum P2. Then compute the single-sided spectrum P1 based on P2 and the even-valued signal length L.
P2_LPF30 = abs(yf_LPF_30/L);
P1_LPF30 = P2_LPF30(1:L/2+1);
P1_LPF30(2:end-1) = 2*P1_LPF30(2:end-1);
figure (5)
%plot

subplot(3,2,1);
plot(f,P1_LPF30)
title('Single-Sided Amplitude Spectrum of LPF 30Hz')
xlabel('f (Hz)')
ylabel('|P1(f)|')
%HPF 270Hz
y_HPF_270 = highpass(y,270,Fs);
yf_HPF_270 = fft(y_HPF_270);
%270Hz
%set up
P2_HPF270 = abs(yf_HPF_270/L);
P1_HPF270 = P2_HPF270(1:L/2+1);
P1_HPF270(2:end-1) = 2*P1_HPF270(2:end-1);
%plot
subplot(3,2,6);
plot(f,P1_HPF270)
title('Single-Sided Amplitude Spectrum of HPF 270Hz')
xlabel('f (Hz)')
ylabel('|P1(f)|')

Best Answer

Last update:
Running your Lowpass code:
%LPF 30Hz
y_LPF_30 = lowpass(y,30,Fs);
yf_LPF_30 = fft(y_LPF_30);
% 30Hz
%set up
%% Compute the two-sided spectrum P2. Then compute the single-sided spectrum P1 based on P2 and the even-valued signal length L.
P2_LPF30 = abs(yf_LPF_30/L);
P1_LPF30 = P2_LPF30(1:L/2+1);
P1_LPF30(2:end-1) = 2*P1_LPF30(2:end-1);
figure
%plot
plot(f,P1_LPF30)
title('Single-Sided Amplitude Spectrum of LPF 30Hz')
xlabel('f (Hz)')
ylabel('|P1(f)|')
gives this output:
untitled.jpg
Update:
For the low-pass try adding some properties such as:
y_LPF_30 = lowpass(y,30,Fs,'StopbandAttenuation',100);
Also, please show us the plots generated by this:
lowpass(y,30,Fs,'StopbandAttenuation',100);
lowpass(___) with no output arguments plots the input signal and overlays the filtered signal.
Hope it helps.
Earlier:
The filters look ok. some example data would be helpful to debug.
Also, it might just be a typo, but you wrote: P2_HPF270 = abs(yf_HPF_220/L);