FIR filter Python code | FIR python script

This FIR filter python code script covers simulation of FIR filter of Low pass type. The output plots of this FIR python script are also mentioned.

Introduction : FIR filter uses only current and past input digital samples to obtain a current output sample value. It does not utilize past output samples.

FIR filter

Simple FIR equation is mention below.
y(n)= h(0)x(n) + h(1)x(n-1) + h(2)x(n-2) + h(3)x(n-3) + h(4)x(n-4)
Refer FIR vs IIR filter for more information.

FIR filter python code

import matplotlib.pyplot as plt
import scipy.signal as sig
import numpy as np
from math import pi
plt.close('all')
N = 20
fc = 100
Fs = 1000
w_c = 2 *fc/Fs
t = sig.firwin(N, w_c)
[w, h] = sig.freqz(t, worN = 2000)
w = Fs*w/(2*pi)
h_db = 20 * np.log10(abs(h))
plt.figure()
plt.plot(w, h_db)
plt.title('FIR filter response')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')
plt.show()

FIR python Output plots

Following are the plots of above FIR python code.

FIR filter python output plots

Other useful DSP codes in Python

Useful Links to MATLAB codes

RF and Wireless tutorials