# FIR filter design by frequency sampling

Digital filters are used extensively in signal processing applications to remove noise, distortions or to isolate particular frequency components from a signal. Finite Impulse Response (FIR) filters are widely used because they offer linear phase and stability. FIR filters are designed by selecting the coefficients of the filter so that the filter's frequency response meets the required specifications.

There are several methods to design FIR filters, and one of the popular methods is the frequency sampling method. In this blog post, we will discuss the frequency sampling method and how to design an FIR digital filter using this method.

Frequency Sampling Method

The frequency sampling method is a technique that uses a set of desired frequency response samples to design an FIR filter. The method is based on the principle that the Fourier transform of the filter impulse response is the desired frequency response. Therefore, the filter coefficients can be obtained by performing the inverse Fourier transform of the desired frequency response.

Design Procedure

The following steps are used to design an FIR filter using the frequency sampling method:

• Define the desired frequency response: The first step in designing an FIR filter using the frequency sampling method is to define the desired frequency response. The frequency response is specified in terms of a set of samples, which represent the magnitude of the frequency response at discrete frequencies.
• Compute the impulse response: The next step is to compute the impulse response of the filter by performing the inverse Fourier transform of the desired frequency response samples.
• Truncate the impulse response: The computed impulse response is usually an infinite-length sequence. Therefore, it must be truncated to a finite length by multiplying it with a window function such as Hamming, Hanning, or Blackman.
• Normalize the impulse response: The truncated impulse response is then normalized by dividing it by the sum of its coefficients.
• Implement the filter: The final step is to implement the filter using the normalized impulse response as the filter coefficients.

MATLAB Code Example

Let's illustrate the frequency sampling method using an example MATLAB code. Consider a low-pass filter with a passband cutoff frequency of 0.4π and a stopband cutoff frequency of 0.6π.

% Define the passband and stopband frequencies
fp = 0.4*pi;
fs = 0.6*pi;

% Define the frequency response samples
N = 100;
Hd = zeros(1,N);

for n=1:N
if (n-1)*pi/N <= fp
Hd(n) = 1;
elseif (n-1)*pi/N >= fs
Hd(n) = 0;
else
Hd(n) = (fs-(n-1)*pi/N)/(fs-fp);
end
end

% Compute the impulse response
h = real(ifft(Hd));

% Truncate the impulse response
h = h(1:N/2);

% Normalize the impulse response
h = h/sum(h);

% Plot the frequency response and impulse response
freqz(h);
figure;
stem(h);


In this code, we first define the passband and stopband frequencies as fp and fs, respectively. We then define the frequency response samples Hd using a for loop and if-else statements. The frequency response is set to 1 for frequencies below fp, 0 for frequencies above fs, and a linear transition in between.

Next, we compute the impulse response of the filter by performing the inverse Fourier transform of the frequency response samples using the ifft() function. The impulse response is then truncated to a finite length of N/2 and normalized by dividing it by the sum of its coefficients.

Finally, we plot the frequency response and impulse response of the filter using the freqz() and stem() functions, respectively. The plots are shown below.

Frequency Response

Impulse Response

Here we showed how FIR digital filters are designed using frequency sampling method. There are other methods used for FIR filter design. One of them is using Z-Transform as illustrated in FIR and IIR filter design with Z-transform.