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

Ee648hw4 Bokhtiar

DIGITAL SIGNAL PROCESSING HOMEWORK

Uploaded by

Bokhtiar Al Zami
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)
8 views

Ee648hw4 Bokhtiar

DIGITAL SIGNAL PROCESSING HOMEWORK

Uploaded by

Bokhtiar Al Zami
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/ 30

EE648 HW4 QUESTION 1B %%

Define the impulse response h[n]

h = [1 0 0 0 1]; % h[n] = δ[n] + δ[n-4]

% Calculate and plot the group delay


[gd, w] = grpdelay(h, 1);

% Plot the group delay


figure;
plot(w/pi, gd);
title('Group Delay of the System');
xlabel('Normalized Frequency (\times\pi rad/sample)');
ylabel('Group Delay (samples)');
grid on;

Published with MATLAB® R2024a

1
EE648 HW4 QUESTION 3a %%
Define the poles and zeros

zeros = [exp(1j*pi/3), exp(-1j*pi/3), -1.1765];


poles = [0.9*exp(1j*pi/3), 0.9*exp(-1j*pi/3), -0.85];

% Convert to polynomial coefficients


numerator = poly(zeros); % Numerator coefficients
denominator = poly(poles); % Denominator coefficients

% Plot pole-zero diagram


zplane(numerator, denominator);
title('Pole-Zero Diagram');
grid on;

% Annotate the plot with pole and zero values


hold on;
plot(real(zeros), imag(zeros), 'ro', 'MarkerSize', 10);
plot(real(poles), imag(poles), 'bx', 'MarkerSize', 10);

text(real(zeros) + 0.1, imag(zeros), arrayfun(@num2str, zeros,


'UniformOutput', false));
text(real(poles) + 0.1, imag(poles), arrayfun(@num2str, poles,
'UniformOutput', false));
hold off;

1
Published with MATLAB® R2024a

2
EE648 HW4 QUESTION 3c %%
% Define the poles and zeros
zeros = [exp(1j*pi/3), exp(-1j*pi/3), -1.1765];
poles = [0.9*exp(1j*pi/3), 0.9*exp(-1j*pi/3), -0.85];

% Convert to polynomial coefficients


numerator = poly(zeros); % Numerator coefficients
denominator = poly(poles); % Denominator coefficients

% Compute the frequency response


[H, w] = freqz(numerator, denominator, 1024, 'whole'); % 1024 points from 0
to 2pi

% Shift the frequency axis to -pi to pi


w = w - pi;

% Plot the magnitude response


subplot(2, 1, 1);
plot(w/pi, abs(fftshift(H))); % fftshift to center the plot around 0
title('Magnitude Response');
xlabel('Normalized Frequency (\times \pi rad/sample)');
ylabel('Magnitude');
grid on;

% Plot the phase response


subplot(2, 1, 2);
plot(w/pi, angle(fftshift(H))); % fftshift to center the plot around 0
title('Phase Response');
xlabel('Normalized Frequency (\times \pi rad/sample)');
ylabel('Phase (radians)');
grid on;

1
Published with MATLAB® R2024a

2
% EE648 HW4 QUESTION 4a %%

% Define the numerator and denominator coefficients


numerator = [1 0 -4]; % Corresponds to z^2 - 4
denominator = [1 -1/4 -3/8]; % Corresponds to z^2 - (1/4)z - (3/8)

% Create the transfer function

H = tf(numerator, denominator, -1);


% Plot the pole-zero diagram
figure;
pzmap(H);
title('Pole-Zero Diagram of H(z)');
grid on;

Published with MATLAB® R2024a

1
EE648 HW4 QUESTION 4d %%
Define the numerator and denominator coefficients for H_min(z)

numerator_min = [1 0 -1/4]; % Corresponds to z^2 - 1/4


denominator_min = [1 -1/4 -3/8]; % Corresponds to z^2 - (1/4)z - (3/8)

% Define the numerator and denominator coefficients for H_ap(z)


numerator_ap = [1 0 -4]; % Corresponds to (z - 2)(z + 2) = z^2 - 4
denominator_ap = [1 0 -1/4]; % Corresponds to (z - 1/2)(z + 1/2) = z^2 - 1/4

% Create the transfer functions


H_min = tf(numerator_min, denominator_min, -1);
H_ap = tf(numerator_ap, denominator_ap, -1);

% Plot the pole-zero diagram for H_min(z)


figure;
pzmap(H_min);
title('Pole-Zero Diagram of H_{min}(z)');
grid on;

% Plot the pole-zero diagram for H_ap(z)


figure;
pzmap(H_ap);
title('Pole-Zero Diagram of H_{ap}(z)');
grid on;

1
2
Published with MATLAB® R2024a

3
Table of Contents
EE648 QUESTION 5 %% .................................................................................................................... 1
a ...................................................................................................................................................... 1
b ...................................................................................................................................................... 2
c ...................................................................................................................................................... 3
d ...................................................................................................................................................... 4
e ...................................................................................................................................................... 5
f ....................................................................................................................................................... 5
g ...................................................................................................................................................... 6
h ...................................................................................................................................................... 7
i ....................................................................................................................................................... 7
j ....................................................................................................................................................... 8

EE648 QUESTION 5 %%
close all,
clear all,
clc,

a
n=-1000:1:1000;
Xn = (1/4)*sinc(n/4);
figure
plot(n,Xn); title(' Xn Plot '); xlabel('n'); ylabel('Xn'); grid on

1
b
xd = downsample(Xn,3);
Xe = upsample(xd,3);
figure,
plot(n,Xe); title(' Xe Plot '); xlabel('n'); ylabel('Xe'); grid on

2
c
nfilt=50;
filt = fir1(nfilt,1/3,'low');
figure, plot(filt); title(' Filter Coefficients '); grid on

3
d
figure,
freqz(filt,1,512);

4
e
Scale=3;
Xr = Scale*filter(filt,1,Xe);

f
figure,
plot(n,Xn);
hold on,
plot(n,Xr,'r');
title('Input and reconstructed sequences'); xlabel('n'); ylabel('Xr/Xn');
legend('Xn', 'Xr'); grid on

5
g
delay = grpdelay(filt);
avg_delay = mean(delay);
X_trunc = Xn(1:end-avg_delay);
Xr_Shift = Xr((avg_delay+1):end);
figure,
plot(X_trunc);
hold on
plot(Xr_Shift,'r');
grid on
title('X trunc and Xr Shift Plot');
legend('X trunc','Xr Shift')
% Display the average delay
disp(['Average delay (avg_delay) introduced by the filter: ',
num2str(avg_delay)]);

Average delay (avg_delay) introduced by the filter: 25

6
h
%MSE = sumsqr(X_trunc - Xr_Shift)/length(X_trunc);

difference = X_trunc - Xr_Shift;


squaredDifference = difference .^ 2;
sumOfSquaredDifferences = sum(squaredDifference);

% Calculate the mean squared error


MSE = sumOfSquaredDifferences / length(X_trunc);
disp(["MSE is :", MSE]);

"MSE is :" "3.4134e-10"

i
FilterOrder = [10, 20, 50, 100, 200];
NewMSE = zeros(1, length(FilterOrder));
GroupDelay = zeros(1, length(FilterOrder));

for r = 1:length(FilterOrder)
nfilt = FilterOrder(r);
filt = fir1(nfilt, 1/3, 'low');
Scale = 3;

7
Xr = Scale * filter(filt, 1, Xe);
delay = mean(grpdelay(filt));
X_trunc = Xn(1:end-delay);
Xr_Shift = Xr((delay+1):end);
difference = X_trunc - Xr_Shift;
squaredDifference = difference .^ 2;
sumOfSquaredDifferences = sum(squaredDifference);
MSE = sumOfSquaredDifferences / length(X_trunc);

NewMSE(r) = MSE;
GroupDelay(r) = delay;
end
% Create a table for the results
resultsTable = table(FilterOrder', NewMSE', 'VariableNames', {'Filter
Order', 'MSE'});
disp(resultsTable);

Filter Order MSE


____________ __________

10 3.7207e-06
20 2.5196e-07
50 3.4134e-10
100 2.1172e-10
200 4.7458e-11

j
Create a table for the results

resultsTable = table(FilterOrder', NewMSE', GroupDelay', 'VariableNames',


{'Filter Order', 'MSE', 'Group Delay'});
disp(resultsTable);

% Observations
fprintf('\nObservations:\n');
fprintf('1. As the filter order increases, the MSE generally decreases,
indicating better signal reconstruction.\n');
fprintf('2. Higher filter orders introduce longer group delays, which need
to be compensated for when aligning the signals.\n');
fprintf('3. Beyond a certain filter order, the reduction in MSE becomes less
significant, indicating diminishing returns.\n');
fprintf('4. The group delay increases approximately linearly with the filter
order.\n');

Filter Order MSE Group Delay


____________ __________ ___________

10 3.7207e-06 5
20 2.5196e-07 10
50 3.4134e-10 25
100 2.1172e-10 50
200 4.7458e-11 100

8
Observations:
1. As the filter order increases, the MSE generally decreases, indicating
better signal reconstruction.
2. Higher filter orders introduce longer group delays, which need to be
compensated for when aligning the signals.
3. Beyond a certain filter order, the reduction in MSE becomes less
significant, indicating diminishing returns.
4. The group delay increases approximately linearly with the filter order.

Published with MATLAB® R2024a

You might also like