Lab Activity 8
Lab Activity 8
Lab Activity 8
IV. Reminders:
• Part 1 (Enumerate each)
1. ERRORS
2. CORRECTION
3. GRAPH
4. EXPLANATION ON THE CORRECTED CODE
5. Corrected Code (softcopy)
Note: Accomplish item 1-4 in handwriting. On BB, attach your checked output then include the code -on the last part
in one PDF File only for your progress report.
V. Supplementary Item
• Objective: To design a digital filter using the Z-transform, apply it to a finite-duration input signal,
and analyze the results using Python. This activity involves computing the Z-transform of both the
filter and the signal, applying the filter in the frequency domain, and reconstructing the filtered
signal in the time domain.
• Problem Statement: Design a low-pass filter using the Z-transform for a finite-duration input
signal x[n], such as a sine wave combined with noise. Perform the following steps:
1. Compute the Z-transform of the input signal.
2. Apply the designed filter in the Z-domain by multiplying the input signal's Z-transform with the
filter's Z-transform.
3. Reconstruct the filtered signal in the time domain using the inverse Z-transform (numerically).
4. Plot and compare the original signal, noisy signal, and filtered signal.
• Reminders
1. Ensure all required libraries, such as numpy, matplotlib, and scipy, are installed and correctly
imported. Use pip to install any missing libraries if a ModuleNotFoundError occurs.
2. Confirm that the input signal xx is properly defined as a finite-duration signal. Use print
statements to verify the sine wave, noise, and combined signal values.
3. Double-check the filter's numerator (b) and denominator (a) coefficients to ensure they are
correctly specified and compatible with the desired filter type (e.g., FIR or IIR).
4. If plots appear incorrectly or do not render, verify the data format, use plt.tight_layout() to
adjust spacing, and ensure matplotlib.pyplot is imported.
5. Check the compatibility of signal length with filter coefficients during computations like lfilter.
Handle warnings such as division by zero or unstable computations carefully.
6. Use the freqz function to analyze the filter's frequency response and confirm that it matches
the desired behavior, such as low-pass or high-pass filtering.
7. Inspect the filtered signal to ensure the filter's effects are correct. Compare the original, noisy,
and filtered signals both visually and numerically.
8. Verify that filter coefficients and signal values are within a range that prevents numerical
instability or overflow during computations.
9. Include appropriate titles, axis labels, and legends for all plots to make the outputs easy to
understand.
10. Add print statements to trace intermediate values such as signal components, filter
coefficients, and filtered output. Remove these prints after resolving issues for cleaner code.
• Questions for Supplementary:
1. How does the Z-transform of the filter H(z) affect the input signal X(z) when applied in the
frequency domain, and what changes can be observed in the filtered output?
2. What is the significance of the filter coefficients (b and a) in determining the behavior of the
filter, and how do these coefficients influence the magnitude and phase response of the filter?
3. After applying the filter, how does the filtered signal compare to the original signal and the
noisy signal in terms of frequency content and noise reduction?
4. Why is it important to avoid including z=0 in the range of z-values during Z-transform
computations, and what issues might arise if z=0 is used?
5. How can the choice of the filter type (e.g., low-pass, high-pass, band-pass) and its design
impact the reconstruction of the original signal from a noisy observation?
CODE:
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import lfilter, freqz