• webmaster@caristem.org

Python is a powerful scripting language, which has a number of numerical and scientific libraries such as Numpy and Scipy. A simple python script to create a Chebychev II lowpass filter is presented which uses the Numpy, Scipy and the Matplotlib library.

The required libraries are imported.

import numpy as np
import scipy.signal as signal
import matplotlib.pyplot as plt

The filter characteristics such as the sampling frequency, filter order, the filter stop band frequency and the stop band magnitude are setup as shown below:

F = 30         # sampling frequency
order = 4      # filter order
fs = 3         # frequency at stop band
As_dB = 40     # stop band magnitdude 

Because we are creating a Chebychev II filter the stop band cut-off frequency is used here and not the 3dB cut-off frequency. The normalized stop frequency is defined and filter characteristics are obtained using the iirfilter function.

wc = 2.0 * fs / F # normalize the frequency

# Determine the filter coefficients
b, a = signal.iirfilter(order, Wn=wc, rs=As_dB, btype='lowpass',
                        analog=False, ftype='cheby2', output='ba');

w, h = signal.freqz(b, a)

The filter frequency response is then plotted using the matplotlib library.

title_str =  'F = {0:.0f}Hz, Fs = {1:.0f}Hz, As = {2}dB'.format(F, fs, -As_dB)

f_nyq = 0.5 * F # Get the Nyquist frequency
f =  f_nyq * w / np.pi  # Get frequency
A_db = 20 * np.log10(abs(h))

plt.figure()
plt.clf()
plt.ion()
plt.plot(f, A_db, color='k', linewidth=2, label='Filter')
plt.axvline(fs, linestyle='--',color='r', label='Fs')
plt.axhline(-As_dB,linestyle='--',color='b', label='As')
plt.xlim([0, f_nyq])
plt.xlabel('Frequency/Hz')
plt.ylabel('Gain/dB')
plt.title(title_str)
plt.legend(loc=0)
plt.grid(True)
plt.show()

Use the following link to obtain the complete source code used.

 

ABOUT THE AUTHOR(S)

Nicholas St. Hill

Nicholas St. Hill is an engineer with over 20 years research and development experience who runs KEIKY, a company specializing in the areas of instrument and sensor modelling and design. He is a Senior Member of the Institute of Electrical and Electronic Engineers (2009) and a Fellow of the Institution of Mechanical Engineers (2010).

 

Published: 2018-03-24