# FIR filter basic implementation problem

5 views (last 30 days)
Lukasz on 13 May 2023
Commented: Lukasz on 15 May 2023
Hi All
I am a beginner in signals theory and, while trying to implement basic code for upsamling, downsamling and filtering of the simple siusoid and noise signal, I encountered some issues with implementation of FIR filter.
The Sinusoid frequency is 0.1Hz and Sampling Frequency 1Hz. I calculated the Low pass FIR filter parameters using "firpmod" for the assumptions Lower Freq 0Hz; Upper Frequency 0.12Hz; passband ripple 0.01 (linear value) ; Stopband ripple 0.01 (linear value), Sampling Freq 1Hz. Later I calculated coefficients of square root cosine FIR filter for 1 response up to 0.25 rad/sec and 0 response above 0.26 rad/sec which is around but above 0.1Hz. The frequency domain is as expected but the Filter seems to truncate sinusoid in time domain for some initial time - therefore I am confused with FIR filter parameters selection - what can be wrong in such implementation causing filtering of the sinusoid initially?.Please see below code and response of the code:
clear all
close all
fm = 0.1; % message signal frequency
Fs = 1;%sampling freq
Ts = 1/Fs; %sampling time
n=0:Ts:999.9; %time range
N=size(n,2);% Size of the n space
dFs=Fs/N; % size of the dFs for Frequency domain plot
sine_wave =sin(2*pi*fm*n);% generating sinusoid
random= 2* round(rand(1,length(n)))-1; % generating noise
% Calculating the Low pass FIR filter parameters for the assumption Lower Freq 0Hz; Upper Frequency 0.12Hz; passband ripple 0.01 (linear) ; Stopband ripple 0.01 (linear), Sampling Freq 1Hz
Order = firpmord([0 0.12],[1 0],[0.01 0.01], 1)
coeffs1 = firls(Order,[0 0.25 0.26 1],[1 1 0 0]); % calculating coeficcients for FIR filter - 0.25 and 0.26 are frequencies in radians/sec
sin_bwlimited = filter(coeffs1,0.1,sine_wave); %filtered sinusoid
random_bwlimited = filter(coeffs1,0.1,random); % filtered noise
subplot(5,1,1);% dividing window
plot(n(1:100),sin_bwlimited(1:100),'--or');
subplot(5,1,2)
plot(n(1:100),random_bwlimited(1:100), '--or');% plottong only 100 points
f3 = 0:dFs:Fs/2-dFs;% space of freq domain
Y = fft(sin_bwlimited)/N;
subplot(5,1,3);
plot(f3,2*abs(Y(1:N/2))) ;
R = fft(random_bwlimited)/N;
subplot(5,1,4);
plot(f3,2*abs(R(1:N/2))) ;

Shaik on 13 May 2023
Hi,
Check this
clear all
close all
fm = 0.1; % message signal frequency
Fs = 1; % sampling freq
Ts = 1/Fs; % sampling time
n = 0:Ts:999.9; % time range
N = size(n,2); % size of the n space
sine_wave = sin(2*pi*fm*n); % generating sinusoid
random = 2*round(rand(1,length(n)))-1; % generating noise
% Calculating the Low pass FIR filter parameters
% for the assumption Lower Freq 0Hz; Upper Frequency 0.12Hz;
% passband ripple 0.01 (linear) ; Stopband ripple 0.01 (linear),
% Sampling Freq 1Hz
Order = firpmord([0 0.12],[1 0],[0.01 0.01], 1);
coeffs1 = firls(Order,[0 0.25 0.26 1],[1 1 0 0]); % calculating coeficcients for FIR filter - 0.25 and 0.26 are frequencies in radians/sec
sin_bwlimited = filter(coeffs1, 1, sine_wave); % filtered sinusoid
random_bwlimited = filter(coeffs1, 1, random); % filtered noise
subplot(5,1,1); % dividing window
plot(n, sine_wave, '-b');
hold on;
plot(n, sin_bwlimited, '--r');
xlabel('Time');
ylabel('Signal');
title('Sinusoidal Signal with Limited Bandwidth');
subplot(5,1,2);
plot(n, random, '-b');
hold on;
plot(n, random_bwlimited, '--r');
xlabel('Time');
ylabel('Signal');
title('Random Noise with Limited Bandwidth');
f3 = 0:Fs/N:Fs/2-Fs/N; % space of freq domain
Y = fft(sin_bwlimited)/N;
subplot(5,1,3);
plot(f3, 2*abs(Y(1:N/2))) ;
xlabel('Frequency');
ylabel('Magnitude');
title('Frequency Response of Filtered Sinusoidal Signal');
R = fft(random_bwlimited)/N;
subplot(5,1,4);
plot(f3, 2*abs(R(1:N/2))) ;
xlabel('Frequency');
ylabel('Magnitude');
title('Frequency Response of Filtered Random Noise');
subplot(5,1,5);
plot(coeffs1, '-o');
xlabel('Sample');
ylabel('Coefficient');
title('FIR Filter Coefficients');

Lukasz on 14 May 2023
Edited: Lukasz on 14 May 2023
Thanks Shaik
Thank you for your help. The sinusoid amplitude is still affected during first 10 samples. I addition, by using the more advanced plot, it appeared that filtered sinusoid is phase shifted if related to nonfiltered signal- which is probably more serious and confusing.
Star Strider on 14 May 2023
@Lukasz — To avoid the phase distortion in the ffiltered signal, use the filtfilt function, not filter.
Lukasz on 15 May 2023
Thanks. It works well now

### Categories

Find more on Digital Filter Analysis in Help Center and File Exchange

R2015a

### Community Treasure Hunt

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

Start Hunting!