LAB 3 Discrete Time Signals Part One

Preview:

Citation preview

LOGO

LAB 3

Discrete Time Signals

Part One

iugaza2010.blogspot.commelasmer@gmail.com

2

3

1. Unit sample sequence

Write a Matlab function to generate

x(n)=delta(n-no), n1<=n<=n2

4

Note

5

function [x,n]=impseq(no,n1,n2)

n=[n1:n2];

x=[(n-no) == 0];

end

Write a Matlab function to generate

x(n)=delta(n-no), n1<=n<=n2

[x,n]=impseq(5,2,6)

Results:

n =2 3 4 5 6

x = 0 0 0 1 02 3 4 5 6

0

0.2

0.4

0.6

0.8

1

6

2. Unit step sequence

Write a Matlab function to generate

x(n)=step(n-no), n1<=n<=n2

7

8

function [x,n]=stepseq(no,n1,n2)

n=[n1:n2];

x=[(n-no) >= 0];

end

Write a Matlab function to generate

x(n)=step(n-no), n1<=n<=n2

[x,n]=stepseq(3,2,6)

Results:

n =2 3 4 5 6

x = 0 1 1 1 12 2.5 3 3.5 4 4.5 5 5.5 6

0

0.2

0.4

0.6

0.8

1

9

3. Real-valued exponential sequence

10

n=[-10:10];

x1=(0.7).^n;

subplot(221),stem(n,x1),title('0<a<1')

x2=(1.4).^n;

subplot(222),stem(n,x2),title('a>1')

x3=(-0.7).^n;

subplot(223),stem(n,x3),title('-1<a<0')

x4=(-1.4).^n;

subplot(224),stem(n,x4),title('-1>a')

11

-10 -5 0 5 100

10

20

30

400<a<1

-10 -5 0 5 100

10

20

30a>1

-10 -5 0 5 10-40

-20

0

20

40

-1<a<0

-10 -5 0 5 10-40

-20

0

20

40

-1>a

12

4. Complex-valued exponential sequence

cos sinje j

cos sin 1je j

Euler's equation:

13

-5 0 5-5

0

5

Quadra

ture

In-Phase

Scatter plot

>> scatterplot(3+j*5)

14

1n nj n je e

n=-10:10;

y=exp((j*pi)*n);

stem(n,y)

scatterplot(y)

-10 -5 0 5 10-1

-0.5

0

0.5

1

-1 0 1-1

0

1

Quadra

ture

In-Phase

Scatter plot

stem(n,y)scatterplot(y)

15

16

What is the mathematical definition of a complex sinusoid?

The complex sinusoid can be thought of as a two-dimensional sinusoid. One dimension

contains a cosine wave, and the perpendicular dimension contains a sinewave. Here is

a picture of a complex sinusoid with the third dimension being time:

17-1 0 1

-1

-0.5

0

0.5

1

Quadra

ture

In-Phase

Scatter plot

n=-10:10;

y=exp((j*pi/3)*n);

scatterplot(y)

/3n

j n je e

-1 -0.5 0 0.5 1-1

-0.5

0

0.5

1

18

-1 -0.5 0 0.5 1-1

-0.5

0

0.5

1

theta=linspace(0,2*pi,100);

y=exp(j*theta);

scatterplot(y)

>> help linspace

LINSPACE Linearly spaced vector.

LINSPACE(X1, X2)

generates a row vector of 100 linearly

equally spaced points between X1 and X2.

LINSPACE(X1, X2, N)

generates N points between X1 and X2.

19

>> L=linspace(0,10,5)

L =0 2.5000 5.0000 7.5000 10.0000

>> L=linspace(3,9,6)

L= 3.0000 4.2000 5.4000 6.6000 7.8000 9.0000

10 02.5

Number of elements 1 5 1

end value start valueStep

9 31.2

6 1Step

Start end points

>> L=linspace(0,10,5)

L =0 2.5000 5.0000 7.5000 10.0000

>> L=0:2.5:10

L =0 2.5000 5.0000 7.5000 10.0000

21

22

histogram

0 1 2 3 4 5 6 7 8 9 1011121314150

0.5

1

1.5

2

2.5

3

>> A=[15 15 6 8 6 6 1];

>> hist(A)

3

>> help randn

RANDN Normally distributed random numbers.

R = RANDN(N) returns an N-by-N matrix containingpseudo-random values drawn from a normal distributionwith mean zero and standard deviation one

>> randn(1,4)

ans =1.9574 0.5045 1.8645 -0.3398

23

randn Gaussian distribution

6. Random sequence (rand ,randn)

24

-3 -2 -1 0 1 2 3 40

10

20

30

40

50

0 20 40 60 80 100 120 140 160 180 200-4

-2

0

2

4

>> randn(1,200)

x=randn(1,200)

figure(1), plot(x)

figure(2), hist(x)

25

>> rand(1,5)

ans = 0.8147 0.9058 0.1270 0.9134

26

▪ OPERATIONS ON SEQUENCES

1. Signal addition:

Build a Matlab function that takes two sequences with its indices

and return the sum of them with the index of the result

sequence.

Before build this function follow the helpful notes next slides

27

For example find: x1+x2

28

%% means find x that is not equal to zero

29

30

>> A=[1 2 3;4 5 6;7 8 9]

A = 1 2 3

4 5 6

7 8 9

>> min(A)

ans =1 2 3

>> min(min(A))

ans = 1

31

>> a=[0 0 0 0 0 0 0 0 0 0 ];

>> b=[10 20 30 40 50];

>> y([6 7 8 9 10])=b

y =0 0 0 0 0 10 20 30 40 50

32

n1=[0,1,2,3,4];

n2=[-2,-1,0,1,2];

n=min( min(n1),min(n2)):max( max(n1),max(n2));

n = -2 -1 0 1 2 3 4>> A=n>=min(n1) % n>=0 {0,0,1,1,1,1,1}

>> B=n<=max(n1) % n<=4 {1,1,1,1,1,1,1}

>> A&B

>> z=find(A&B==1) % index

A = 0 0 1 1 1 1 1

B = 1 1 1 1 1 1 1

A&B = 0 0 1 1 1 1 1

z = 3 4 5 6 7

33

34

35

========================================================

% Function adds two discrete time sequences

y(n)=x1(n)+x2(n)

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

n=min(min(n1),min(n2)):max(max(n1),max(n2));

y1=zeros(1,length(n));

y2=y1;

y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;

y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;

y=y1+y2;

end

========================================================

36

using sidadd function x1=[0,2,4,6,8];

n1=[0,1,2,3,4];

x2=[1,3,5,7,9];

n2=[-2,-1,0,1,2];

[y,n]=sigadd(x1,n1,x2,n2)

y = 1 3 5 9 13 6 8

n =-2 -1 0 1 2 3 4

37

2. Signal multiplication :

38

3. Signal scaling:

In this operation each sample is multiplied by a scalar α.

39

4. Signal shifting:

x=[1,1,1,1];

n=[1,2,3,4];

n2=n+6;

n3=n-6;

stem(n,x),hold on

stem(n2,x),hold on

stem(n3,x)

For example:

40

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

0.5

1

1.5

2

41

=============================================

% Function does signal shifting y(n)=x(n-no)

function [y,n]=sigshift(x,n,no)

n=n+no;

y=x;

end

=============================================

% x=[1,1,1,1];

% n=[1,2,3,4];

%[y1,n2]=sigshift(x,n,6)

%[y2,n3]=sigshift(x,n,-6)

42

5. Signal Folding:

each sample of x(n) is flipped around n=0

43

x=[1,2,3];

n=[2,3,4];

y=fliplr(x);

n2=-fliplr(n);

% [y,n2]=sigfold(x,n)

stem(n,x),hold on,stem(n2,y)

For example:

44

-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 60

1

2

3

4

45

==========================================

% Function does signal folding y(n)=x(-n)

function [y,n]=sigfold(x,n)

y=fliplr(x);

n=-fliplr(n);

end

==========================================

46

6. Sample summation :

47

7. Sample product:

48

LOGO

49

Recommended