Lab Activity 7
Lab Activity 7
plt.figure(figsize=(10, 6))
plt.stem(lags, correlation, use_line_collection='False')
max_lag = lags[np.argmax(correlation)]
print(f"Maximum correlation occurs at lag {max_lag}")
Part 2: Filtering with Convolution and Cross-Correlation
To apply a simple filter on the input signal using convolution and then use cross-correlation to detect the
filtered signal.
• Define an input signal x[n]=[1,2,3,4,5] and a filter h[n]=[1,0,−1]
• Perform convolution of x[n] with h[n] to get the filtered output y[n].
• Use cross-correlation to compare the filtered signal with the original signal.
• Visualize the output of the convolution and the cross-correlation.
• Expected Output
• The first plot will show the filtered signal obtained via convolution.
• The second plot will display the cross-correlation of the input signal with the filter.
• The filtered signal will be smoothed or modified according to the characteristics of the
filter.
• Code:
# Define input signal and filter
x = np.array([1, 2, 3, 4, 5]) # Input signal
h = np.array([1, 0, -1]) # Filter
# Convolution (Filtering)
y = np.convolve(x, h, mode='correct')
# Plot Cross-Correlation
plt.subplot(2, 1, 2)
plt.stem(np.arange(len(cross_corr)), cross_corr, use_line_collection='True')
plt.tight_layout()
plt.show()
# Display results
print("Filtered Signal y[n]:", y)
print("Cross-Correlation values:", cross_corr)
plt.figure(figsize=(10, 6))
plt.stem(lags, correlation, use_line_collection=True)
plt.title("Cross-Correlation between y[n] and x[n]")
plt.xlabel("Lag (m)")
plt.ylabel("Correlation Value")
plt.grid(True)
plt.show()
# Find the lag with maximum correlation
max_lag = np.argmax(correlation)
print(f"Maximum correlation occurs at lag {max_lag}")
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
The goal of this activity is to apply signal detection, filtering, and time delay estimation techniques to
identify errors in a transmitted signal, particularly focusing on detecting error codes through cross-
correlation and filtering methods.
• Required Python libraries: numpy, matplotlib, scipy. You are provided with a reference signal
(ideal signal) x[n] and a received signal y[n] which contains noise and possible error codes. The
error code may appear as a distortion or shift in the received signal. The Steps are:
• Signal Detection and Error Code Identification
1. Generate the reference signal x[n] (e.g., a sinusoidal signal) and the received signal y[n]
by adding noise and a shift (time delay or phase shift) to the reference signal.
A. Reference signal x[n]=sin (2πfn)
B. Received signal y[n]=x[n−Δn] + noise (where Δn is the time shift and noise is
added)
2. Perform cross-correlation between x[n] and y[n]. Plot the cross-correlation and identify
the peak that corresponds to the time delay. This will help detect if there is a shift or
delay in the received signal, which may indicate an error in transmission.
3. Identify error code. If the peak is not aligned as expected, an error code may be
detected. The misalignment suggests that either the time delay or noise distortion is
affecting the signal. Determine the lag at which the peak occurs and estimate the error
in time delay or alignment.
• Signal Filtering for Error Correction
1. Design a filter (e.g., low-pass filter) to remove high-frequency noiseUse scip. y.signal to
design a filter (e.g., scipy.signal.butter for a Butterworth filter).
2. Filter the received signal y[n] and plot the result. Compare the filtered signal with the
original reference signal x[n] to check if the noise and error have been mitigated.
3. Evaluate filter performance. Check if the error code is removed by comparing the cross-
correlation of the filtered signal with the reference signal. If the time delay has been
corrected, the cross-correlation plot should show a clear peak at the expected lag.
• Time Delay Estimation and Error Detection. Use cross-correlation to estimate the time delay
between the reference signal x[n] and the received (and filtered) signal y[n]. Compare this
time delay with the known time shift to detect errors.
1. Estimate the time delay. Use cross-correlation on the filtered signal to detect the lag
that gives the best alignment between x[n] and y[n].
2. Detect the error. If the estimated time delay deviates significantly from the expected
delay (based on Δn), report this as an error. The error can be attributed to a distortion
or noise introduced in the received signal.
Note:
• After each part of the code (especially filtering and cross-correlation), visually inspect the signals (plots) and
check for any noticeable issues, such as misalignments, unexpected peaks, or noise artifacts.
• Use print statements to check intermediate results, such as the length of signals after the roll or filter, and the
values of the cross-correlation at various lags.
• Look for out-of-bounds errors or unexpected behaviors (e.g., spikes in the correlation or strange signal patterns)
to locate potential issues in signal processing steps.
Questions:
1. How does cross-correlation help in detecting time delays or shifts in the received signal?
2. How does the filtering process affect the accuracy of time delay estimation?
3. What challenges did you face in detecting the error code, and how did the combination of filtering and cross-
correlation help mitigate these errors?
4. What role does noise play in error detection, and how effective was the filter in reducing the impact of noise?
5. Explain how you could improve this system for more complex error detection scenarios, such as in
communication systems with more significant distortions or multiple error codes.
CODE FOR ITEM 5
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal