dsp using matlab® - 4

Post on 10-Apr-2015

5.180 Views

Category:

Documents

6 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Lecture 4: Discrete Signal Analysis

Mr. Iskandar Yahya

Prof. Dr. Salina A. Samad

Sequence GenerationTypes of Sequences

Unit Sample Sequence Create s single impulse between a finite number of zeros. Having a function is useful, alternatively use “zeros”

function.

On the main window, type “impseq(0,-5,5)”

function [x,n] = impseq(n0,n1,n2)%generates x(n) = delta(n-n0); n1 <= n <= n2%--------------------------------------------%[x,n] = impseq(n0,n1,n2)%

n = [n1:n2]; x = [(n-n0) == 0]; %giving values of n and x

stem(n,x); %stem plot or impulse plotting functiontitle('Unit Sample Sequence') xlabel('n'); ylabel('x(n)');

Sequence GenerationUnit Sample Sequence

The resulting plot looks like this

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

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2Unit Sample Sequence

n

x(n)

Sequence GenerationUnit Step Sequence

We can have another elegant way to produce a step function

Alternatively, we can use the “ones” functionfunction [x,n] = stepseq(n0,n1,n2)%generates x(n) = u(n-n0); n1 <= n <= n2%--------------------------------------------%[x,n] = stepseq(n0,n1,n2)%

n = [n1:n2]; x = [(n-n0) >= 0];

stem(n,x);title('Unit Sample Sequence')xlabel('n'); ylabel('x(n)');

Sequence GenerationUnit Step Sequence

Type “stepseq(0,-5,5)” we get:

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

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2Unit Sample Sequence

n

x(n)

Sequence GenerationReal-valued exponential Sequence

The operator “.^” is required to implement a real exponential sequence.

For example, to generate x(n) = (0.9)n, 0<= n<=10, we will need the following script:

Example tutorial: Create a function (M-file) to generate an exponential

sequence. Use the previous examples as your guide.

>> n = [0:10]; x = (0.9).^n;

Sequence GenerationComplex-valued exponential Sequence

A matlab function “exp” is used to generate exponential sequences.

For example, to generate x(n) = exp[(2+j3)n], 0<= n<=10, we will need the following script:

Note that you need different plot commands for plotting real and imaginary components.

Sinusoidal sequence A matlab function “cos” (or sin) is used to generate

sinusoidal sequences. To generate x(n) = 3cos(0.1πn + π/3) + 2sin(0.5

πn), 0<= n<=10, we will need the following script:

>> n = [0:10]; x = exp((2+3j)*n);

>> n = [0:10]; x = 3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n);

Sequence GenerationRandom Sequences

A random or stochastic sequences are characterised by parameters of the associated probability density functions or their statistical moments.

In matlab, 2 types of (psuedo ) random sequences are avalable: “rand(1,N)” generates a length N random sequence

whos elements are uniformly distributed between 0 and 1.

“randn(1,N) generates a length N gaussian random sequence with mean 0 and variance 1.

Other random sequences can be generated suing transformation of the above functions. (Check previous lecture slides)

Sequence GenerationPeriodic sequence

A sequence is periodic if x(n) = x(n +N). To generate P periods of x(n) from one period, we can

copy x(n) P times:

An elegant approach is to use matlab’s indexing capabilites: Generate a matrix containing P rows of x(n) values. Concatenate P rows into a long row vector using the

construct (:). Have to use matrix transposition operator (‘) to

provide the same effect on rows:

>> xtilde = [x,x,x,x...,x];

>> xtilde = x' * ones(1,P); %P columns of x; x is a row vector>> xtilde = xtilde(:); %long coloumn vector>> xtilde = xtilde'; %long row vector

Sequence OperationSignal addition

To add two sequences x1(n) and x2(n) requires the operator “+”.

But the length of both sequences must be equal and have the same sample position

We have to first augment both x1(n) and x2(n) so that they have the same position vector and hence length.

This requires careful attention to matlab’s indexing operations.

Sequence OperationSignal addition

The following function, called “sigadd”, demonstrate this operation:

function [y,n] = sigadd(x1,n1,x2,n2)%generates y(n) = x1(n) + x2(n) %--------------------------------------------%[y,n] = sigadd(x1,n1,x2,n2)% y = sum sequence over n, which includes n1 and n2% x1 = first sequence over n1% x2 = second sequence over n2 (n2 can be different from n1)%

n = min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)y1 = zeros(1,length(n)); y2 = y1; % Initilizationy1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of yy2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of yy = y1+y2; % sequence addition

Sequence Operation

Signal Multiplication Sample-to-sample multiplication (or “dot”

multiplication) Implemented in matlab by the array operator “.*” Again, same restriction as using “+” We can create a function called “sigmult”:

Sequence Operation

function [y,n] = sigmult(x1,n1,x2,n2)%generates y(n) = x1(n)*x2(n) %--------------------------------------------%[y,n] = sigmult(x1,n1,x2,n2)% y = product sequence over n, which includes n1 and n2% x1 = first sequence over n1% x2 = second sequence over n2 (n2 can be different from n1)%

n = min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)y1 = zeros(1,length(n)); y2 = y1; % Initilizationy1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of yy2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of yy = y1 .* y2; % sequence Multiplication

Sequence OperationSignal Scaling

Each sample is multiplied by a scalar . Use the command “*” for scaling.

Signal Shifting Each sample of x(n) is shifted by an amount k to

obtain a shifted sequence y(n)

)}n(x{)}n(x{

)}kn(x{)n(y

)}m(x{)km(ythen

kmn

knmletif

% MATLAB functionfunction [y,n] = sigshift(x,m,n0)% implements y(n) = x(n-n0)% ----------------------% [y,n] = sigshift(x,m,n0)%n = m+n0; y = x;

Sequence OperationFolding

Each sample is of x(n) is flipped around n=0 to obtain a folded sequence y(n)

Implement “sigfold” by “fliplr(x)” & “-fliplr(x)”.

Signal Summation Adds all sample values of x(n) between n1 and n2 Implement by “sum(x(n1:n2))” function

)}n(x{)n(y

% MATLAB functionfunction [y,n] = sigfold(x,n)% implements y(n) = x(-n)% --------------------% [y,n] = sigfold(x,n)%y = fliplr(x); n = -fliplr(n);

)n(x)n(x)n(xn

nn 212

1

Sequence OperationSample Products

Multiplies all sample values of x(n) between n1 and n2.

Implement by “prod(x(n1:n2))” function..

Signal Energy

Superscript * denotes the operation of complex conjugation

Implemented by

)n(x)n(x)n(xn

n21

2

1

2

)n(x)n(x)n(x *x

% MATLAB codeEx = sum(x .* conj(x)) % one approachEx = sum(abs(x) .^ 2) % another approach

Sequence OperationSignal Power

Average power of a periodic sequence with fundamental period N

1

0

21 Nx )n(x

NP

Sequence OperationExample 1:

Let

Determine and plot the following sequences

},,,,,,,,,,,,{)n(x 1234567654321

)n(x)n(x)n(x)n(x.b

)n(x)n(x)n(x.a

23

4352

2

1

Sequence OperationExample 1 coding:

% MATLAB code for part (a)n = [-2:10]; x = [1:7,6:-1:1];[x11,n11] = sigshift(x,n,5); [x12,n12] = sigshift(x,n,-4);[x1,n1] = sigadd(2*x11,n11,-3*x12,n12);

% MATLAB code for part (b)n = [-2:10]; x = [1:7,6:-1:1];[x21,n21] = sigfold(x,n); [x21,n21] = sigshift(x21,n21,3);[x22,n22] = sigshift(x,n,2); [x22,n22] = sigmult(x,n,x22,n22);[x2,n2] = sigadd(x21,n21,x22,n22);

% Plotting codingsubplot(2,1,1); stem(n1,x1); title('Sequence in Example 1a')xlabel('n'); ylabel('x1(n)'); subplot(2,1,2); stem(n2,x2); title('Sequence in Example 1b')xlabel('n'); ylabel('x2(n)');

Sequence OperationExample 1 plots:

-10 -5 0 5 10 15-30

-20

-10

0

10

20Sequence in Example 1a

n

x1(n

)

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

10

20

30

40Sequence in Example 1b

n

x2(n

)

Sequence Operation

Example 2: Generate the complex-valued signal

and plot its magnitude, phase, the real part, and the imaginary part in four separate subplots

10103010 n,e)n(x n).j.(

Sequence OperationExample 2 coding:

% MATLAB code exmple2n = [-10:1:10]; alpha = -0.1+0.3j;x = exp(alpha*n);subplot(2,2,1); stem(n,real(x));title('real part');xlabel('n')subplot(2,2,2); stem(n,imag(x));title('imaginary part');xlabel('n')subplot(2,2,3); stem(n,abs(x));title('magnitude part');xlabel('n')subplot(2,2,4); stem(n,(180/pi)*angle(x));title('phase part');xlabel('n')

Sequence OperationExample 2 plots:

-10 -5 0 5 10-3

-2

-1

0

1

2real part

n-10 -5 0 5 10

-2

-1

0

1imaginary part

n

-10 -5 0 5 100

1

2

3magnitude part

n-10 -5 0 5 10

-200

-100

0

100

200phase part

n

Sequence SynthesisUnit Sample Synthesis

Arbitrary sequence x(n) can be synthesized as a weighted sum of delayed and scaled unit sample sequences

Even and odd synthesis

Any arbitrary real-valued sequence x(n) can be decomposed into its even and odd component

2

1

n

nk)kn()k(x)n(x

)symmetric(evencalledis)n(x

)n(x)n(x

then

if

e

ee

)ricantisymmet(oddcalledis)n(x

)n(x)n(x

then

if

o

oo

)]n(x)n(x[)n(x)],n(x)n(x[)n(x

)n(x)n(x)n(x

oe

oe

21

21

Sequence Synthesis Implement “evenodd” function

function [xe, xo, m] = evenodd(x,n)% Real signal decomposition into even and odd parts% ---------------------------------------------% [xe, xo, m] = evenodd(x,n)% if any(imag(x) ~= 0) error('x is not a real sequence')endm = -fliplr(n);m1 = min([m,n]); m2 = max([m,n]); m = m1:m2;nm = n(1)-m(1); n1 = 1:length(n);x1 = zeros(1,length(m));x1(n1+nm) = x; x = x1;xe = 0.5*(x + fliplr(x));xo = 0.5*(x - fliplr(x));

Sequence Synthesis Example 2: Even-Odd Components:

% MATLAB code example 2n = [0:10]; x = stepseq(0,0,10)-stepseq(10,0,10);[xe,xo,m] = evenodd(x,n);subplot(1,1,1)subplot(2,2,1); stem(n,x); title('Rectangular pulse')xlabel('n'); ylabel('x(n)'); axis([-10,10,0,1.2])subplot(2,2,2); stem(m,xe); title('Even Part')xlabel('n'); ylabel('xe(n)'); axis([-10,10,0,1.2])subplot(2,2,4); stem(m,xo); title('Odd Part')xlabel('n'); ylabel('xo(n)'); axis([-10,10,-0.6,0.6])

Sequence Synthesis Example 2: The plots

-10 -5 0 5 100

0.2

0.4

0.6

0.8

1

Rectangular pulse

n

x(n

)

-10 -5 0 5 100

0.2

0.4

0.6

0.8

1

Even Part

n

xe(n

)

-10 -5 0 5 10

-0.4

-0.2

0

0.2

0.4

0.6Odd Part

n

xo(n

)

Sequence SynthesisThe Geometric Series

A one-sided exponential sequence of the form

ttanconsarbitraryis},n,{ n 0

1

0

0

1

1

11

1

N

n

Nn

nn

for,

for,

Sequence SynthesisCorrelation of sequences

Measure of the degree to which two sequences are similar Crosscorrelation If) x(n), y(n) is finite energy Then) crosscorrelation of x(n), y(n)

Autocorrelation Special case of crosscorrelation when y(n)=x(n)

n

ny,x parameterlogorshift:l),ln(y)n(x)l(r

n

nx,x )ln(x)n(x)l(r

ConvolutionConvolution can be evaluated in many different

ways.If arbitrary sequences are of infinite duration,

then MATLAB cannot be used directly to compute the convolution.

As you already know, there are 3 conditions (cases) for evaluation: No overlap Partially overlap Complete overlap

A built in function to compute convolution of 2 finite duration is called “conv”.

It assumes that the two sequences begin at n = 0: >> y = conv(x,h)

ConvolutionExample of a simple convolution:

>>x = [3,11,7,0,-1,4,2];>> h = [2,3,0,-5,2,1];>> y = conv(x,h)y = 6 31 47 6 -51 -5 41 18 -22 -3 8 2

>> n = [1:length(y)];>> m = [1:length(x)]; o = [1:length(h)];>> figure(1); clf>> subplot(2,2,2); stem(m,x)>>title('Sequence x(m)')>>xlabel('m'); ylabel('x(m)');>> subplot(2,2,3); stem(o,h)>> title('Sequence y(o)')>>xlabel('o'); ylabel('y(o)');>> subplot(2,2,1); stem(n,y);>> title('Convolved sequence')>> xlabel('n'); ylabel('y(n)');

x(n) = [3,11,7,0,-1,4,2]

h(n) = [2, 3,0,-5,2,1]

ConvolutionExample of a simple convolution:

0 2 4 6 8-5

0

5

10

15Sequence x(m)

m

x(m

)

0 2 4 6-6

-4

-2

0

2

4Sequence y(o)

o

y(o

)

0 5 10 15-100

-50

0

50Convolved sequence

n

y(n

)

Is This Correct???

ConvolutionBased on the plots, “conv” function neither

provides nor accepts any timing information.We need the beginning and the end point of

y(n).A simple extension of the “conv” function,

called “conv_m”, can be written to perform the convolution of arbitrary support sequences:

function [y,ny] = conv_m(x,nx,h,nh)% Modified convolution routine for signal processing% -----------------------------------------------------% [y,ny] = conv_m(x,nx,h,nh)% [y,ny] = convolution result% [x,nx] = first signal% [h,nh] = second signal%nyb = nx(1)+nh(1); nye = nx(length(x)) + nh(length(h));ny = [nyb:nye];y = conv(x,h);

ConvolutionLets do the convolution again from the

previous example:

Hence we get the correct convolution:

More info? HELP convolution (conv)

>> x = [3,11,7,0,-1,4,2]; nx = [-3:3];>> h = [2,3,0,-5,2,1]; nh = [-1:4];>> [y,ny] = conv_m(x,nx,h,nh)

y = 6 31 47 6 -51 -5 41 18 -22 -3 8 2

ny = -4 -3 -2 -1 0 1 2 3 4 5 6 7

Y(n) = {6,31,47,6,-51,-5,41,18,-22,-3,8,2}

ConvolutionLets plot y(n):

-4 -2 0 2 4 6 8-60

-40

-20

0

20

40

60Convolved Signal y(n)

ny

y(n

)

CorrelationsThere is a close resemblance in convolution and

correlation.Crosscorrelation and autocorrelation can be

computed using the “conv” function if sequences are of finite duration.

Example: In this example let

x(n) = [3,11,7, 0,-1,4,2]

be a prototype sequence, and let y(n) be its noise corrupted and shifted version:

y(n) = x(n-2)+w(n)where w(n) is Gaussian sequence with mean 0 and variance 1.Let’s compute the cross correlation between y(n) and x(n).

Correlations

From the construction of y(n), it follows that y(n) is “similar” to x(n-2)

Hence their strongest similarity is when l = 2. Compute the crosscorrelation using two different

noise sequences in Matlab.

Correlations Matlab commands:

% Noise sequence 1x = [3,11,7,0,-1,4,2]; nx=[-3:3]; % given signal x(n)[y,ny] = sigshift(x,nx,2); % Obtain x(n-2)w = rand(1,length(y)); nw = ny; % generate w(n)[y,ny] = sigadd(y,ny,w,nw); % obtain y(n) = x(n-2) + w(n)[x,nx] = sigfold(x,nx); % obtain x(-n)[rxy,nrxy] = conv_m(y,ny,x,nx); % 1st crosscorrelation%% noise sequence 2x = [3,11,7,0,-1,4,2]; nx=[-3:3]; % given signal x(n)[y,ny] = sigshift(x,nx,2); % Obtain x(n-2)w = rand(1,length(y)); nw = ny; % generate w(n)[y,ny] = sigadd(y,ny,w,nw); % obtain y(n) = x(n-2) + w(n)[x,nx] = sigfold(x,nx); % obtain x(-n)[rxy2,nrxy2] = conv_m(y,ny,x,nx); % 2nd crosscorrelation%figure(1); clf % clear figure(1)subplot(1,1,1), subplot(2,1,1); stem(nrxy,rxy)axis([-4,8,-50,250]); xlabel('lag variable 1')ylabel('rxy');title('Crosscorrelation: noise sequence 1')subplot(2,1,2); stem(nrxy2,rxy2)axis([-4,8,-50,250]); xlabel('lag variable 1')ylabel('rxy');title('Crosscorrelation: noise sequence 2')

Correlations The plots:

-4 -2 0 2 4 6 8-50

0

50

100

150

200

250

lag variable 1

rxy

Crosscorrelation: noise sequence 1

-4 -2 0 2 4 6 8-50

0

50

100

150

200

250

lag variable 1

rxy

Crosscorrelation: noise sequence 2

Correlations Based on the plots, the correlation is proven to peak at l =

2. This implies that y(n) is similar to x(n) shifted by 2.

We can use the signal-processing toolbox in matlab for crosscorrelation:

>> xcorr(x,y)computes the crosscorrelation between vectors x and y,while,

>> xcorr(x)computes the autocorrelation of vector x.

This function does not provide timing information as our little “conv_m” function does.

top related