Matlab (2 1-2 8) Final
Matlab (2 1-2 8) Final
Matlab (2 1-2 8) Final
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:
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)');
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)');
>> 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)');
>> 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)
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.