How to resolve the algorithm Apply a digital filter (direct form II transposed) step by step in the Python programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Apply a digital filter (direct form II transposed) step by step in the Python programming language

Table of Contents

Problem Statement

Digital filters are used to apply a mathematical operation to a sampled signal. One of the common formulations is the "direct form II transposed" which can represent both infinite impulse response (IIR) and finite impulse response (FIR) filters, as well as being more numerically stable than other forms. [1] Filter a signal using an order 3 low-pass Butterworth filter. The coefficients for the filter are a=[1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17] and b = [0.16666667, 0.5, 0.5, 0.16666667] The signal that needs filtering is the following vector: [-0.917843918645, 0.141984778794, 1.20536903482, 0.190286794412, -0.662370894973, -1.00700480494, -0.404707073677 ,0.800482325044, 0.743500089861, 1.01090520172, 0.741527555207, 0.277841675195, 0.400833448236, -0.2085993586, -0.172842103641, -0.134316096293, 0.0259303398477, 0.490105989562, 0.549391221511, 0.9047198589] [Wikipedia on Butterworth filters]

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Apply a digital filter (direct form II transposed) step by step in the Python programming language

The provided Python code demonstrates digital signal processing by applying a lowpass Butterworth filter to a given signal and visualizing the result. Here's a step-by-step explanation:

  1. Import Libraries: The code starts by importing necessary libraries:

    • from __future__ import print_function: This line ensures Python 2 compatibility for printing.
    • from scipy import signal: Imports the signal module from the SciPy library for signal processing functions.
    • import matplotlib.pyplot as plt: Imports the matplotlib.pyplot module for data visualization.
  2. Define the Signal: The variable sig is defined as a list of values representing the input signal. These values are numerical samples of the signal over time.

  3. Create Lowpass Butterworth Filter: The code creates a third-order (order 3) lowpass Butterworth filter using the signal.butter function. The filter is designed to have a cutoff frequency of 0.5. The coefficients of the filter are stored in the variables a and b.

  4. Apply the Filter: The signal.lfilter function is used to apply the lowpass filter to the input signal. The filtered signal is stored in the variable filt.

  5. Print Filtered Signal: The filtered signal is printed to the console for verification.

  6. Plot Input and Filtered Signals: The code plots the original input signal (in blue) and the filtered signal (in red dashed lines) using the matplotlib.pyplot library. The plt.show() command displays the plot window.

In summary, this code demonstrates how to apply a lowpass Butterworth filter to a signal using SciPy and visualize the result using Matplotlib. The lowpass filter removes high-frequency components from the signal, resulting in a smoothed version. The cutoff frequency of 0.5 indicates that only components below half the sampling frequency are preserved in the filtered output.

Source code in the python programming language

#!/bin/python
from __future__ import print_function
from scipy import signal
import matplotlib.pyplot as plt

if __name__=="__main__":
	sig = [-0.917843918645,0.141984778794,1.20536903482,0.190286794412,-0.662370894973,-1.00700480494,
		-0.404707073677,0.800482325044,0.743500089861,1.01090520172,0.741527555207,
		0.277841675195,0.400833448236,-0.2085993586,-0.172842103641,-0.134316096293,
		0.0259303398477,0.490105989562,0.549391221511,0.9047198589]

	#Create an order 3 lowpass butterworth filter
	#Generated using b, a = signal.butter(3, 0.5)
	a = [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17]
	b = [0.16666667, 0.5, 0.5, 0.16666667]

	#Apply the filter to signal
	filt = signal.lfilter(b, a, sig)
	print (filt)

	plt.plot(sig, 'b')
	plt.plot(filt, 'r--')
	plt.show()


  

You may also check:How to resolve the algorithm Square but not cube step by step in the Ring programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Onyx programming language
You may also check:How to resolve the algorithm LZW compression step by step in the Rust programming language
You may also check:How to resolve the algorithm Entropy step by step in the AWK programming language
You may also check:How to resolve the algorithm Binary digits step by step in the Ada programming language