You are on page 1of 26

MATLAB CODE FOR STANDARD SIGNALS:

tmin=-5;
dt=0.01;
tmax=5;
t=tmin:dt:tmax;
x1=1;
x2=0;
x=x1.*(t==0)+x2.*(t~=0);
subplot(3,3,1);
plot(t,x);
xlabel('t');
ylabel('x(t)');
title('unit impulse');
x=1*(t>=0);
subplot(3,3,2);
plot(t,x);
xlabel('t');
ylabel('x(t)');
title('unit step');
x1=t;
x2=0;
x=x1.*(t>=0)+x2.*(t<0);
subplot(3,3,3);
plot(t,x);
xlabel('t');
ylabel('x(t)');
title('unit ramp');
A=0.4;
x1=(A*(t.^2))/2;
x2=0;
x=x1.*(t>=0)+x2.*(t<0);
subplot(3,3,4);
plot(t,x);
xlabel('t');
ylabel('x(t)');
title('parabolic');
T=2;
F=1/T;
x=sin(2*pi*F*t);
subplot(3,3,5);
plot(t,x);
xlabel('t');
ylabel('x(t)');
title('sinusoidal');
a=2;
x1=1-abs(t)/a;
x2=0;
x=x1.*(abs(t)<=a)+x2.*(abs(t)>a);
subplot(3,3,6);
plot(t,x);

xlabel('t');
ylabel('x(t)');
title('triangular');
x1=1;
x2=0;
x3=-1;
x=x1.*(t>0)+x2.*(t==0)+x3.*(t<0);
subplot(3,3,7);
plot(t,x);
xlabel('t');
ylabel('x(t)');
title('signum');
a=2;
x=exp(-a.*(t.^2));
subplot(3,3,8);
plot(t,x);
xlabel('t');
ylabel('x(t)');
title('gaussian');
x=sinc(t);
subplot(3,3,9);
plot(t,x);
xlabel('t');
ylabel('x(t)');
title('sinc signal');

unit impulse

unit step

0
t
parabolic

0
t
gaussian

0
t

0
t
sinc signal

0
t

x(t)

0.5
0
-5

0.5
0
-5

0
t
triangular

0
-1
-5

x(t)

x(t)

0
-5

x(t)

x(t)

0
t
signum

-1
-5

0
t
sinusoidal

x(t)

0
-5

0.5
0
-5

5
x(t)

0.5
0
-5

unit ramp

1
x(t)

x(t)

0
t

0
-1
-5

clc
t=0:0.01:4
x1=1.*(t>=0 & t<=2)
xa=1
xb=-1
x2=xa.*(t>=0 & t<=1)+xb.*(t>=1 & t<=2)
x3=convn(x1,x2)
n3=length(x3)
t1=0:1:n3-1;
subplot(3,1,1)
plot(t,x1)
grid
xlabel('t')
ylabel('x1(t)')
title('signal x1(t)')
subplot(3,1,2)
plot(t,x2)
grid
xlabel('t')
ylabel('x2(t)')
title('signal x2(t)')
subplot(3,1,3)
plot(t1,x3)
grid
xlabel('t/0.01')
ylabel('x3(t)/0.01')
title('signal x1(t)*x2(t)')

signal x1(t)
x1(t)

1
0.5
0

0.5

1.5

0.5

1.5

100

200

300

2
t
signal x2(t)

2.5

3.5

2
2.5
t
signal x1(t)*x2(t)

3.5

600

700

800

x2(t)

1
0

x3(t)/0.01

-1

100
0
-100

400
t/0.01

500

x=[1 2 3]
y=[4 5 6]
[r,lag]=xcorr(x,y)
stem(lag,r)
title('cross correlation of x(n) and y(n)')
xlabel('lag index I')
ylabel('Rxy')

cross correlation of x(n) and y(n)


35

30

25

Rxy

20

15

10

0
-2

-1.5

-1

-0.5

0
lag index I

0.5

1.5

tmin=-5;
dt=1;
tmax=5;
t=tmin:dt:tmax;
x1=1;
x2=0;
x=x1.*(t==0)+x2.*(t~=0);
subplot(3,3,1);
stem(t,x);
xlabel('t');
ylabel('x(t)');
title('unit impulse');

x=1*(t>=0);
subplot(3,3,2);
stem(t,x);
xlabel('t');
ylabel('x(t)');
title('unit step');
x1=t;
x2=0;
x=x1.*(t>=0)+x2.*(t<0);
subplot(3,3,3);
stem(t,x);
xlabel('t');
ylabel('x(t)');
title('unit ramp');
A=0.4;
x1=(A*(t.^2))/2;
x2=0;
x=x1.*(t>=0)+x2.*(t<0);
subplot(3,3,4);
stem(t,x);
xlabel('t');
ylabel('x(t)');
title('parabolic');
T=2;
F=1/T;
x=sin(2*pi*F*t);
subplot(3,3,5);
stem(t,x);
xlabel('t');
ylabel('x(t)');
title('sinusoidal');
a=2;
x1=1-abs(t)/a;
x2=0;
x=x1.*(abs(t)<=a)+x2.*(abs(t)>a);
subplot(3,3,6);
stem(t,x);
xlabel('t');

ylabel('x(t)');
title('triangular');
x1=1;
x2=0;
x3=-1;
x=x1.*(t>0)+x2.*(t==0)+x3.*(t<0);
subplot(3,3,7);
stem(t,x);
xlabel('t');
ylabel('x(t)');
title('signum');
a=2;
x=exp(-a.*(t.^2));
subplot(3,3,8);
stem(t,x);
xlabel('t');
ylabel('x(t)');
title('gaussian');
x=sinc(t);
subplot(3,3,9);
stem(t,x);
xlabel('t');
ylabel('x(t)');
title('sinc signal');

clc
t0=10
t=-10:0.01:10
x=10*sin(10*pi*t)
xsq=x.^2
E=trapz(t,xsq)
P=E/(2*t0)
disp(['energy, E=',num2str(E),'joules'])
disp(['power, P=',num2str(P),'joules'])

E=
1000.0000
P=
50.0000
energy, E=1000joules
power, P=50joules

t=-3:0.1:3
x1=exp(2*t)
x2=exp(-2*t)
xe=(x1+x2)/2
xo=(x1-x2)/2
subplot(2,2,1)
plot(t,x1)
grid
xlabel('t')
ylabel('x1(t)')
title('signal x(t)')
subplot(2,2,2)
plot(t,x2)
grid
xlabel('t')
ylabel('x2(t)')
title('signal x(-t)')
subplot(2,2,3)
plot(t,xe)
grid
xlabel('t')
ylabel('xe(t)')
title('even part of x(t)')
subplot(2,2,4)
plot(t,xo)
grid
xlabel('t')
ylabel('xo(t)')
title('odd part of x(t)')

signal x(t)

signal x(-t)

400

400
x2(t)

600

x1(t)

600

200

0
-4

200

-2

0
2
t
even part of x(t)

0
-4

300

0
2
t
odd part of x(t)

400
200
xo(t)

xe(t)

200

100

0
-4

-2

0
-200

-2

0
t

-400
-4

-2

0
t

clc
f=100
fs=1000
ts=1/fs
N=1024
n=[0:N-1]*ts
x=0.8*cos(2*pi*f*n)
figure
plot(n,x)
grid
axis([0 0.05 -1 1])
title('cosine signal of frequency f')
xlabel('time n(sec)')
ylabel('x(n)')
xk=fft(x,N)
k=0:N-1
figure
xmag=abs(xk)
subplot(2,1,1)
plot(k,xmag)
grid
title('magnitude of fourier transform')
xlabel('frequency index k')
ylabel('magnitude')
subplot(2,1,2)
plot(k,angle(xk))
grid
title('phase of fourier transform')
xlabel('frequency index k')
ylabel('phase')

magnitude of fourier transform

magnitude

400
300
200
100
0

200

400

600
800
frequency index k
phase of fourier transform

200

400

1000

1200

1000

1200

phase

1
0
-1
-2

600
800
frequency index k

clc
N=3
t=0:0.0001:1
y=square(2*pi*t)
%plot(t,y,'r','linewidth',2)%
axis([0 1 -1.5 1.5])
hold
sq=zeros(size(t))
for n=1:2:N
sq=(sq+(4/(pi*n)*sin(2*pi*n*t))
end
plot(t,sq)
grid
xlabel('t')
ylabel('sq(t)')
title('synthesized squarewave using fourier')
synthesized squarewave using fourier
1.5

sq(t)

0.5

-0.5

-1

-1.5

0.1

0.2

0.3

0.4

0.5
t

0.6

0.7

0.8

0.9

clc
x1=randn(1,5000)
x2=randn(1,5000)
figure
plot(x1,x2,'.')
title('scatter plot of gaussian distributed random numbers')
x1=rand(1,5000)
x2=rand(1,5000)
figure
plot(x1,x2,'.')
title('scatter plot of gaussian distributed random numbers')
x3=rand(1,100000)
figure
subplot(2,1,1);hist(x3)
title('uniform distribution')
y=randn(1,100000)
subplot(2,1,2);hist(y)
title('uniform distribution')
ymu=mean(y)
ymsq=sum(y.^2)/length(y)
ysigma=std(y)
yvar=var(y)
yskew=skewness(y)
ykurt=kurtosis(y)
scatter plot of gaussian distributed random numbers
4
3
2
1
0
-1
-2
-3
-4
-4

-3

-2

-1

scatter plot of gaussian distributed random numbers


1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

0.7

0.8

0.9

uniform distribution
15000

10000

5000

0.1

0.2

0.3

0.4

0.5

0.6

uniform distribution

x 10

3
2
1
0
-5

-4

-3

-2

-1

clc;
num=[1 0.75 0.5 -0.25];
den=1;
N=10;
n=0:N-1;
imp=[1 zeros(1,N-1)];
h=filter(num,den,imp);
disp('the impulse response of LTI system is');
disp(h);
stem(n,h);
xlabel('time n');
ylabel('h(n)');
title('impulse response of LTI system') ;

impulse response of LTI system


1

0.8

0.6

h(n)

0.4

0.2

-0.2

-0.4

time n

x1=input('type the sampels of x1')


x2=input('type the sampels of x2')
if(length(x1)~=length(x2))
disp('ERROR:lengths of x1 & x2 are different')
return
end
h=input('type the sampels of h')
N=length(x1)+length(x2)-1
disp('length of output signal will be')
disp(N)
a1=input('the scale factor a1 is')
a2=input('the scale factor a2 is')
x=a1*x1+a2*x2

y=convn(x,h)
y1=convn(x1,h)
y1s=a1*y1;
y2=convn(x2,h)
y2s=a2*y2
z=y1s+y2s
disp('input signal x1 is')
disp(x1)
disp('input signal x2 is')
disp(x2)
disp('output signal y is')
disp(y)
disp('output signal z is')
disp(z)
if(y==z)
disp('hence the LTI system is linear')
end

clc;
num=[1 -2 1];
den=[1 6 11 16];
h=tf(num,den);
[p,z]=pzmap(h);
disp('zeros are at');
disp(z);
disp('poles are at');
disp(p);
figure;
pzmap(h);

Pole-Zero Map
2

1.5

Imaginary Axis

0.5

-0.5

-1

-1.5

-2
-5

-4

-3

-2
Real Axis

-1

clc
t=linspace(-30,30,1000)
b=0.2
T=1
h1=(sin(pi*t/T))./(pi*t/T)
h2=(cos(pi*t/T))./(1-(2*b*t/T).^2)
h=h1.*h2
figure(1)
plot(t,h)
grid

0.8

0.6

0.4

0.2

-0.2

-0.4
-30

-20

-10

10

clc;
x=[0,2+j*4,-3+j*2,5-j*1,-2-j*4,-j*3,0];
n=-3:3;
subplot(2,1,1);
stem(n,real(x));
title('realpart of complex signal x(n)');
xlabel('n');
ylabel('magnitude of real[x(n)]');
subplot(2,1,2);
stem(n,imag(x));
title('imaginarypart of complex signal x(n)');
xlabel('n');
ylabel('magnitude of imag[x(n)]');
end;

20

30

magnitude of imag[x(n)]

magnitude of real[x(n)]

realpart of complex signal x(n)


5

-5
-3

-2

-1

0
1
n
imaginarypart of complex signal x(n)

4
2
0
-2
-4
-3

-2

N=20;
n=-N:N;
w=pi/10;
r=2;
xR=(r.^n).*cos(w*n);
subplot(2,1,1);
stem(n,xR);
xlabel('n');
ylabel('magnitude');
title('real part');
xI=(r.^n).*sin(w*n);
subplot(2,1,2);
stem(n,xI);
xlabel('n');
ylabel('magnitude');
title('imaginary part');

-1

0
n

magnitude

15

real part

x 10

10
5
0
-5
-20

-15

-10

-5

-15

-10

-5

magnitude

x 10

0
5
n
imaginary part

10

15

20

10

15

20

-1

-2
-20

0
n

clc
num=[1 -2.4 2.88]
den=[1 -0.8 0.64]
N=10
n=0:N-1
u=ones(1,N)
s=filter(num,den,u)
disp('the step response of LTI system is')
disp(s)
stem(n,s)
xlabel('time n')
ylabel('s(n)')
title('step response of LTI system')

step response of LTI system


3
2.5
2

s(n)

1.5
1
0.5
0
-0.5
-1

5
time n

clc
t=0:0.01:30
x1=sin(2*pi*1/3*t)
x2=sin(2*pi*1/5*t)
y=x1+x2
z=x1.*x2
subplot(4,1,1)
plot(t,x1)
grid
xlabel('t(sec)')
ylabel('x1(t)')
title('sine wave of freq 1/3');
subplot(4,1,2)
plot(t,x2)
grid
xlabel('t(sec)')
ylabel('x2(t)')
title('sine wave of freq 1/5');
subplot(4,1,3)
plot(t,y)
grid
xlabel('t(sec)')
ylabel('y(t)')
title('sum');
subplot(4,1,4)
plot(t,z)

grid
xlabel('t(sec)')
ylabel('z(t)')
title('product');

z(t)

y(t)

x2(t)

x1(t)

sine wave of freq 1/3


1
0
-1

1
0
-1

2
0
-2

1
0
-1

10

10

15
t(sec)
sum

10

10

syms s
T=1
F1=1-exp(-T*s/2);
F2=s*(1+exp(-T*s/2));
F=F1/F2;
f=ilaplace(F)
pretty(simplify(f))
ezplot(f)

15
20
t(sec)
sine wave of freq 1/5

25

30

20

25

30

15
t(sec)
product

20

25

30

15
t(sec)

20

25

30

(-1)floor(2 t)

0.5

-0.5

-1

-6

-4

-2

0
t

clc
fs=100
t=0:1/fs:10
x=sin(2*pi*15*t)+sin(2*pi*30*t)
N=512
X=fft(x,N)
f=fs*(0:N-1)/N
power=X.*conj(X)/N
figure(1)
plot(f,power)
title('power spectrum through fast fourier transform')
xlabel('frequency')
ylabel('power')
figure(2)
rxx=xcorr(x,x)
sxx=fft(rxx,512)
plot(f,abs(sxx))
title('fast fourier transform of auto correlation')
xlabel('frequevcy f')
ylabel('abs(sxx)')

power spectrum through fast fourier transform


120

100

power

80

60

40

20

10

20

3.5

30

40

50
60
frequency

70

80

90

100

80

90

100

fast fourier transform of auto correlation

x 10

abs(sxx)

2.5

1.5

0.5

10

20

30

40

50
60
frequevcy f

70

You might also like