Lab 3
Lab 3
CONVOLUTION
Submitted by
- Header/Footer Yes No
- Spelling Yes No
Total Score
Date:
Signature
List of Figures
Figure 1. Result of the program in Problem 1. 10
𝑦(𝑛) = ∑ ℎ (𝑚)𝑥(𝑛 − 𝑚)
𝑚=0
where:
• 𝑥(𝑛) is the input sequence
• ℎ(𝑛) is the impulse response
• 𝑦(𝑛) is the output sequence
• 𝑚 is the summation index
Properties of Convolution
Length Property: For two finite sequences of lengths 𝐿𝑥 and 𝐿ℎ , the convolution result has
length:
𝐿𝑦 = 𝐿𝑥 + 𝐿ℎ − 1
Index Range: For causal sequences starting at n=0, the output sequence spans:
0 ≤ 𝑛 ≤ 𝐿𝑥 + 𝐿ℎ − 2
Frequency Domain Relationship
The convolution theorem states that convolution in the time domain corresponds to
multiplication in the frequency domain:
If 𝑥(𝑛) ↔ 𝑋(𝑒 𝑗𝜔 ) and ℎ(𝑛) ↔ 𝐻(𝑒 𝑗𝜔 ), then:
𝑦(𝑛) = 𝑥(𝑛) ∗ ℎ(𝑛) ↔ 𝑌(𝑒 𝑗𝜔 ) = 𝑋(𝑒 𝑗𝜔 ) ⋅ 𝐻(𝑒 𝑗𝜔 )
This relationship provides an alternative method for computing convolution using Fast Fourier
Transform (FFT) algorithms, which can be computationally more efficient for long sequences.
𝑦(𝑛) = ∑ ℎ (𝑚)𝑥(𝑛 − 𝑚)
𝑚=0
Experimental Results
Problem 1:
% Problem 1a: Basic convolution with user input
clear all; close all;
% Compute convolution
y = conv(x, h);
ny_start = nx_start + nh_start;
ny = ny_start:(ny_start + length(y) - 1);
figure;
subplot(3,1,1);
stem(n, x_b, 'b', 'LineWidth', 2);
title('Input Signal x[n] = u[n+10] - u[n-5]');
subplot(3,1,2);
stem(n, h_b, 'r', 'LineWidth', 2);
title('System Response h[n] = 2^n u[n]');
subplot(3,1,3);
stem(ny_full, y_conv, 'k', 'LineWidth', 2);
title('Convolution Output y[n] = x[n] * h[n]');
X_fft = fft(x_padded);
H_fft = fft(h_padded);
Y_fft = X_fft .* H_fft;
y_ifft = real(ifft(Y_fft));
% Compare results
max_diff = max(abs(y_conv - y_ifft(1:length(y_conv))));
fprintf('Maximum difference: %e\n', max_diff);
figure;
subplot(1,2,1);
stem(ny_full, y_conv, 'b', 'LineWidth', 2);
title('Direct Convolution');
subplot(1,2,2);
stem(ny_full, y_ifft(1:length(y_conv)), 'r', 'LineWidth', 2);
title('FFT-based Convolution');
Result
for n = 1:Ly
for m = 1:Lh
x_idx = ny(n) - nh(m);
x_pos = find(nx == x_idx);
if ~isempty(x_pos)
y(n) = y(n) + h(m) * x(x_pos);
end
end
end
end
nx = 0:5;
x = ones(1, length(nx));
% Compute convolution
[y_custom, ny] = customConv(x, h, nx, nh);
y_matlab = conv(x, h);
% Display results
max_error = max(abs(y_custom - y_matlab));
fprintf('Maximum error: %.2e\n', max_error);
subplot(3,1,1);
stem(nx, x, 'b', 'filled', 'LineWidth', 2);
title('Input Signal x[n] = 1, 0 ≤ n ≤ 5');
xlabel('n'); ylabel('x[n]');
grid on;
subplot(3,1,2);
stem(nh, h, 'r', 'filled', 'LineWidth', 2);
title('Impulse Response h[n] = e^{-n}, -10 ≤ n ≤ 10');
xlabel('n'); ylabel('h[n]');
grid on;
% Comparison figure
figure('Position', [150, 150, 800, 400]);
subplot(1,2,1);
stem(ny, y_custom, 'b', 'filled', 'LineWidth', 2);
title('Custom Convolution Result');
xlabel('n'); ylabel('y[n]');
grid on;
subplot(1,2,2);
stem(ny, y_matlab, 'r', 'filled', 'LineWidth', 2);
title('MATLAB conv() Result');
xlabel('n'); ylabel('y[n]');
grid on;
Result
% Plot results
figure;
subplot(2,1,1);
plot(t_orig, x, 'b');
title('Original Audio Signal');
xlabel('Time (s)'); ylabel('Amplitude');
subplot(2,1,2);
plot(t_echo, y, 'r');
title('Echo-Added Audio Signal');
xlabel('Time (s)'); ylabel('Amplitude');
figure;
stem(0:length(h)-1, h, 'filled', 'k');
title('Echo Impulse Response h[n]');
xlabel('Sample Index'); ylabel('Amplitude');
% Audio playback
sound(x, Fs);
pause(length(x)/Fs + 1);
sound(y, Fs);
audiowrite('output_echo.wav', y, Fs);
Practical Applications: The echo generator successfully created realistic audio effects using
0.2-second delay and 0.8 amplitude scaling, demonstrating convolution's role in real-world
signal processing.
THE END