1. Generation of basic signals sine, cosine, ramp, step, impulse and exponential in continuous and discrete domains using user defined functions.

 Code:

//Continuous Waveform

clc;

clear all;

close;

function y=ramp(t)

m=length(t);

for i=1:m

if t(i)>=0 then

y(i)=t(i);

else

y(i)=0;

end

end

return

endfunction

function y=impulse(t)

m=length(t);

for i=1:m

if t(i)==0 then

y(i)=1;

else

y(i)=0;

end

end

return

endfunction

function y=unit(t)

m=length(t);

for i=1:m

if t(i)>=0 then

y(i)=1;

else

y(i)=0;

end

end

return

endfunction

function y=cosine(t)

y=0.5*(exp(%i*w*t)+exp(-1*%i*w*t));

return

endfunction

function y=sine(t)

y=0.5*-1*%i*(exp(%i*w*t)-exp(-1*%i*w*t));

return

endfunction

function y=exponential(b, t)

y=exp(b*t)

return

endfunction

t1=-5:0.1:20;

t2=-5:0.001:30;

t3=0:0.1:10;

figure(1);

subplot(2,3,1);

a=gca();

a.x_location="origin";

a.y_location="origin";

plot2d(t2,impulse(t2));

a.children.children.foreground=5

xtitle('continuous impulse function','time','amplitude');

figure(1);

subplot(2,3,2);

a=gca();

a.x_location="origin";

a.y_location="origin";

plot2d(t2,unit(t2));

a.children.children.foreground=5

xtitle('continuous unit step function','time','amplitude');

figure(1);

subplot(2,3,3);

a=gca();

a.x_location="origin";

a.y_location="origin";

plot2d(t2,ramp(t2));

a.children.children.foreground=5

xtitle('continuouse ramp function','time','amplitude');

figure(1);

subplot(2,3,4);

a=gca();

a.x_location="origin";

a.y_location="origin";

w=%pi/10;

plot2d(t2,cosine(t2));

a.children.children.foreground=3

xtitle('continuous cosine function','time','amplitude');

figure(1);

subplot(2,3,5);

a=gca();

a.x_location="origin";

a.y_location="origin";

w=%pi/10;

plot2d(t2,sine(t2));

a.children.children.foreground=3

xtitle('continuous sine function','time','amplitude');

figure(1);

subplot(2,3,6);

a=gca();

a.x_location="origin";

a.y_location="origin";

w=%pi/10;

b=2

plot2d(t3,exponential(b,t3));

a.children.children.foreground=3

xtitle('continuous exponential function','time','amplitude');






///Discrete waveform

clc;

clear all;

close;

function y=ramp(n)

m=length(n);

for i=1:m

if n(i)>=0 then

y(i)=n(i);

else

y(i)=0;

end

end

return

endfunction

function y=impulse(n)

m=length(n);

for i=1:m

if n(i)==0 then

y(i)=1;

else

y(i)=0;

end

end

return

endfunction

function y=unit(n)

m=length(n);

for i=1:m

if n(i)>=0 then

y(i)=1;

else

y(i)=0;

end

end

return

endfunction

function y=cosine(n)

y=0.5*(exp(%i*w*n)+exp(-1*%i*w*n));

return

endfunction

function y=sine(n)

y=0.5*-1*%i*(exp(%i*w*n)-exp(-1*%i*w*n));

return

endfunction

function y=exponential(b, n)

y=b.^(n)

return

endfunction

n1=-5:0.1:20;

n2=-5:30;

n3=0:10;

figure(1);

subplot(2,3,1);

a=gca();

a.x_location="origin";

a.y_location="origin";

plot2d3(n2,impulse(n2));

a.children.children.foreground=5

xtitle('discrete impulse function','time','amplitude');

figure(1);

subplot(2,3,2);

a=gca();

a.x_location="origin";

a.y_location="origin";

plot2d3(n2,unit(n2));

a.children.children.foreground=5

xtitle('discrete unit step function','time','amplitude');

figure(1);

subplot(2,3,3);

a=gca();

a.x_location="origin";

a.y_location="origin";

plot2d3(n2,ramp(n2));

a.children.children.foreground=5

xtitle('discrete ramp function','time','amplitude');

figure(1);

subplot(2,3,4);

a=gca();

a.x_location="origin";

a.y_location="origin";

w=%pi/10;

plot2d3(n2,cosine(n2));

a.children.children.foreground=3

xtitle('discrete cosine function','time','amplitude');

figure(1);

subplot(2,3,5);

a=gca();

a.x_location="origin";

a.y_location="origin";

w=%pi/10;

plot2d3(n2,sine(n2));

a.children.children.foreground=3

xtitle('discrete sine function','time','amplitude');

figure(1);

subplot(2,3,6);

a=gca();

a.x_location="origin";

a.y_location="origin";

w=%pi/10;

b=2

plot2d3(n3,exponential(b,n3));

a.children.children.foreground=3

xtitle('discrete exponential function','time','amplitude');

Comments