You are on page 1of 44

1

Matlab

Course Code:ECE 206

Course Title:Signals and Systems Sessional


2

Index
S.No. Name of the Experiments Page No. Remarks
1 Matlab code for Generation of unit impulse
signal with delay and advance
2 Matlab code for Generation of unit step
signal with delay and advance
3 Matlab code for Generation of unit ramp •
signal with delay and advance
4 y=sin(t)
5 y=4*sin(t)^2

-2*pi<t<2*pi
6 y=4*sin(ωt)+cos(ωt)

-2*pi<t<2*pi
7 y=cos(t)
8 y=t==0

-2*pi<t<2*pi
9 y=4*cos(t)^2

-2*pi<t<2*pi
10 Step =t>=0

t=-1:0.109:1
11 y=t*ramp

ramp=t>=0

t=-1:0.1:1
12 When x(n)={1 ; n >= 0 , 0 ; n <0
3

13 x(n-2)={1 ; n>=0 , 0 ; n<0


14 x(n+2)={1 ; n>=0 , 0 ; n<0
15 x(n)={n ; n>0 , 0 ; n=<0
16 x(n-2)={n ; n>0 , 0 ; n=<0
17 x(n+2)={n ; n>0 , 0 ; n=<0
18 x(n)={1 ; n=0 , 0 ; otherwise
19 x(n-5)={1 ; n=0 , 0 ; otherwise
20 x(n+5)={1 ; n=0 , 0 ; otherwise
21 x(n+8)={1 ; n=0 , 0 ; otherwise
22 x(n-3)={1 ; n=0 , 0 ; otherwise
23 x(n-3)={n ; n>0 , 0 ; n =< 0
24 x(n+4)={n ; n>0 , 0 ; n =< 0
25 x(n+3)={1 ; n>=0 , 0 ; n < 0
26 x(n-3)={1 ; n>=0 , 0 ; n < 0
27 x(n)={1 ; n=0 , 0 ; n ≠ 0
28 x(n-2)={1 ; n=2 , 0 ; n ≠ 2
29 x(n)={1 ; n >=0 , 0 ; n < 0
30 x(n-2) = {1 ; n >= 2 , 0 ; n < 2
31 x(n-2) = {n-2 ; n >= 2 , 0 ; n < 2
32 Generating a sine wave
33 Generating a cosine wave
34 Generating a sine wave add to the cosine
wave
35 Sawtooth generates a sawtooth wave with
peaks at +-1 and a period of 2π .An
optional width parameter specifiers a
fractional multiple of 2π at which the
signals maximum occurs.
36 To generate 1.5s of a 50Hz sawtooth wave
4

with a sample rate of 10kHz and plot 0.2s


of the generate waveform.
37 Generating Exponential signal.
38 Generating a unit ramp.

39 Generates 40 samples of a unit step


signal,u(n).
40 Generating 64 samples of a unit impulse
signal.
5

01.Experiment Name:Matlab code for Generation of unit impulse signal with


delay and advance.
Matlab Code:
%Generation of unit impulse signal with delay and advance
clc;
clf;
clear all;
close all;
N=20;
n=-N:1:N;
subplot(3,1,1);
x=[zeros(1,N) 1 zeros(1,N)];
stem(n,x);
grid;
title("Unit impulse signal");
xlabel("Discrete time ---N>>>");
ylabel("Amplitude");
subplot(3,1,2);
x=[zeros(1,N+5) 1 zeros(1,N-5)];
stem(n,x);
grid;
title("Unit impulse delay signal");
xlabel("Discrete time ---N>>>");
ylabel("Amplitude");
subplot(3,1,3);
x=[zeros(1,N-5) 1 zeros(1,N+5)];
stem(n,x);
grid;
title("Unit impulse advance signal");
xlabel("Discrete time ---N>>>");
ylabel("Amplitude");
6

Output:

02.Experiment Name:Matlab code for Generation of unit step signal with delay
and advance.
Matlab Code:
%Generation of unit step signal with delay and advance
clc;
clf;
clear all;
close all;
N=20;
n=-N:1:N;
subplot(3,1,1);
x=[zeros(1,N) 1 ones(1,N)];
stem(n,x);
grid;
title("Unit step signal");
xlabel("Discrete time ---N>>>");
ylabel("Amplitude");
subplot(3,1,2);
x=[zeros(1,N+5) 1 ones(1,N-5)];
7

stem(n,x);
grid;
title("Unit step delay signal");
xlabel("Discrete time ---N>>>");
ylabel("Amplitude");
subplot(3,1,3);
x=[zeros(1,N-5) 1 ones(1,N+5)];
stem(n,x);
grid;
title("Unit step advance signal");
xlabel("Discrete time ---N>>>");
ylabel("Amplitude");

Output:

03.Experiment Name:Matlab code for Generation of unit ramp signal with delay
and advance.
Matlab Code:
%Generation of unit ramp signal with delay and advance
clc;
clf;
8

clear all;
close all;
N=20;
n=-N:1:N;
subplot(3,1,1);
x=n.*(n>=0);
stem(n,x);
grid;
title("Unit ramp signal");
xlabel("Discrete time ---N>>>");
ylabel("Amplitude");
subplot(3,1,2);
x=n.*(n>=5);
stem(n,x);
grid;
title("Unit ramp delay signal");
xlabel("Discrete time ---N>>>");
ylabel("Amplitude");
subplot(3,1,3);
x=n.*(n>=-5);
stem(n,x);
grid;
title("Unit ramp advance signal");
xlabel("Discrete time ---N>>>");
ylabel("Amplitude");

Output:
9

04.Experiment Name:y=sin(t).
Matlab Code:
clc;
clf;
clear all;
close all;
t=-pi:(pi/16):pi;
y=sin(t);
plot(t,y);
hold on;
stem(t,y);
hold on;
plot(t,y);
grid;
title('sin wave');
xlabel('Continous time(t)');
ylabel('Amplitude')

Output:
10

05.Experiment Name:y=4*sin(t)^2 , -2*pi<t<2*pi


Matlab Code:
clc;
clf;
clear all;
close all;
t= -2*pi:(pi/16):2*pi;
y=4*sin(t).^2;
plot(t,y);
hold on;
grid;
title('4*sin(t)^2');
xlabel('Continous time(t)');
ylabel('Amplitude')

Output:
11

06.Experiment Name:y=4*sin(ωt)+cos(ωt) , -2*pi<t<2*pi


Matlab Code:
clc;
clf;
clear all;
close all;
t=-2*pi:(pi/5):2*pi;
y=4*sin(2*pi*5*t)+cos(2*pi*5*t);
plot(t,y);
hold on;
stem(t,y);
grid;
title('sin and cosine wave');
xlabel('Continous time(t)');
ylabel('Amplitude')

Output:
12

07.Experiment Name:y=cos(t)
Matlab Code:

clc;
clf;
clear all;
close all;
t=-pi:(pi/5):2*pi;
y=cos(t);
plot(t,y);
hold on;
stem(t,y);
grid;
title('cosine Wave');
xlabel('Continous time(t)');
ylabel('Amplitude')

Output:
13

08.Experiment Name:y=t==0 , -2*pi<t<2*pi


Matlab Code:
clc;
clf;
clear all;
close all;
t=-2*pi:(pi/5):2*pi;
y=t==0;
plot(t,y);
hold on;
grid;
title('unit impulse signal');
xlabel('Continous time(t)');
ylabel('Amplitude')

Output:
14

09.Experiment Name:y=4*cos(t)^2 , -2*pi<t<2*pi


Matlab Code:
clc;
clf;
clear all;
close all;
t= -2*pi:(pi/10):2*pi;
y=4*cos(t).^2;
plot(t,y);
hold on;
grid;
title('4*cos(t)^2');
xlabel('Continous time(t)');
ylabel('Amplitude')

Output:
15

10.Experiment Name:step =t>=0 , t=-1:0.109:1


Matlab Code:
clc;
clf;
clear all;
close all;
t=-1:0.109:1;
step=t>=0;
plot(t,step);
hold on;
stem(t,step);
grid;
title('Unit step');
xlabel('Continuous time(t)');
ylabel('Amplitude');

Output:
16

11.Experiment Name:y=t*ramp , ramp=t>=0 , t=-1:0.1:1


Matlab Code:

clc;
clf;
clear all;
close all;
t=-1:0.109:1;
ramp=t>=0;
y=t.*ramp;
plot(t,y);
hold on;
stem(t,y);
grid;
title('Unit ramp');
xlabel('Continuous time(t)');
ylabel('Amplitude');

Output:
17

12.Experiment Name:When x(n)={1 ; n >= 0 , 0 ; n <0


Matlab Code:
clc;
clf;
clear all;
close all;
n=-20:20;
x(n>=0)=1;
title('Straight line');
stem(n,x)

Output:
18

13.Experiment Name:x(n-2)={1 ; n>=0 , 0 ; n<0


Matlab Code:
clc;
clf;
clear all;
close all;
n=-20:20;
x((n-2)>=0)=1;
stem(n,x);
title('Straight line');

Output:
19

14.Experiment Name:x(n+2)={1 ; n>=0 , 0 ; n<0


Matlab Code:
clc;
clf;
clear all;
close all;
n=-20:20;
x((n+2)>=0)=1;
stem(n,x);
title('Straight line');
xlabel('n');
ylabel('x');

Output:
20

15.Experiment Name:x(n)={n ; n>0 , 0 ; n=<0


Matlab Code:

clc;
clf;
clear all;
close all;
n=-20:20;
x=n.*(n>0);
stem(n,x);
title('Mid line');

Output:
21

16.Experiment Name:x(n-2)={n ; n>0 , 0 ; n=<0


Matlab Code:
clc;
clf;
clear all;
close all;
n=-20:20;
x=n.*((n-2)>0);
stem(n,x);

Output:
22

17.Experiment Name:x(n+2)={n ; n>0 , 0 ; n=<0


Matlab Code:

clc;
clf;
clear all;
close all;
n=-20:20;
x=n.*((n+2)>0);
stem(n,x);

Output:
23

18.Experiment Name:x(n)={1 ; n=0 , 0 ; otherwise


Matlab Code:
clc;
clf;
clear all;
close all;
n=-20:20;
N=40;
x=[zeros(1,(N/2)),1,zeros(1,(N/2))];
stem(n,x);

Output:
24

19.Experiment Name:x(n-5)={1 ; n=0 , 0 ; otherwise


Matlab Code:
clc;
clf;
clear all;
close all;
n=-20:20;
N=40;
x=[zeros(1,(N/2-5)),1,zeros(1,(N/2+5))];
stem(n,x);

Output:
25

20.Experiment Name:x(n+5)={1 ; n=0 , 0 ; otherwise


Matlab Code:

clc;
clf;
clear all;
close all;
n=-20:20;
N=40;
x=[zeros(1,(N/2+5)),1,zeros(1,(N/2-5))];
stem(n,x);

Output:
26

21.Experiment Name:x(n+8)={1 ; n=0 , 0 ; otherwise


Matlab Code:
clc;
clf;
clear all;
close all;
n=-20:20;
N=40;
x=[zeros(1,(N/2+8)),1,zeros(1,(N/2-8))];
stem(n,x);

Output:
27

22.Experiment Name:x(n-3)={1 ; n=0 , 0 ; otherwise


Matlab Code:
clc;
clf;
clear all;
close
n=-
28

23.Experiment Name:x(n-3)={n ; n>0 , 0 ; n =< 0


Matlab Code:
clc;
clf;
clear all;
close all;
n=-20:20;
x=n.*((n-3)>0);
stem(n,x);

Output:

24.Experiment Name:x(n+4)={n ; n>0 , 0 ; n =< 0


Matlab Code:
clc;
clf;
clear all;
close all;
n=-20:20;
x=n.*((n+4)>0);
29

stem(n,x);

Output:

25.Experiment Name:x(n+3)={1 ; n>=0 , 0 ; n < 0


Matlab Code:
clc;
clf;
clear all;
close all;
n=-20:20;
x((n+3)>=0)=1;
stem(n,x);

Output:
30

26.Experiment Name:x(n-3)={1 ; n>=0 , 0 ; n < 0


Matlab Code:
clc;
clf;
clear all;
close
n=-
x((n-
31

27.Experiment Name:x(n)={1 ; n=0 , 0 ; n ≠ 0


Matlab Code:
clc;
clf;
clear all;
close all;
n=-5:5;
x=(n==0);
stem(n,x);

Output:

28.Experiment Name:x(n-2)={1 ; n=2 , 0 ; n ≠ 2


Matlab Code:
clc;
clf;
clear all;
close all;
n=-5:5;
x=((n-2)==0);
32

stem(n,x);

Output:

29.Experiment Name:x(n)={1 ; n >=0 , 0 ; n < 0


Matlab Code:
clc;
clf;
clear all;
close all;
n=-5:5;
x=(n>=0);
stem(n,x);

Output:
33

30.Experiment Name:x(n-2) = {1 ; n >= 2 , 0 ; n < 2


Matlab Code:
clc;
clf;
clear all;
close all;
n=-5:5;
x=((n-2)>=0);
stem(n,x);

Output:
34

31.Experiment Name:x(n-2) = {n-2 ; n >= 2 , 0 ; n < 2


Matlab Code:
clc;
clf;
clear all;
close all;
n=-20:20;
x=(n-2).*((n-2)>=0);
stem(n,x);

Output:
35

32.Experiment Name:Generating a sine wave


Matlab Code:
%continous sinewave
clc;
clf;
clear all;
close all;
t=0:0.1:10;
y=2*sin(2*pi*0.5*t);
plot(t,y)
title("continous sinewave");
xlabel("time");
ylabel("amplitude");
grid on;

Output:
36

33.Experiment Name:Generating a cosine wave


Matlab Code:
%continous cosinewave
clc;
clf;
clear all;
close all;
t=0:0.1:10;
y=2*cos(2*pi*0.5*t);
plot(t,y)
title("continous cosinewave");
xlabel("time");
ylabel("amplitude");
grid on;

Output:
37

34.Experiment Name:Generating a sine wave add to the cosine wave


Matlab Code:
%continous sinewave
clc;
clf;
clear all;
close all;
t=0:0.1:10;
y=2*sin(2*pi*0.5*t);
plot(t,y,'r');
hold on;
%continous cosinewave
t=0:0.1:10;
y=2*cos(2*pi*0.5*t);
plot(t,y,'b');
xlabel("time");
ylabel("amplitude");
grid on;
38

Output:

35.Experiment Name:Sawtooth generates a sawtooth wave with peaks at +-1 and


a period of 2π .An optional width parameter specifiers a fractional multiple of
2π at which the signals maximum occurs.
Matlab Code:
%sawtooth wave
clc;
clf;
clear all;
close all;
t=0:0.1:30;
x=sawtooth(t);
plot(t,x)
title(" Sawtooth wave");
xlabel("Time index");
ylabel("Amplitude");
grid on;
39

Output:

36.Experiment Name:o generate 1.5s of a 50Hz sawtooth wave with a sample


rate of 10kHz and plot 0.2s of the generate waveform.
Matlab Code:
clc;
clf;
close all;
clear all;
fs = 10000;
t = 0:1/fs:1.5;
x = sawtooth(2*pi*50*t);
plot(t,x),axis([0 0.2 -1.2 1.2]);
xlabel('Time index');
40

ylabel('Amplitude');
title('50 Hz sawtooth wave');
grid;

Output:

37.Experiment Name:Generating Exponential signal.


Matlab Code:
%Generate Exponential signal x(t)=exp(-1*t)
clc;
clf;
clear all;
close all;
41

t=0:0.1:40;
x=exp(-0.1*t);
plot(t,x);
grid;
title("Exponential Signal");
xlabel("Time Index[ms]");
ylabel("Amplitude");

Output:

38.Experiment Name:Generating a unit ramp.


Matlab Code:
%Generate a unit ramp
clc;
clf;
clear all;
close all;
t=-1:0.01:1;
r=t.*(t>=0);
plot(t,r);
42

grid;
title("Unit ramp");
xlabel("t");
ylabel("r");

Output:

39.Experiment Name:Generates 40 samples of a unit step signal,u(n).


Matlab Code:
clc;
clf;
clear all;
close all;
N=40;
n=-20:20;
u=[zeros(1,(N/2)+1),ones(1,(N/2))];
plot(n,u);
grid;
title("A unit step signal");
xlabel("Sample Numbers");
43

ylabel("Amplitude");

Output:

40.Experiment Name:Generating 64 samples of a unit impulse signal.


Matlab Code:
clc;
clf;
clear all;
close all;
N=64;
n=-(N/2):((N/2)-1);
x=zeros(1,N);
x((N/2)+1)=1.0;
plot(n,x);
grid;
title("A unit impulse signal");
xlabel("Sample Numbers");
ylabel("Amplitude");
44

Output:

You might also like