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

Scilab 2 PDF

The document outlines a tutorial and lab session on using the open source software Scilab for digital signal processing, including how to handle vectors and matrices, use plot functions, generate and sample signals, perform convolution, correlation, discrete Fourier transforms, and digital filter design. The tutorial covers the basics of Scilab and signal processing concepts, while the lab session demonstrates installing Scilab and its built-in functions through examples and hands-on practice.

Uploaded by

Marites Cervo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Scilab 2 PDF

The document outlines a tutorial and lab session on using the open source software Scilab for digital signal processing, including how to handle vectors and matrices, use plot functions, generate and sample signals, perform convolution, correlation, discrete Fourier transforms, and digital filter design. The tutorial covers the basics of Scilab and signal processing concepts, while the lab session demonstrates installing Scilab and its built-in functions through examples and hands-on practice.

Uploaded by

Marites Cervo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

AICTE­SDP 

on Open Source Software, PSG Tech, June 22nd,2007 – July 6th,2007

for
Digital Signal Processing

S Subathra
Project Engineer, NRCFOSS
AU­KBC Research Centre
MIT Campus of Anna University
Chennai – 600 044
subathra@au­kbc.org
Plan for Presentation
Tutorial Session
 Signal Processing with Scilab   45mins (Tutor)
Lab Session
 Installation Demo 10mins (Tutor)
 Installation of Scilab 15mins (Participants)
 Scilab Build­in Demo  50mins (Participants)
 Hands­on Practise 60mins (Participants)

Scilab for Digital Signal Processing 2


Tutorial Session

Scilab for Digital Signal Processing 3


Outline
 Scilab
 Signal Processing
 Handling vectors & matrices
 Using plot functions
 Signal generation 
 Sampling
 Convolution
 Correlation
 Discrete Fourier Transform
 Digital Filter Design

Scilab for Digital Signal Processing 4


Scilab

 Free software
 Numerical programming
 Rapid Prototyping
 Extensive libraries and toolboxes
 Easy for matrix manipulation

Scilab for Digital Signal Processing 5


Scilab

 Free software
 Numerical programming
Ideal for Signal Processing
 Rapid Prototyping
 Extensive libraries and toolboxes
 Easy for matrix manipulation

Scilab for Digital Signal Processing 6


Signal Processing
 A signal carries information, and the objective of signal 
processing is to extract useful information carried by the 
signal. 
 The method of information extraction depends on the type 
of signal and the nature of the information being carried by 
the signal.
 Signal Processing is concerned with the mathematical 
representation of the signal and the algorithmic operation 
carried out on it to extract the information present.

Scilab for Digital Signal Processing 7


Signal Processing
 A 1­D signal is a function of a single independent variable.
 Eg: Speech Signal => time
 A 2­D signal is a function of a two independent variable.
 Eg: Image(like photograph) => two spatial variables
 A M­D signal is a function of more than one variable.
 Eg: Each frame of a black­and­white video signal is a 2­
D image signal that is a function of two discrete spatial 
variables, with each frame occurring sequentially at 
discrete instants of time. => two spatial variables & time

Scilab for Digital Signal Processing 8


Signal Processing
 The representation of the signal can be in terms of basis 
functions in the domain of the original independent 
variable(s), or it can be in terms of basis functions in a 
transformed domain.
 A signal can be generated by a single source or by 
multiple sources. 
 Single Source => Scalar
 Multiple Source => Vector

Scilab for Digital Signal Processing 9


Signal Processing
 The representation of the signal can be in terms of basis 
functions in the domain of the original independent 
variable(s), or it can be in terms of basis functions in a 
transformed domain.
How to process vectors with Scilab
 A signal can be generated by a single source or by 
multiple sources. 
 Single Source => Scalar
 Multiple Source => Vector

Scilab for Digital Signal Processing 10


Handling vectors
 To create a zero vector with 20 dimensions
    >a=zeros(1,20);

 To create a vector and store it in a variable
    >b=[1 2 3 5 1];

 To extract the element 1 to 3 of the above vector 
    >b(1:3)
    [1 2 3]

Scilab for Digital Signal Processing 11


Handling matrices
 To initialize matrix
 To get the size of matrix
    >m=zeros(3,3);     >size(h)
    >k=rand(3,3);     >[3 4]
    >h=[1 4 6 7; 4 3 2 1; 1 4 3 
8];  Transpose of a matrix
    >h=[1 4 6 7; 4 3 2 1; 1 4 3 
 To extract the elements of  8];
the matrix     >h’
    >h(2:3,2:4)    |1 2 9|
    |1 7 8|    |4 1 2|
    |2 1 5|    |6 7 1|
   |7 8 5|

Scilab for Digital Signal Processing 12


Operator Notation
 To multiply two matrices ‘a’ and ‘b’
    >a*b

 For multiplying transpose of matrix ‘a’ with itself
    >a’*a

 For elementwise multiplication and addition
    >c=a.*b
    >c=a.+b

Scilab for Digital Signal Processing 13


Using Plot Function
 Plot function is used to 
produce number of two  0.9

dimensional plots 0.8

0.7

To generates a random 
0.6

0.5

vector of length 10 and plots  0.4

the points with straight line  0.3

joining each points 0.2

0.1

0.0

    >y=rand(1,10);
1 2 3 4 5 6 7 8 9 10

    >plot(y); Fig. 1 Plot Function ­ Sample 1

Scilab for Digital Signal Processing 14


Using Plot Function – color
 Color of the line joining the 
points changed using a 
parameter 0.9

 ‘y’ – yellow 0.8

 ‘c’ – cyan 0.7

 ‘g’ – green 0.6

 ‘w’ – white 0.5

 ‘m’ – magenta 0.4

 ‘r’ – red 0.3

 ‘b’ – blue 0.2

 ‘k’ – black 0.1

0.0
1 2 3 4 5 6 7 8 9 10

>y=rand(1,10);
>plot(y,’r’); Fig. 2 Plot Function ­ Sample 2

Scilab for Digital Signal Processing 15


Using Plot Function
 To produce a discontinious plot 0.9
 ‘.’ – uses dot at each point
0.8
 ‘*’ – uses asterisks at each point
  ‘d’ – uses blank diamond at each  0.7

point 0.6

 ‘^’ – uses upward triangles at each  0.5

point
‘o’ – uses circle at each point
0.4

 ‘+’ – uses cross at each point 0.3

 ‘v’ – uses inverted triangle at each  0.2

point 0.1

0.0

>y=rand(1,10);
1 2 3 4 5 6 7 8 9 10

>plot(y,’r’);

Fig. 3 Plot Function ­ Sample 3

Scilab for Digital Signal Processing 16


Using Plot Function – subplot
 Several plots can be accommodated in a single figure window 
using ‘sub­plot’ command. 

 This command takes three parameters as input, first two 
parameters specifies the grid size, i.e. how many rows and 
columns and the third parameter specifies the position of the plot 
in the grid.

 For e.g.,
           subplot(3,2,2)
     tells scilab that the figure window is divided as three rows, two 
columns and the plot has to be placed in the second column of the 
first row.

Scilab for Digital Signal Processing 17


Using Plot Function – subplot
0.9 1.0
Example: 0.8 0.9
0.8

x1=rand(1,10);
0.7
0.7
0.6 0.6

subplot(2,2,1); 0.5 0.5

0.4 0.4

plot(x1); 0.3
0.3
0.2

x2=rand(1,20);
0.2 0.1
0.1 0.0
1 2 3 4 5 6 7 8 9 10 0 2 4 6 8 10 12 14 16 18 20
subplot(2,2,2);
plot(x2); 0.9 1.0

0.8 0.9

x3=rand(1,15); 0.7
0.8
0.7

subplot(2,2,3);
0.6 0.6
0.5 0.5

plot(x3); 0.4 0.4


0.3
0.3
x4=rand(1,25); 0.2
0.2
0.1

subplot(2,2,4);
0.1 0.0
0 2 4 6 8 10 12 14 16 0 5 10 15 20 25

plot(x4);
Fig. 4 Plot Function ­ Sample 4

Scilab for Digital Signal Processing 18


Using Plot Function - label

 To mark label, the function  1.0

‘gca’ returns the handle for  0.9

the current axes 0.8

0.7

0.6

    t=1:1:50

amplitude
0.5

0.4

    y=rand(1,50); 0.3

   plot(t,y); 0.2

0.1

   a=gca(); 0.0
0 5 10 15 20 25 30 35 40 45 50

   a.x_label.text=“time”;
time

   a.y_label.text=“amplitude”; Fig. 5 Plot Function ­ Sample 5

Scilab for Digital Signal Processing 19


Using Plot Function - Mesh plot

 Mesh plot can be 
generated using plot3d 
command. 
1

Z
0

     m=rand(5,7);
2

3 1

     i=1:1:5; 
4 2

5 3
Y
6 4

     j=1:1:7;
X

7 5

     plot3d(i,j,m); 
Fig. 6 Plot Function ­ Sample 6

Scilab for Digital Signal Processing 20


Signal generation
//sine wave Sine Wave Generation

t=0:0.01:3.14; 1.0
0.8
y=sin(2*3.14*t); 0.6
0.4
subplot(2,1,1); 0.2

Amplitude
a=gca(); 0.0
­0.2
a.x_label.text="Time"; ­0.4

a.y_label.text="Amplitude";
­0.6
­0.8

plot2d(t,y); ­1.0
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5

xtitle("Sine Wave Generation"); Time
Cosine Wave Generation
1.0
0.8
//cos wave 0.6

t=0:0.01:3.14;
0.4
0.2

y=cos(2*3.14*t);

Amplitude
0.0
­0.2
subplot(2,1,2); ­0.4

a=gca(); ­0.6
­0.8

a.x_label.text="Time"; ­1.0
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5

a.y_label.text="Amplitude"; Time

plot2d(t,y);
xtitle("Cosine Wave Generation"); Fig. 7 Signal generation

Scilab for Digital Signal Processing 21


Sampling
 The process in which the analog continuous signal 
are measured at equal interval of time and finally a 
discretized set of digital numbers are created.

 For proper sampling of analog signal Nyquist­
Shannon sampling theorem should be satisfied, i.e. 
sampling frequency should be a greater than twice 
the maximum frequency of the input signal.

Scilab for Digital Signal Processing 22


Sampling - Example
fo=input('Frequency of since wave in hz='); Continuous signal

ft=input('Sampling frequency in hz='); 1.0
0.8
t=0:0.01:1; 0.6

T=1.0/ft;
0.4
0.2

x=sin(2*3.14*fo*t);

Amplitude
0.0
­0.2
subplot(2,1,1); ­0.4

a=gca();
­0.6
­0.8

a.x_label.text="Time"; ­1.0
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

a.y_label.text="Amplitude"; Time
Sampled Signal

plot(t,x); 1.0
0.8
xtitle("Continuous signal"); 0.6
0.4
0.2

n=0:ft;

Amplitude
0.0
­0.2
y=sin(2*3.14*fo*n*T); ­0.4

subplot(2,1,2); ­0.6
­0.8
a=gca(); ­1.0
0 5 10 15 20 25 30 35 40 45 50

a.x_label.text="Time"; Time

a.y_label.text="Amplitude";
plot2d3(n,y); Fig. 8 Sampling 
xtitle("Sampled Signal");

Scilab for Digital Signal Processing 23


Convolution
 Convolution is the process in which the response of a LTI system is 
computed for an input signal.

 The LTI system can be defined using the impulse response of the 
system.

 Convolution of the input signal and impulse response of the system 
gives the output signal. This process is also called digital filtering.

 Mathematically,

Scilab for Digital Signal Processing 24


Correlation
 Correlation takes two signals as input and produces third signal as output.

 If both the input signals are one and the same, then it is called as auto 
correlation

 If both the input signals are different, then it is called as cross correlation

 Correlation is used to measure the similarity between the two input signals at 
that particular time.

 Mathematically,

Scilab for Digital Signal Processing 25


Discrete Fourier Transform
 Used for analysing the frequency components of a sampled 
signal

 DFT decomposes the input signal into set of sinusoids

 In DFT, we take a sequence of real numbers(sampled signal) 
as input and it is transformed into a sequence of complex 
numbers.

 Mathematically,

Scilab for Digital Signal Processing 26


Digital Filter Design – Analog & Digital

 Filter ­ Used to remove the unwanted signal from the 
useful signal.
 For eg, removing noise from audio signal

 Two types – Analog & Digital filters
 Analog filters uses inductors, capacitors to produce 
the required effects
 Digital filter uses modern processors for doing the 
same

Scilab for Digital Signal Processing 27


Digital Filter Design – FIR & IIR

 Digital filters are classified as non recursive 
filters(FIR) and recursive filters(IIR)
 FIR Filter – the output will be dependent on the 
current & previous input values
 IIR Filter – dependent on previous output in 
addition to the input values

Scilab for Digital Signal Processing 28


FIR – LPF
// Low pass filter
fp=input('Enter the cutoff frequency in  FIR low pass filter

Hz ­­ fp='); 1.2

n=input('Enter the order of the filter ­­ 1.0

n=');
F=input('Enter sampling frequency in Hz  0.8

­­F=');

Magnitude
wc=fp/F; 0.6

[coeffval,famp,ffreq]=wfir('lp',n,[wc 0],  0.4

'hm', [0 0]);
//frequency response of the filter 0.2

plot2d(ffreq,famp);
0.0

a=gca(); 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50

Frequency

a.x_label.text="Frequency";
a.y_label.text="Magnitude";
xtitle("FIR low pass filter"); Fig. 9 FIR ­ LPF

Scilab for Digital Signal Processing 29


FIR – HPF
// High pass filter
fp=input('Enter the cutoff frequency in 
Hz ­­ fp=');
FIR high pass filter

1.0
n=input('Enter the order of the filter ­­
n=');
0.9

F=input('Enter sampling frequency in Hz 
0.8

­­F=');
0.7

wc=fp/F;
0.6

Magnitude
0.5

[coeffval,famp,ffreq]=wfir('hp',n,[wc 0], 
'hm', [0 0]);
0.4

0.3

//frequency response of the filter
0.2

plot2d(ffreq,famp); 0.1

a=gca(); 0.0

a.x_label.text="Frequency";
0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50

Frequency

a.y_label.text="Magnitude";
xtitle("FIR high pass filter"); Fig. 10 FIR ­ HPF

Scilab for Digital Signal Processing 30


IIR – LPF
// Low pass filter
fp=input('Enter the cutoff frequency in Hz ­­  Discrete IIR filter low pass

fp='); 1.2

n=input('Enter the order of the filter ­­n=');
F=input('Enter sampling frequency in Hz ­­ 1.0

F=');
wc=fp/F;
0.8

hz=iir(3,'lp','butt',[wc 0],[0 0]);

Magnitude
0.6

[hzm,fr]=frmag(hz,256);
plot2d(fr',hzm'); 0.4

a=gca();
a.x_label.text="Frequency"; 0.2

a.y_label.text="Magnitude";
xtitle('Discrete IIR filter low pass');
0.0
0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50

q=poly(0,'q'); //to express the result in terms of 
Frequency

the ...
hzd=horner(hz,1/q) // delay operator q=z^­1
Fig. 11 IIR ­ LPF

Scilab for Digital Signal Processing 31


IIR – HPF
// High pass filter
Discrete IIR filter high pass
fp=input('Enter the cutoff frequency in Hz ­­ 
fp=');
1.2

n=input('Enter the order of the filter ­­n=');
1.0

F=input('Enter sampling frequency in Hz ­­
F='); 0.8

wc=fp/F
hz=iir(3,'hp','butt',[wc 0],[0 0]);

Magnitude
0.6

[hzm,fr]=frmag(hz,256);
plot2d(fr',hzm'); 0.4

a=gca();
a.x_label.text="Frequency";
0.2

a.y_label.text="Magnitude"; 0.0

xtitle('Discrete IIR filter high pass'); 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50

Frequency

q=poly(0,'q'); //to express the result in terms of 
the ...
hzd=horner(hz,1/q) // delay operator q=z^­1
Fig. 12 IIR ­ HPF

Scilab for Digital Signal Processing 32


Reference
[1] John G.Proakis and Dimitris G.Manolakis, “Digital Signal Processing principles, algorithms, and 
applications” – Third edition.

[2] Sanjit K. Mitra, “Digital Signal Processing a computer based approach” – Second edition.

[3] Scilab Documentation, Available at www.scilab.org

Scilab for Digital Signal Processing 33


Lab Session

Scilab for Digital Signal Processing 34


Scilab Installation – How To
 Go to www.scilab.org
 Check for the latest downloadable version of scilab (i.e. scilab­
4.1.1.bin.linux­i686.tar.gz) and download it.
 Untar the scilab­4.1.1.bin.linux­i686.tar.gz using the command 
# tar –xzvf scilab­4.1.1.bin.linux­i686.tar.gz
where 
          x refers to extract, 
          z refers to gzip, 
          v refers to verbose, 
          f refers to file
 Run Scilab by executing “scilab”(shell script in bin)

Scilab for Digital Signal Processing 35


Scilab Build-in Demo

Scilab demonstration
http://www.scilab.org/doc/demos_html/index.html

Matlab and Scilab functions 
http://www.scilab.org/product/dic­mat­sci/M2SCI_doc.htm

Scilab for Digital Signal Processing 36


More about DSP

Signal Processing Information Base (SPIB)
http://spib.rice.edu/spib.html

DSP Online Classes
http://bores.com/index_dsp.htm

http://bores.com/index_online.htm

Scilab for Digital Signal Processing 37


Scilab for Digital Signal Processing 38

You might also like