0% found this document useful (0 votes)
32 views

Lab - 2 Sol

The document contains MATLAB code for generating and plotting various signals including unit sample functions, unit step functions, unit ramp functions, sinusoidal signals with different amplitudes and phases, and the average power of a composite signal. The code includes user inputs for parameters, calculation of output signals based on the inputs, and plotting the signals.

Uploaded by

Onnu Randu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Lab - 2 Sol

The document contains MATLAB code for generating and plotting various signals including unit sample functions, unit step functions, unit ramp functions, sinusoidal signals with different amplitudes and phases, and the average power of a composite signal. The code includes user inputs for parameters, calculation of output signals based on the inputs, and plotting the signals.

Uploaded by

Onnu Randu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 18

PROGRAM

PLOT 1
clc;
clear all;
close all;
n= input('Please enter a value for span, n: ');
k= input('Please enter a value for delay, k: ');
t=-n:1:n;
y1=(t==0);
y2=((t-k)==0);
y1
y2
subplot(2,1,1)
stem (t,y1);
title(sprintf('Unit Sample Delta'));
xlabel ('n');
ylabel ('Amplitude');
subplot(2,1,2)
stem (t,y2);
title(sprintf('Unit Sample Delta (n- %d)',k));
xlabel ('n');
ylabel ('Amplitude');

OUTPUT
Please enter a value for span, n: 5
Please enter a value for delay, k: 3
y1 =
0 0 0 0 0 1 0 0 0
y2 =
0 0 0 0 0 0 0 0 1

PROGRAM 2
clc;
clear all;
close all;
n= input('Please enter a value for span, n: ');
k= input('Please enter a value for delay, k: ');
t=-n:1:n;
y1=(t>=0);
y2=((t-k)>=0);
y3=fliplr(y1);
y4=fliplr(y2);
y1
y2
y3
y4
subplot(2,2,1)
stem (t,y1);
title(sprintf('Unit Step'));
xlabel ('n');
ylabel ('Amplitude');
subplot(2,2,2)
stem (t,y2);
title(sprintf('Shifted Unit Step (n- %d)',k));
xlabel ('n');
ylabel ('Amplitude');
subplot(2,2,3)
stem (t,y3);
title(sprintf('Unit Step folded over n=0'));
xlabel ('n');
ylabel ('Amplitude');
subplot(2,2,4)
stem (t,y4);
title(sprintf('Shifted Unit Step folded over n=0'));
xlabel ('n');
ylabel ('Amplitude');

OUTPUT
Please enter a value for span, n: 10
Please enter a value for delay, k: 3
y1 =
Columns 1 through 11
0 0 0 0 0 0 0
Columns 12 through 21
1 1 1 1 1 1 1

y2 =
Columns 1 through 11
0 0 0 0 0 0 0
Columns 12 through 21
0 0 1 1 1 1 1

y3 =
Columns 1 through 11
1 1 1 1 1 1 1
Columns 12 through 21
0 0 0 0 0 0 0

y4 =
Columns 1 through 11
1 1 1 1 1 1 1
Columns 12 through 21
0 0 0 0 0 0 0

PLOT

PROGRAM 3

clc;
clear all;
close all;
n= input('Please enter a value for span, n: ');
k= input('Please enter a value for delay, k: ');
t=-n:1:n;
y1=(t);
y2=(t-k);
y3=fliplr(y1);
y4=fliplr(y2);
y1
y2
y3
y4
subplot(2,2,1)
stem (t,y1);
title(sprintf('Unit Ramp'));
xlabel ('n');
ylabel ('Amplitude');
subplot(2,2,2)
stem (t,y2);
title(sprintf('Shifted Unit Ramp (n- %d)',k));
xlabel ('n');
ylabel ('Amplitude');
subplot(2,2,3)
stem (t,y3);
title(sprintf('Unit Ramp folde d over n=0'));
xlabel ('n');
ylabel ('Amplitude');
subplot(2,2,4)
stem (t,y4);
title(sprintf('Shifted Unit Ramp folded over n=0'));
xlabel ('n');
ylabel ('Amplitude');

OUTPUT

Please enter a value for span, n: 10


Please enter a value for delay, k: 3
y1 =
Columns 1 through 11
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Columns 12 through 21
1 2 3 4 5 6 7 8 9 10

y2 =
Columns 1 through 11
-13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3
Columns 12 through 21
-2 -1 0 1 2 3 4 5 6 7
y3 =
Columns 1 through 11
10 9 8 7 6 5 4 3 2 1 0
Columns 12 through 21
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
y4 =
Columns 1 through 11
7 6 5 4 3 2 1 0 -1 -2 -3
Columns 12 through 21
-4 -5 -6 -7 -8 -9 -10 -11 -12 -13

PLOT

PROGRAM 4

clc;
clear all;
close all;
n=0:1000;
f=4*exp(i*1000*pi*n/100000);
figure(1)
plot(n,f);
title('Plot of function');
xlabel('n');
ylabel('f=4e^{j*w*n}');
figure(2)
subplot(2,2,1);
plot(n,real(f));
title('Real part of function');
xlabel('n');
ylabel('real(f)');
subplot(2,2,3);
plot(n,imag(f));
title('Imaginary part of function');
xlabel('n');
ylabel('imag(f)');
subplot(2,2,2);
plot(n,abs(f));
title('Magnitude plot of function');
xlabel('n');
ylabel('|f|');
subplot(2,2,4);
plot(n,phase(f));
title('Phase plot of function');
xlabel('n');
ylabel('angle(f)');

PLOT

PROGRAM 5

clc;
clear all;
close all;
T=input('Please enter the temperature in Farenheit scale : ');
K=(5/9*T-32)+273.15;
fprintf('The temperature in Kelvin scale is : %f \n',K);

OUTPUT
Please enter the temperature in Farenheit scale : 120
The temperature in Kelvin scale is : 307.816667

PROGRAM 6
clc;
clear all;
close all;
t=-10:0.1:10;
a=3+6*cos(t);
b=-2+9*sin(t);
c=7+2*cos(t-45);
d=8+6*sin(t+30);
plot(t,a,'o',t,b,'.',t,c,'*',t,d,'+');
xlabel('t');
ylabel('Functions: a(o), b(.), c(*), d(+)');
grid on

PLOT

PROGRAM 7
clc;
clear all;
close all;
n=0:1:1000;
sa=sin(2*pi*256.00*(0:0.000125:0.5));
re=sin(2*pi*288.00*(0:0.000125:0.5));
ga=sin(2*pi*320.00*(0:0.000125:0.5));
ma=sin(2*pi*341.33*(0:0.000125:0.5));
pa=sin(2*pi*384.00*(0:0.000125:0.5));
dha=sin(2*pi*426.666*(0:0.000125:0.5));
ni=sin(2*pi*480.00*(0:0.000125:0.5));
saa=sin(2*pi*512.00*(0:0.000125:0.5));
line=[sa re ga sa ga ni pa dha];
song=[line];
plot(song);
wavplay(song,8000);

PLOT

PROGRAM 8
clc;
clear all;
close all;
n=0:1:5;
x=(6-n);
subplot(2,2,1);
stem(n,x);
xlabel('n');
ylabel('Function - x(n)');
subplot(2,2,2);
stem(-n,x);
xlabel('n');
ylabel('Function - x(-n)');
subplot(2,2,3);
stem(-n+4,x);
xlabel('n');
ylabel('Function - y(n)=x(4-n)');

PLOT

PROGRAM 9
clc;
clear all;
close all;
n=0:1:64;
A=input('Please enter the amplitude of the signal: ');
phase=input('Please enter the phase of the signal: ');
PHI=pi*phase/180;
%converts degrees to radians%
x=A*cos(pi*n/32);
%function without phase change%
xp=A*cos(pi*n/32+PHI);
%function with phase change%
subplot(2,1,1);
stem((pi*n/32)*(180/pi),x) %value of function v/s angle in degrees%
title('Function without phase change');
xlabel('Angle in degrees');
ylabel('Amplitude');
grid on
subplot(2,1,2);
stem((pi*n/32)*(180/pi),xp); %value of function v/s angle in degrees%
title(sprintf('Function with phase change of %d degrees',phase));
xlabel('Angle in degrees');
ylabel('Amplitude');
grid on

OUTPUT
Please enter the amplitude of the signal: 2
Please enter the phase of the signal: 30

PLOT

PROGRAM 10
clc;
clear all;
close all;
n=0:1:15;
x=cos(pi*n/4)-sin(pi*n/8)+2*cos(pi*n/2);
y=sum(x*x');
y=y/16;
fprintf('The average power for the given signal is =%f\n',y);

OUTPUT
The average power for the given signal is =3.000000

PROGRAM 11

clc;
clear all;
close all;
for t=1:1:500;
for x=1:1:500
a=mod(t,2);
f1(t,x)=5*(sin(t*pi*x/50)/t);
if(a==1)
f(t,x)=5*(sin(t*pi*x/50)/t);
e lse
f(t,x)=0;
e nd
end
end
d=sum(f1);
c=sum(f);
subplot(2,1,1);
plot(c);
title('Square Wave');
subplot(2,1,2);
plot(d);
title('Sawtooth Wave');

PLOT

You might also like