0% found this document useful (0 votes)
0 views

Lab 6 DSP

This document outlines Lab Number 6 for the EC312 Digital Signal Processing course, focusing on linear convolution and moving averaging filters using MATLAB. It includes tasks such as implementing manual convolution, using MATLAB's built-in convolution function, generating a noisy sinusoidal signal, and applying a moving average filter with different window sizes. The results are visualized through various plots comparing original, noisy, and filtered signals.

Uploaded by

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

Lab 6 DSP

This document outlines Lab Number 6 for the EC312 Digital Signal Processing course, focusing on linear convolution and moving averaging filters using MATLAB. It includes tasks such as implementing manual convolution, using MATLAB's built-in convolution function, generating a noisy sinusoidal signal, and applying a moving average filter with different window sizes. The results are visualized through various plots comparing original, noisy, and filtered signals.

Uploaded by

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

DEPARTMENT OF COMPUTER &

SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI

EC312 Digital Signal Processing


Lab Number: 6

SUBMITTED TO:
LE Sundas

SUBMITTED BY:
Shaheer Mukhtiar
Reg # 432017
DE- 44 Dept CE

Submission Date: 18 March 2025


LAB # 06 Linear convolution and Moving Averaging Filter
Lab Task:

a) Write a MATLAB function which takes two signals x[n] and h[n] as parameters and
perform the convolution of the two signals.
Code:
function y = man_conv(x, h)
y = zeros(length(x) + length(h) -1);

for i = 1:length(x)
for j = 1:length(h)
y(i+j-1) = y(i+j-1) + x(i) * h(j);
end
end
end

x_n = [1 3 7 9];
h_n = [1 1 1];

y_man = man_conv(x_n, h_n);

stem(y_man)
xlabel("Samples")
ylabel("Value")
title("Manual Convolution")

Output:
b) Use MATLAB built-in function of convolution to perform the convolution of two same
signals and compare the results with Lab Task # 01. The result of convolution should be
same. MATLAB function for convolution is conv.
Code:

y_mat = conv(x_n, h_n);

subplot(2, 1, 1)
stem(y_man)
xlabel("Samples")
ylabel("Value")
title("Manual Convolution")

subplot(2, 1, 2)
stem(y_mat)
xlabel("Samples")
ylabel("Value")
title("Matlab Convolution")

Output:

c) Generate a clean sinusoidal signal with a duration of 2 seconds and a sampling frequency
of 1000 Hz. Then, create Gaussian noise with an amplitude of 0.5 and add it to the clean
sinusoidal signal to produce a noisy signal
Code:
fs = 1000;
T = 1;
t = 0:1/fs:T-1/fs;
clean_signal = sin(2*pi*10*t);

noise = 0.5 * randn(size(t));


noisy_signal = clean_signal + noise;

subplot(2, 1, 1)
plot(clean_signal)
xlabel("Time")
ylabel("Amplitude")
title("Original Signal")

subplot(2, 1, 2)
plot(noisy_signal)
xlabel("Time")
ylabel("Amplitude")
title("Noisy Signal")

Output:

d) Create a MATLAB function to implement a moving average filter for the noisy signal
generated in Task 2. Apply the moving average filter using different window sizes (e.g.,
3, 5, 10) and compare the filtered signals. Analyze the trade-off between the level of
smoothing achieved and the potential distortion of the original signal.
Code:

%% Task 4

function y = av_filter(x, M)

y = zeros(length(x));

for i = 1:length(x)
start = max(1, i - floor(M/2));
edge = min(length(x), i+floor(M/2));
y(i) = 1/M * mean(x(start:edge));
end
end

y_filtered_3 = av_filter(noisy_signal, 3);


y_filtered_5 = av_filter(noisy_signal, 5);
y_filtered_10 = av_filter(noisy_signal, 10);

subplot(5, 1, 1)
plot(clean_signal)
xlabel("Time")
ylabel("Amplitude")
title("Original Signal")

subplot(5, 1, 2)
plot(noisy_signal)
xlabel("Time")
ylabel("Amplitude")
title("Noisy Signal")

subplot(5, 1, 3)
plot(y_filtered_3)
xlabel("Time")
ylabel("Amplitude")
title("Filtered Signal by 3")

subplot(5, 1, 4)
plot(y_filtered_5)
xlabel("Time")
ylabel("Amplitude")
title("Filtered Signal by 5")

subplot(5, 1, 5)
plot(y_filtered_10)
xlabel("Time")
ylabel("Amplitude")
title("Filtered Signal by 10")
Output:

You might also like