ASSIGNMENT#1 REPORT Assignment 1 -...

36
ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the numbers 0...99. Create the unit impulse function δ(n) and the unit step function u(n) with zeros and ones for the sample interval 0 n 99. Plot the signals in different intervals with stem and plot respectively. Always use help to learn the command syntax it’s function, for example for the command zoom. MATLAB CODE: n=[0:99]; impulse=[1,zeros(1,99)]; step=[zeros(1,10),ones(1,99)]; plot(impulse,'r') ylabel('UNIT IMPULSE') title('UNIT IMPULSE') grid figure(2); plot(step,'r') ylabel('UNIT STEP') title('UNIT STEP') grid figure(3); stem(impulse,'r') title('UNIT IMPULSE SEQUENCE') grid figure(4); stem(step,'g') title('UNIT STEP SEQUENCE') grid;

Transcript of ASSIGNMENT#1 REPORT Assignment 1 -...

Page 1: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

ASSIGNMENT#1 REPORT

Assignment 1.1

Generate a time vector which contains the numbers 0...99. Create the unit

impulse function δ(n) and the unit step function u(n) with zeros and ones for the

sample interval 0 ≤ n ≤ 99. Plot the signals in different intervals with stem and

plot respectively. Always use help to learn the command syntax it’s function, for

example for the command zoom.

MATLAB CODE:

n=[0:99]; impulse=[1,zeros(1,99)];

step=[zeros(1,10),ones(1,99)];

plot(impulse,'r')

ylabel('UNIT IMPULSE')

title('UNIT IMPULSE')

grid

figure(2);

plot(step,'r')

ylabel('UNIT STEP')

title('UNIT STEP')

grid

figure(3);

stem(impulse,'r')

title('UNIT IMPULSE SEQUENCE')

grid

figure(4);

stem(step,'g')

title('UNIT STEP SEQUENCE')

grid;

Page 2: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

OUT PUT PLOT:

0 10 20 30 40 50 60 70 80 90 1000

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1U

NIT

IM

PU

LS

E

UNIT IMPULSE

0 20 40 60 80 100 1200

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

UN

IT S

TE

P

UNIT STEP

Page 3: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

0 10 20 30 40 50 60 70 80 90 1000

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1UNIT IMPULSE SEQUENCE

0 20 40 60 80 100 1200

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1UNIT STEP SEQUENCE

Page 4: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

Assignment 1.2 Generate an exponentially decaying series x(n) = an · u(n) with a = 0.5, 0.9, 1.0,

1.1, 2.0 for the previous time interval and create a plot. Elementwise operations

are done with the point sign, for example should you use .ˆ inthis assignment.

Also, calculate the sum of the samples without using loops and compare the

result with the expression for the geometric series. Use the command input in

this assignment.

MATLAB CODE:

function Q12 % a is base of

exponential function;

a=input('Enter value of a '); % Takes input value

of a from user;

n=[0:99]; % n is time vector;

c=a.^n;

plot(c);

grid

s=sum(c) % calculates sum

without using loops;

end

OUTPUT PLOT:

0 10 20 30 40 50 60 70 80 90 1000

1

2

3

4

5

6

7x 10

29

Page 5: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

Assignment 1.3

Create the signal x(n) = cos(2πf0n) using a time vector oflength 8192. Plot the

first 20-100 samples for the frequencies 0, 1/16, 1/8, 1/4, 1/2and 1/√5. Did you

expected the results? Are they periodic? Listen to the signalsusing the command

sound. Which frequency do you hear. Repeat the assignmentfor a sinusoid with

frequency 1/2 and explain the resulting plot.

MATLAB CODE:

x1=0:1:8192;

n=20:35;

f0=input(' GIVE INPUT FREQUENCY:'); % f0 is given input

frequency

xx1= cos(2*pi*f0*n); % f0=0,1/16,1/8,

1/4,1/2,1/sqrt(5)

subplot(2,1,1)

plot(xx1,'g');

grid;

xlabel('TIME VECTOR');

ylabel('xx1');

title('FOR f0=1/sqrt(5)')

subplot(2,1,2)

grid;

stem(xx1,'r');

grid;

xlabel('TIME VECTOR');

ylabel('xx1');

sound(xx1)

title('FOR f0=1/sqrt(5)')

Page 6: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

OUTPUT PLOT:

0 2 4 6 8 10 12 14 160

0.5

1

1.5

2

TIME VECTOR

xx1

FOR f0=1/sqrt(5)

0 2 4 6 8 10 12 14 160

0.5

1

TIME VECTOR

xx1

FOR f0=1/sqrt(5)

0 2 4 6 8 10 12 14 16-2

-1

0

1

2x 10

-13

TIME VECTOR

xx2

for f0=1/2

0 2 4 6 8 10 12 14 16-2

-1

0

1

2x 10

-13

TIME VECTOR

xx2

for f0=1/2

Page 7: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

Assignment 1.3 b

Repeat the assignment for a sinusoid with frequency 1/2 and explain the

resulting plot.

MATLAB CODE:

x1=0:1:8192;

n=20:35;

f0=input('GIVE FREQUENCY:')% f0 is given input frequency

xx2= sin(2*pi*f0*n) % f0=0,1/16,1/8, 1/4,1/2,1/sqrt(5)

%xn1= cos(2*pi*f0*n);

subplot(2,1,1)

plot(xx2,'g');

grid;

xlabel('TIME VECTOR');

ylabel('xx2');

title('for f0=1/2')

subplot(2,1,2)

stem(xx2,'r');

grid;

xlabel('TIME VECTOR');

ylabel('xx2');

sound(xx2)

title('for f0=1/2')

Page 8: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

OUTPUT PLOT:

0 2 4 6 8 10 12 14 16-1.5

-1

-0.5

0

0.5

1x 10

-14

TIME VECTOR

xx2

for f0=1/2

0 2 4 6 8 10 12 14 16-2

-1

0

1x 10

-14

TIME VECTOR

xx2

for f0=1/2

Assignment 1.4

Continue by adding the signals x1(n) = cos(2πf1n) and x2(n) = cos(2πf2n) in a

vector of length 8192 with the adjacent frequencies f1 = 1/8 − 1/8192 and f2 = 1/8

+ 1/8192. Listen and plot and rewrite your program using the results from

assignment 1.4. Then, listen only to x1(n). Change the frequency f1 = 1/2 −

1/8192 and listen again. Can you explain the beating effect although there is only

one cosine???

MATLAB CODE:

n=0:1:8192;

f1=(1/8-1/8192);

f2=(1/8+1/8192);

xx1= cos(2*pi*f1*n);

xx2= cos(2*pi*f2*n);

subplot(2,2,1)

plot(xx1,'g');

Page 9: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

grid;

xlabel('TIME');

ylabel('xx1');

subplot(2,2,2)

grid;

plot(xx2,'g');

grid;

xlabel('TIME');

ylabel('xx2');

subplot(2,2,3)

grid;

plot(xx1+xx2,'b');

grid;

xlabel('TIME');

ylabel('xx1+xx2');

xx3= cos(2*pi*(1/2-1/8192)*n);

subplot(2,2,4)

grid;

plot(xx3,'r');

grid;

xlabel('TIME');

ylabel('xx3 (1/2-1/8192)');

sound(xx1+xx2)

sound(xx1)

sound(xx3)

OUTPUT PLOT:

0 5000 10000-1

-0.5

0

0.5

1

TIME

xx1

0 5000 10000-1

-0.5

0

0.5

1

TIME

xx2

0 5000 10000-2

-1

0

1

2

TIME

xx1+

xx2

0 5000 10000-1

-0.5

0

0.5

1

TIME

xx3 (

1/2

-1/8

192)

Page 10: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

Assignment 1.5

Plot x(n) = cos(2πf0n) for frequencies 1/8, 7/8 and 9/8. In which way differ these

functions?

MATLAB CODE:

n=0:99;

f1=1/8 %FREQUENCY FOR F1

frequency1=cos(2*pi*f1*n);

subplot(3,1,1)

plot(frequency1,'g')

grid;

title('FREQUENCY f=1/8');

xlabel('TIME INTERVAL');

ylabel('FREQUENCY 1');

f2=7/8 %FREQUENCY FOR F2

frequency2=cos(2*pi*f2*n);

subplot(3,1,2)

plot(frequency2,'b')

grid;

title('FREQUENCY f=7/8');

xlabel('TIME INTERVAL');

ylabel('FREQUENCY 2');

f3=9/8 %FREQUENCY FOR F3

frequency3=cos(2*pi*f3*n);

subplot(3,1,3)

plot(frequency3,'r')

grid;

title('FREQUENCY f=9/8');

xlabel('TIME INTERVAL');

ylabel('FREQUENCY3');

sound(frequency1)

sound(frequency2)

sound(frequency3)

Page 11: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

OUTPUT PLOT:

0 10 20 30 40 50 60 70 80 90 100-1

0

1FREQUENCY f=1/8

TIME INTERVAL

FR

EQ

UE

NC

Y 1

0 10 20 30 40 50 60 70 80 90 100-1

0

1FREQUENCY f=7/8

TIME INTERVAL

FR

EQ

UE

NC

Y 2

0 10 20 30 40 50 60 70 80 90 100-1

0

1FREQUENCY f=9/8

TIME INTERVAL

FR

EQ

UE

NC

Y3

Assignment 1.6

Create the complex signal x(n) = zn = anej2πf0n = elog(a)n+j2πf0n where z = |z|ej arg z =

|a|ej2πf0 for any a and f0 using a vector of length 8192. Plot x(n) in a diagram. Also

plot |x(n)| and the real- and imaginary part of x(n) in an appropriate interval in the

same figure, use the command hold. Listen to the signals using sound. See

which variables you have in Matlab using whos and remove one or more

variables with clear.

MATLAB CODE:

clear

a=0.8; fo=0.45; %a and f0 are some

arbitary value

n=0:1:90;

xn=((a).^n).*exp(j*2*pi*fo*n);

figure,

subplot(221)

Page 12: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

plot(xn);

grid;

xlabel('time interval n');

ylabel('xn');

title('COMPLEX SIGNAL xn');

r=abs(((a).^n).*cos(2*pi*fo*n));

img=(((a).^n).*sin(2*pi*fo*n));

% figure,

subplot(222)

plot(r,'g');

grid;

xlabel('time interval n');

ylabel('REAL PART');

title('REAL PART x(n)');

% figure,

subplot(223)

plot(img,'r');

grid;

xlabel('time interval n');

ylabel('IMGINARY PART');

title('IMAGINARY OF x(n)');

% figure,

subplot(224)

plot(abs(xn),'b*'); %abs value of x(n)in

blue

hold on

plot(real(xn),'g'); %imag part of x(n) in

green

grid;

xlabel('time interval n');

ylabel('real(xn)');

hold on

plot(imag(xn),'r'); %real part of x(n)in

red

xlabel('time interval n');

ylabel('IMAGINARY(xn)');

sound(xn)

sound(abs(xn))

sound(real(xn))

Page 13: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

OUTPUT PLOT:

-1 -0.5 0 0.5 1-0.5

0

0.5

time interval n

xn

COMPLEX SIGNAL xn

0 50 1000

0.5

1

time interval n

RE

AL P

AR

T

REAL PART x(n)

0 50 100-0.5

0

0.5

time interval n

IMG

INA

RY

PA

RT

IMAGINARY OF x(n)

0 50 100-1

-0.5

0

0.5

1

time interval n

IMA

GIN

AR

Y(x

n)

Assignment 1.7 Generate a chirp signal, i.e. a signal where the pitch (frequency)increases

gradually, and listen to it. Also, create sinusoids with beating-, vibratoand

echo effects. Read ”Hallelujah” load handel, play it and add echo to it. Can

you change the pitch of the music?

MATLAB CODE:

n=[1:.001:35];

x=chirp(n,1,9,90); %x is a chrip signal

%plot(x(1:1000),'r')

n=0:0.05:10;

subplot(2,2,1)

plot(y1n,'g');

grid;

title('CHIRP SIGNAL')

n1=0:1:8192;

f1=(1/2-1/8192);

Page 14: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

x1n=cos(2*pi*f1*n1);

subplot(2,2,2)

plot(x1n,'b');

grid;

title('x1n')

f2=(1/2+1/8192);

x2n=cos(2*pi*f2*n1);

subplot(2,2,3)

plot(x2n,'y')

grid;

title('x2n')

x3n=x1n-x2n;

subplot(2,2,4)

plot(x3n,'r')

title('VIBRATION EFFECT')

grid;

sound(x)

load handel;

player = audioplayer(y, Fs); %% following code

is to load and play an audio file

play(player,[1 (get(player, 'SampleRate')*6)]);

OUTPUT PLOT:

0 1 2 3 4

x 104

-1

-0.5

0

0.5

1CHIRP SIGNAL

0 5000 10000-1

-0.5

0

0.5

1x1n

0 5000 10000-1

-0.5

0

0.5

1x2n

0 5000 10000-10

-5

0

5x 10

-12VIBRATION EFFECT

Page 15: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

Assignment 1.8

Generate a siren sound, i.e. a signal that alternates between two tones. Also, try

to generate a siren that glides back and forth between tones (American siren).

MATLAB CODE:

clear all,

clc,

close all,

for i=1:20

n=[i:.001:90];

end

signalx=chirp(n,40,1,300);

plot(signalx,'m')

grid;

sound(signalx)

zoom on

title('Zoomed version of the output plot')

OUTPUT PLOT:

2.2 2.205 2.21 2.215 2.22 2.225

x 104

-0.359

-0.358

-0.357

-0.356

-0.355

-0.354

-0.353

Zoomed version of the output plot

Page 16: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

Assignment 1.9

Generate a sequence x(n) = 1+cos(2πf0n) of 8192 samples with f0 = 0.0625. Let

a filter calculate the 1st difference, i.e. y(n) = x(n) − x(n − 1). Use loops for..end

or use subtraction with ‘two ‘vectors. Study the signal and explain the result.

What is the impulse response of the system calculating the 1st difference? Is it

causal?? Stable??

MATLAB CODE:

close all,

clc,

clear all,

fo=0.0625;

n=1:8192

x(n)=1+cos(2*pi*fo*n);

x=[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];

hn=x;

h(1)=x(1)

for n=2:10

h(n)=x(n)-x(n-1);

end

subplot(2,1,1)

stem(x(1:10),'r');

grid;

title('SEQUENCE OF SAMPLES')

subplot(2,1,2)

stem(h(1:10),'g');

grid;

title('SYTEM IMPULSE RESPONSE h(n)')

Page 17: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

OUTPUT PLOT:

1 2 3 4 5 6 7 8 9 100

0.5

1SEQUENCE OF SAMPLES

1 2 3 4 5 6 7 8 9 10-1

-0.5

0

0.5

1SYTEM IMPULSE RESPONSE h(n)

Assignment 1.10

Let a filter calculate the cumulative sum

Is this system stable?? Causal?? What is the impulse response h(n)??

MATLAB CODE:

close all

clear all

clc,

fo=0.025;

n=0:1:100;

x=1+cos(2*pi*fo*n);

x=[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0];

h(1)=x(1);

for n=2:9

h(n)=h(n-1)+x(n); %THIS IS A NON CAUSAL SYSTEM

end

Page 18: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

stem(h(1:9),'m');

grid;

title('SYSTEM IMPULSE RESPONSE h(n)');

OUTPUT PLOT:

1 2 3 4 5 6 7 8 90

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1SYSTEM IMPULSE RESPONSE h(n)

Assignment 1.11

Finally, let a filter calculate finite cumulative sums,

i.e. a gliding mean. Is this system stable?? Causal?? What is the impulse

response

h(n)??

Page 19: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

MATLAB CODE:

clear all;

clc,

close all;

x=[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];

n=1:10;

x(1,n)=0;

x(1,2)=1;

h(1,1)=x(1,1);

h(1,2)=x(1,1)+x(1,2);

h(1,3)=x(1,1)+x(1,2)+x(1,3);

h(1,4)=x(1,1)+x(1,2)+x(1,3)+x(1,4);

for j=5:10

h(1,j)=x(1,(j-4)),x(1,(j-3))+x(1,(j-2))+x(1,(j-1))+x(1,j);

end

figure,

subplot(2,1,1)

stem(x,'g')

grid;

title('INPUT SIGNAL');

subplot(2,1,2)

stem(h,'r');

grid;

title('IMPULSE RESPONSE');

OUTPUT PLOT:

0 2 4 6 8 10 12 14 160

0.5

1INPUT SIGNAL

1 2 3 4 5 6 7 8 9 100

0.5

1IMPULSE RESPONSE

Page 20: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

Assignment 1.12

Add 10δ(n − 10) − 10δ(n − 20) to x(n) = 1+cos(2πf0n) for a time vector with

length 8192 where f0 = 0.025. Perform computation of a gliding median,ny(n) =

Medianx(k)k=n−4i.e. a different gliding ‘mean ‘. Is this system stable?? Causal??

Linear?.What is it’s impulse response??

MATLAB CODE:

close all

clc,

clear all

for t=1:8192

n=t;

if n==10

i=10;

elseif n==20

i=-10;

else i=0;

end

x(1,n)=(1+cos(2*pi*0.025*n))+i;

end

%FOR MEDIAN CALCULATION

for j=5:8192

i=[x(1,j) x(1,(j-1)) x(1,(j-2)) x(1,(j-3)) x(1,(j-4))];

h(1,(j-4))=median(i);

end

subplot(2,1,1)

plot(x(1:20),'c');

grid;

% [XMIN XMAX hMIN hMAX]

title('ADDITION OF TWO SIGNALS')

subplot(2,1,2)

plot(h(1:20),'R');

grid;

title('SYSTEM IMPULSE RESPONSE h(n)');

Page 21: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

OUTPUT PLOT:

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

0

10

20ADDITION OF TWO SIGNALS

0 2 4 6 8 10 12 14 16 18 200

0.5

1

1.5

2SYSTEM IMPULSE RESPONSE h(n)

Assignment 1.13

Convolve signals [1 2 3 4 5] and [1 3 2] with the Matlab functions conv and

xcorr. Also, calculate the correlation between the signals, both with conv, and

with xcorr.

MATLAB CODE:

close all

clc,

clear all

x=[1 2 3 4 5];

h=[1 3 2];

convy= conv(h,x)

corrk= xcorr(h,x)

subplot(2,2,1)

plot(convy,'g');

grid;

Page 22: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

subplot(2,2,2)

stem(convy,'r')

grid;

subplot(2,2,3)

plot(corrk,'m');

grid;

subplot(2,2,4)

stem(corrk,'c');

grid;

0 2 4 6 80

5

10

15

20

25

0 2 4 6 80

5

10

15

20

25

0 5 100

5

10

15

20

25

0 5 100

5

10

15

20

25

Assignment 1.14 Redo assignments 1.9-1.11 by using conv. compare the andconvince yourself of

good results by studying the samples and the vector dimensionswith whos or by

plotting in one diagram. Use hold on and hold off, and plotwith different colors

Page 23: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

MATLAB CODE:

close all

clc,

clear all

x=[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];

h(1)=x(1);

for n1=2:10

h(n1)=x(n1)-x(n1-1);

end

outputh=conv(x,h);

subplot(3,1,1)

stem(x(1:10),'m');

grid;

title('INPUT SIGNAL')

subplot(3,1,2)

stem(h,'c');

grid;

title('IMPULSE RESPONSE h(n)')

subplot(3,1,3)

stem(outputh(1:10),'r');

grid;

title('SIGNAL AFTER CONVOLUTION');

OUTPUT PLOT:

1 2 3 4 5 6 7 8 9 100

0.5

1INPUT SIGNAL

1 2 3 4 5 6 7 8 9 10-1

0

1IMPULSE RESPONSE h(n)

1 2 3 4 5 6 7 8 9 10-1

0

1SIGNAL AFTER CONVOLUTION

Page 24: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

MATLAB CODE 1.14:

%USING CONVOLUTION

clear all,

clc,

close all,

p = [1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

q = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ]

outputh=conv(p,q);

subplot(3,1,1)

stem(p,'g');

grid;

title('IMPULSE RESPONSE h(n)')

subplot(3,1,2)

stem(q,'r');

grid;

title('SIGNAL AFTER CONVOLUTION')

subplot(3,1,3)

plot(outputh);

grid;

title('SIGNAL')

0 2 4 6 8 10 12 14 16-1

0

1IMPULSE RESPONSE h(n)

0 2 4 6 8 10 12 14 160

0.5

1SIGNAL AFTER CONVOLUTION

0 5 10 15 20 25 30 35-1

0

1SIGNAL

Page 25: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

MATLAB CODE 1.14:

% ASSIGNMENT 1.14 USING CONV COMMAND FOR ASSIGNMENT % 1.11

clear all;

clc,

close all;

n=1:10;

x(1,n)=0;

x(1,2)=1;

h(1,1)=x(1,1);

h(1,2)=x(1,1)+x(1,2);

h(1,3)=x(1,1)+x(1,2)+x(1,3);

h(1,4)=x(1,1)+x(1,2)+x(1,3)+x(1,4);

for k=5:10

h(1,k)=x(1,(k-4))+x(1,(k-3))+x(1,(k-2))+x(1,(k-1))+x(1,k);

end

outputh=conv(h,x);

subplot(2,1,1)

stem(h,'m');

hold on

grid;

title('IMPULSE RESPONSE')

subplot(2,1,2)

stem(outputh,'r');

hold off

grid;

title('SIGNAL AFTER CONVOLUTION')

Page 26: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

OUTPUT PLOT:

1 2 3 4 5 6 7 8 9 100

0.5

1IMPULSE RESPONSE

0 2 4 6 8 10 12 14 16 18 200

0.5

1SIGNAL AFTER CONVOLUTION

Assignment 1.15

Redo assignment 1.9-1.11 by using the MaTLab function filter. Compare the

results and convince yourself that they are the same.

MATLAB CODE:

% ASSIGNMENT 1.15a BY USING FILTER FUNCTION

% ON ASSIGNMENT 1.9

close all

clc,

clear all

hold on

fo=0.0625;

n=0:1:8192;

x=1+cos(2*pi*fo*n);

x=[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];

Page 27: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

h(1)=x(1);

for n1=2:10

h(n1)=x(n1)-x(n1-1);

end

outputy=filter(h,[1],x);

subplot(3,1,1)

stem(x(1:10),'m');

grid;

title('INPUT SIGNAL')

subplot(3,1,2)

stem(h,'g');

grid;

title('SYSTEM IMPULSE RESPONSE')

subplot(3,1,3)

stem(outputy(1:10),'r');

grid;

title('SIGNAL AFTER FILTERING')

OUTPUT PLOT:

1 2 3 4 5 6 7 8 9 100

0.5

1INPUT SIGNAL

1 2 3 4 5 6 7 8 9 10-1

0

1SYSTEM IMPULSE RESPONSE

1 2 3 4 5 6 7 8 9 10-1

0

1SIGNAL AFTER FILTERING

Page 28: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

MATLAB CODE: % ASSIGNMENT 1.15 BY USING FILTER FUNCTION ON

%ASSIGNMENT 1.10

close all

clc,

clear all

fo=0.025;

n=0:1:100;

x=1+cos(2*pi*fo*n);

h(1)=x(1);

for n=2:100

h(n)=h(n-1)+x(n);

end

outputy=filter(h,1,x);

subplot(2,1,1)

stem(h,'r');

grid;

title('SYSTEM IMPULSE RESPONSE h(n)')

subplot(2,1,2)

stem(outputy(1:100),'m');

grid;

title('OUTPUT SIGNAL AFTER FILTERING')

OUTPUT PLOT:

0 10 20 30 40 50 60 70 80 90 1000

50

100

150SYSTEM IMPULSE RESPONSE h(n)

0 10 20 30 40 50 60 70 80 90 1000

2000

4000

6000OUTPUT SIGNAL AFTER FILTERING

Page 29: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

MATLAB CODE:

% ASSIGNMENT 1.15c

%USING filter COMMAND ON ASSIGNMENT 1.11

clear all;

clc

close all;

n=1:10;

x(1,n)=0;

x(1,2)=1;

h(1,1)=x(1,1);

h(1,2)=x(1,1)+x(1,2);

h(1,3)=x(1,1)+x(1,2)+x(1,3);

h(1,4)=x(1,1)+x(1,2)+x(1,3)+x(1,4);

for j=5:10

h(1,j)=x(1,(j-4))+x(1,(j-3))+x(1,(j-2))+x(1,(j-1))+x(1,j);

end

output=filter(h,1,x),

subplot(2,1,1)

stem(h,'m');

grid;

title('IMPULSE RESPONSE')

subplot(2,1,2)

stem(output,'r');

grid;

title('SIGNAL AFTER FILTERING')

Page 30: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

OUTPUT PLOT:

1 2 3 4 5 6 7 8 9 100

0.5

1IMPULSE RESPONSE

1 2 3 4 5 6 7 8 9 100

0.5

1SIGNAL AFTER FILTERING

Assignment 1.16

Load ”Hallelujah” and filter it using the filter in assignment 1.9, and with the filter

in assignment 1.11 using impulse responses of different length. Can you hear the

effect of the filter?

% ASSIGNMENT 1.16 USING FILTER FUNCTION

load handel %load 'Hallelujah'

N=20;

n=zeros(1,N);

x1=n;

x1(1,2)=1; % BY USING ASSIGNMENT 1.9

h1=[1 1 4];

h1=conv(x1,h1);

y1=filter(h1,1,y);

subplot(2,2,1)

stem(h1,'m');

Page 31: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

grid;

subplot(2,2,2)

plot(y1,'g');

grid;

sound(y1) % BY USING ASSIGNMENT 1.11

h2=[1 3 5 1];

h2=conv(x1,h2);

y2=filter(h2,1,y);

subplot(2,2,3)

stem(h2,'b');

grid;

subplot(2,2,4)

plot(y2,'r');

grid;

sound(y2)

OUTPUT PLOT:

0 10 20 300

1

2

3

4

0 2 4 6 8

x 104

-5

0

5

0 10 20 300

2

4

6

0 2 4 6 8

x 104

-10

-5

0

5

10

Page 32: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

Assignment 1.17

A system has step response s(n) = 0.5n cos(πn/6) · u(n). Determine the impulse

response using Matlab and plot both the step response and the impulse

response in one and the same diagram. Compare with your analytic expression.

MATLAB CODE:

% ASSIGNMENT 1.17

close all;

clc,

clear all;

n=1:1:10;

x=((0.5).^n).*cos(pi*n/6);

y(1,1)=x(1,1);

j=2:10;

y(1,j)=x(1,j)-x(1,(j-1));

figure(1)

stem(x,'m');

xlabel('TIME INTERVAL');

ylabel('AMPLITUDE');

title('STEP RESPONSE & IMPULSE RESPONSE');

hold on;

stem(y,'g');

grid;

Page 33: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

OUTPUT LAB:

1 2 3 4 5 6 7 8 9 10-0.4

-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

0.5

TIME INTERVAL

AM

PLIT

UD

ESTEP RESPONSE & IMPULSE RESPONSE

Assignment 1.18

A system has impulse response h(n) = 0.5n cos(πn/6) · u(n). Determine the step

response with MATLab and plot both the step response and the impulse

response in one and the same diagram. Compare with your analytic expression.

MATLAB CODE:

% ASSIGNMENT 1.18

close all;

clc,

clear all;

n=1:1:20;

h=((0.5).^n).*(cos(((pi).*n)/6));

x(1,1)=1;

for t=2:20,

x(1,t)=x(1,(t-1))+h(1,t);

end

Page 34: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

figure(1)

stem(x,'m');

hold on;

stem(h,'g');

grid;

xlabel('TIME INTERVAL n');

ylabel('AMPLITUDE');

title('STEP RESPONSE s(n) IMPULSE RESPONSE h(n)');

OUTPUT PLOT:

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

0

0.2

0.4

0.6

0.8

1

1.2

TIME INTERVAL n

AM

PLIT

UD

E

STEP RESPONSE s(n) IMPULSE RESPONSE h(n)

Assignment 1.19

A helicopter has a rotor which rotates with 780 rotations per minute. The rotor

with accompanying propeller generates harmonics and together they constitute

helicopter sound (noise). An analysis shows that harmonics 6...15 dominate the

Page 35: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

sound. Generate helicopter sound and listen! What happens when you remove

one of the harmonics?

MATLAB CODE:

close all

clc,

clear all

n=0:0.01:50;

x=cos(2*pi*13*n);

subplot(3,1,1)

plot(x,'g');

grid;

xlabel('Time interval');

ylabel('x(n)');

y=cos(2*pi*13*n)+cos(6*2*pi*13*n)+cos(7*2*pi*13*n)+cos(8*2*

pi*13*n)+cos(9*2*pi*13*n)+cos(10*2*pi*13*n)+cos(11*2*pi*13*

n)+cos(12*2*pi*13*n)+cos(13*2*pi*13*n)+cos(14*2*pi*13*n)+co

s(15*2*pi*13*n);

subplot(3,1,2)

plot(y);

grid;

xlabel('Time interval');

title('signal with harmonics');

y=y-

(cos(8*2*pi*13*n)+cos(9*2*pi*13*n)+cos(10*2*pi*13*n)+cos(12

*2*pi*13*n));

subplot(3,1,3)

plot(y,'m');

grid;

xlabel('Time interval');

title('After removing some harmonics');

sound(y)

Page 36: ASSIGNMENT#1 REPORT Assignment 1 - read.pudn.comread.pudn.com/downloads150/sourcecode/math/652422... · ASSIGNMENT#1 REPORT Assignment 1.1 Generate a time vector which contains the

OUTPUT PLOT:

0 1000 2000 3000 4000 5000 6000-1

0

1

Time interval

x(n

)

0 1000 2000 3000 4000 5000 6000-10

0

10

20

Time interval

signal with harmonics

0 1000 2000 3000 4000 5000 6000-5

0

5

10

Time interval

After removing some harmonics