Lab 6 DSP
Lab 6 DSP
SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI
SUBMITTED TO:
LE Sundas
SUBMITTED BY:
Shaheer Mukhtiar
Reg # 432017
DE- 44 Dept CE
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];
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:
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);
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
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: