You are on page 1of 8

DSP SOLVED EXERCISES:

LAB1

Note: The following functions need to be in current folder:


1) sigadd()
2) sigmult()
3) sigshift()
4) sigfold()

1) x1(n) = 2 * x(n+3) + x(n) * x(5-n)

2) x2(n) = x(n+2) * x(n) – x(-n-1)

Solution:

1)
% Solution of Exercise1
% Given x(n):
x = [1,-2,4,6,-5,8,10];
xn = [-4:2];
% 1) Compute x1(n) = 2 * x(n+3) + x(n) * x(5-n)

% let p1 = 2 * x(n+3) ,shifting by -3 then multiply by 2


[p1,pn1] = sigshift(x,xn,-3);
p1 = 2 * p1;

% let p2 = x(n)
p2 = x;
pn2 = xn;

%let p3 = x(5-n) = x(-(n-5)) ,shifting by 5 then folding


[p3,pn3] = sigshift(x,xn,5);
[p3,pn3] = sigfold(p3,pn3);

% let p4 = x(n) * x(5-n) = p2 * p3


[p4,pn4] = sigmult(p2,pn2,p3,pn3);

% Now x1 = x(n+3) + x(n) * x(5-n) = p1 + p4


[x1,xn1] = sigadd(p1,pn1,p4,pn4)

% Plotting
stem(xn1,x1);
Command window result:

x1 =

2 -4 8 18 -18 8 26 0 0 0

xn1 =

-7 -6 -5 -4 -3 -2 -1 0 1 2

2) % Solution of Exercise2
% Given x(n):
x = [1,-2,4,6,-5,8,10];
xn = [-4:2];
% 2) Compute x2(n) = x(n+2) * x(n) – x(-n-1)

% let p1 = x(n+2) shifting by -2


[p1,pn1] = sigshift(x,xn,-2);

% let p2 = x(n)
p2 = x;
pn2 = xn;

%let p3 = x(-n-1) = x(-(n+1)) shifting by -1 then folding


[p3,pn3] = sigshift(x,xn,-1);
[p3,pn3] = sigfold(p3,pn3);
Followed in same script:
% let p4 = x(n+2) * x(n) = p1 * p2
[p4,pn4] = sigmult(p1,pn1,p2,pn2);

% Finally x2(n) = x(n+1) * x(n) - x(-n-1) = p4 - p3 = p4 + (-p3)


[x2,xn2] = sigadd(p4,pn4,-p3,pn3)

% Plotting
stem(xn2,x2);

Command window result:

x2 =

0 0 4 -12 -20 38 -58 5 -6 -4 2 -1

xn2 =

-6 -5 -4 -3 -2 -1 0 1 2 3 4 5
LAB2

Let x(n) = [0,0,0,0,1,1,1,1,1,0,0,0,0] , for -6 <= n <= 6


h(n) = [1,1,1,1] , for 0 <= n <=3
Compute the linear convolution y(n) = x(n) * h(n), using conv_m() function

Note: The following functions need to be in current folder:


1) conv_m()

Solution:
% Solution of Exercise1 LAB2
% Given x(n):
x = [0,0,0,0,1,1,1,1,1,0,0,0,0];
xn = [-6:6];
% Given h(n):
h = [1,1,1,1];
hn = [0:3];
% Compute the convolution sum y(n) = x(n) * h(n)
[y,yn] = conv_m(x,xn,h,hn)
subplot(3,1,1),stem(xn,x); % x(n)
subplot(3,1,2),stem(hn,h); % h(n)
subplot(3,1,3),stem(yn,y); % y(n)

Command window result:

y=

0 0 0 0 1 2 3 4 4 3 2 1 0 0 0 0

yn =

-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
LAB3

Using dtft() and idtft(), compute the magnitude and phase spectrum for the following finite
duration signal:
x(n) = [-1,-1,-1,-1,1,1,1,1] for -4 <= n <= 3

Note: The following functions need to be in current folder:


1) dtft()

Solution:
% Solution of Exercise1 LAB3
% Given x(n):
x = [-1,-1,-1,-1,1,1,1,1];
xn = [-4:3];

% Sample w by taking 101 values in interval [-pi,pi]


w = linspace(-pi,pi,101);

% Compute the spectrum


X = dtft(x,xn,w);

% Plotting
subplot(2,1,1),plot(w,abs(X)),xlabel('w'),ylabel('Magnitude Spectrum'),grid on;
subplot(2,1,2),plot(w,angle(X)),xlabel('w'),ylabel('Phase Spectrum'),grid on;
LAB5

Using the dft.m function, compute the DFT of the following signal:

x(n) = [0,1,2,3,0,1,2,3] , where N = 8

Plot its time and magnitude spectrum.

Note: The following functions need to be in current folder:


1) dft()

Solution:
% Solution of Exercise1 LAB5
% Given x(n):
N = 8;
x = [0,1,2,3,0,1,2,3];
xn = [0:N-1];

% K frequency vector and w


k = [-N/2:N/2];
w = (2*pi)/N * k;

% Compute the spectrum and reorder the negative then positive frequencies
X = dft(x,N);
Xamp = abs([X(N/2+1:N) X(1:N/2+1)]);

% Plotting
subplot(2,1,1),stem(xn,x),xlabel('n'),ylabel('Amplitude'),grid on;
subplot(2,1,2),stem(w,Xamp),xlabel('w'),ylabel('Magnitude'),grid on;
LAB6

Solution:
a) Transforming the difference equation into system equation H(z) we get:

z −1
H (z)=
1−z−1−z −2
b)
b = [0,1];
a = [1,-1,-1];
zplane(b,a);
poles = roots(a)

poles = ROC is |Z| > 1.6180 , since unity circle is not within ROC, the system is
-0.6180 unstable. In other words, the system is unstable since not all poles within the unity circle
1.6180
c)
b = [0,1];
a = [1,-1,-1];
imp_sig = [1 , zeros(1,100)];
n = [0:100];
yn = filter(b,a,imp_sig);
stem(n,yn),xlabel('n'),ylabel('Amplitude'),title('Unit impulse response h(n)');

To find the frequency response of the system, H(e^jw):


b = [0,1];
a = [1,-1,-1];
w = linspace(0,pi,101);
H = freqz(b,a,w);
Hmag = abs(H); Hangle = angle(H);
subplot(2,1,1),plot(w,Hmag),grid on,xlabel('w'),ylabel('Magintude');
subplot(2,1,2),plot(w,Hangle),grid on,xlabel('w'),ylabel('Phase');

You might also like