FIR Digital Filter Design in MATLAB: Source Code and Implementation

This section provides MATLAB source code for implementing FIR digital filters. It covers FIR filter designs using Rectangular, Bartlett, Blackman, Chebyshev, Hamming, and Hanning window types. This particular example focuses on the FIR Digital Filter with the Hamming window, which is the default used by the fir1 MATLAB function.

FIR digital filters operate on discrete-time signals. They utilize present and past input samples to generate a current output sample. A key characteristic of FIR filters is that they do not rely on previous output samples. There are several types of FIR filters tailored to specific needs, including low-pass, high-pass, band-pass, and band-stop filters. The code below demonstrates a low-pass filter implementation.

FIR Filter Specifications

When designing FIR filters (besides the window type), these specifications are important:

INPUT:

  • Window Type
  • Passband and Stopband Ripples
  • Passband and Stopband Edge Frequencies
  • Sampling Frequency
  • Order of the Filter
  • Window Coefficients

OUTPUT:

  • Magnitude and Phase Responses

Entering Input Parameters

clc;
clear all;
close all;

rp=input('enter the passband ripple(Example:0.02):');
rs=input('enter the stopband ripple(Example:0.01):');
fp=input('enter the passband freq(Example:1500):');
fs=input('enter the stopband freq(Example:2000):');
f=input('enter the sampling freq(Example:6000):');


## MATLAB built-in `fir1` MATLAB function

```matlab
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;

if (rem(n,2)~=0)
    n1=n;
    n=n-1;
end

y=hamming(n1); % low-pass filter
b=fir1(n,wp,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));

figure,plot(o/pi,m);
title('FIR Filter Response');
ylabel('Gain in dB');
xlabel('Normalised frequency');

Input and Output of Low Pass FIR Filter

FIR filter input

FIR filter output

Low Pass and High Pass FIR Filter using kaiser,rectangular and triangular window method

  • [FIR LPF HPF using triangular window method](Link Not Provided)
  • [FIR LPF HPF using rectangular window method](Link Not Provided)
  • [FIR LPF HPF using kaiser window method](Link Not Provided)