Scilab File
Scilab File
(ETEC-356)
Semester: 6th
Group:E8
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');
Output:
///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');
Output:
Experiment – 2
Aim : To find linear and circular convolution.
Code:
clc;
clear all;
close;
//linear convolution
X_lin=[x,zeros(1,nh-1)];
//append zeros to make length equal to nx+nh-1
H_lin=[h,zeros(1,(nx-1))];
//append zeros to make length equal to nx+nh-1
for i=1:N
for j=1:N
if (i<j) then
X2_lin(i,j)=0;
else
X2_lin(i,j)=X_lin(i-j+1);
end
end
end
plot2d3(n1,x);
a.children.children.foreground=5;
xtitle('input signal x(n)','n','input');
subplot(2,2,2);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n2,h);
a.children.children.foreground=5;
xtitle('input signal h(n)','n','input');
subplot(2,2,3);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n,Y_lin);
a.children.children.foreground=5;
xtitle('Linearly convolved signal','n','Y(n)');
//Circular Convolution
M=max(nx,nh);
X_circ=[x,zeros(1,M-nx)];
H_circ=[h,zeros(1,M-nh)];
for i=1:M
for j=1:M
if (i-j+1)>0 &(i-j+1)<M+1 then
X2_circ(i,j)=X_circ(i-j+1);
elseif (i<j) then
X2_circ(i,j)=X_circ(i-j+1+M)
end
end
end
Y_circ=X2_circ*H_circ';
// Matrix multiplication to obtain circular convolution
m=0:M-1;
subplot(2,2,4);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(m,Y_circ);
a.children.children.foreground=5;
xtitle('Circilarly convolved signal','n','Y(n)');
Output:
Experiment – 3
Aim : To find linear and circular cross convolution.
Code:
clc;
clear all;
close;
//linear convolution
y=mtlb_fliplr(y);
nx=length(x);
ny=length(y);
N=nx+ny-1;
n1=0:nx-1;
n2=0:ny-1;
n=0:nx+ny-2;
X_lin=[x,zeros(1,ny-1)];
//append zeros to make length equal to nx+nh-1
Y_lin=[y,zeros(1,(nx-1))];
//append zeros to make length equal to nx+nh-1
for i=1:N
for j=1:N
if (i<j) then
Y2_lin(i,j)=0;
else
Y2_lin(i,j)=Y_lin(i-j+1);
end
end
end
rxy_lin=Y2_lin*X_lin';
// matrix multiplication to obtain linear correlation
figure(1);
subplot(2,2,1);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n1,x);
a.children.children.foreground=5;
xtitle('input signal x(n)','n','input');
subplot(2,2,2); a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n2,y);
a.children.children.foreground=5;
xtitle('input signal y(n)','n','input');
subplot(2,2,3);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n,rxy_lin);
a.children.children.foreground=5;
xtitle('Linearly correlated signal','n','rxy(l)');
//Circular Correlation
M=max(nx,ny);
if nx<M then
X_circ=[x,zeros(1,M-nx)];
else
X_circ=x;
end
if ny<M then
Y_circ=[y,zeros(1,M-ny)];
else
Y_circ=y;
end
//Formation of toeplitz matrix
for i=1:M
for j=1:M
if (i-j+1)>0 &(i-j+1)<M+1 then
Y2_circ(i,j)=Y_circ(i-j+1);
elseif (i<j) then
Y2_circ(i,j)=Y_circ(i-j+1+M)
end
end
end
rxy_circ=Y2_circ*X_circ';
// Matrix multiplication to obtain circular correlation
m=0:M-1;
subplot(2,2,4);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(m,Y_circ);
a.children.children.foreground=5;
xtitle('Circilarly convolved signal','n','Y(n)');
Output
Experiment – 4
Aim: To perform linear convolution from circular convolution and vice versa
Code:
clc;
clear all;
close;
//input sequences
x=[1,2,0,3,4,1];
h=[2,0,1,3,1];
//Circular convolution
x_len=[x,zeros(1,M-nx)];
h_len=[h,zeros(1,M-nh)];
DFT_x=fft(x_len);
DFT_h=fft(h_len);
DFT_xh=DFT_x.*DFT_h;
circ_xh=ifft(DFT_xh);
subplot(2,2,1);
xlabel("Time");
ylabel("Amplitude");
title("Input Sequence x(n)")
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(x);
subplot(2,2,2);
xlabel("Time");
ylabel("Amplitude");
title("Input Sequence h(n)")
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(h);
subplot(2,2,3);
xlabel("Time");
ylabel("Amplitude");
title("Circular Convolution from Linear Convolution")
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(Y2);
subplot(2,2,4);
xlabel("Time");
ylabel("Amplitude");
title("Linear Convolution from Circular Convolution")
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(Y3);
Output:
Experiment – 5
Aim : To find N point DFT and plot its magnitude and phase spectra.
Code:
if length(x)<nl then
x=[x zeros(1,nl-length(x))]
end
n=0:nl-1;
k=0:nl-1;
//dft
Xdft= x*exp(-%i*((2*%pi)/nl)* k'*n); //dft formula
Xdft1=fft(x); // direct Command
//Idft
Xidft = (Xdft*exp(%i*((2*%pi)/nl)* k'*n))/nl; //idft formula
Xidft1=ifft(X); /// direct command
figure(1);
//input sequence
subplot(2,2,1);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n,x)
ylabel("x[n]");
xlabel("n");
title("Input sequence");
plot2d3(n,Xidft)
ylabel("X[n]");
xlabel("n");
title("IDFT sequence");
Output:
Experiment – 6
Aim : Perform the following properties of DFT-
a) Circular shift of a sequence.
b) Circular fold of a sequence.
Code:
clc;
clear all;
close;
Output:
Code:
clc;
clear all;
close;
nx=length(x);
n1=[0:1:nx-1];
n2=pmodulo(-n1,nx);
y=x(n2+1);
Output:
Experiment – 7
Aim : Design FIR Low pass filter using
a) Rectangular window
b) Hanning window
c) Hamming window
d) Bartlett window
Code:
Output:
Experiment – 8
Aim: Implement a Low pass / High pass / Band pass / Band stop IIR Filter
using Butterworth approximation.
Code:
Output: