You are on page 1of 30

Q1-1 Write a set of commands to generate and plot the following exponential damped sinusoidal

signal in 10 seconds:
x( t ) = 5sin( 3t + ) e 6t
>> t=0:0.01:10;
>> x=5*sin(3*t+pi).*exp(-6*t);
>> plot(t,x);
>> title('Exponential Damped Sine Wave');
>> xlabel('Time');
>> ylabel('Amplitude')
Exponential Damped Sine Wave
0.1
0
-0.1
-0.2

Amplitude

-0.3
-0.4
-0.5
-0.6
-0.7
-0.8
-0.9

5
Time

10

Q1-2 Use the Matlab code to generate the triangular wave as shown in Figure Q1-2 below
for the time range [-1, 1].

>> t=0:0.001:1;
>> b=1;
>> f=5*pi;
>> w=0.5;
>> x=b*sawtooth(f*t,w);
>> plot(t,x);
>> title('sawtooth');
>> xlabel('Time');
>> ylabel('Amplitude')

sawtooth
1
0.8
0.6
0.4

Amplitude

0.2
0
-0.2
-0.4
-0.6
-0.8
-1

0.1

0.2

0.3

0.4

0.5
Time

0.6

0.7

0.8

0.9

Q1-3 Write a set of Matlab commands to generate and plot the output of a system with the
system impulse response
h[ n] = 0.5 n ( u[ n] u[ n10] )
and the input signal
x[ n] = 2{u[ n] u[ n10] } .
>> n=0:20;
>> h=(0.5.^n).*[ones(1,10),zeros(1,11)];
>> x=2*[ones(1,10),zeros(1,11)];
>> y=conv(h,x);
>> subplot(3,1,1);
>> stem(n,h);
>> title('System Impulse Response');
>> subplot(3,1,2);
>> stem(n,x);

>> title('Input Signal');


>> subplot(3,1,3);
>> stem(y);
>> title('Output Signal')
System Impulse Response
1
0.5
0

10

12

14

16

18

20

12

14

16

18

20

Input Signal
2
1
0

10

Output Signal
4
2
0

10

15

>> t=0:0.01:4;
>> x1=2*exp(-t).*cos(0.3*pi*t);
>> x2=2*exp(-t).*sin(0.3*pi*t);
>> x=x1+i*x2;
>> y=abs(x);
>> subplot(1,3,1);
>> plot(t,x1);

20

25

30

35

40

45

>> title('real cos');


>> subplot(1,3,2);
>> plot(t,x2);
>> title('imag sin');
>> subplot(1,3,3);
>> plot(t,y);
>> title('complete y')
real cos

imag sin

complete y

0.7

2
1.8

0.6
1.5

1.6
0.5
1.4
0.4

1.2

0.3
0.5

1
0.8

0.2

0.6
0.1
0

0.4
0

-0.5

>> t=-4:0.01:4;
>> x1=2*cos(0.5*pi*t);
>> x2=3*cos(3*pi*t);
>> x3=x1+x2;
>> subplot(3,1,1);
>> plot(t,x1);

-0.1

0.2

>> title('x1');
>> subplot(3,1,2);
>> plot(t,x2);
>> title('x2');
>> subplot(3,1,3);
>> plot(t,x3)
>> title('x3')
x1
2
0
-2
-4

-3

-2

-1

x2
5
0
-5
-4

-3

-2

-1

0
x3

5
0
-5
-4

-3

>> N=6;
>> n=0:1:5;
>> x=cos(pi/3*n);
>> a=(1/N)*fft(x);
>> a1=real(a);

-2

-1

>> b1=imag(a);
>> subplot(2,1,1);
>> stem(n,a1);
>> ylabel('Real Part of x');
>> subplot(2,1,2);
>> stem(n,b1);
>> ylabel('Imag Part of x');

0.6
Real Part of x

0.4
0.2
0
-0.2

0.5

1.5

2.5

3.5

4.5

1.5

2.5

3.5

4.5

-16

Imag Part of x

x 10

1
0
-1
-2

0.5

>> a=[0,0.5,0,0.5,0,0];
>> N=6;
>> x=N*ifft(a);
>> a1=real(x);

>> b1=imag(x);
>> subplot(2,1,1);
>> stem(a1);
>> ylabel('Real Part of x');
>> subplot(2,1,2);
>> stem(b1);
>> ylabel('Imag Part of x');

Real Part of x

1
0.5
0
-0.5
-1

1.5

2.5

3.5

4.5

5.5

1.5

2.5

3.5

4.5

5.5

Imag Part of x

0.5

-0.5

Q2-3

Write a Matlab program to determine the CTFT of x(t) = sin(50t).

>> t=0:0.01:10;
>> x=sin(50*t);
>> a=0.01*fft(x,512);
>> w=2*pi*(0:511)/(512*0.01);
>> subplot(2,1,1);
>> plot(w,abs(a));
>> subplot(2,1,2);
>> plot(w,angle(a))

2.5
2
1.5
1
0.5
0

100

200

300

400

500

600

700

100

200

300

400

500

600

700

4
2
0
-2
-4

>> a=[1,-0.5,1,0,0,0,0];
>> N=7;
>> n=0:1:6;
>> y=(1/N)*fft(a);
>> stem(y);
>> title('DTFS Cofficients');
>> xlabel('n');
>> ylabel('ak')
DTFS Cofficients
0.15

0.1

0.05

ak

-0.05

-0.1

-0.15

-0.2

4
n

>> n=[0:50];
>> x=exp(-(0.1*n).^2/2);
>> w=2*pi*(-50:49)/100;
>> figure(1);
>> stem(w,abs(fftshift(fft(x,100))));
>> y=exp(-(0.1*n*2).^2/2);
>> figure(2);
>> stem(w,abs(fftshift(fft(y,100))));
>> z=exp(-(0.1*n*4).^2/2);
>> figure(3);
>> stem(w,abs(fftshift(fft(z,100))))

14

12

10

0
-4

-3

-2

-1

-3

-2

-1

0
-4

4
3.5
3
2.5
2
1.5
1
0.5
0
-4

-3

-2

-1

Q3-1: Consider the following system:

y[ n] + 3 y[ n1] =

x[ n] + x[ n2]

Find the frequency response at 6 evenly spaced frequencies between 0 and .


>> y=[1 3];
>> x=[1 0 1];
>> n=6;
>> w=linspace(0,pi,n);
>> h=freqz(x,y,w);
>> stem(w,abs(h));
>> title('Frequency Response');
>> xlabel('w');
>> ylabel('abs(H)')

Frequency Response
1
0.9
0.8
0.7

abs(H)

0.6
0.5
0.4
0.3
0.2
0.1
0

0.5

1.5

2
w

>> y=[1 2 1 ];
>> x=[1 -2];
>> w=linspace(0,3*pi);
>> h=freqs(x,y,w);
>> plot(w,abs(h));
>> title('Frequency Response');
>> xlabel('w');
>> ylabel('abs(H)')

2.5

3.5

Frequency Response
2
1.8
1.6
1.4

abs(H)

1.2
1
0.8
0.6
0.4
0.2
0

(a)
>> a=[1 4 8 8];
>> b=[8];
>> n=10;
>> w=linspace(0,pi,n);
>> h=freqs(b,a,w);
>> plot(w,abs(h));
>> title('Magnitude Response');

5
w

10

>> ylabel('w');
>> xlabel('abs(h)')

Magnitude Response
1
0.9
0.8

0.7
0.6
0.5
0.4
0.3
0.2

0.5

1.5

2
abs(h)

(b)
>> a=[6 0 2];
>> b=[1 3 3 1];
>> n=10;
>> w=linspace(0,pi,n);
>> h=freqz(b,a,w);
>> stem(w,abs(h));

2.5

3.5

>> title('Magnitude Response');


>> ylabel('w');
>> xlabel('abs(h)')
Magnitude Response
1
0.9
0.8
0.7

0.6
0.5
0.4
0.3
0.2
0.1
0

0.5

1.5

2.5

abs(h)

Q3-4: Use Matlab to find the output of the system with input
x[n] = u[n] + 2u[n 3] u[n 6]
and impulse response
h[n] = u[n + 1] u[n 10] .
>> n=-3:1:12;
>> x=[zeros(1,3),(-1)*ones(1,3),ones(1,3),zeros(1,7)];
>> h=[0,0,ones(1,11),0,0,0];
>> y=conv(x,h);
>> subplot(3,1,1);
>> stem(n,x);
>> title('Input');

3.5

>> subplot(3,1,2);
>> stem(n,h);
>> title('Impluse Response');
>> subplot(3,1,3);
>> stem(y);
>> title('Output')
Input
1
0
-1
-4

-2

10

12

10

12

Impluse Response
1
0.5
0
-4

-2

Output
5
0
-5

10

>> omega=pi/3;
>> x=exp(j*omega*[0:53]);
>> h=[0.25 0.25 0.25 0.25];
>> y=conv(x,h);

15

20

25

30

35

>> subplot(2,1,1);
>> stem([0:56],real(y));
>> title('Real Value of Output');
>> xlabel('Time');
>> ylabel('Amplitude');
>> subplot(2,1,2);
>> stem([0:56],imag(y));
>> title('Imag Value of Output');
>> xlabel('Time');
>> ylabel('Amplitude')
Real Value of Output
0.4

Amplitude

0.2
0
-0.2
-0.4

10

20

10

20

30
40
Time
Imag Value of Output

50

60

50

60

Amplitude

0.5

-0.5

>> num=[2 1 0 -2 0];


>> den=[1 7 18 20 8];
>> [z,p,k]=tf2zp(num,den)
z=
0

30
Time

40

-0.6790 + 0.8392i
-0.6790 - 0.8392i
0.8581
p=
-2.0000 + 0.0000i
-2.0000 - 0.0000i
-2.0000
-1.0000
k=
2
>>sys=zpk(z,p,k)
Zero/pole/gain:
2 s (s-0.8581) (s^2 + 1.358s + 1.165)
------------------------------------(s+2)^3 (s+1)
>> pzmap(sys)
Pole-Zero Map
1
0.8
0.6

Imaginary Axis

0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-2.5

-2

-1.5

-1

-0.5
Real Axis

>> num=[1 2 3];

0.5

>> den=[1 3 2 0];


>> [r,p,k]=residue(num,den)
r=
1.5000
-2.0000
1.5000
p=
-2
-1
0
k=
[]

>> num=[1 0 0 1];


>> den=[1 0 2 0 1];
>> z=roots(num);
>> p=roots(den);
>> k=1;
>> syszpk=zpk(z,p,k)
Zero/pole/gain:
(s+1) (s^2 - s + 1)
------------------(s^2 + 1)^2
>> systf=tf(syszpk)
Transfer function:
s^3 + 6.661e-016 s^2 - 3.331e-016 s + 1
----------------------------------------------s^4 + 9.714e-017 s^3 + 2 s^2 + 9.629e-017 s + 1
>> pzmap(systf)

Pole-Zero Map
1.5

Imaginary Axis

0.5

-0.5

-1

-1.5
-1

-0.5

0
Real Axis

>> a=[-1 1;0 -2];


>> b=[3;-1];
>> c=[1 2];
>> d=[0];
>> sys=ss(a,b,c,d)
a=
x1 x2
x1 -1 1
x2 0 -2
b=
u1
x1 3
x2 -1

0.5

c=
x1 x2
y1 1 2
d=
u1
y1 0
Continuous-time model.
>> systf=tf(sys)
Transfer function:
s+3
------------s^2 + 3 s + 2

>> num=1;
>> den1=[1,10000/(2/5),10000^2];
>> den2=[1,10000/(1),10000^2];
>> den3=[1,10000/(200),10000^2];
>> w=[0:100:100000];
>> h1=freqs(num,den1,w);
>> subplot(3,1,1);
>> plot(w,20*log10(abs(h1)));
>> title('Q=2/5');
>> h2=freqs(num,den2,w);

>> subplot(3,1,2);
>> plot(w,20*log10(abs(h2)));
>> title('Q=1');
>> h3=freqs(num,den3,w);
>> subplot(3,1,3);
>> plot(w,20*log10(abs(h3)));
>> title('Q=200')
Q=2/5
-150
-200
-250

10
4

x 10
Q=1
-150

-200

10
4

x 10
Q=200
-100
-150
-200

10
4

x 10

>> t=0:0.01:10;
>> x=0.5*exp(-2*t);
>> num=1;
>> den=[1,0.5];
>> y=lsim(num,den,x,t);

>> plot(t,y);
>> title('Frequency Response');
>> xlabel('Time');
>> ylabel('Amplitude')
Frequency Response
0.16
0.14
0.12

Amplitude

0.1
0.08
0.06
0.04
0.02
0

>> num=[0 1 0 -1];


>> den=[1 0 0 -1];
>> n=0:100;
>> u=(1/3).^n;
>> y=filter(num,den,u);

5
Time

10

>> stem(y);
>> title('Output');
>> xlabel('n');
>> ylabel('Amplitude')
Output
1
0.8
0.6
0.4

Amplitude

0.2
0
-0.2
-0.4
-0.6
-0.8
-1

20

40

>> num=[1 -1 1];


>> den=[1 -3.5 -1.5 1];
>> [r,p,k]=residuez(num,den)
r=
0.7563

60
n

80

100

120

0.4506
-0.2069
p=
3.8239
-0.6984
0.3745
k=
[]

>> a=[1 -1.143 0.4128];


>> b=[0.0675 0.1349 0.675];

>> x1=ones(1,51);
>> zi=filtic(b,a,[0]);
>> y1=filter(b,a,x1,zi);
>> subplot(2,1,1);
>> stem(y1);
>> title('Step Response');
>> n=0:1:99;
>> x2=cos((pi*n)/5);
>> y2=filter(b,a,x2);
>> subplot(2,1,2);
>> stem(y2)
>> title('Response')
Step Response
4
3
2
1
0

10

20

30

40

50

60

Response
4
2
0
-2
-4

10

20

30

40

50

60

70

>> n=-4:1:14;
>> x=[0,0,2*ones(1,15),0,0];
>> h=(0.9.^n).*[zeros(1,6),ones(1,12),0];

80

90

100

>> y=conv(x,h);
>> subplot(3,1,1);
>> stem(n,x);
>> title('Input');
>> subplot(3,1,2);
>> stem(n,h);
>> title('Impulse Response');
>> subplot(3,1,3);
>> stem(y);
>> title('Output')
Input
2
1
0
-4

-2

10

12

14

10

12

14

Impulse Response
1
0.5
0
-4

-2

Output
20
10
0

10

15

20

25

30

35

40

You might also like