bao cao lab1 dsp thang 08dt3

Upload: tran-ngoc-lam

Post on 03-Jun-2018

256 views

Category:

Documents


7 download

TRANSCRIPT

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    1/15

    I HC NNG

    TRNG I HC BCH KHOA

    KHOA IN T- VIN THNG

    ---- ----

    BO CO LAB1:

    Introduction toMATLAB and

    Scripts

    Gio vin hng dn: Nguyn Hi Triu Anh

    Sinh vin thc hin: Phan Quc Thng

    Lp : 08DT3

    Nhm: 14B

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    2/15

    1. Ma trn: To ma trn x:

    Truy cp gi trca ma trn x:

    To ma trn z tma trn y:

    Cc php ton tma trn:

    >> x=[3 1 5; 6 4 1]

    x =

    3 1 5

    6 4 1

    >> y=[1 2 3]

    y =

    1 2 3

    >> z=[1 2 3]'

    z =

    1

    2

    3

    >> x(1,2)

    ans =

    1

    >> x=[2;4]

    x =

    2

    4

    >> y=[3;1]

    y =

    3

    1

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    3/15

    2. Plot and Subplot: title('Here is a title'); - Adds the text "Here is a title" to the top of the plot. xlabel('Control Voltage (mV)'); - Adds text to the X-axis. ylabel('Current (mA)'); - Adds text to the Y-axis. grid on; - Adds a grid to the plot.

    Vth theo 3 trc x, y, z

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    0.5

    1

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    0.5

    1

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    0.5

    1

    >> x*y'

    ans =

    6 2

    12 4

    >> x'*y

    ans =

    10

    >> x.*y

    ans =

    6

    4

    >> subplot(3,1,1);

    >> subplot(3,1,2);

    >> subplot(3,1,3);

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    4/15

    Create and plot a signal x0 (t) = te^(-t)

    Lnh matlab:

    ththu c:

    -10 -8 -6 -4 -2 0 2 4 6 8 10-0.4-0.3-0.2-0.1

    00.10.20.30.4

    >> t = -10:0.1:10;

    xo = t .* exp(-abs(t));

    plot(t, xo);

    grid;

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    5/15

    create the signals: xe (t) = !t!*e^(-!t!)x (t) = 0:5 _ [x0 (t) + xe (t)]

    Plot all signals together using 3 plots stacked on top of each other with the subplotcommand:Lnh matlab:

    thvc tmatlab:

    -10 -8 -6 -4 -2 0 2 4 6 8 10-0.5

    0

    0.5

    -10 -8 -6 -4 -2 0 2 4 6 8 100

    0.2

    0.4

    -10 -8 -6 -4 -2 0 2 4 6 8 100

    0.2

    0.4

    >> t = -10:0.1:10;

    xo = t .* exp(-abs(t));

    xe=abs(t).*exp(-abs(t));

    x = 0.5 * [xo + xe];

    subplot(3,1,1);

    plot(t,xo);

    subplot(3,1,2);

    plot(t,xe);

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    6/15

    3.Complex Numbers:Lnh matlab:

    MATLAB uses the letter i instead of j by default. Electrical Engineers prefer using jhowever,

    and MATLAB will recognize that as well. Try entering i+j, does this make sense.

    De_ne z1 = 1 + j . Find the magnitude, phase, real and imaginary parts of z (using abs(),angle(), real(), imag(), respectively).

    >> sqrt(-1)

    ans =

    0 + 1.0000i

    >> abs(z1)

    ans =

    1.4142

    >> real(z1)

    ans =

    1

    >> imag(z1)

    ans =

    1

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    7/15

    Find the magnitude of z1 + z2 where z2 = 2e^(j*pi/3)

    Compute the value of j^j:

    4.Complex Functions:Create a signal x1 (t) = te*(jt):

    Lnh matlab:

    >> z1=1+j

    z1 =

    1.0000 + 1.0000i

    >> z2=2*exp(j*pi/3)

    z2 =

    1.0000 + 1.7321i

    >> z=z1+z2

    z =

    2.0000 + 2.7321i

    >> j^j

    ans =

    0.2079

    >> t = -10:0.1:10;

    >> x1=t.*exp(j*t);

    >> plot(t, x1); grid;

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    8/15

    5.Playing and Plotting a Sound:Lnh matlab:

    -10 -8 -6 -4 -2 0 2 4 6 8 10

    -10

    -8

    -6

    -4

    -2

    0

    2

    4

    6

    8

    10

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

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    >> load handel;

    plot(linspace(0,9,7311

    3),y);

    sound(y);

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    9/15

    6. Script Files:a. Exercise 1:

    Now we are going to edit the dampedCosine.m file to create our own script file. At

    the top of the file, you will see the following commands

    diary 'your_name_Lab1.txt'

    disp('NAME: your name')

    disp('SECTION:your section')

    1.Edit the dampedCosine.m (download from link above) script and enter your name and

    section where indicated. Save this new version of the script as yourName_dampedCosine.m

    2. Edit the script to create a second signal where the cosine with twice the period

    (which gives half the frequency) of the first.

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    10/15

    3. Add to the script the commands to plot these together with the first signal on top and the

    second on the bottom. In other words, you should have a single figure with two different plots,

    one on top and one on bottom. You will need to use subplot and plot. Save this plot as

    yourName_dampedCosine.fig

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    11/15

    .

    4. Show the TA your dampedCosine plot. What is the period of the cosine?

    - y1(x) signal have the period of the cosine is :T1 =

    =1 (s)

    - y2(x) signal have the period of the cosine isT2=

    =2 (s)

    b.Exercise 2:

    Download and run compexp.m2 , which includes a 3-D plot of a complex exponential, y

    (t) , as well as 2-D magnitude/phase and real/imaginary plots. You need 2 2-D plots to have the

    same information as the 3-D plot

    ddng kho st sthay i ca tn hiu,ta sdng thm 2 lnh:

    a=input('push a:');

    f=input('push f:');

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

    -0.5

    0

    0.5

    1

    time

    amplitude

    f(x)=exp(-abs(x))cos(2*pi*x)

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

    0

    0.5

    1

    time

    amplitude

    f(x)=exp(-abs(x))cos(pi*x)

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    12/15

    vi f v a l cc gi trnhp tbn phm

    Ta c hm nh sau:

    Khi chy chng trnh,trn ca scommand sxut hin:

    push a:

    push f:

    ta sln lt nhp cc gi trca f v a vo.

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    13/15

    1/ How would you change the script to make the oscillation frequency lower by half?

    tn sgim i mt na,ta ginguyn gi trca a=1,v ln lt thay i gi trca f l f=1v f=0.5.Ta c hnh vnh sau:

    Khi a=1,f=1 Khi a=1,f=0.5

    2/ How would you change the script to make the decay faster?

    gim bin ca tn hiu,ta ginguyn gi trf=1,v ln lt thay i gi trca a l a=1 va=2.Ta c hnh vnh sau

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    14/15

    Khi a=1,f=1 Khi a=2,f=1

    Tn hiu y(t) c phn tch nh sau:

    y(t)=()

    =3 *

    =3 *(cos(-2 jsin(-2)

    =3 *(cos() +jsin( ))

    Chnh v vy,khi ta thay i a th slm cho bin ca tn hiu thay i,v khi thay i f chnhl thay i tn sca tn hiu(hay thay i chu k ca tn hiu)

  • 8/13/2019 Bao Cao Lab1 DSP Thang 08DT3

    15/15