Experiment NO:-6 Date:-
Program:-1(A)
AIM:- Write a MATLAB code for different types of Signal Generation
MATLAB CODE:-
% <----------Signal Generation------------>
clc;
close all;
%<------STEP SIGNAL----->
t=-1:0.001:1;
x=(t>=0);
subplot(3,2,1);
plot(t,x);
title('step signal is:');
ylabel('amplitude');
xlabel('time index');
%<------RAMP SIGNAL----->
t=-0:0.001:5;
x=t;
subplot(3,2,2);
plot(t,x);
1
title('ramp signal is:');
ylabel('amplitude');
xlabel('time index');
%<------SINE SIGNAL----->
x=0:0.005:20;
subplot(3,2,3);
plot(x,sin(x));
title('sine signal is:');
ylabel('amplitude');
xlabel('time index');
%<------COSINE SIGNAL---->
x=0:0.001:20;
subplot(3,2,4);
plot(x,cos(x));
title('cosine signal is:');
ylabel('amplitude');
xlabel('time index');
%<-----TRIANGLE PULSE----->
x=-0.5:0.001:0.5;
y = tripuls(x)
2
subplot(3,2,5)
plot(x,y)
title('triangle signal is:');
ylabel('amplitude');
xlabel('time index');
COMMAND WINDOW:
Columns 1 through 9
0 0.0020 0.0040 0.0060 0.0080 0.0100 0.0120 0.0140
0.0160
Columns 10 through 18
0.0180 0.0200 0.0220 0.0240 0.0260 0.0280 0.0300 0.0320
0.0340
Columns 19 through 27
0.0360 0.0380 0.0400 0.0420 0.0440 0.0460 0.0480 0.0500
0.0520
Columns 28 through 36
0.0540 0.0560 0.0580 0.0600 0.0620 0.0640 0.0660 0.0680
0.0700
3
FIGURE:-
4
Program:-1(B) Date:-
AIM:- Write a MATLAB code for different types of Signal Operations
MATLAB CODE:-
%<------SIGNAL OPERATIONS------>
clc;
close all;
figure(1);
%<-----Sine signal----->
x=0:0.001:10;
subplot(4,2,1);
plot(x,sin(x))
title('Sine signal:');
ylabel('amplitude');
xlabel('time index');
%<-----Compression of Sine signal----->
x=0:0.001:10;
subplot(4,2,2);
plot(x,sin(x*2))
5
title('Compressed sine signal:');
ylabel('amplitude');
xlabel('time index');
%<-----Expansion of Sine signal----->
x=0:0.001:20;
subplot(4,2,3);
plot(x,sin(x/2))
title('Expanded sine signal:');
ylabel('amplitude');
xlabel('time index');
%<-----Shifting of Sine signal----->
x=0:0.001:20;
subplot(4,2,4);
plot(x,sin(x+5))
title('Shifted sine signal:');
ylabel('amplitude');
xlabel('time index');
%<-----Cosine Signal----->
x=0:0.001:10;
subplot(4,2,5);
6
plot(x,cos(x))
title('Cosine signal:');
ylabel('amplitude');
xlabel('time index');
%<-----Compression of Cosine signal----->
x=0:0.001:10;
subplot(4,2,6);
plot(x,cos(x*2))
title('Compressed cosine signal:');
ylabel('amplitude');
xlabel('time index');
%<-----Expansion of Cosine signal----->
x=0:0.001:20;
subplot(4,2,7);
plot(x,cos(x/2))
title('Expanded cosine signal:');
ylabel('amplitude');
xlabel('time index');
%<-----Shifting of Cosine signal----->
x=0:0.001:20;
7
subplot(4,2,8);
plot(x,cos(x+5));
title('Shifted cosine signal:');
ylabel('amplitude');
xlabel('time index');
figure(2);
%<-----Step Signal----->
t=-5:0.001:5;
x=(t>=0)
subplot(4,2,1);
plot(t,x);
title('Step signal:');
ylabel('amplitude');
xlabel('time index');
%<-----Shifting of step signal----->
t=-5:0.001:5;
x=(t>=2)
subplot(4,2,2);
plot(t,x);
title('Shifted step signal:');
ylabel('amplitude');
8
xlabel('time index');
%<-----Ramp Signal----->
t=-5:0.001:5;
x=t
subplot(4,2,3);
plot(t,x);
title('Ramp signal:');
ylabel('amplitude');
xlabel('time index');
%<-----Shifting of Ramp signal----->
t=-5:0.001:5;
x=t/4
subplot(4,2,4);
plot(t,x);
title('Shifted ramp signal:');
ylabel('amplitude');
xlabel('time index'):
COMMAND WINDOW:
x=
Columns 1 through 9
-5.0000 -4.9900 -4.9800 -4.9700 -4.9600 -4.9500 -4.9400 -4.9300
9
-4.9200
Columns 10 through 18
-4.9100 -4.9000 -4.8900 -4.8800 -4.8700 -4.8600 -4.8500 -4.8400
-4.8300
Columns 19 through 27
-4.8200 -4.8100 -4.8000 -4.7900 -4.7800 -4.7700 -4.7600 -4.7500
-4.7400
Columns 28 through 36
-4.7300 -4.7200 -4.7100 -4.7000 -4.6900 -4.6800 -4.6700 -4.6600
-4.6500
FIGURE:
10
11
Experiment NO:-7 Date:-
Program:-2
AIM:- Write a MATLAB code for ENERGY & POWER Signal calculations
MATLAB CODE:-
%<--------Energy Calculations of Aperiodic siganl(Ramp)------->
%<------Ramp Signal------>
figure(1);
t=-1:0.01:1;
x=t;
subplot(3,1,1);
plot(t,x);
grid;
xlabel('t');
ylabel('x(t)');
%<------Energy of Ramp Signal----->
subplot(3,1,2);
plot(t,x.^2);
xlabel('t');
ylabel('x^2(t)');
12
title('Energy of Ramp signal')
axis([-1 1 -.2 1.5]);
grid;
%<-----Energy-Area under signal----->
subplot(3,1,3);
area(t,x.^2);
xlabel('t');
ylabel('x^2(t)');
title('The colored area gives the energy')
axis([-1 1 -.2 1.1]);
grid;
%<-------Power Calculations of Aperiodic Signal(Sine)----->
t=-2:0.01:2;
x1=sin(2*pi*t);
n=-40:40;
x2=sin(pi*(1/10)*n);
%<------Analog Sine Signal------>
figure(2);
subplot(2,1,1)
plot(t,x1);
grid on;
13
title('Analog & Discrete sine');
xlabel('t');
ylabel('x_1(t)');
%<------Descrete Sine Signal------>
subplot(2,1,2);
stem(n,x2);
grid on;
xlabel('n');
ylabel('x_2(n)');
%<------Power of Sine Signal------->
figure(3);
subplot(2,1,1);
plot(t,x1.^2); %Energy of X1
grid on;
hold on
% red lines marking 0 & 1
plot([0 0], [-.2 1.2],'g', [1 1], [-.2 1.2],'g');
hold off
axis([-2 2 -.2 1.2]);
title('Squared sines');
xlabel('t');
14
ylabel('x_1^2(t)');
subplot(2,1,2);
stem(n,x2.^2); %Energy of X2
grid on;
hold on;
% red lines marking 1 & 20
plot([1 1], [-.2 1.2],'r', [20 20], [-.2 1.2],'r');
hold off;
axis([-40 40 -.2 1.2]);
xlabel('n');
ylabel('x_2^2(n)');
%calculating power of one period of X1.
%disp('Power, one period:');
%P1 = (1/1)*sum(x1(1:1).^2)
%calculating power of one period of X2.
disp('Power, one period:');
P2 = (1/20)*sum(x2(1:20).^2)
15
COMMAND WINDOW:
Power, one period:
P2 =0.5000
FIGURE:-
16
17
Experiment NO:-8 Date:-
Program:-3
AIM:- Write a MATLAB code for Auto Co-Relation & Cross Co-Relation
MATLAB CODE:-
%-----------------------------------------
clc;
close all;
clear all;
%--------------------------------
a=[1 0 2]
b=[1 1 2]
c=xcorr(a,b)
d=xcorr(a,a)
e=xcorr(b,b)
%<-----------Sine Signal----------->
x=0:0.1:25;
subplot(3,2,1);
w=sin(x);
plot(x,w);grid;
title('sine signal is:');
18
ylabel('amplitude');
xlabel('time index');
%<------COSINE SIGNAL---->
subplot(3,2,2);
v=cos(x);
plot(x,v);grid
title('cosine signal is:');
ylabel('amplitude');
xlabel('time index');
%<-----Co-relations of signals----->
subplot(3,2,3);
p=xcorr(w,v)
plot(p);grid;
subplot(3,2,4);
q=xcorr(w,w)
plot(q);grid;
subplot(3,2,5);
r=xcorr(v,v)
plot(r);grid;
19
COMMAND WINDOW:
a= 1 0 2
b= 1 1 2
c= 2.0000 1.0000 5.0000 2.0000 2.0000
d= 2 0 5 0 2
e= 2 3 6 3 2
p=
Columns 1 through 9
-0.0000 0.0990 0.2941 0.5806 0.9519 1.3998 1.9142 2.4836
3.0954
Columns 10 through 18
3.7358 4.3899 5.0426 5.6780 6.2802 6.8334 7.3221 7.7314
8.0475
Columns 19 through 27
8.2572 8.3491 8.3131 8.1409 7.8259 7.3638 6.7524 5.9916
5.0837
20
Columns 28 through 36
q=
Columns 1 through 9
0.0000 -0.0132 -0.0493 -0.1175 -0.2265 -0.3838 -0.5964 -0.8698
-1.2083
Columns 10 through 18
-1.6150 -2.0912 -2.6370 -3.2506 -3.9287 -4.6665 -5.4574 -6.2934
-7.1651
Columns 19 through 27
-8.0616 -8.9709 -9.8800 -10.7748 -11.6407 -12.4625 -13.2248
-13.9121 -14.5089
r=
Columns 1 through 9
0.9912 1.9593 2.8848 3.7488 4.5335 5.2223 5.7998 6.2526
6.5690
Columns 10 through 18
6.7396 6.7572 6.6168 6.3162 5.8554 5.2372 4.4669 3.5524
2.5038
Columns 19 through 27
1.3338 0.0571 -1.3095 -2.7476 -4.2369 -5.7561 -7.2828 -8.7939
-10.2657
21
FIGURE:
22
Experiment NO:-9 Date:-
Program:-4
AIM:- Write a MATLAB code for finding Exponential and Trigonometric
Fourier Series
MATLAB CODE:-
%<-------To Find Exponential Fourier series Co-efficient------->
%M is the number of coefficients to be computed
clc;
close all;
clear all;
%-----------------------------------
T0=pi;
N0=40;
Ts=T0/N0;
M=10;
t=0:Ts:Ts*(N0-1);
t=t';
g=exp(-t/2);
g(1)=0.604;
Dn=fft(g)/40; % Dn is the Samples of g(t) over ONE PERIOD
[Dnangle,Dnmag]=cart2pol(real(Dn),imag(Dn));
23
k=0:39;
k=k';
figure(1);
subplot(2,1,1)
stem(k,Dnmag);grid
title('Amplitude spectrum');
ylabel('Dn');
xlabel('w');
subplot(2,1,2)
stem(k,Dnangle);grid
title('Phase spectrum');
ylabel('Angle');
xlabel('w');
%<-------To find Trigonometric Fourier Series coefficients------->
c0=Dnmag(1) % c0=Dnmag(1)=ao
Cn=2*Dnmag(2:M); % Cn for M= 2 to 10 (Because Cn(M=1)=c0
Amplitudes=[c0;Cn] % c0 to Cn total 10 amplitude
Angles=Dnangle(1:M); % c0 to Cn total 10 Angles (Because M=10)
Angles=Angles*(180/pi)
%<-------To plot the Fouries coefficients------>
24
k=0:9; % k= 0:length(Amplitudes)-1
k=k';
figure(2);
subplot(2,1,1)
stem(k,Amplitudes);grid;
title('Amplitude spectrum');
ylabel('Cn');
xlabel('w');
subplot(2,1,2)
stem(k,Angles);grid;
title('Phase spectrum');
ylabel('Angle');
xlabel('w');
COMMAND WINDOW:
Dn =
0.5043
0.0297 - 0.1184i
0.0078 - 0.0615i
0.0035 - 0.0410i
0.0020 - 0.0304i
0.0013 - 0.0238i
0.0009 - 0.0194i
0.0007 - 0.0161i
0.0006 - 0.0136i
0.0005 - 0.0116i
0.0004 - 0.0099i
25
0.0003 - 0.0085i
0.0003 - 0.0072i
0.0003 - 0.0061i
0.0002 - 0.0050i
0.0002 - 0.0041i
0.0002 - 0.0032i
0.0002 - 0.0024i
0.0002 - 0.0016i
0.0002 - 0.0008i
0.0002
0.0002 + 0.0008i
0.0002 + 0.0016i
0.0002 + 0.0024i
0.0002 + 0.0032i
0.0002 + 0.0041i
0.0002 + 0.0050i
0.0003 + 0.0061i
0.0003 + 0.0072i
0.0003 + 0.0085i
0.0004 + 0.0099i
0.0005 + 0.0116i
0.0006 + 0.0136i
0.0007 + 0.0161i
0.0009 + 0.0194i
0.0013 + 0.0238i
0.0020 + 0.0304i
0.0035 + 0.0410i
0.0078 + 0.0615i
0.0297 + 0.1184i
c0 =
0.5043
26
Amplitudes =
0.5043
0.2441
0.1241
0.0822
0.0608
0.0478
0.0388
0.0323
0.0272
0.0232
Angles =
0
-75.9040
-82.7544
-85.0529
-86.1739
-86.8169
-87.2159
-87.4704
-87.6286
-87.7151
27
FIGURE:
28
29
Experiment NO:-10 Date:-
Program:-5
AIM:- Write a MATLAB code for finding Fourier Transform of e(-2t) *u(t)
MATLAB CODE:-
% Fourier transform of e^(-2t)*u(t)
%-----------------------------------------
clc;
close all;
clear all;
%--------------------------------
Ts=1/12;
T0=3;
N0=T0/Ts;
t=0:Ts:Ts*(N0-1);
t=t';
g=Ts*exp(-2*t);
g(1)=Ts*0.5;
G=fft(g)
[Gp,Gm]=cart2pol(real(G),imag(G))
k=0:N0-1;
%k=k';
w=2*pi*k/T0;
30
subplot(2,1,1);
stem(w(1:32),Gm(1:32));
title('Amplitude Spectrum');
ylabel('|G(w)|');
xlabel('w');
subplot(2,1,2);
stem(w(1:32),Gp(1:32))
title('Phase spectrum');
ylabel('Angle');
xlabel('w');
COMMAND WINDOW:
G=
0.4998
0.2389 - 0.2479i
0.0937 - 0.1915i
0.0470 - 0.1405i
0.0280 - 0.1078i
0.0186 - 0.0858i
0.0134 - 0.0700i
0.0103 - 0.0581i
0.0082 - 0.0487i
0.0068 - 0.0410i
0.0058 - 0.0345i
0.0050 - 0.0288i
0.0045 - 0.0238i
0.0041 - 0.0192i
0.0038 - 0.0150i
31
0.0036 - 0.0111i
0.0035 - 0.0073i
0.0034 - 0.0036i
0.0034
0.0034 + 0.0036i
0.0035 + 0.0073i
0.0036 + 0.0111i
0.0038 + 0.0150i
0.0041 + 0.0192i
0.0045 + 0.0238i
0.0050 + 0.0288i
0.0058 + 0.0345i
0.0068 + 0.0410i
0.0082 + 0.0487i
0.0103 + 0.0581i
0.0134 + 0.0700i
0.0186 + 0.0858i
0.0280 + 0.1078i
0.0470 + 0.1405i
0.0937 + 0.1915i
0.2389 + 0.2479i
Gp =
0
-0.8038
-1.1160
-1.2483
-1.3169
-1.3568
-1.3812
-1.3962
-1.4044
-1.4073
-1.4053
32
-1.3979
-1.3840
-1.3607
-1.3224
-1.2561
-1.1270
-0.8186
0
0.8186
1.1270
1.2561
1.3224
1.3607
1.3840
1.3979
1.4053
1.4073
1.4044
1.3962
1.3812
1.3568
1.3169
1.2483
1.1160
0.8038
Gm =
0.4998
0.3443
0.2132
0.1482
0.1114
0.0878
0.0713
33
0.0590
0.0494
0.0415
0.0349
0.0292
0.0242
0.0197
0.0155
0.0116
0.0081
0.0049
0.0034
0.0049
0.0081
0.0116
0.0155
0.0197
0.0242
0.0292
0.0349
0.0415
0.0494
0.0590
0.0713
0.0878
0.1114
0.1482
0.2132
0.3443
34
FIGURE:
35