0% found this document useful (0 votes)
17 views6 pages

Lab Activity 5

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)
17 views6 pages

Lab Activity 5

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/ 6

Lab Activity 5: Convolution Properties in Digital Signal Processing (DSP) Using Python

I. Objective
a. To explore the Time Scaling Property and Area Property of convolution using
Python for Linear Time-Invariant (LTI) systems..
II. Lab Activity Procedures
A. Necessary libraries
Familiarity with Python and libraries like NumPy and Matplotlib.

B. Time Scaling Property in LTI Systems

a. This part will focus on verifying this property by convolving two signals and scaling
one of them.:
i. X1(t) = u(t), rectangular function
ii. X2(t) = rect (t), rectangular pulse
iii. Code 1: Convolution of Unit Step and Scaled Rectangular Pulse
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import convolve

# Time vector
t = np.linspace(-5, 5, 1000)

# Unit step function (u(t))


def unit_step(t):
return np.where(t >= 0, 1, 0)

# Rectangular pulse (rect(t))


def rect(t):
return np.where(np.abs(t) <= 1, 1, 0)

# Time scaling factor


a=2

# Original signals
x1 = unit_step(t)
x2 = rect(t)

# Scaled signals
x2_scaled = rect(t / a)

# Convolution of original signals


conv_original = convolve(x1, x2, mode='same') * (t[1] - t[0])

# Convolution of time-scaled signal


conv_scaled = convolve(x1, x2_scaled, mode='same') * (t[1] - t[0])

# Apply time scaling property (error here: incorrect time scaling


factor)
conv_scaled_adjusted = (1 / (a + 1)) * conv_original # ERROR: It
should be (1 / a)

# Plotting
plt.figure(figsize=(12, 8))

plt.subplot(3, 1, 1)
plt.plot(t, x1, label='x1(t) = u(t)')
plt.plot(t, x2_scaled, label='x2(t) = rect(t/a)')
plt.title('Original and Scaled Signals')
plt.legend()

plt.subplot(3, 1, 2)
plt.plot(t, conv_original, label='Convolution: x1(t) * rect(t)')
plt.title('Convolution of Original Signals')
plt.legend()

plt.subplot(3, 1, 3)
plt.plot(t, conv_scaled_adjusted, label='Scaled Convolution (1/a *
Convolution)')
plt.plot(t, conv_scaled, label='Convolution of Scaled Signals',
linestyle='dashed')
plt.title('Verification of Time Scaling Property')
plt.legend()

plt.tight_layout()
plt.show()
Conditions of Code:
1. Signal Definition define a unit step function u(t) and a rectangular pulse rect(t).
2. Time Scaling scale the time of the signals by a factor of a=2.
3. Convolution perform the convolution of the original signals and compare it with the
convolution of the time-scaled signals, verifying the time scaling property.
4. In the section where you are adjusting the convolution for the time scaling property, check if
you're correctly applying the scaling factor. Remember, the time scaling factor 𝑎 plays a
specific role in adjusting the output.
iv. Code 2: Convolution of Unit Step and Scaled Exponential
# Exponential function
def exp_decay(t, a):
return np.where(t >= 0, np.exp(-a * t), 0)

# Time scaling factor


a=2

# Original signals
x1 = unit_step(t)
x2 = exp_decay(t, 1)

# Scaled exponential signal (error: incorrect time scaling)


x2_scaled = exp_decay(t, a + 1) # ERROR: Should be exp_decay(t, a)

# Convolution of original signals


conv_original = convolve(x1, x2, mode='same') * (t[1] - t[0])

# Convolution of time-scaled signal


conv_scaled = convolve(x1, x2_scaled, mode='same') * (t[1] - t[0])

# Apply time scaling property


conv_scaled_adjusted = (1 / a) * conv_original

# Plotting
plt.figure(figsize=(12, 8))

plt.subplot(3, 1, 1)
plt.plot(t, x1, label='x1(t) = u(t)')
plt.plot(t, x2_scaled, label='x2(t) = exp(-at) u(t)')
plt.title('Original and Scaled Signals')
plt.legend()

plt.subplot(3, 1, 2)
plt.plot(t, conv_original, label='Convolution: x1(t) * exp(-t)')
plt.title('Convolution of Original Signals')
plt.legend()

plt.subplot(3, 1, 3)
plt.plot(t, conv_scaled_adjusted, label='Scaled Convolution (1/a *
Convolution)')
plt.plot(t, conv_scaled, label='Convolution of Scaled Signals',
linestyle='dashed')
plt.title('Verification of Time Scaling Property')
plt.legend()

plt.tight_layout()
plt.show()

Conditions:
1. Take a close look at the time scaling factor when generating the scaled exponential signal.
The exponent should reflect the correct time-scaling factor 𝑎. The scaling of the signal
itself should be consistent with the original definition of the exponential.

C. Area Property in LTI Systems

i. X1(t) =tri(t), triangular function


ii. X2(t) = rect (t), rectangular pulse

iii. Code 3: Convolution of Two Rectangular Pulses


# Define rectangular pulse
def rect(t):
return np.where(np.abs(t) <= 1, 1, 0)

# Define two rectangular pulses


x1_area = rect(t)
x2_area = rect(t)

# Convolution of x1 and x2
conv_area = convolve(x1_area, x2_area, mode='same') * (t[1] - t[0])

# Calculate areas (error: missing np.abs() on areas)


area_x1 = np.trapz(x1_area, t) # Area of first rectangular pulse
area_x2 = np.trapz(x2_area, t) # Area of second rectangular pulse
area_conv = np.trapz(conv_area, t) # Area of the convolution

# Print calculated areas


print(f"Area of x1(t) (rectangular pulse): {area_x1}")
print(f"Area of x2(t) (rectangular pulse): {area_x2}")
print(f"Area of x1(t) * x2(t) (convolution): {area_conv}")
print(f"Product of areas: {area_x1 * area_x2}")

# Plotting
plt.figure(figsize=(12, 6))

plt.subplot(3, 1, 1)
plt.plot(t, x1_area, label='x1(t) = rect(t)')
plt.title('First Rectangular Pulse')
plt.legend()

plt.subplot(3, 1, 2)
plt.plot(t, x2_area, label='x2(t) = rect(t)')
plt.title('Second Rectangular Pulse')
plt.legend()

plt.subplot(3, 1, 3)
plt.plot(t, conv_area, label='Convolution: x1(t) * x2(t)')
plt.title('Convolution of Two Rectangular Pulses')
plt.legend()

plt.tight_layout()
plt.show()

Conditions:
1. We define a triangular pulse tri(t) and a rectangular pulse rect(t).
2. The areas of the individual signals are computed using numerical integration (np.trapz),
and we check if the area of the convolution equals the product of these areas.
3. When calculating the area of a function using numerical integration (np.trapz), ensure
you’re accounting for the sign of the values. Areas should always be positive unless
there's a specific reason to expect a negative result. Consider whether taking the absolute
value of the areas might be necessary for a more accurate result.

iv. Code 4: Convolution of Triangular Pulse and Rectangular Pulse


# Triangular pulse (tri(t))
def tri(t):
return np.where(np.abs(t) <= 1, 1 - np.abs(t), 0)

# Triangular pulse and rectangular pulse


x1_area = tri(t)
x2_area = rect(t)

# Convolution of x1 and x2
conv_area = convolve(x1_area, x2_area, mode='same') * (t[1] - t[0])

# Calculate areas (error: incorrect function used for convolution)


area_x1 = np.trapz(x1_area, t) # Area of triangular pulse
area_x2 = np.trapz(x2_area, t) # Area of rectangular pulse
area_conv = np.sum(conv_area) # ERROR: Should be np.trapz(conv_area,
t)

# Print calculated areas


print(f"Area of x1(t) (triangular pulse): {area_x1}")
print(f"Area of x2(t) (rectangular pulse): {area_x2}")
print(f"Area of x1(t) * x2(t) (convolution): {area_conv}")
print(f"Product of areas: {area_x1 * area_x2}")

# Plotting
plt.figure(figsize=(12, 6))

plt.subplot(3, 1, 1)
plt.plot(t, x1_area, label='x1(t) = tri(t)')
plt.title('Triangular Pulse')
plt.legend()

plt.subplot(3, 1, 2)
plt.plot(t, x2_area, label='x2(t) = rect(t)')
plt.title('Rectangular Pulse')
plt.legend()

plt.subplot(3, 1, 3)
plt.plot(t, conv_area, label='Convolution: x1(t) * x2(t)')
plt.title('Convolution of Triangular and Rectangular Pulse')
plt.legend()

plt.tight_layout()
plt.show()

Conditions:
1. When calculating the area of the convolution result, are you using the right function to
compute the integral? You should be using a method that approximates the area under a
curve. Summing the values directly isn’t always an accurate way to compute the area, so
consider using an integration function that accounts for the continuous nature of the signal.

III. Reminders:
a. Progress Report Hierarchy (Code 1, Code 2, Code 3, Code 4)
i. TS Property
1. Plots Code 1
2. Plots Code 2
3. Correction made for 1
4. Correction made for 2
5. Explanation if the property is fulfilled by the corrected codes.
6. 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.

You might also like