Code covered by the BSD License  

Highlights from
MatLab Solutions: "Introduction to Digital Signal Processing: A Computer Laboratory Textbook".

image thumbnail

MatLab Solutions: "Introduction to Digital Signal Processing: A Computer Laboratory Textbook".

by

 

29 Oct 2012 (Updated )

These files are the MatLab solutions of exercises contained in the above DSP lab textbook.

ex438.m
% Exercise 4.3.8. Fractional Sample Delay.

clc; clear; close all;

%% Signal Definition.
n = 0:256;
x = sin(pi/32*n);
%% Step (b).  
% Create a Lh+1-point low pass filter with cutoff pi/L and gain L.
Lh = 128;
M = 1;
L  = 2;
w0 = pi/L;
h = L*[ sin(w0*(-Lh/2:-1))./(pi*(-Lh/2:-1)) w0/pi sin(w0*(1:Lh/2))./(pi*(1:Lh/2))]; % zero-phase low-pass FIR with cutoff at pi/L.
wh = hamming(Lh+1).'; % Lh+1-point symmetric Hamming window.

y1 = fraction_delay3(x,h.*wh,M,L); 
% Keep the valid samples only (truncate the leading and trailing samples):
y1T = y1(Lh/L+2:end-Lh/L-1);
Ly = length(y1);

figure('Name','Exercise 4.3.8. Fractional Sample Delay');
subplot(1,2,1);
plot(n,x,'b.-');
hold on;
title('x[n] = sin(\pi/*n) (blue) and y_1[n] = x[n-1/2] (red)');
grid on;
axis tight;

subplot(1,2,1);
stem(y1T,'r.');
% title('y_1[n] = x[n-1/2]');
grid on;
axis tight;

%% Computation of an approximation of X(j) and Xhat(j):
w1 = [-pi/4  pi/4];

[w     X] = my_DTFT2(x,n,w1);
[w   Y1] = my_DTFT2(y1,0:Ly-1,w1);

%% Plot the magnitude of X(j):
subplot(2,2,2);
plot(w,abs(X),'b','Linewidth',4);
hold on;
title(['|X(j\omega)| (blue), |Y_1(j\omega)| (red)']);
set(gca,'XTick',-pi/4:pi/16:pi/4);
set(gca,'XTickLabel',{'-pi/4','-3pi/16','-pi/8','-pi/16','0','pi/16','pi/8','3pi/16','pi/4' })
xlabel('\omega (rad/sample)');
axis tight;
grid on;

% Plot the magnitude of Y1(j):
subplot(2,2,2);
plot(w,abs(Y1),'r','Linewidth',2);
set(gca,'XTick',-pi/4:pi/16:pi/4);
set(gca,'XTickLabel',{'-pi/4','-3pi/16','-pi/8','-pi/16','0','pi/16','pi/8','3pi/16','pi/4' })
axis tight;
grid on;

subplot(2,2,4);
plot(w,angle(X));
hold on;
title(['\angleX(j\omega) (blue), \angleY_1(j\omega) (red)']);
set(gca,'XTick',-pi/4:pi/16:pi/4);
set(gca,'XTickLabel',{'-pi/4','-3pi/16','-pi/8','-pi/16','0','pi/16','pi/8','3pi/16','pi/4' })
xlabel('\omega (rad/sample)');
axis tight;
grid on;

subplot(2,2,4);
plot(w,angle(Y1),'r');
set(gca,'XTick',-pi/4:pi/16:pi/4);
set(gca,'XTickLabel',{'-pi/4','-3pi/16','-pi/8','-pi/16','0','pi/16','pi/8','3pi/16','pi/4' })
axis tight;
grid on;

%% Step (c). Delay [n-64] by M/L = 1/2 sample.
Ly2 = 128;
n = 0:Ly2-1;
d = [ zeros(1,Ly2/2) 1 zeros(1,Ly2/2-1)];

y2 = fraction_delay3(d,h.*wh,M,L); 
y2T = y2(Lh/L+2:end-Lh/L-1);

figure('Name','Exercise 4.3.8. Fractional Sample Delay');
stem(n,d,'b.');
hold on;
title('\delta[n-64] (blue) and y_2[n] = \delta[n-64-1/2] (red)');
grid on;

plot(y2T,'r.');
grid on;

%% Step (d).
n = 0:79;
x3 = [ones(1,64) zeros(1,16)];

y3 = fraction_delay3(x3,h.*wh,M,L); 

y3T= y3(Lh/L+2:end-Lh/L-1);
Ly = length(y3);

figure('Name','Exercise 4.3.8. Fractional Sample Delay');stem(n,x3,'b.');
hold on;
title('x_3[n] (blue) and y_3[n] = x_3[n-1/2] (red)');
grid on;
% axis tight;

plot(y3T,'r.');
% title('y_3[n] = x_3[n-1/2] (red)');
grid on;
% axis tight;

Contact us