You are on page 1of 14

Bie daalt

P2.1 Generate the following sequences using the basic MATLAB signal functions and the basic
MATLAB signal operations discussed in this chapter. Plot signal samples using the stem
function.
1. x1(n) = 3δ(n + 2) + 2δ(n) − δ(n − 3) + 5δ(n − 7), −5 ≤ n ≤ 15.

Editor:function [x,n] = impseq(n0,n1,n2)

% Generates x(n) = delta(n-n0); n1 <= n <= n2

% ----------------------------------------------

% [x,n] = impseq(n0,n1,n2)

n = [-5:15]; x = [(n-n0) == 0];

Command: n=[-5:15]; x=3*impseq(-2,-5,15)+2*impseq(-5,15)-impseq(3,-5,15)+5*impseq(7,-5,15);

Grapic:

3. x3(n) = 10u(n) − 5u(n − 5) − 10u(n − 10) + 5u(n − 15).

Editor: function [x,n] = stepseq(n0,n1,n2)

% Generates x(n) = u(n-n0); n1 <= n <= n2

% ------------------------------------------

% [x,n] = stepseq(n0,n1,n2)

n = [0:20]; x = [(n-n0) >= 0];


Command:

n = [0:20];

>> x=10*stepseq(0,0,20) - 5*stepseq(5,0,20) - 10*stepseq(10,0,20) + 5*stepseq(15,0,20);

>> stem(n,x)

Grapic:

4. x4(n) = e0 . 1 n[u(n + 20) − u(n − 10)].

Editor: f unction [x,n] = stepseq(n0,n1,n2)

% Generates x(n) = u(n-n0); n1 <= n <= n2

% ------------------------------------------

% [x,n] = stepseq(n0,n1,n2)

n = [n1:n2]; x = [(n-n0) >= 0];

Command:

n = [-25:15];

x = exp(0.1*n).*(stepseq(-20,-25,15) - stepseq(10,-25,15));

stem(n,x)
Grapic:

5. x5(n) = 5[cos(0.49πn) + cos(0.51πn)], −200 ≤ n ≤ 200. Comment on the waveform


shape.

Editor: function [x,n] = stepseq(n0,n1,n2)

% Generates x(n) = u(n-n0); n1 <= n <= n2

% ------------------------------------------

% [x,n] = stepseq(n0,n1,n2)

n = [n1:n2]; x = [(n-n0) >= 0];

Command:

n = [-200:200];

x = 5*(cos(0.49*pi*n) + cos(0.51*pi*n) );

stem(n,x)

Grapic:
6. x6(n) = 2 sin(0.01πn)cos(0.5πn), −200 ≤ n ≤ 200. Comment on the waveform shape.

Editor: function [x,n] = stepseq(n0,n1,n2)

% Generates x(n) = u(n-n0); n1 <= n <= n2

% ------------------------------------------

% [x,n] = stepseq(n0,n1,n2)

n = [n1:n2]; x = [(n-n0) >= 0];

Command:

n = [-200:200];

x = 2*sin(0.01*pi*n).*cos(0.5*pi*n);

stem(n,x)

Grapic:

7. x7(n) = e − 0 . 05 n sin(0.1πn + π/3), 0 ≤ n ≤ 100. Comment on the waveform shape.

Editor: function [x,n] = stepseq(n0,n1,n2)

% Generates x(n) = u(n-n0); n1 <= n <= n2

% ------------------------------------------

% [x,n] = stepseq(n0,n1,n2)

n = [n1:n2]; x = [(n-n0) >= 0];

Command:

n = [0:100];
x = exp(-0.05*n).*sin(0.1*pi*n + pi/3);

stem(n,x)

Grapic:

8. x8(n) = e0 . 01 n sin(0.1πn), 0 ≤ n ≤ 100. Comment on the waveform shape.

Editor: function [x,n] = stepseq(n0,n1,n2)

% Generates x(n) = u(n-n0); n1 <= n <= n2

% ------------------------------------------

% [x,n] = stepseq(n0,n1,n2)

n = [n1:n2]; x = [(n-n0) >= 0];

Command:

n = [0:100];

x = exp(0.01*n).*sin(0.1*pi*n);

stem(n,x)

Grapic:
P2.2 Generate the following random sequences and obtain their histogram using the hist function with 100 bins.
Use the bar function to plot each histogram.
1. x1(n) is a random sequence whose samples are independent and uniformly distributed over [0, 2] interval.
Generate 100,000 samples.

Command:

n1 = [0:100000-1]; x1 = 2*rand(1,100000);

Hf_1 = figure; set(Hf_1,NumberTitle,off,Name,

P0202a);

[h1,x1out] = hist(x1,100); bar(x1out, h1);

axis([-0.1 2.1 0 1200]);

xlabel(interval,FontSize,LFS);

ylabel(number of elements,FontSize,LFS);

title(Histogram of sequence x_1(n) in 100 bins,FontSize,TFS);

Grapic:
2. x2(n) is a Gaussian random sequence whose samples are independent with mean 10 and variance 10.
Generate 10,000 samples.

Command:

n2 = [1:10000]; x2 = 10 + sqrt(10)*randn(1,10000);

Hf_1 = figure; set(Hf_1,’NumberTitle’,’off’,’Name’,’P0202b’);

[h2,x2out] = hist(x2,100); bar(x2out,h2);

xlabel(’interval’,’FontSize’,LFS);

ylabel(’number of elements’,’FontSize’,LFS);

title(’Histogram of sequence x_2(n) in 100 bins’,’FontSize’,TFS);

Grapic:
3. x3(n) = x1(n) + x1(n − 1) where x1(n) is the random sequence given in part 1 above. Comment on the
shape of this histogram and explain the shape.
Command:

n1 = [0:100000-1]; x1 = 2*rand(1,100000);
Hf_1 = figure; set(Hf_1,’NumberTitle’,’off’,’Name’,’P0202c’);
[x11,n11] = sigshift(x1,n1,1);
[x3,n3] = sigadd(x1,n1,x11,n11);
[h3,x3out] = hist(x3,100);
bar(x3out,h3); axis([-0.5 4.5 0 2500]);
xlabel(’interval’,’FontSize’,LFS);
ylabel(’number of elements’,’FontSize’,LFS);
title(’Histogram of sequence x_3(n) in 100 bins’,’FontSize’,TFS);

4. x4(n) = P4k =1 yk(n) where each random sequence yk(n) is independent of others with samples uniformly
distributed over [−0.5, 0.5]. Comment on the shape of this histogram.

Command:

y1 = rand(1,100000) - 0.5; y2 = rand(1,100000) - 0.5;


y3 = rand(1,100000) - 0.5; y4 = rand(1,100000) - 0.5;
x4 = y1 + y2 + y3 + y4;
Hf_1 = figure; set(Hf_1,’NumberTitle’,’off’,’Name’,’P0202d’);
[h4,x4out] = hist(x4,100); bar(x4out,h4);
xlabel(’interval’,’FontSize’,LFS);
ylabel(’number of elements’,’FontSize’,LFS);
title(’Histogram of sequence x_4(n) in 100 bins’,’FontSize’,TFS);

Grapic:
P2.3 Generate the following periodic sequences and plot their samples (using the stem function) over the
indicated number of periods.

1. x˜1(n) = {. . . , −2, −1, 0, 1, 2, . . .}periodic. Plot 5 periods.

2. x˜2(n) = e0.1 n[u(n) − u(n − 20]periodic. Plot 3 periods.

Command:

n2 = [0:21]; x2 = exp(0.1*n2).*(stepseq(0,0,21)-stepseq(20,0,21));
x2 = x2’*ones(1,3); x2 = (x2(:))’; n2 = [-22:43];
Hf_1 = figure; set(Hf_1,’NumberTitle’,’off’,’Name’,’P0203b’);
Hs = stem(n2,x2,’filled’); set(Hs,’markersize’,2);
axis([min(n2)-2,max(n2)+4,min(x2)-1,max(x2)+1]);
xlabel(’n’,’FontSize’,LFS); ylabel(’x_2(n)’,’FontSize’,LFS);
title(’Sequence x_2(n)’,’FontSize’,TFS);
ntick = [n2(1):4:n2(end)-5 n2(end)];
set(gca,’XTickMode’,’manual’,’XTick’,ntick); stem(n2,x2)

Grapic:

3. x˜3(n) = sin(0.1πn)[u(n) − u(n − 10)]. Plot 4 periods.

Command:

n3 = [0:11]; x3 = sin(0.1*pi*n3).*(stepseq(0,0,11)-stepseq(10,0,11));
x3 = x3’*ones(1,4); x3 = (x3(:))’; n3 = [-12:35];
Hf_1 = figure; set(Hf_1,’NumberTitle’,’off’,’Name’,’P0203c’);
Hs = stem(n3,x3,’filled’); set(Hs,’markersize’,2);
axis([min(n3)-1,max(n3)+1,min(x3)-0.5,max(x3)+0.5]) ;
xlabel(’n’,’FontSize’,LFS); ylabel(’x_3(n)’,’FontSize’,LFS);
title(’Sequence x_3(n)’,’FontSize’,TFS);
ntick = [n3(1):4:n3(end)-3 n3(end)];
set(gca,’XTickMode’,’manual’,’XTick’,ntick); stem(n3,x4);

Grapic:

P2.15 Determine analytically the convolution y(n) = x(n) ∗ h(n) of the following sequences, and
verify your answers using the conv_m function.
1. x(n) = {2, −4, 5, 3, −1, −2, 6 }, h(n) = {1, −1, 1, −1, 1 }

Editor: function [y,ny] = conv_m(x,nx,h,nh)

nyb = nx(1)+nh(1); nye = nx(length(x)) + nh(length(h));

ny = [nyb:nye]; y = conv(x,h);

Command:

x=[2,-4,5,3,-1,-2,6];

nx=[-3:3];

h=[1,-1,1,-1,1]; nh=[-1:3];

[y,ny] = conv_m(x,nx,h,nh)

y =2 -6 11 -8 7 -7 9 -4 7 -8 6

ny = -4 -3 -2 -1 0 1 2 3 4 5 6

2. x(n) = {1, 1, 0, 1, 1 }, h(n) = {1, −2, −3, 4}

Command:

x=[1,1,0,1,1]; nx=[-2:2]; h=[1,-2,-3,4]; nh=[-3:0]; [y,ny] = conv_m(x,nx,h,nh)

y = 1 -1 -5 2 3 -5 1 4

ny = -5 -4 -3 -2 -1 0 1 2
3. x(n) = (1/4)−n[u(n + 1) − u(n − 4)], h(n) = u(n) − u(n − 5): Matlab script:

Editor:

Conv_m.m: function [y,ny] = conv_m(x,nx,h,nh)

% Modified convolution routine for signal processing

% --------------------------------------------------

% [y,ny] = conv_m(x,nx,h,nh)

% [y,ny] = convolution result

% [x,nx] = first signal

% [h,nh] = second signal

nyb = nx(1)+nh(1); nye = nx(length(x)) + nh(length(h));

ny = [nyb:nye]; y = conv(x,h);

stepseq.m: function [x,n] = stepseq(n0,n1,n2)

n = [n1:n2]; x = [(n-n0) >= 0];

sigadd.m: function [y,n] = sigadd(x1,n1,x2,n2)

n = min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)

y1 = zeros(1,length(n)); y2 = y1; % initialization

y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y

y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y

y = y1+y2; % sequence addition

Command:

n1 = -2:5; [x11,nx11] = stepseq(-1,-2,5); [x12,nx12] = stepseq(4,-2,5);

[x13,n13] = sigadd(x11,nx11,-x12,nx12); x14 = 0.25 .^ -n1; n14 = n1;

x = x14 .* x13;

n2 = 0:6; [h11,nh11] = stepseq(0,0,6); [h12,nh12] = stepseq(5,0,6); h=h11-h12;


[y,n] = conv_m(x,n1,h,n2); y, n,

y =0.00000 0.25000 1.25000 5.25000 21.25000 85.25000 85.00000 84.00000 80.00000 64.00000 0.00000
0.00000 0.00000 0.00000

n = -2 -1 0 1 2 3 4 5 6 7 8 9 10 11

4. x(n) = n/4[u(n) − u(n − 6)], h(n) = 2[u(n + 2) − u(n − 3)]: Matlab script:

Command:
n1 = 0:7; [x11,nx11] = stepseq(0,0,7); [x12,nx12] = stepseq(6,0,7);
[x13,n13] = sigadd(x11,nx11,-x12,nx12); x14 = n1/4; n14 = n1; x = x14 .* x13;
n2 = -3:4; [h11,nh11] = stepseq(-2,-3,4); [h12,nh12] = stepseq(3,-3,4);
h = 2 * (h11 - h12); [y,n] = conv_m(x,n1,h,n2); y, n,
y = 0 0 0.5000 1.5000 3.0000 5.0000 7.5000 7.0000 6.0000 4.5000 2.5000 0 0 0 0
n = -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11
P2.16 Let x(n) = (0.8) nu(n), h(n) = ( −0.9) nu(n), and y(n) = h(n) ∗ x(n). Use 3 columns and 1
row of subplots for the following parts.
1. Determine y(n) analytically. Plot first 51 samples of y(n) using the stem function.
2. Truncate x(n) and h(n) to 26 samples. Use conv function to compute y(n). Plot y(n)
using the stem function. Compare your results with those of part 1.
3. Using the filter function, determine the first 51 samples of x(n) ∗ h(n). Plot y(n) using
the stem function. Compare your results with those of parts 1 and 2.

Command:

n = [0:50]; x = 0.8.^n; h = (-0.9).^n;

y1 = ((0.8).^(n+1) - (-0.9).^(n+1))/(0.8+0.9);

subplot(1,3,1); stem(n,y1)

x2 = x(1:26); h2 = h(1:26); y2 = conv(h2,x2);

subplot(1,3,2);

stem(n,y2)

y3 = filter([1],[1,0.9],x);

subplot(1,3,3);

stem(n,y3)
P2.18 Matlab function conv_tp:

Editor:

function [y,H]=conv_tp(h,x)

Nx = length(x); Nh = length(h);

hc = [h,zeros((Nx-1),1)]; hr = [h(1),zeros(1,Nx-1)];

H = toeplitz(hc,hr); y = H*x;

You might also like