# RF Wireless World

## Home of RF and Wireless Vendors and Resources

One Stop For Your RF and Wireless Need

## Low Pass FIR Filter Implementation

This section of MATLAB source code covers Low Pass FIR Filter matlab code. It covers filter design using firrcos function and matlab FDA tool.

### Using FIRRCOS MATLAB Function

clc;clear all;close all;
FFT_size=1024;
t=0:0.001:(FFT_size-1)/1000;
x=sin(2*pi*50*t)+sin(2*pi*200*t);
coeff=firrcos(50,100,50,1000);
y=conv(x,coeff);
y1=fft(y,1024);
figure; plot(abs(fft(x,1024)));
figure;plot(abs(y1));

Here we are taking input signal as two peaks of sinusoidal waves at 50 Hz and 200 Hz. Therefore, if we pass this signal through the low pass FIR filter then the frequency above the cut-off frequency should be eliminated. Here the length of the FFT is 1024. In this Implementation we are using MATLAB pre-defined function to generate coefficients for the filter design.
coeff=firrcos(n,fc,bw,fs);
Where,

'n' - order of the filter. n is a positive integer.
'fc' - Specifies the cutoff frequency of the filter. fc must be greater than 0 and less than fs/2.
'bw' - Specifies the transition bandwidth. bw must be greater than 0.
[fc-bw/2, fc+bw/2] must fall in the range [0, fs/2].
'fs' - Specifies the sampling frequency. fs must be greater than 0.
Here we are also using 'conv' pre-defined function to convolve the input signal with the coefficients to get the time domain output.

y=conv(x,coeff);
Where,
'x' - input signal.
'coeff' - coefficients.
After getting this convolution output ('y') then plot figures of FFT output(y) with along with the FFT input(x). FFT is done to convert the time domain signal to frequency domain signal. Figure 1: Input signal (x) in frequency domain at 50 Hz and 200 Hz.

The right side peaks in the above figure are just the mirror image of the original input signal.
Below is the output signal (y) in frequency domain. Therefore, from these two figures we can conclude that the peak at 200 Hz is getting eliminated because it is greater than the cutoff frequency (100 Hz).
Note:-

Suppose if we give an input signal of 110 Hz and the cutoff frequency is 100 Hz, then the input signal will not be totally eliminated in the output, because it will take time to decay completely. This decaying time is known as transition period. Figure-2 : Output signal (y) in frequency domain.

### Implementing low pass FIR filter in MATLAB using FDA Tool

clear all;
close all;
clc;
FFT_size=1024;
t=0:0.001:(FFT_size-1)/1000;
x1=sin(2*pi*15*t)+sin(2*pi*100*t);
x1=x1/2;
%%fs = 1000 Hz, fpass = 50 Hz, fstop = 100 Hz
b=[ -0.00075204 -0.0034964 -0.0025769 -0.0029542 -0.0019023 7.5785e-005 0.0029216
0.0060432 0.0086036 0.0096315 0.0082863 0.0041324 -0.0026099 -0.010933 -0.019081 -0.024793 -0.025704 -0.019846 -0.0061564 0.015154 0.042456 0.072834 0.1025 0.12742 0.14402 0.14984 0.14402 0.12742 0.1025 0.072834 0.042456 0.015154 -0.0061564 -0.019846 -0.025704 -0.024793 -0.019081 -0.010933 -0.0026099 0.0041324 0.0082863 0.0096315 0.0086036 0.0060432 0.0029216 7.5785e-005 -0.0019023 -0.0029542 -0.0025769 -0.0034964 -0.00075204];
b_dec=b*(32768);
b_deca=b_dec(:);
x_dec=x1*(32768);
x_decaa=x_dec(:);
x=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 x1]; for k= 55:1077

y(k) = (x(k)*b(1)) + ...
(x(k-1)*b(2)) + ...
(x(k-2)*b(3)) + ...
(x(k-3)*b(4)) + ...
(x(k-4)*b(5)) + ...
(x(k-5)*b(6)) + ...
(x(k-6)*b(7)) + ...
(x(k-7)*b(8)) + ...
(x(k-8)*b(9)) + ...
(x(k-9)*b(10)) + ...
(x(k-10)*b(11)) + ...
(x(k-11)*b(12)) + ...
(x(k-12)*b(13)) + ...
(x(k-13)*b(14)) + ...
(x(k-14)*b(15)) + ...
(x(k-15)*b(16)) + ...
(x(k-16)*b(17)) + ...
(x(k-17)*b(18)) + ...
(x(k-18)*b(19)) + ...
(x(k-19)*b(20)) + ...
(x(k-20)*b(21)) + ...
(x(k-21) *b(22)) + ...
(x(k-22) *b(23)) + ...
(x(k-23) *b(24)) + ...
(x(k-24) *b(25)) + ...
(x(k-25) *b(26)) + ...
(x(k-26) *b(27)) + ...
(x(k-27) *b(28)) + ...
(x(k-28) *b(29)) + ...
(x(k-29) *b(30)) + ...
(x(k-30) *b(31)) + ...
(x(k-31) *b(32)) + ...
(x(k-32) *b(33)) + ...
(x(k-33) *b(34)) + ...
(x(k-34) *b(35)) + ...
(x(k-35) *b(36)) + ...
(x(k-36) *b(37)) + ...
(x(k-37) *b(38)) + ...
(x(k-38) *b(39)) + ...
(x(k-39) *b(40)) + ...
(x(k-40) *b(41)) + ...
(x(k-41) *b(42)) + ...
(x(k-42) *b(43)) + ...
(x(k-43) *b(44)) + ...
(x(k-44) *b(45)) + ...
(x(k-45) *b(46)) + ...
(x(k-46) *b(47)) + ...
(x(k-47) *b(48)) + ...
(x(k-48) *b(49)) + ...
(x(k-49) *b(50));
y_dec=y*32768;
y_deca=y_dec(:);

end

z=y(51:end);
y1=fft(z,1024);
figure; plot(abs(fft(x1,1024)));
figure;plot(abs(y1));
y1_dec=y1*32768;
y1_deca=y1_dec(:); Figure-3:Input signal in frequency domain at 15 Hz and 100 Hz. Figure 4: Output signal(y) in frequency domain at 15 Hz. From the above two figures we can conclude that the input signal at 100 Hz frequency is getting cutoff at the output.

In this implementation, we are using FDA (Filter Design Analysis) tool to generate the filter coefficients. The specifications of this filter are :
Fs = 1000 Hz.
Fpass = 50 Hz.
Fstop = 100 Hz. Figure-5: FDA tool to generate coefficients for low pass FIR filter. Figure-6: Structure of Low pass FIR filter.

In this implementation the length of the FFT is 1024. Here, the input signal consists of two peaks of sin waves at 15 Hz and at 100 Hz.
x1=sin(2*pi*15*t)+sin(2*pi*100*t);

We are scaling this input signal because when input (x) is not in the Q-15 format range (i.e,-1 to 0.9999999). Therefore, we are dividing x input by 2.i.e,x1/2.

Now the coefficients are assigned to 'b'. We will now do zero padding for 'x1' because in the filter equation the previous input signal is also multiplied with the coefficients. The filter equation is designed for Low pass FIR filter of order '50'.The 'k' value is starting from 55 because the original input signal is starting from 55th location in MATLAB and ends uptill 1077th location.

Filter equation is implemented as follows:
Y (n)= x(n) * b(1) + x(n-1) * b(2) +.....+x(n-N) * b(N)
If 1st input comes then it is multiplied with 1st coefficient and the remaining coefficients are multiplied with previous inputs which are of '0' value.

If 2nd input comes then 2nd input is multiplied with 1st coefficient and the 1st input is then multiplied with the 2nd coefficient and the remaining of the coefficients are multiplied with previous inputs which are of '0' value.

It goes in the similar manner until inputs come and the coefficients gets repeated periodically. Then we plot the FFT of input and output values.

### Useful Links to MATLAB codes

Refer following as well as links mentioned on left side panel for useful MATLAB codes.
OFDM Preamble generation  Time off estimation corr  Freq off estimation corr  channel estimation  11a WLAN channel  PN sequence generation  OFDMA Tx Rx  AES DES  carrier aggregation  CCDF  FIR Filter  IIR Filter  Low Pass FIR  Viterbi decoder  CRC8 CRC32