0% found this document useful (0 votes)
62 views4 pages

Lab Activity 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views4 pages

Lab Activity 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Activity 6: Correlation in Digital Signal

Processing (DSP) Using Python


I. Objective
a. To identify and correct intentional errors in Python code for calculating cross-
correlation and auto-correlation of discrete signals. Each code block contains
errors. Follow the procedures below to troubleshoot and correct these errors.

II. Lab Activity Procedures


Part 1: Cross-Corelation
1.1 Cross-Correlation Between Two Signals
This script calculates the cross-correlation between two discrete signals, x and y, to
analyze their relationship over different lags. Cross-correlation can reveal how the two
signals align when one signal is shifted by various time steps.

import numpy as np
import matplotlib.pyplot as plt

# Define the two signals (discrete arrays)


x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 6, 8, 10])

# Perform cross-correlation
cross_corr = np.correlate(x, y, mode='sam')

# Find the length of the cross-correlation result (intended for plot adjustment)
length_corr = len(cross_corr) + 2

# Plot the result


plt.plot(cross_corr, color='purple')
plt.title("Cross Correlation of x and Y")
plt.xlabel("lag")
plt.ylabel("Correlation")
plt.show()
1.2 Cross-Correlation with Zero-Padding
This script introduces zero-padding to x and y to avoid boundary effects. Cross-correlation
with zero-padding is crucial in signal processing, as it reduces wrap-around effects at
boundaries when calculating similarity between two signals.

# Define the two signals with zero-padding (both should be padded equally)
x = np.array([1, 2, 3, 4, 5, 0, 0])
y = np.array([2, 4, 6, 8, 10, 0, 0, 0])

# Perform cross-correlation
cross_corr = np.correlate(x, y, mode='same')

# Define x-axis for lags (should align with cross-correlation length but has error)
lags = np.arange(-len(cross_corr) // 2, len(cross_corr) // 2)

# Plot the result


plt.plot(cross_corr, linestyle='--')
plt.title("Cross-Correlation with Zero Padding")
plt.xlabel("Lag")
plt.ylabel("Correlation")
plt.show()

Part 2: Auto-Corelation
2.1 Auto-Correlation of a Signal
Auto-correlation measures how well a signal correlates with a delayed version of itself. It’s
useful for identifying repeating patterns within a signal. In this script, intentional errors
hinder proper calculation and plotting.

# Define the signal


x = np.array([1, 2, 1, 2, 1])

# Perform auto-correlation
auto_corr = np.correlate(x, x, mode='ful')

# Adjusted auto-correlation result (introducing unnecessary slicing)


auto_corr = auto_corr[1:]

# Plot the result


plt.plot(autocorrelation)
plt.title("Auto-correlation of signal x")
plt.ylabel("Correlationn")
plt.xlabel("Lag")
plt.show()
2.2 Normalized Auto-Correlation
This code normalizes the auto-correlation result, scaling it to a range of 1 for easier
comparison. Proper normalization allows consistent comparison between signals of
different amplitudes.

# Perform auto-correlation and normalize


auto_corr = np.correlate(x, x, mode='full')
normalized_auto_corr = auto_corr / np.amax(auto_corr[:])

# Incorrectly shifted normalized_auto_corr array


normalized_auto_corr = normalized_auto_corr[:-1]

# Plot the normalized auto-correlation


plt.plot(normalized_auto_corr, color='r')

# Title format inconsistent with standards


plt.title("normalized Auto Correlation of Signal X")

# Label spelling error


plt.ylabel("Normalized corelation")
plt.xlabel("Lag")
plt.show()

III. Output Graph Description


a. 1.1: The resulting graph shows correlation values on the y-axis and lag on the x-
axis. At zero lag, the peak value of the cross-correlation indicates the degree of
alignment between x and y without any time shift. Positive and negative lag values
represent shifts of y relative to x, which allow for observing the degree of
similarity over various time displacements. The corrected graph should show a
single peak (since y is a scaled version of x), indicating the signals' high similarity.
b. 1.2: The graph shows correlation values across a broader range of lags, extending
to account for the zero-padded values. By using zero-padding, the cross-
correlation result maintains accurate shifts without aliasing at the boundaries. The
graph should display a central peak at zero lag, with lower values symmetrically
distributed around it, showing the strongest similarity when x and y align without
a time shift. The tails, near the boundaries, drop to zero as the zero-padded
sections correlate with the original signals.
c. 2.1: The graph displays correlation values on the y-axis, with the x-axis
representing lag values. A strong peak at zero lag represents the maximum
similarity when x is aligned with itself. If x has periodic elements, additional peaks
will appear at intervals corresponding to the signal’s period, showing where the
signal’s structure repeats. For a signal without periodic patterns, the graph
decreases smoothly on both sides of the zero lag, representing reduced similarity
as x shifts out of alignment with itself.
d. The peak value at zero lag is exactly 1. This peak represents maximum alignment
when the signal perfectly overlaps with itself. The graph’s shape remains
consistent with the auto-correlation result, but all values are now between -1 and
1. Secondary peaks, if present, indicate periodicity, scaled in proportion to the
main peak. The zero-padded sections of the signal, if any, should correlate close to
zero, emphasizing the core structure of the original signal.

IV. Reminders:
a. Activity 1.1
i. ERRORS
ii. CORRECTION
iii. GRAPH
iv. EXPLANATION ON THE CORRECTED CODE
v. Code (softcopy)

Note: Accomplish item 1-5 in handwriting. On BB, attach your checked output then include the
code -on the last part in one PDD File only for your progress report.

Questions: (reflected on individual report)


1. Explain the primary difference between cross-correlation and auto-correlation. How does
each function help us understand relationships within and between signals? Provide a real-
world example of when cross-correlation might be useful and one where autocorrelation
could reveal essential insights.
2. In Activity 1.1, you computed the cross-correlation between two signals, x and y, which
were linearly related (one was a scaled version of the other). How would the cross-
correlation result change if x and y were more distinct signals (for example, one being a
random sequence and the other a repeating pattern)? Test this hypothesis by replacing y
with a random sequence and observe the result. Discuss how this helps us understand the
cross-correlation’s sensitivity to signal similarity.
3. Describe the role of zero-padding in signal processing and its effect on cross-correlation, as
observed in Activity 1.2. In which scenarios might zero-padding be essential, and in which
situations could it introduce misleading results? Provide examples to illustrate your answer.
4. In Activity 2.1, the auto-correlation of a signal 𝑥𝑥 was calculated. What does the peak at zero
lag represent, and why is it usually the highest peak in an auto-correlation plot? If you
observe additional peaks in the auto-correlation result, what might these indicate about
the signal’s structure or periodicity? Discuss how these peaks could aid in identifying
repeating patterns.
5. Activity 2.2 introduced normalization in auto-correlation. Discuss why normalization is
helpful in comparing signals of varying amplitudes. How does it enhance the interpretability
of correlation results? Consider a scenario where two different signals are recorded from
separate sources with different noise levels. Explain how normalization might impact the
reliability of comparing these signals’ auto-correlation results.

You might also like