Professional Documents
Culture Documents
ABOUT SCILAB
Overview of Scilab
create 2D and 3D plots as well as user interfaces. The Xcos environment provides a hybrid
dynamic systems modeler and simulator.
How to get and install Scilab
Whatever the platform is (i.e. Windows, Linux or Mac), Scilab binaries can be downloaded
directly from the Scilab homepage http://www.scilab.org or from the Download area
http://www.scilab.org/download. Scilab binaries are provided for both 32 and 64-bit platforms so
that they match the target installation machine. Scilab can also be downloaded in source form, so
that we can compile Scilab by ourselves and produce our own binary. Compiling Scilab and
generating a binary is especially interesting when we want to understand or debug an existing
feature, or when we wantto add a new feature. To compile Scilab, some prerequisites binary files
are necessary, which are also provided in the Download center. Moreover, a Fortran and a C
compilerare required.
After double clicking the icon to launch Scilab, the Scilab environment by default consists of the
following docked windows: The console, files and variables browsers and the command history.
In the console, after the prompt
-->
Just type a command and press the Enter key (Windows and Linux) or the Return key (Mac OS
X) in the keyboard to obtain the corresponding result.
Applications
• The command history allows us to find all the commands from previous
sessions to the current session.
Dept. of E&CE, MITE, Moodabidri Page 2
Basic Signal Processing Laboratory (21EC33)
• The variables browser allows us to find all variables previously used during the
current session.
Edit
Preferences (in Scilab menu under Mac OS X) allows us to set and customize colours, fonts and
font size in the console and in the editor, which is very useful for screen projection. Clicking on
Clear Console clears the entire content of the console. In this case, the command history is still
available and calculations made during the session remain in memory. Commands that have been
erased are still available through the keyboard’s arrow keys.
The Editor
Typing directly into the console has two disadvantages: it is not possible to save the commands
and it is not easy to edit multiple lines of instruction. The editor is the appropriate tool to run
multiple instructions.
Opening the Editor
To open the editor from the console, click on the first icon in the toolbar or on Applications
SciNotes in the menu bar. The editor opens with a default file named “Untitled 1 “.
Saving
The extension “.sce “at the end of a file name will launch automatically Scilab when opening it
(except under Linux and Mac OS X).
Copying into the console, executing a program
• Execute “… file with echo “(Ctrl+L under Windows and Linux, Cmd+L under Mac OS X):
rewrites the file into the console and executes it.
• Execute “…until the caret, with echo “(Ctrl+E under Windows and Linux, Cmd+E under
Mac OS X): rewrites the selection chosen with the mouse into the console and executes it or
executes the file data until the caret position defined by the user. Standard copy/paste can also be
used.
Variables
Scilab is not a computer algebra system. It calculates only with numbers. All calculations are done
with matrices, although this may go unnoticed. Even if the concept of matrices is unknown, vectors
and sequences of numbers can explain it, as they are, in fact, matrices of dimension 1 × nor n × 1
and a number is itself a matrix of dimension 1 × 1. Variables do not need to be declared in advance,
but any variable must have a value. For example, obtaining the value of a variable which has not
been given a value produces an error.
-->a
! --error 4
Undefined variable: a
If a value is assigned to the variable a, there is no longer an error:
--> a=%pi/4 a =
--> a
a=
0.7853982
0.7853982
Variables may take any name that is not already defined by the system:
--> Piby2=%pi/2 Piby2 =
1.5707963
The result of a calculation that is not assigned to a variable is automatically assigned to the variable
called ans:
-->3*(4-2)
ans =
6.
-->ans ans =
6.
Functions
Functions are the easiest and most natural way to make computations from variables a nd for
obtaining results from variables.
Dept. of E&CE, MITE, Moodabidri Page 4
Basic Signal Processing Laboratory (21EC33)
Display Writing
Typing the name of a variable displays its value, except when there is “; “at the end of the command
line.
Brackets
Matrices are defined using square brackets.
As mentioned before, matrix computation is the basis of calculations in Scilab. A spac e or comma
is used to separate columns and semicolons are used to separate rows.
To define a column vector and obtain a column display:
-->v=[3;-2;5]
v=
3.
- 2.
5.
disp function
The disp must always be used with parentheses. With the vector v previously defined:
-->v(2)
ans =
- 2.
-->disp(v(2))
- 2.
-->m= [1 2 3;4 5 6]
m=
1. 2. 3.
4. 5. 6.
-->m(2,3)
ans =
6.
-->m(2,3)=23
m=
1. 2. 3.
4. 5. 23.
The “ : “ operator is used to designate all the rows or all columns of a matrix. To view the second
row of the matrix m, type:
-->m(2,:)
ans =
4. 5. 23.
To view the third column:
-->m(:,3)
ans = 3.
23.
To obtain the transpose of a matrix or a vector use the single quote symbol “ ' “:
-->m'
ans =
1. 4.
2. 5.
3. 23.
Operations
The operations * “, “/ “are matrix operations. To make element wise operations, we need to put dot
before the operating sign: “.* “, “. / “.
Dept. of E&CE, MITE, Moodabidri Page 6
Basic Signal Processing Laboratory (21EC33)
Length
The length function returns the number of coordinates for a vector. The size function returns the
dimensions (rows, columns) for a matrix.
-->U=[1:10]
U=
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
-->length(U)
ans =
10.
-->size(U) ans =
2. 3.
a. To Create/Modify a Vector
b. To Create/Modify a Matrix
disp(‘ all element of row 1 is’, a(1,:)) // accessing all element in row 1
disp(‘ elements of second and third row of each column’, a(: , 2 : 3))
Result
Result:
b. Matrix Multiplication
clc
m=input("Enter number of rows of the first Matrix: ");
n=input("Enter number of columns of the first Matrix: ");
p=input("Enter number of rows of the second Matrix: ");
q=input("Enter number of columns of the second Matrix: ");if
n==p
disp('Matrices are conformable for multiplication')else
disp('Matrices are not conformable for multiplication')
break;
end
disp('enter the first Matrix')
for i=1:m
for j=1:n
A(i,j)=input('\');
end
end
disp('enter the second Matrix')
for i=1:p
for j=1:q
B(i,j)=input('\');
end
end
C=zeros(m,q);
for i=1:m
for j=1:q for
k=1:n
C(i,j)=C(i,j)+A(i,k)*B(k,j);end
end
end
disp('The first matrix is')
disp(A)
disp('The Second matrix is')
disp(B)
disp('The product of the two matrices is')
disp(C)
Result:
C. Matrix Transpose
clc
m=input("Enter number of rows of the Matrix: ");
n=input("Enter number of columns of the Matrix: ");
disp('Enter the Matrix')
for i=1:m
for j=1:n
A(i,j)=input(' ');
end
end
B=zeros(n,m);
for i=1:n
for j=1:m
B(i,j)=A(j,i)
end
end
disp('Entered matrix is')
disp(A)
disp('Transposed matrix is')
Dept. of E&CE, MITE, Moodabidri Page 13
Basic Signal Processing Laboratory (21EC33)
disp(B)
Result:
clc
A=input("Enter the value of A in Matrix Form: ");
Y=input("Enter the value of Y in Matrix form: ");
disp('Matrix A is ')
disp(A)
disp('Matrix Y is ')
disp(Y)
Yt = Y’;
disp(‘Transposed Matrix Yt is)
disp(Yt)
X = A\Yt;
Result:
A= [1 0 1;1 0 0; 2 1 0];
disp(A, ‘A=’);
[m,n] = size(a);
for k=1:n
V(: , k) = A(:,k);
for j =1:k-1;
R(j , k) = V(: , j)’ * A(: , k);
Disp(v, ‘Q=’);
Result:
Given a square matrix A, we find the characteristic polynomial of A by expanding the matrix
equation | A – λ I |. The Eigen values of A are obtained solving the characteristic equation
| A – λ I | = 0. The corresponding Eigen vectors are obtained by solving the system of equations
Ax = λx.
Clc;clear;close;
A = [3 -2 5; -2 3 6; 5 6 4];
lam = poly(0, ‘lam’);
lam=lam;
Char_Mat = A – lam*eye(3,3);
Disp(Char_Mat, ‘The characteristic Maatrix is’)
Char_Poly = poly(A, ‘lam’)
Disp(Char_Poly, ‘The characteristic polynomial is’)
Lam = spec(A)
Disp(lam, ‘ The eigen value of A are’)
Function[x,lam] = eigenvectors(A)
[n,n] = size(A);
lam = spec(A)’ ;
x = [ ];
for k =1 : 3
B = A –lam(k) * eye(3 , 3); // characteristic matrix
C = B (1 : n-1 , 1: n-1); // coeff matrix for the reduced system
b = -B(1 : n-1 , n); // RHS vector for the reduced system
y = C \ b; // solution for the reduced system
y = [y ; 1];
y = y / norm(y) ; // make unit eigen vector
x = [x y];
end
endfuction
get f(‘eigenvectors’)
Results:
clear;
close;
clc;
A=[-1 2 2] ' ;
disp('A=', A );
[U diagnol V]= svd(A);
disp('U=', U );
disp('diagnol=',diagnol );
disp ('V = ',V');
disp ('U*diagnol*V =A = ', U*diagnol*V')
clear;
close;
clc;
A=[-1 2 2 ; 1 1 2];
disp('A=', A );
[U diagnol V]= svd(A);
disp('U=', U );
disp('diagnol=',diagnol );
disp ('V = ',V');
disp ('U*diagnol*V =A = ', U*diagnol*V')
Result
//Impulse function:
l = 5;
n = -l:l;
x = [zeros(1,l), ones(1,1), zeros(1,l)];
subplot(3,3,5)
plot2d3(n,x);
title ( ' Impluse Signal ' ) ;
xlabel ( ' sample(n) ' ) ;
ylabel ( ' Amplitude ' ) ;
//Ramp function:
n = 0:10;
x = n;
subplot(3,3,6)
plot2d3(n,x);
title ( ' Ramp Signal ' ) ;
xlabel ( ' sample(n) ' ) ;
ylabel ( ' Amplitude ' ) ;
//Step function:
t=0:4;
y=ones(1,5);
subplot(3,3,7);
plot2d3 (t,y);
xlabel('time');
ylabel('amplitude');
title('Unit Step Discrete Signal');
Results:
// amplification
x= input ( ’ Enter input sequence x : ’ );
a= input ( ’ Enter amplification factor a : ’ );
b= input ( ’ Enter attenuation factor b : ’ );
c= input ( ’ Enter amplitude reversal factor c : ’ );
y1=a*x;
y2=b*x;
y3=c*x;
n= length (x);
// Amplification
subplot (2 ,3 ,2);
plot2d3 (0:n -1, y1);
xlabel ( ’ t ime ’ );
ylabel ( ’ Amplitude ’ );
title ( ’ Amplified signal ’ );
// attenuation
subplot (2 ,3 ,3);
plot2d3 (0:n -1, y2);
xlabel ( ’ time ’ );
ylabel ( ’ Amplitude ’ );
title ( ’ Attenuated signal ’ );
// Amplitude Reversal
subplot (2 ,3 ,4);
plot2d3 (0:n -1, y3);
xlabel ( ’ time ’ );
ylabel ( ’ Amplitude ’ );
title ( ’ Amplitude reverse signal ’ );
subplot (2 ,3 ,6);
plot2d3 (j,x);
xlabel ( ’ t ime ’ );
ylabel ( ’ Amplitude ’ );
title ( ’ Negative Shifted Signal ’ );
Result:
clc;
clear all;
close ;
x = input(' enter x seq = ' );
h = input(' enter h seq = ');
SIx = input ('enter starting index of input x = ');
EIx = input ('enter ending index of input x = ');
SIh = input ('enter starting index of input h = ');
EIh = input ('enter ending index of input h = ');
SIy = SIx+SIh;
EIy = EIx+EIh;
m = length(x);
n = length(h);
subplot(3,1,1)
m1= SIx:1:EIx;
plot2d3(m1,x)
xlabel('sample ');
ylabel('amplitude');
title(' input signal x[n] ');
n1=SIh:1:EIh;
subplot(3,1,2)
plot2d3(n1,h)
xlabel('sample ');
ylabel('amplitude');
title(' input signal h[n]');
N1=SIy:1:EIy;
subplot(3,1,3)
plot2d3(N1,conv_sum)
xlabel('sample ');
ylabel('amplitude');
title(' output convoluted signal y[n]');
Re
sul
t:
Result
//Z - transform of [ 1 0 3 -1 2 ]
clear ;
clc ;
close ;
function [za ]= ztransfer ( sequence ,n)
z= poly (0, ' z ' , ' r ' )
za= sequence*(1/ z)^n'
endfunction
x1 =[1 0 3 -1 2];
n =0: length (x1) -1;
zz= ztransfer (x1 ,n);