Matlab (2 1-2 8) Final

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

COSAIN, Saimah S.

ECE104 LAB

2.5 PROBLEMS

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.
MATLAB CODE:
>> disp('1. x(n) = 3δ(n + 2) + 2δ(n) − δ(n − 3) + 5δ(n − 7), −5 ≤ n ≤ 15.');
1. x(n) = 3δ(n + 2) + 2δ(n) − δ(n − 3) + 5δ(n − 7), −5 ≤ n ≤ 15.
>> n1 = [-5:15];
>> x1 = 3*impseq(-2,-5,15);
>> n2 = [-5:15];
>> x2 = 2*impseq(0,-5,15);
>> x21 = impseq(3,-5,15);
>> x22 = 5*impseq(7,-5,15);
>> [x11,n11] = sigadd(x1,n1,x2,n2);
>> [x12,n12] = sigadd(-x21,n1,x22,n2);
>> [x,n] = sigadd(x11,n11,x12,n12);
>> stem(n,x,'r');
>> xlabel('n');
>> ylabel('x(n)');
>> title('Stem Plot Of x(n)_1');
>> axis([-5 15 -2 6]);
SCREENSHOT:
2. x2(n) = ∑5𝑘=−5 e-|k|δ(n − 2k), −10 ≤ n ≤ 10.
MATLAB CODE:
>> n = [-10:10];
>> N = length(n);
>> x = zeros(1,N);
>> for k = -5:5
x = x + exp(-abs(k))*impseq(2*k ,-10,10);
end
>> stem(n,x,'b','filled','markersize',4);
>> axis([-10 10 -1 2]);
>> xlabel('n'); ylabel('x_2(n)');
>> title('Stem Plot Of x_2(n)');
SCREENSHOT:

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


MATLAB CODE:
>> disp('3. x3(n) = 10u(n) − 5u(n − 5) − 10u(n − 10) + 5u(n − 15).');
3. x3(n) = 10u(n) − 5u(n − 5) − 10u(n − 10) + 5u(n − 15).
>> n = [-10:15];
>> n1 = [-10:15];
>> x1 = 10*stepseq(0,-10,15);
>> x2 = 5*stepseq(5,-10,15);
>> x21 = 10*stepseq(10,-10,15);
>> x22 = 5*stepseq(15,-10,15);
>> [x11,n11] = sigadd(x1,n1,-x2,n1);
>> [x12,n12] = sigadd(-x21,n1,x22,n1);
>> [x,n] = sigadd(x11,n11,x12,n12);
>> stem(n,x,'b','filled','markersize',4);
>> axis([-10 15 -11 11]);
>> xlabel('n'); ylabel('x_3(n)');
>> title('Stem Plot Of x_3(n)');
SCREENSHOT:

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


>> disp('4. x4(n) = e0.1n[u(n + 20) − u(n − 10)].');
4. x4(n) = e0.1n[u(n + 20) − u(n − 10)].
>> n1 = [-25:25];
>> x1 = exp(0.1*n1);
>> x2 = stepseq(-20,-25,25);
>> x3 = stepseq(10,-25,25);
>> [x11,n11] = sigadd(x2,n1,-x3,n1);
>> [x,n] = sigmult(x11,n11,x1,n1);
>> stem(n,x,'b','filled','markersize',4);
>> axis([-30 30 0 3]);
>> xlabel('n'); ylabel('x_4(n)');
>> title('Stem Plot Of x_4(n)');
SCREENSHOT:
5. x5(n) = 5[cos(0.49πn) + cos(0.51πn)], −200 ≤ n ≤ 200. Comment on the waveform shape.
>> n = [-200:200];
>> x = 5*(cos(0.49*pi*n) + cos(0.49*pi*n));
>> stem(n,x,'r','filled','markersize',3);
>> axis([-250 250 -15 15]);
>> xlabel('n'); ylabel('x_5(n)');
>> title('Stem Plot for x_5(n)');

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


>> n = [-200:200];
>> x = 2*sin(0.01*pi*n).*cos(0.5*pi*n);
>> stem(n,x,’b’,’filled,’markersize’,2);
>>axis([-200 200 -15 15]);
>> xlabel(‘n’); ylabel(‘x_6(n)’);
>> title(‘Plot Sequence of x_6(n)’);
7. x7(n) = e−0.05n sin(0.1πn + π/3), 0 ≤ n ≤ 100. Comment on the waveform shape.
>> n = [0:100];
>> x = exp(-0.05*n).*sin(0.1*pi*n+pi/3);
>> stem(n,x,’b’,’filled’,’markersize’,2);
>> axis([0 120 -1.5 1.5]);
>> xlabel(‘n’); ylabel(‘x_7(n)’);
>> title(‘Plot Sequence of x_7(n)’);

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


>> n= [0:100];
>> x1 = exp(0.01*n);
>> x2 = sin(0.1*pi*n);
>> [x,n]=sigmult(x1,n,x2,n);
>> stem(n,x,'r','filled','markersize',3);
>> axis([-0.5 105 -3 3]);
>> xlabel('n'); ylabel('x_8(n)');
>> title('Stem plot of x_8(n)');
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.

2. x2(n) is a Gaussian random sequence whose samples are independent with mean 10 and variance 10. Generate
10,000 samples.
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.

4
4. x4(n) =∑𝑘=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.

P2.3 Generate the following periodic sequences and plot their samples (using the stem function) over the indicated number
of periods.
1. ˜x1(n) = {..., −2, −1, 0, 1, 2, ...} periodic. Plot five period.
>> x = [-2,-1,0,1,2];
>> xtilde = x'*ones(1,5);
>> xtilde = (xtilde(:))';
>> n = [1:25];
>> stem(n,xtilde,'b','filled','markersize',4);
>> axis([0 26 -3 3]);
>> xlabel('n'); ylabel('x_1(n)');
>> title('Periodic Sequenc of x_1(n)');
SCREENSHOT:
2. x2(n) = e0.1n[u(n) − u(n − 20)]periodic. Plot three periods.
>> n1 = [0:21];
>> x1 = exp(0.1*n1);
>> x2 = stepseq(0,0,21);
>> x3 = stepseq(20,0,21);
>> [x11,n11] = sigadd(x2,n1,-x3,n1);
>> [x,n] = sigmult(x11,n11,x1,n1);
>> subplot(2,1,1);
>> stem(n,x,'b','filled','markersize',4);
>> axis([0 25 0 8]);
>> xlabel('n'); ylabel('x_2(n)'); title('Stem Plot Of x_2(n)');
>> x = x'*ones(1,3);
>> x = (x(:))';
>> n2 = [-22:43];
>> subplot(2,1,2);
>> stem(n2,x,'b','filled','markersize',4);
>> axis([-30 50 0 8]);
>> xlabel('n'); ylabel('x_2(n)'); title('Periodic Sequence Of x_2(n)');
SCREENSHOT:
3. ˜x3(n) = sin(0.1πn)[u(n) − u(n − 10)]. Plot four periods.
>> n1 = [0:10];
>> x1 = sin(0.1*pi*n1);
>> x2 = stepseq(0,0,10);
>> x3 = stepseq(10,0,10);
>> [x11,n11] = sigadd(x2,n1,-x3,n1);
>> [x,n] = sigmult(x11,n11,x1,n1);
>> subplot(2,1,1);
>> stem(n,x,'b','filled','markersize',4);
>> xlabel('n'); ylabel('x_3(n)'); title('Stem Plot Of x_3(n)');
>> axis([-1 11 0 1.2]);
>> x = x'*ones(1,4);
>> x = (x(:))';
>> n2 = [-11:32];
>> subplot(2,1,2);
>> stem(n2,x,'b','filled','markersize',4);
>> axis([-15 35 0 1.2]);
>> xlabel('n'); ylabel('x_3(n)'); title('Periodic Sequence of x_3(n)');
SCREENSHOT:
4. ˜x4(n) = {..., 1, 2, 3,...}periodic + {..., 1, 2, 3, 4,...}periodic, 0 ≤ n ≤ 24. What is the period of ˜x4(n)?
↑ ↑
>> n = [0:24];
>> x1 = [1 2 3];
>> x2 = [1 2 3 4];
>> x1 = x1'*ones(1,10); x2 = x2'*ones(1,10);
>> x1 = (x1(:))'; x2 = (x2(:))';
>> x11 = x1(1:25); x12 = x2(1:25);
>> x = x11 + x12;
>> stem(n,x,'r','filled','markersize',4);
>> axis([-1 25 0 8]);
>> xlabel('n'); ylabel('x_4(n)');
>> title('Periodic sequence of x_4(n)');
P2.4 Let x(n) = {2, 4, −3, 1, −5, 4, 7}. Generate and plot the samples (use the stem function) of the following sequences.

1. x1(n)=2x(n − 3) + 3x(n + 4) − x(n)
>> n = [-3:3];
>> x = [2,4,-3,1,-5,4,7];
>> [x1,n1] = sigshift(x,n,3);
>> [x2,n2] = sigshift(x,n,-4);
>> [x3,n3] = sigadd(2*x1,n1,3*x2,n2);
>> stem(n3,x3,'r','filled','markersize',4);
>> axis([-8 8 -16 25]);
>> xlabel('n'); ylabel('x_1(n)');
>> title('Periodic Sequence of x_1(n)');

2. x2(n)=4x(4 + n)+5x(n + 5) + 2x(n)


>> n = [-3:3];
>> x = [2,4,-3,1,-5,4,7];
>> [x1,n1] = sigshift(x,n,-4);
>> [x2,n2] = sigshift(x,n,-5);
>> [x3,n3] = sigadd(4*x1,n1,5*x2,n2);
>> [x4,n4] = sigadd(x3,n3,2*x,n);
>> stem(n4,x4,'r','filled','markersize',4);
>> axis([-10 10 -35 65]);
>> xlabel('n'); ylabel('x_2(n)');
>> title(‘Plot Sequence of x_2(n)’);
3. x3(n) = x(n + 3)x(n − 2) + x(1 − n)x(n + 1)
>> n = [-3:3];
>> x = [2,4,-3,1,-5,4,7];
>> [x1,n1] = sigshift(x,n,-3);
>> [x2,n2] = sigshift(x,n,2);
>> [x3,n3] = sigmult(x1,n1,x2,n2);
>> [x4,n4] = sigfold(x,n);
>> [x4,n4] = sigshift(x4,n4,1);
>> [x5,n5] = sigshift(x,n,-1);
>> [x6,n6] = sigmult(x4,n4,x5,n5);
>> [x,n] = sigadd(x3,n3,x6,n6);
>> stem(n,x,'r','filled','markersize',4);
>> axis([-8 8 -30 60]);
>> xlabel('n'); ylabel('x_3(n)');
>> title('Plot Sequence of x_3(n)');
4. x4(n)=2e0.5nx(n) + cos (0.1πn) x (n + 2), −10 ≤ n ≤ 10
>> n = [-3:3];
>> x = [2,4,-3,1,-5,4,7];
>> n1 = [-10:10];
>> x1 = 2*exp(0.5*n1);
>> x12 = cos(0.1*pi*n1);
>> [x11,n11] = sigmult(x1,n1,x,n);
>> [x12,n12] = sigshift(x,n,-2);
>> [x13,n13] = sigmult(x12,n11,x12,n12);
>> [x14,n14] = sigadd(x11,n11,x13,n13);
>> stem(n14,x14,'r','filled','markersize',4);
>> axis([-11 11 -20 70]);
>> xlabel('n'); ylabel('x_4(n)')
>> title('Plot sequence of x_4(n)');

P2.5 The complex exponential sequence ejω0n or the sinusoidal sequence cos (ω0n) are periodic if the normalized
𝜔 K
frequency f0 = 2𝜋
is a rational number; that is, f0 = N , where K and N are integers.
1. Prove the above result.
2. Generate exp(j0.1πn), −100 ≤ n ≤ 100. Plot its real and imaginary parts using the stem function. Is this sequence periodic?
Yes, the plot sequence is periodic. If it is, what is its fundamental period? From the examination of the plot, what
interpretation can you give to the integers K and N above?
3. Generate and plot cos(0.1n), −20 ≤ n ≤ 20. Is this sequence periodic? What do you
conclude from the plot? If necessary, examine the values of the sequence in MATLAB to
arrive at your answer.

P2.6 Using the evenodd function, decompose the following sequences into their even and odd
components. Plot these components using the stem function.
1. x1(n) = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

>> x = [0:9];
>> n = [0:9];
>> [xe,xo,m] = evenodd(x,n);
>> subplot(211);
>> stem(m,xe,'r','filled','markersize',4);
>> axis([-10 10 0 5]);
>> xlabel(n); ylabel('x_even(n)');
>> title('Plot Sequence for Even Part of x_1(n)');
>> xlabel('n'); ylabel('x_e_v_e_n(n)');
>> subplot(212);
>> stem(m,xo,'r','filled','markersize',4);
>> axis([-10 10 -5 5]);
>> xlabel('n'); ylabel('x_o_d_d(n)');
>> title('Plot Sequence for Odd Part of x_1(n)');

2. x2(n) = e0.1n[u(n + 5) − u(n − 10)]

>> n = [-10:15];
>> x1 = exp(0.1*n);
>> x2 = stepseq(-5,-10,15);
>> x3 = stepseq(10,-10,15);
>> [x11,n11]=sigadd(x2,n,-x3,n);
>> [x,n]=sigmult(x11,n11,x1,n);
>> [xe,xo,m] = evenodd(x,n);
>> subplot(211);
>> stem(m,xe,'r','filled','markersize',4);
>> xlabel('n');ylabel('x_e_v_e_n(n)');
>> title('Plot Sequence for Even Part of x_2(n)');
>> axis([-18 18 0 2]);
>> subplot(212);
>> stem(m,xo,'r','filled','markersize',4);
>> xlabel('n');ylabel('x_O_d_d(n)');
>> title('Plot Sequence for Odd Part of x_2(n)');

3. x3(n) = cos (0.2πn + π/4), −20 ≤ n ≤ 20

>> n = [-20:20];
>> x = cos(0.2*pi*n + pi/4);
>> [xe,xo,m] = evenodd(x,n);
>> subplot(211);
>> stem(m,xe,'b','filled','markersize',4);
>> axis([-25 25 -1 1]);
>> xlabel('n'); ylabel('x_E_v_e_n(n)');
>> title('Plot Sequence for Even Part of x_3(n)');
>> subplot(212);
>> stem(m,xo,'b','filled','markersize',4);
>> axis([-25 25 -1 1]);
>> xlabel('n'); ylabel('x_O_d_d(n)');
>> title('Plot Sequence for Odd Part of x_3(n)');
4. x4(n) = e−0.05n sin (0.1πn + π/3), 0 ≤ n ≤ 100

>> n = [0:100];
>> x1 = exp(-0.05*n);
>> x2 = sin(0.1*pi*n + pi/3);
>> [x,n]=sigmult(x1,n,x2,n);
>> [xe,xo,m] = evenodd(x,n);
>> subplot(211);
>> stem(m,xe,'b','filled','markersize',3);
>> axis([-100 100 -0.5 1]);
>> xlabel('n'); ylabel('x_E_v_e_n(n)');
>> title('Plot Sequence For Even Part of x_4(n)');
>> subplot(212)
>> stem(m,xo,'b','filled','markersize',3);
>> axis([-100 100 -0.7 0.7]);
>> xlabel('n'); ylabel('x_O_d_d(n)');
>> title('Plot Sequence For Odd Part of x_4(n)');
P2.7 A complex-valued sequence xe(n) is called conjugate-symmetric if xe(n) = 𝑥𝑒∗ (−n), and acomplex-valued sequence
xo(n) is called conjugate-antisymmetric if xo(n) = −𝑥𝑜∗ (−n). Then any arbitrary complex-valued sequence x(n) can be
decomposed into x(n) = xe(n) + xo(n), where xe(n) and xo(n) are given by
1 1
xe(n) = 2 [x(n) + x∗(−n)] and xo(n) = 2
[x(n) − x∗(−n)] (2.27)

respectively.
1. Modify the even odd function discussed in the text so that it accepts an arbitrary sequence and decomposes it into its
conjugate-symmetric and conjugate-antisymmetric components by implementing (2.27).
2. Decompose the sequence,
x(n) = 10 exp ([−0.1 + j0.2π]n), 0 ≤ n ≤ 10
into its conjugate-symmetric and conjugate-antisymmetric components. Plot their real and imaginary parts to verify the
decomposition. (Use the subplot function.)
P2.8 The operation of signal dilation (or decimation or down-sampling) is defined by
y(n) = x(nM)

in which the sequence x(n) is down-sampled by an integer factor M. For example, if

x(n) = {..., −2, 4, 3 ↑ , −6, 5, −1, 8,...}

then the down-sampled sequences by a factor 2 are given by

y(n) = {..., −2, 3 ↑ , 5, 8,...}

1. Develop a MATLAB function dnsample that has the form


function [y,m] = dnsample(x,n,M)

% Downsample sequence x(n) by a factor M to obtain y(m)


to implement the above operation. Use the indexing mechanism of MATLAB with careful attention to the origin of
the time axis n = 0.

2. Generate x(n) = sin(0.125πn), − 50 ≤ n ≤ 50. Decimate x(n) by a factor of 4 to generate y(n). Plot both x(n)
and y(n) using subplot, and comment on the results.

3. Repeat the above using x(n) = sin(0.5πn), − 50 ≤ n ≤ 50. Qualitatively discuss the effect of down-sampling on
signals.

You might also like