BEC502 Lab Manual
BEC502 Lab Manual
BEC502
INTRODUCTION TO SCILAB
Overview:
SCILAB is a freely distributed and open source scientific software package. A powerful open
computing environment for Engineering and Scientific applications. Developed since 1990
by researchers from INRIA 2 (Institut Nationale de Recherche en Informatique et en
Automatique) and ENPC (National School of Bridges and Roads). Now maintained and
developed by Scilab consortium since 2003. Since 1994 it is distributed freely along with
source code through the Internet. (www.scilab.org). Scilab users can develop their own
module so that they can solve their particular problems. The Scilab language allows to
dynamically compile and link other languages such as Fortran and C: this way, external
libraries can be used as if they were a part of Scilab built-in features. Scilab also interfaces
LabVIEW, a platform and development environment for a visual programming language
from National Instruments.
Scilab’s Main Features:
A high-level programming language
Scilab is an interpreted language
Integarated object-oriented 2-D and 3-D graphics with animation
A dedicated Editor
An XML-based help system
Interface with symbolic computing packages (Maple and MuPAD 3.0)
An interface with Tcl/Tk
Scilab works with most Unix systems including GNU/Linux and on Windows
(9X/NT/2000/XP/Vista/7), and Mac operating system
Scilab coded Toolboxes:
Linear algebra and Sparse matrices
Polynomials and Rational functions
2-D and 3-D graphics with animation
{
δ ( n )= 1 , for n=0
0 , for n ≠ 0 }
2. The unit step sequence is denoted by u(n) and is defined as
{
u ( n ) = 1 , for n≥ 0
0 , for n< 0 }
3. The exponential signal is a sequence is defined as
x ( n )=a n for all n
Where parameter is real, then x(n) is real signal and For decreasing a<1 and For increasing
exponential a>1.
4.A discrete time Sinusoidal Sequence signal may be expressed as
5. A random signal -A signal that has uncertainty about its occurrence is known as random
signal. A random signal has irregular pattern and cannot be represented by the mathematical
equations.
Example-Noise
Results: Elementary Discrete-Time signals like unit sample sequence, unit step sequence,
Exponential sequence, sinusoidal sequence and random sequence
Outcome: After finishing this experiment the students are able to Generate elementary signals/
waveforms.
EXPERIMENT-2
Aim: To perform operations on the dependent and independent variables of discrete time-signals
Theory:
Addition: The sum of two signals x 1(n) and x2(n) is a signal y(n), whose value at any instant is
equal to the sum of the values of these two signals at that instant, that is.
y(n) = x1(n) + x2(n) -∞ < n< ∞
Multiplication: The product of two signals x1(n) and x2(n) is a signal y(n), whose value at any
instant is equal to the product of the values of these two signals at that instant, that is.
y(n) = x1(n) * x2(n) -∞ < n< ∞
Scaling: Let x(n) be a discrete-time signal. Then the signal,
y(n)=c x(n)
is called scaled version of x(n) where c is a scaling factor. The signal y(n) is obtained by
multiplying the value of x(n) by scalar c for all n.
Shifting: A signal x(n) may be shifted in time by replacing the independent variable n by n-k,
where k is an integer.
If A: is a positive integer, the time shift results in a delay of the signal by k units of time.
If k is a negative integer, the time shift results in an advance of the signal by |k| units in time.
Time Folding:
Time folding of discrete time signal x(n) is reflected version of x(n). Time folding signal is
obtained by replacing n by-n.
Scilab Program
a) Signal addition, b) Signal multiplication
clc;
close;
clear all;
Dept of ECE, NCEH Page 7
DSP Lab Manual-
BEC502
x=input('Enter the sequence A=');
y=input('Enter the sequence B=');
m=length(x);
n=length(y);
if m>n then
y=[y,zeros(1,m-n)];
else
x=[x,zeros(1,n-m)];
end
z1=x+y;
L1=length(z1);
disp(z1, 'Addition of two sequences:=');
z2=x.*y;
L2=length(z2);
disp(z2, 'Multiplication of two sequences:=');
subplot(211);
plot(0:L1-1,z1,'r.');
plot2d3(0:L1-1,z1);
xtitle('Addition of two sequences','n' , 'z1=x1(n)+x2(n)' );
subplot(212);
plot(0:L2-1,z2,'r.');
plot2d3(0:L2-1,z2);
xtitle('Multiplication of two sequences','n' , 'z2=x1(n)*x2(n)' );
Output:
Enter the sequence A=[1 2 3 4]
Enter the sequence B=[1 2 3]
2. 4. 6. 4.
"Addition of two sequences:="
1. 4. 9. 0.
"Multiplication of two sequences:="
c)Scaling
clc ;
clear ;
close ;
x = input('Enter input sequence x :') ;
a = input ( 'Enter amplification factor a : ') ;
b = input ( 'Enter amplitude reversal factor b : ') ;
y1 = a *x ; // Amplitude Scaling
y2 = b *x ;
n = length ( x ) ;
subplot (2 ,1 ,1) ;
plot(0: n -1 , y1,'r.') ;
plot2d3 (0: n -1 , y1 ) ;
xtitle('Amplified signal','n' , 'y1=a*x' );
subplot (2 ,1 ,2) ;
plot(0: n -1 , y2 ,'r.') ;
d) Shifting
clc ;
clear ;
close ;
x = input('Enter input sequence x :') ;
n0 = input ('Enter the +ve shift :');
Dept of ECE, NCEH Page 10
DSP Lab Manual-
BEC502
n1 = input ('Enter the −ve shift :');
n = length ( x ) ;
L = length ( x ) ;
i = n0 : L+ n0 -1;
j = n1 : L+ n1 -1;
subplot (3 ,1 ,1) ;
plot(0: n -1 , x, 'r.') ;
plot2d3 (0: n -1 , x ) ;
xtitle('Input signal','n' , 'x' );
subplot (3,1,2) ;
plot(i ,x ,'r.') ;
plot2d3 (i ,x ) ;
xtitle('Positive[right] shifted signal','n' , 'x' );
subplot (3,1,3) ;
plot (j ,x ,'r.') ;
plot2d3 (j ,x ) ;
xtitle('Negative[left] shifted signal','n' , 'x' );
e)Folding
clc ;
clear ;
close ;
x = input('Enter input sequence x :') ;
n = length ( x ) ;
n1=0:n-1;
subplot (2 ,1 ,1) ;
plot(n1, x ,'r.') ;
plot2d3 (n1, x ) ;
xtitle('Input signal','n' , 'x' );
subplot (2 ,1 ,2) ;
plot(-n1, x ,'r.') ;
plot2d3 (-n1, x ) ;
xtitle('Folded signal','n' , 'x' );
Outcome: After finishing this experiment the students are able to perform different operations on signals
Where x(n) is input signal, h(n) is impulse response, and y(n) is output.*denotes convolution.
Here we multiply the terms of x(k) by the terms of a time-shifted h(n) and add them up. The
output length of y(n) =length[x(n)]+length[h(n)]-1.
Scilab Program:
clc;
clear;
close;
x=input('enter the 1st sequence=');
h=input('enter the 2nd sequence=');
xr=input('enter the range of 1st seq=');
hr=input('enter the range of 2nd seq=');
m=length(x);
n=length(h);
X=[x,zeros(1,n)];
H=[h,zeros(1,m)];
for i=1:n+m-1
Y(i)=0;
for j=1:m
if(i-j+1>0)
Y(i)=Y(i)+X(j)*H(i-j+1);
else
Dept of ECE, NCEH Page 14
DSP Lab Manual-
BEC502
end
end
end
disp(Y,'Linear Convolution of two sequences=')
xr1=xr(1):xr(2);
hr1=hr(1):hr(2);
yr1=xr(1)+hr(1):xr(2)+hr(2);
subplot(311);
plot(xr1,x,'r.');
plot2d3(xr1,x);
xtitle('x input sequence','n' , 'x' );
subplot(312);
plot(hr1,h,'r.');
plot2d3(hr1,h);
xtitle('h input sequence','n' , 'h' );
subplot(313);
plot(yr1,Y,'r.');
plot2d3(yr1,Y);
xtitle('Y output sequence','n' , 'y' );
Result:
enter the 1st sequence=[1 2 3 4]
enter the 2nd sequence=[1 2 1 2]
enter the range of 1st seq=[-1 2]
enter the range of 2nd seq=[-1 2]
1.
4.
8.
14.
15.
10.
8.
Aim: To obtain the Discrete Fourier Transform (DFT) of a given sequence and to plot its
magnitude and phase responses.
Theory:
Discrete Fourier Transform (DFT) is used for performing frequency analysis of discrete time
signals. DFT gives a discrete frequency domain representation whereas the other transforms
are continuous in frequency domain.
The N point DFT of discrete time signal x(n) is given by the equation
𝑁−1
Result:
enter the sequence x(n)=[1 2 3 4]
enter the length 4
Suppose that we have a finite duration sequence 𝑥(𝑛) of length L which excites an FIR filter
having an impulse response ℎ(𝑛) of length M. The output of 𝑦(𝑛) of the filter in the time
domain can be expressed as a convolution of 𝑥(𝑛) and ℎ(𝑛), that is
In the frequency domain, the same linear convolution 𝑦(𝑛) can be obtained by performing
N-point 𝐷𝐹𝑇 for which 𝑁 ≥ 𝑀 + 𝐿 − 1, that is
𝑌(𝑘) = 𝑋(𝑘)𝐻(𝑘)
In turn this implies that the N-point circulation of 𝑥(𝑛) with ℎ(𝑛) must be equivalent to the
linear convolution of 𝑥(𝑛) with ℎ(𝑛).
Scilab Program:
clear all;
clc;
x=input('enter the x sequence');// x input sequence
h=input('enter the h sequence');// h input sequence
N1=length(x);
N2=length(h);
x=[x,zeros(1,N2-1)]; //append zeros to x(n)
disp(x);
Dept of ECE, NCEH Page 22
DSP Lab Manual-
BEC502
h=[h,zeros(1,N1-1)];//append zeros to h(n)
Aim: To obtain the circular convolutions of two given sequences using DFT and IDFT.
Theory:
Suppose that we have two finite-duration sequences of length N, 𝑥(𝑛) and ℎ(𝑛). Their
𝑛=0 𝑁
𝐻(𝑘) = ∑𝑁−1 ℎ(𝑛) 𝑊𝑛𝑘 0≤𝑘 ≤𝑁−1
𝑛=0 𝑁
Scilab program:
clear all;
clc;
x=input('enter the 1st sequence');//x input sequence
h=input('enter the 2nd sequence');//h input sequence
N1=length(x);
N2=length(h);
N3=N1-N2;
if(N1>N2)
h=[h,zeros(1,N3)];//append zeros to h(n)
else if (N2>N1)
x=[x,zeros(1,-N3)]; //append zeros to x(n)
end
end
Xk=fft(x);//DFT of x(n)
Hk=fft(h); //DFT of h(n)
Dept of ECE, NCEH Page 26
DSP Lab Manual-
BEC502
disp('DFT of x(n)');
Result:
enter the 1st sequence[1 2 3 4]
enter the 2nd sequence[2 1 2 1]
"DFT of x(n)"
10. + 0.i -2. + 2.i -2. + 0.i -2. - 2.i
𝑁−𝑃𝑜𝑖𝑛𝑡 𝐷𝐹𝑇
If 𝑥(𝑛) ↔−−−−−−−−→ 𝑋(𝑘) then
𝑁−𝑃𝑜𝑖𝑛𝑡 𝐷𝐹𝑇
If 𝑥(𝑛) ↔−−−−−−−−→ 𝑋(𝑘) then
Result:
enter the sequence x=[1,-1,1,-1]
"xs="
1. -1. 1. -1.
"circular time shift value lhs ="
0. 0. 4. 0.
"circular time shift value rhs="
0. + 0.i 0. + 0.i 4. + 0.i 0. + 0.i
"It satisfies Circular Timeshift Property"
if (LHS==RHS) then
disp('Frequency shift property
satisfied'); else
disp('Frequency shift property is not satisfied');
end
Result:
THEORY: A digital IIR(Infinite Impulse Response ) filter is a linear time invariant discrete
time systems which has infinite duration impulse response h(n).IIR filters are recursive i.e
the present output sample depends on the present and /or past input and past output samples.
The design of a digital IIR filter involves the design of the filter in the analog domain and
then transforms it into the digital domain using various analog to digital transformation
methods.
The different steps involved in the design of a digital IIR filter are,
(i) The frequency response specification in digital domain is converted into analog and an
equivalent analog filter Ha(s) is designed by using any one of the approximation techniques
like Butterworth, chebyshev etc.
(ii) The analog transfer function Ha(s) is converted to digital transfer function H(z) using
various analog to digital transformation methods like Impulse invariance, Bilinear
transformation, backward difference, matched Z-transform etc
Scilab program:
clc;
clear;
close;
fc=1000;//cutoff frequency
fs=3000; //Sampling Frequency
Result: Observed the magnitude response of Low pass filter for given specifications
Result: Observed the magnitude response of High pass filter for given specifications
THEORY: A FIR filter has finite number of samples in its impulse response. The FIR filters
are of non-recursive type, whereby the present output sample is depending on the present
input sample and previous input samples.
The Design Procedure of FIR filters using windows method.
Step 1: Select the desired frequency response of the filter
N−1
𝐻𝑑 (𝑒𝑗𝑤) = 𝑒−𝑗𝑤α 𝑤ℎ𝑒𝑟𝑒 α =
2
Step 2: Find the desired unit impulse response hd(n) from the desired frequency response
Hd(ejw) by obtaining the inverse Fourier transform using the equation
𝝅
ℎ𝑑 (𝑛) = 1
∫ (𝑒𝑗𝑤)𝑒𝑗𝑤𝑛𝑑𝑤
2
𝜋 𝐻𝑑
−𝝅
Step 3: Since hd(n) is an infinite sequence, truncate it to get a finite sequence of length N,say
from n=0 to N-1
i.e h(n)=hd(n)* w(n)
Where w(n) is a “window sequence”
Step 4: By taking Z-transform of h(n),obtain the Transfer function H(z) of the filter
Step 5: Obtain the frequency response i.e magnitude response and phase response
Table: Summary of important features of window functions
8
BEC502
(𝑁 −
𝜋 |𝑛 − 2
)
1 1−2
25
𝑁 |
Bartlett (Triangular)
𝑁−1
8 2𝜋𝑛
𝜋 0.5 − 0.5cos ( )
𝑁−1
44
Hanning
𝑁
8 2𝜋𝑛
𝜋 0.54 − 0.46𝑐𝑜𝑠 ( )
𝑁−1
53
Hamming
𝑁
THEORY: A FIR filter has finite number of samples in its impulse response. The FIR filters
are of non-recursive type, whereby the present output sample is depending on the present
input sample and previous input samples.
The Design Procedure of FIR filters using windows method.
Step 1: Select the desired frequency response of the filter
N−1
𝐻𝑑 (𝑒𝑗𝑤) = 𝑒−𝑗𝑤α 𝑤ℎ𝑒𝑟𝑒 α =
2
Step 2: Find the desired unit impulse response hd(n) from the desired frequency response
Hd(ejw) by obtaining the inverse Fourier transform using the equation
𝝅
ℎ𝑑 (𝑛) = 1
∫ (𝑒𝑗𝑤)𝑒𝑗𝑤𝑛𝑑𝑤
2
𝜋 𝐻𝑑
−𝝅
Step 3: Since hd(n) is an infinite sequence, truncate it to get a finite sequence of length N,say
from n=0 to N-1
i.e h(n)=hd(n)* w(n)
Where w(n) is a “window sequence”
Step 4: By taking Z-transform of h(n),obtain the Transfer function H(z) of the filter
Step 5: Obtain the frequency response i.e magnitude response and phase response
Table: Summary of important features of window functions
8
BEC502
(𝑁 −
𝜋 |𝑛 − 2
)
1 1−2
Bartlett(Triangular) 25
𝑁 |
𝑁−1
8 2𝜋𝑛
𝜋 0.5 − 0.5cos ( )
𝑁−1
Hanning 44
𝑁
8 2𝜋𝑛
𝜋 0.54 − 0.46𝑐𝑜𝑠 ( )
𝑁−1
Hamming 53