Ecte301 Laboratory Report

Embed Size (px)

Citation preview

  • 8/2/2019 Ecte301 Laboratory Report

    1/17

    SECTE Laboratory Report 1-3

    1

    (i) Answer:

    (ii) Matlab Code:

    Ts = 0.01;t = 0:Ts:0.25;xn = 5*cos(120*pi*t + (pi/6));stem(t, xn, 'filled'),title('Signal XN')

    From the plot is can be seen that the

    period N is 5.

    (ii) Matlab Plot:

    (iii) Answer:

    Aliasing has occurred. Nyquist theory states we need at least 120 Hz to avoid aliasing (twice the value of

    fmax). The sampling frequency used to plot x(n) was only 100 Hz.

    0 0.05 0.1 0.15 0.2 0.25-5

    -4

    -3

    -2

    -1

    0

    1

    2

    3

    4

    5Signal XN

    ECTE301 - Digital Signal Processing: Lab Report 1

    Student Name Ben Armstrong

    Student ID 3110035 E-mail [email protected]

    Lab Report Mark Date

    Instructions:

    Answer the problems for the lab report 1-3.

    Problem 1.1:

    Consider the signal0

    ( ) s5cos(120 30 ), 0 t 0.25 seca

    x t t . Sample this signal uniformly using a

    sampling T = 10 msperiod to obtain a discrete time signal x(n).

    (i) What is the corresponding sampling rate?

    (ii) Plot the signal x(n) using Matlab command stem and deduce its period Nfrom the plot.

    (iii) Is there aliasing, and why?

  • 8/2/2019 Ecte301 Laboratory Report

    2/17

    SECTE Laboratory Report 1-3

    2

    Problem 1.2:

    Generate and plot the samples (use the stem(..., filled) function) of the following signals.

    (i) 1( ) 2 ( 2) 0.5 ( 1) ( ) 1.5 ( 1) ( 2), -5 5 x n n n n n n n

    (ii)2 ( ) 2 ( 2) 2 ( 3), -5 5 x n u n u n n

    (iii)4

    3( ) (0.75) ( 4), 0 15n

    x n u n n

    (iv)2

    4 ( ) (0.8) sin(0.3 ) ( 2), 0 15n x n n u n n

    To save space, please plot all figures on the same page, using plot(41x) or plot (22x), where x =

    1,...,4.

    (i)-(iv) Matlab Code:

    n = -5:5xn1 = 2*(n==-2) + 0.5*(n==-1) - (n==0) + 1.5*(n==1) + (n==2)subplot(411), stem(n, xn1, 'filled'), title('Signal XN1')n = -5:5xn2 = 2*(n >= -2) - 2*(n > 3)subplot(412), stem(n, xn2, 'filled'), title('Signal XN2')n = 0:15xn3 = (0.75).^(n-4).*(n >= 4)subplot(413), stem(n, xn3, 'filled'), title('Signal XN3')n = 0:15xn4 = (0.8).^(n-2).*sin(0.3*pi*n).*(n >= 2)subplot(414), stem(n, xn4, 'filled'), title('Signal XN4')

    (i)-(iv) Matlab Plot:

    -5 -4 -3 -2 -1 0 1 2 3 4 5-2

    0

    2Signal XN1

    -5 -4 -3 -2 -1 0 1 2 3 4 50

    1

    2Signal XN2

    0 5 10 150

    0.5

    1Signal XN3

    0 5 10 15-1

    0

    1Signal XN4

  • 8/2/2019 Ecte301 Laboratory Report

    3/17

    SECTE Laboratory Report 1-3

    3

    Problem 1.3:

    Consider the following single tone signal

    ( ) sin(200 ) ( ), 0 0.2sec. x t t u t t (i) Sample this signal at the following rates: 2000 Hz, 125 Hz, and 105 Hz. Name the signals x1,

    x2, and x3, respectively.

    (ii) Using subplot(311), plot and compare the three signals. The first graph should contain all

    three signals, the second graph contains the signal x2 and its samples, and the third graph

    contains the signal x3 and its samples.

    (iii) Find the normalized frequency, f(samples/cycle), and the period, N(in samples), of these

    discrete-time signals.

    (iv) If three analog signals are to be reconstructed from the three sampled signals, using the

    respective sampling rate used to obtain the sampled signal, find the frequencies of the

    resulting analog signals.

    (i) Matlab Code:

    Fs1 = 2000;Fs2 = 125;Fs3 = 105;

    t1 = 0:(1/Fs1):0.2t2 = 0:(1/Fs2):0.2t3 = 0:(1/Fs3):0.2

    xn1 = sin(200*pi*t1).*(t1 >= 0)xn2 = sin(200*pi*t2).*(t2 >= 0)xn3 = sin(200*pi*t3).*(t3 >= 0)

    subplot(311), title('Plot of Xn1(Blue), Xn2(red) and Xn3(Magenta)')hold onplot(t1, xn1, 'b')

    plot(t2, xn2, 'r')plot(t3, xn3, 'm')

    subplot(312), title('Stem Plot of Xn2')hold onstem(t2, xn2, 'filled'), plot(t2, xn2)

    subplot(313), title('Stem Plot of Xn3')hold onstem(t3, xn3, 'filled'), plot(t3, xn3)

    hold off

  • 8/2/2019 Ecte301 Laboratory Report

    4/17

    SECTE Laboratory Report 1-3

    4

    (ii) Matlab Plot:

    (iii) Matlab Code:

    F1 = (1/(2*pi))*((200*pi)/Fs1)F2 = (1/(2*pi))*((200*pi)/Fs2)F3 = (1/(2*pi))*((200*pi)/Fs3)

    N can be obtained from the Matlabplot.

    (iii) Answer:

    X1 X2 X3

    F

    (samples/cycles)

    0.05 0.8 0.9524

    N (samples) 20 5 22

    (iv) Matlab Code:

    [Y, I1] = max(xn1);[Y, I2] = min(xn1);half_period = abs(t1(I1) - t1(I2));

    f1 = 1 / (2 * half_period)

    [Y, I1] = max(xn2);[Y, I2] = min(xn2);half_period = abs(t2(I1) - t2(I2));f2 = 1 / ((2 * half_period)+t2(2))

    [Y, I1] = max(xn3);[Y, I2] = min(xn3);half_period = abs(t3(I1) - t3(I2));f3 = 1 / (2 * half_period)

    (iv) Answer:

    f1 =

    100.0000

    f2 =

    25.0000

    f3 =

    4.7727

    0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2-1

    0

    1Plot of Xn1(Blue), Xn2(red) and Xn3(Magenta)

    0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2-1

    0

    1Stem Plot of Xn2

    0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2-1

    0

    1Stem Plot of Xn3

  • 8/2/2019 Ecte301 Laboratory Report

    5/17

    SECTE Laboratory Report 1-3

    5

    (i) ) Matlab Code:

    n1 = 0:10x = (0.7).^n1n2 = 0:10h = (n2 >= 3) - (n2 >= 8)y = conv(x,h)n = length(x) + length(h) -1; n3 = 0:n-1;

    (ii) Matlab Code:

    subplot(311)stem(n1, x,'filled')title('Plot of x(n)')ylabel('x(n)')

    subplot(312)stem(n2, h,'filled')title('Plot of h(n)')ylabel('h(n)')

    subplot(313)stem(n3, y, 'filled')title('y(n) = x(n) * h(n)')xlabel('n'), ylabel('y(n)')

    (ii) Matlab Plot:

    (iii) Answer:

    0 1 2 3 4 5 6 7 8 9 100

    0.5

    1Plot of x(n)

    x(n)

    0 1 2 3 4 5 6 7 8 9 100

    0.5

    1Plot of h(n)

    h(n)

    0 2 4 6 8 10 12 14 16 18 200

    2

    4y(n) = x(n) * h(n)

    n

    y(n)

    Problem 2.1:

    Perform the convolution of the following signals:

    1; 3 8(0.7) ; 0 10( ) and ( )0; otherwise0; otherwise

    n

    nnh n x n

    (i) Compute the sequence ( ) ( ) ( )y n x n h n .

    (ii) Plot the three sequences, x(n) , h(n), y(n) and in three separate graph windows using subplot

    and stem.

    (iii) What is the length of the sequence y(n)? What is the range of over which ( ) 0y n ?

  • 8/2/2019 Ecte301 Laboratory Report

    6/17

    SECTE Laboratory Report 1-3

    6

    Problem 2.2:

    The image below is blurred with a 1-D FIR filter in the horizontal direction. The blurring filter is

    h(n) = [0.0392 0.0529 0.0634 0.0714 0.0771 0.0809 0.0832 0.0843 0.0843 0.08340.0683 0.0553 0.0442 0.0348 0.0269 0.0202 0.0145 0.0098 0.0059];

    Blurred Image.

    Restore the original image using deconvolution. What is the message hidden in the image?

    First, you need to load the file prac2_prb2_file.mat using the command load prac2_prb2_file. This

    file contains the blurred image, y, and the impulse response of the FIR filter, h.

    Matlab Code:

    clear all% clears all the variables from the workspace.clf % clears the figure.load prac2_prb_file% loads an image stored in a file called prac2_prb_file.who % lists the current variable (i.e., image).subplot(211), imagesc(y), colormap graytitle('Blurred Image, Y')

    [N, M] = size(y);Xr = [ ]; % create an empty matrix in for the restored image.for i = 1:NXr = [Xr; deconv(y(i,:), h)]; % Restore the image one row at a time.end

    subplot(212), imagesc(Xr), colormap graytitle('Restored Image, Xr')

  • 8/2/2019 Ecte301 Laboratory Report

    7/17

    SECTE Laboratory Report 1-3

    7

    Answer:

    Blurred Image, Y

    20 40 60 80 100 120 140 160 180

    5

    10

    15

    20

    Restored Image, Xr

    20 40 60 80 100 120 140 160 180

    5

    10

    15

    20

  • 8/2/2019 Ecte301 Laboratory Report

    8/17

    SECTE Laboratory Report 1-3

    8

    Problem 2.3:

    Consider the discrete-time causal system

    ( ) 0.5 ( 1) 0.5 ( 2) 0.25 ( 3) ( )y n y n y n y n x n (i) Compute and plot the impulse and step responses, for 0 50n .

    (ii) What is the steady-state value of the step response? Solve for this by hand, then check

    your answer using the graph and the last value of y(n), i.e., y(length(y)). Do all your answers

    agree, why?

    (iii) Compute the response of this system to the sinusoidal input ( ) 20sin(0.4 ) x n n . What is

    the frequency and amplitude of the steady-state response, i.e., the frequency and amplitude

    of as n .

    (iv) Find the poles and zero of the system, plot the pole-zero diagram, and determine thestability of the system.

    (i) Matlab Code:

    n = 0:50A = [1 0.5 -0.5 -0.25]B = [1]

    d = n==0h = filter(B, A, d) % Impulse responsesubplot(211), stem(n, h, 'filled'), title('Impulse Response')xlabel('n'),ylabel('h(n)')

    u = n>=0y = filter(B, A, u) % Step responsesubplot(212), stem(n, y, 'filled'), title('Stem Response')xlabel('n'),ylabel('y(n)')

  • 8/2/2019 Ecte301 Laboratory Report

    9/17

    SECTE Laboratory Report 1-3

    9

    (i) Matlab Plot:

    (ii) Answer:

    These results agree because by y(51) the system has already reached stability.

    (iii) Matlab Code:

    clf

    x = 20*sin(0.4*pi*n)y = filter(B, A, x) % Step responsestem(n, y, 'filled')title('Response to x(n) = 20sin(0.4*pi*n)')xlabel('n'),ylabel('y(n)')

    (iii) Answer:

    0 5 10 15 20 25 30 35 40 45 50-0.5

    0

    0.5

    1Impulse Response

    n

    h(n)

    0 5 10 15 20 25 30 35 40 45 500

    0.5

    1

    1.5Stem Response

    n

    y(n)

  • 8/2/2019 Ecte301 Laboratory Report

    10/17

    SECTE Laboratory Report 1-3

    10

    (iv) Matlab Code:

    p = roots(A);r = abs(p); max(r)zplane(B,A)

    (iv) Answer:

    (v) Answer:

    All the poles lie within the unit circle, therefore the system is stable.

    -1 -0.5 0 0.5 1

    -1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    3

    Real Part

    ImaginaryPart

  • 8/2/2019 Ecte301 Laboratory Report

    11/17

    SECTE Laboratory Report 1-3

    11

    (i) Matlab Code:

    s = [0 1 1 0 1 0 1 1]w = sqrt(0.36)*randn(1,length(s))xn = s + wstem(xn, 'filled')title('Received Signal')

    (i) Matlab Plot:

    1 2 3 4 5 6 7 8-1

    -0.5

    0

    0.5

    1

    1.5

    2Received Signal

    Problem 3.1:

    Consider the two template signals 1 sin(0.45 )s n and 2 cos(0.1 )s n for 0 19n . In a digital

    communication link using frequency shift keying (FSK) the signals1( )s n and 2 ( )s n represent the

    two binary numbers 1 and 0, respectively. Suppose the binary sequence s = {1, 1, 0, 1, 0, 1, 1, 0}is

    transmitted down a channel that is corrupted by zero-mean white Gaussian noise of variance 0.36.

    (i) Construct and plot the received signal ( ) ( ) ( )x n s n w n , where ( )s n is the signal

    representing the binary sequence sand ( )w n is the white noise sequence.

    (ii) Compute and plot the correlation sequences1 2 0 0 1 0 0 1

    ( ), ( ), ( ) and ( )s s s s s s s sr r r r .

    (iii) Compute and plot1 0( ) and ( )

    xs xsr r the crosscorrelation sequences of the received signal

    x(n) with the template signals 1 0s ( ) and s ( )n n .

    (iv) Determine a procedure for detecting and identifying the symbols.

    (v) Determine and plot the impulse responses 1 0h ( ) and h ( )n n of the matched filters

    corresponding to the signals1 0s ( ) and s ( )n n , respectively.

    (vi) Compute and illustrate1 0y ( ) and y ( )n n , the responses of the 1 0h ( ) and h ( )n n to the

    received signal x(n) . Compare these responses with the crosscorrelation sequences found

    in (ii). What can you conclude?

    (vii) A binary sequence was transmitted and the received signal is stored in the file fsk.mat.

    Identify the transmitted sequence of 0s and 1s.

  • 8/2/2019 Ecte301 Laboratory Report

    12/17

    SECTE Laboratory Report 1-3

    12

    (ii) Matlab Code:

    n = 0:19s1 = sin(0.45*pi*n)

    s0 = -cos(0.1*pi*n)

    [rs1, ls1] = xcorr(s1)[rs0, ls0] = xcorr(s0)[rs1s0, ls1s0] = xcorr(s1, s0)[rs0s1, ls0s1] = xcorr(s0, s1)

    subplot(411)stem(ls1, rs1, 'filled')title('Correlated sequencers1s1')subplot(412)

    stem(ls0, rs0, 'filled')title('Correlated sequencers0s0')subplot(413)stem(ls1s0, rs1s0, 'filled')title('Correlated sequencers1s0')subplot(414)stem(ls0s1, rs0s1, 'filled')title('Correlated sequencers0s1')

    (ii) Matlab Plot:

    (iii) Answer:

    -20 -15 -10 -5 0 5 10 15 20-10

    0

    10Correlated sequence rs1s1

    -20 -15 -10 -5 0 5 10 15 20-10

    0

    10Correlated sequence rs0s0

    -20 -15 -10 -5 0 5 10 15 20-2

    0

    2Correlated sequence rs1s0

    -20 -15 -10 -5 0 5 10 15 20-2

    02

    Correlated sequence rs0s1

    -20 -15 -10 -5 0 5 10 15 20-2

    -1

    0

    1

    2

    -20 -15 -10 -5 0 5 10 15 20-4

    -2

    0

    2

    4

  • 8/2/2019 Ecte301 Laboratory Report

    13/17

    SECTE Laboratory Report 1-3

    13

    (iv) Matlab Code:

    for i = 1:length(s)if xn(i) < 0.5

    symbols(i)= 0;

    elsesymbols(i) = 1;end

    end

    (iv) Matlab Plot:

    (v) Matlab Code:

    h1 = fliplr(s1)h0 = fliplr(s0)

    subplot(211)stem(n, h1, 'filled')subplot(212)stem(n, h0, 'filled')

    (v) Matlab Plot:

    1 2 3 4 5 6 7 8

    0

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

    0.8

    0.9

    1Received symbols from xn

    0 2 4 6 8 10 12 14 16 18 20

    -1

    -0.5

    0

    0.5

    1

    0 2 4 6 8 10 12 14 16 18 20-1

    -0.5

    0

    0.5

    1

  • 8/2/2019 Ecte301 Laboratory Report

    14/17

    SECTE Laboratory Report 1-3

    14

    (vi) Matlab Code:

    y1 = conv(xn, h1);y0 = conv(xn, h0);temp = length(xn) + length(h1) -

    1;nn = -20:temp-21;

    subplot(211)stem(nn, y1, 'filled')subplot(212)stem(nn, y0, 'filled')

    These responses are the same of that in

    part iii. Thus we can conclude that to

    perform correlation with convolution. The

    same computations will occur if h(n) is

    equal to the flipped version of s(n).

    (vi) Matlab Plot:

    (vii) Answer:

    Symbols =

    0 1 0 0 1 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 1 1 1 1 0 1 1 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0

    0 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 1 0

    0 0 0 1 0 0 0 0 0 0 0 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 1 1

    -20 -15 -10 -5 0 5 10-2

    -1

    0

    1

    2

    -20 -15 -10 -5 0 5 10-4

    -2

    0

    2

    4

  • 8/2/2019 Ecte301 Laboratory Report

    15/17

    SECTE Laboratory Report 1-3

    15

    (i) Matlab Code:

    n = 0:20;s = [1 -1 1 -1 -1 1];w = sqrt(0.25)*randn(1,

    length(n));x = [0 0 0 0 0 0 0 0 0 0 s 00 0 0 0];x = 0.8*x + w;stem(n, x, 'filled')title('Signal x(n)')

    The delay D cannot be identified from

    the plot.

    (i) Matlab Plot:

    0 2 4 6 8 10 12 14 16 18 20-1.5

    -1

    -0.5

    0

    0.5

    1

    1.5

    Signal x(n)

    Problem 3.2:

    Consider the template signal s(n) ={ 1 ,-1 , 1 ,-1 ,-1 , 1}. Let the signal x(n)be a delayed and noise

    corrupted version

    ( ) ( ) ( ),x n s n D w n

    where w(n)is a zero-mean white noise sequence and Dis the delay in number of samples.

    (i) Generate and plot the signal x(n) for 0 20n . (Assume that D= 10, = 0.8 and the white

    noise has variance equal to 0.25). Can you identify the delay Dfrom the plot of x(n)?

    (ii) Compute and illustrate the crosscorrelation ( )xsr and determine the time delay of x(n).

    (iii) Identify and plot the matched filter impulse response h(n).

    (iv) Compute and plot y(n)the response of the matched filter to x(n), compare y(n)to ( )xsr .

    (v) Can you determine the time delay of x(n)from the output of the matched filter y(n)?

  • 8/2/2019 Ecte301 Laboratory Report

    16/17

    SECTE Laboratory Report 1-3

    16

    (ii) Matlab Code:

    [rxs, lxs] = xcorr(x,s)stem(lxs, rxs,'filled')title('Crosscorrelation ofrxs')

    (ii) Matlab Plot:

    (iii) Answer:

    h = fliplr(s)t = 0:5stem(t, h,'filled')

    -20 -15 -10 -5 0 5 10 15 20-5

    -4

    -3

    -2

    -1

    0

    1

    2

    3

    4

    5Crosscorrelation of rxs

    0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1Plot of h(n)

  • 8/2/2019 Ecte301 Laboratory Report

    17/17

    SECTE Laboratory Report 1-3

    title('Plot of h(n)')

    (iv) Matlab Code:

    y = conv(x, h);temp = length(x) + length(h)-1; nn = -5:temp-6;stem(nn, y, 'filled')title('y(n) = x(n)*h(n)')xlabel('n'), ylabel('y(n)')

    (iv) Matlab Plot:

    (v) Answer:

    The time delay of x(n) can be determined from the above plot by looking at the peak value of the sequence.

    The peak value is located at t =10, thus the time delay is equal to 10. This is as expected.

    -5 0 5 10 15 20-5

    -4

    -3

    -2

    -1

    0

    1

    2

    3

    4

    5y(n) = x(n)*h(n)

    n

    y(n)