You are on page 1of 27

# Scilab Manual for

## Signals and Systems

by Mrs Nalini Karchi
Electrical Engineering
KLE Dr M.S Sheshgiri College of Engg &
Tech1

Solutions provided by
Mrs Nalini Karchi
Electrical Engineering
VTU University

February 7, 2018

## 1 Funded by a grant from the National Mission on Education through ICT,

http://spoken-tutorial.org/NMEICT-Intro. This Scilab Manual and Scilab codes
written in it can be downloaded from the ”Migrated Labs” section at the website
http://scilab.in
1
Contents

sequence 17

## 8 Sampling the signal 25

2
List of Experiments

## Solution 1.1obtaining the discrete signal from analog signa . . . . 5

Solution 1.6 find remaining Xk if xn is real . . . . . . . . . . . . . 6
Solution 2.2find energy of signal . . . . . . . . . . . . . . . . . . . 9
Solution 3.3observe the time shifting and time scaling using sinewave
and exponential signals . . . . . . . . . . . . . . . . . 13
Solution 4.3find dft by fft also obtain mag and phase spectrum . . 17
Solution 5.5determine the linear convolution of given two sequences 19
Solution 6.6find remining samples of Xk . . . . . . . . . . . . . . . 21
Solution 7.7find z transform of sequence . . . . . . . . . . . . . . . 23
Solution 8.8sampling the sine signal to verify theorem . . . . . . . 25

3
List of Figures

## 1.1 obtaining the discrete signal from analog signa . . . . . . . . 6

1.2 find remaining Xk if xn is real . . . . . . . . . . . . . . . . . 7

## 2.1 find energy of signal . . . . . . . . . . . . . . . . . . . . . . 11

2.2 find energy of signal . . . . . . . . . . . . . . . . . . . . . . 12

3.1 observe the time shifting and time scaling using sinewave and
exponential signals . . . . . . . . . . . . . . . . . . . . . . . 14

4.1 find dft by fft also obtain mag and phase spectrum . . . . . 18

4
Experiment: 1

## To obtain discrete signal from

analog signal

Scilab code Solution 1.1 obtaining the discrete signal from analog signa

1 clc ; clear ;
2 t =0:0.01:2;
3 xt =2* exp ( -1* t ) ;
4 subplot (1 ,2 ,1) ;
5 plot (t , xt ) ;
6 xlabel ( ’ t ’ ) ;
7 ylabel ( ’ x ( t ) ’ ) ;
8 title ( ’CONTINUOUS TIME SIGNAL ’)
9
10 // O b t a i n D i s c r e t e t s i g n a l , s a m p l i n g a t T=0.1
11 // t=nT
12 T =2;
13 n=t/T;
14 xn =2* exp ( -1* n ) ;
15 subplot (1 ,2 ,2) ;
16 plot2d3 (n , xn ) ;
17 xlabel ( ’ n ’ ) ;

5
Figure 1.1: obtaining the discrete signal from analog signa

18 ylabel ( ’ x ( n ) ’ ) ;
19 title ( ’DISCRETE TIME SIGNAL ’)

## Scilab code Solution 1.6 find remaining Xk if xn is real

1 // i f x(n) i s r e a l sequence
2 // Program t o f i n d r e m a i n i n g s a m p l e s o f t h e s e q u e n c e
X( k )
3 //X( 0 ) =12 ,X( 1 )=−1+j 2 , X( 2 ) =2+j 5 , X( 3 ) =1−j 4 , X( 4 ) =5
4 clear ;
5 clc ;
6 close ;
7 j = sqrt ( -1) ;
8 z =1;

6
Figure 1.2: find remaining Xk if xn is real

7
9 X (0+ z ) =12 , X (1+ z ) = -1+ j *2 , X (2+ z ) =2+ j *5 , X (3+ z ) =1 - j *4 , X
(4+ z ) =5;
10 for a =5:1:8 do X ( a ) = conj ( X (10 - a ) ) , end ;
11 // D i s p l a y t h e c o m p l e t e s e q u e n c e X [ k ] i n command
window
12 disp (X , ”X [ k ]= ” ) ;

8
Experiment: 2

given signal

## Scilab code Solution 2.2 find energy of signal

1 clc ; clear ;
2 // Energy and power s p e c t r u m o f g i v e n discrete
signal [1 2 3 4]
3 x = input ( ”ENTER THE FIRST SEQUENCE ”);
4 sz = length ( x ) ;
5 n =0: sz -1
6 Y = fft (x , -1)
7 disp (Y , ”DFT o f s i g n a l ” )
8
9 Yr = real ( Y )
10 disp ( Yr , ” R e a l v a l u e s o f DFT” )
11 n =0: sz -1;
12 subplot (221)
13 plot2d3 (n , Yr )
14 xlabel ( ” k=n ” ) ;
15 ylabel ( ” D i s c r e t e f o u r i e r t r a n s f o r m r e a l v a l u e s o f Y”
);
16 title ( ” D i s c r e t e f o u r i e r t r a n s f o r m ” ) ;
17

9
18 E = Yr .^2
19 disp (E , ” Energy o f s i g n a l ” )
20 //P=E/ s z
21 Energy = sum ( E )
22 // ENERGY SPECTRUM
23 subplot (222)
24 n =0: sz -1
25 plot2d (n , E )
26 xlabel ( ” k=n ” ) ;
27 ylabel ( ” i n s t a n t e n e r g y v a l u e s ” ) ;
28 title ( ”ENERGY SPECTRUM” ) ;
29 disp ( Energy , ” Energy o f s i g n a l ” )
30
31 //POWER SPECTRUM
32 // s u b p l o t ( 2 2 3 )
33 // n =0: s z −1
34 // p l o t 2 d ( n , P)
35 // x l a b e l ( ” k=n ” ) ;
36 // y l a b e l ( ” i n s t a n t power v a l u e s ” ) ;
37 // t i t l e ( ”POWER SPECTRUM” ) ;
38 // Power=Energy / s z
39 // d i s p ( Power , ” power o f s i g n a l ” )

10
Figure 2.1: find energy of signal

11
Figure 2.2: find energy of signal

12
Experiment: 3

## Time shifting and time scaling

Scilab code Solution 3.3 observe the time shifting and time scaling using
sinewave and exponential signals

1
2 clc ;
3 clf ;
4 clear all ;
5 // g e n e r a t i o n o f s i n e wave
6 f =0.2;
7 t =0:0.1:10;
8 x = sin (2* %pi * t * f ) ;
9 subplot (3 ,2 ,1)
10 plot (t , x ) ;
11 title ( ’ s i n e wave ’ ) ;
12 xlabel ( ’ t ’ ) ;
13 ylabel ( ’ x ’ ) ;
14 // t i m e s h i f t i n g d e l a y by 2
15 subplot (3 ,2 ,2)
16 f =0.2;
17 t =0:0.1:10;
18 x1 = sin (2* %pi *( t -2) * f ) ;

13
Figure 3.1: observe the time shifting and time scaling using sinewave and
exponential signals

14
19 plot (t , x1 ) ;
20 title ( ’ s i n e wave ’ ) ;
21 xlabel ( ’ t ’ ) ;
22 ylabel ( ’ x1 ’ ) ;
23 // a d v a n c e d i n t i m e by 2
24 subplot (3 ,2 ,3)
25 f =0.2;
26 t =0:0.1:10;
27 x2 = sin (2* %pi *( t +2) * f ) ;
28 plot (t , x2 ) ;
29 title ( ’ s i n e wave ’ ) ;
30 xlabel ( ’ t ’ ) ;
31 ylabel ( ’ x2 ’ ) ;
32
33 // t i m e s c a l i n g
34 n =0:0.5:10;
35 xt =4* exp ( -1* n ) ;
36 subplot (3 ,2 ,4)
37 plot2d3 (n , xt ) ;
38 xlabel ( ’ n ’ ) ;
39 ylabel ( ’ x ( n ) ’ ) ;
40 title ( ’EXPONENTIAL SIGNAL ’)
41
42
43 xt1 =4* exp ( -1*((1/2) * n ) ) ;
44 subplot (3 ,2 ,5) ;
45 plot2d3 (n , xt1 ) ;
46 xlabel ( ’ n ’ ) ;
47 ylabel ( ’ x ( n ) ’ ) ;
48 title ( ’SCALED EXPONENTIAL TIME SIGNAL ext ((1/2) n)
’)
49
50
51 xt2 =4* exp ( -1*(3) * n ) ;
52 subplot (3 ,2 ,6) ;
53 plot2d3 (n , xt2 ) ;
54 xlabel ( ’ n ’ ) ;
55 ylabel ( ’ x ( n ) ’ ) ;

15
56 title ( ’SCALED EXPONENTIAL TIME SIGNAL SCAlED BY 3
’)

16
Experiment: 4

## To odtain discrete time fourier

transform of dicrete time
sequence

Scilab code Solution 4.3 find dft by fft also obtain mag and phase spec-
trum

1
2 clear ;
3 clc ;
4 x = input ( ’ e n t e r i /p x ( n ) : ’ ) ;
5 // x =[1 2 3 4 5 6 ]
6 m = length ( x ) ;
7
8 X = fft (x , -1) ;
9 subplot (3 ,2 ,1) ;
10 n =0:1: m -1
11 plot2d3 (n , x ) ;
12 ylabel ( ’ a m p l i t u d e −−−> ’ ) ;
13 xlabel ( ’ x ( n ) n−−−> ’ ) ;
14

17
Figure 4.1: find dft by fft also obtain mag and phase spectrum

15 subplot (3 ,2 ,2) ;
16 plot2d3 (n , abs ( X ) ) ;
17 ylabel ( ’ a m p l i t u d e s p e c t r u m −−−> ’ ) ;
18 xlabel ( ’X( k ) k−−−> ’ ) ;
19
20 phase = atan ( imag ( X ) , real ( X ) )
21 subplot (3 ,2 ,3) ;
22 plot2d3 (n , phase ) ;
23 ylabel ( ’ p h a s e s p e c t r u m −−−−−−> ’ ) ;
24 xlabel ( ’X( k ) k−−−> ’ ) ;

18
Experiment: 5

## To linear convolution sum

Scilab code Solution 5.5 determine the linear convolution of given two
sequences

1 clc ; clear ;
2 x = input ( ”ENTER THE FIRST SEQUENCE ” ) ; // x ( n ) =[1
2 3 4]
3 h = input ( ”ENTER THE SECOND SEQUENCE ” ) ; // h ( n ) =[1
2 3]
4 l = length ( x ) + length ( h ) -1
5 y = convol (x , h ) ; //
6 plot2d3 ( ’ gnn ’ ,y ) ;
7 xlabel ( ” time −−−−−−−>n ” ) ;
8 ylabel ( ” v a l u e s o f o u t p u t s e q u e n c e y ( n ) ” ) ;
9 title ( ”LINEAR CONVOLUTION” ) ;

19
Figure 5.1: determine the linear convolution of given two sequences

20
Experiment: 6

given signal

## Scilab code Solution 6.6 find remining samples of Xk

1 // i f x(n) i s r e a l sequence
2 // Program t o f i n d r e m a i n i n g s a m p l e s o f t h e s e q u e n c e
X( k )
3 //X( 0 ) =12 ,X( 1 )=−1+j 2 , X( 2 ) =2+j 5 , X( 3 ) =1−j 4 , X( 4 ) =5
4 clear ;
5 clc ;
6 close ;
7 j = sqrt ( -1) ;
8 z =1;
9 X (0+ z ) =12 , X (1+ z ) = -1+ j *2 , X (2+ z ) =2+ j *5 , X (3+ z ) =1 - j *4 , X
(4+ z ) =5;
10 for a =5:1:8 do X ( a ) = conj ( X (10 - a ) ) , end ;
11 // D i s p l a y t h e c o m p l e t e s e q u e n c e X [ k ] i n command
window
12 disp (X , ”X [ k ]= ” ) ;

21
Figure 6.1: find remining samples of Xk

22
Experiment: 7

function

## Scilab code Solution 7.7 find z transform of sequence

1 clear ;
2 clc ;
3 // x =[1 2 3 2 4 ]
4 // N e g e t i v e s e q u e n c e i s g i v e n f i n d z t r a n s f o r m
5 x1 = input ( ” e n t e r t h e c o e f f i c i e n t s o f n e g e t i v e
sequence ”)
6 Xz = poly ([ x1 ] , ”Z” ,” c o e f f ” )
7 disp ( Xz , ’X( z ) f o r n e g e t i v e s e q u e n c e ’ )
8
9 // i f g i v e n s e q u e n c e c o n s i d e r e d a s p o s i t i v e s e q u e n c e
10 z = poly (0 , ’ z ’ )
11 disp ( ”X( z ) f o r p o s i t i v e s e q u e n c e=” )
12 Xz1 = horner ( Xz ,1/ z )
13 disp ( Xz1 , ’X( z ) ’ )
14
15 // Given l a p l a c e t r a n s f e r f u n c t i o n c o n v e r t t o
d i s c r e t e form
16 disp ( ” g i v e n t r a n s f e r f u n c t i o n ” )
17 s = poly (0 , ’ s ’ ) ; z = poly (0 , ’ z ’ ) ;

23
Figure 7.1: find z transform of sequence

## 18 sl = syslin ( ’ c ’ ,( s +1) /( s ^2 -5* s +2) ) ;

19 T = input ( ” s a m p l i n g t i m e ” )
20 disp ( ” i t s d i s c r e t e form ” )
21 sl1 = horner ( sl ,(2/ T ) *( z -1) /( z +1) )
22 disp ( sl1 )

24
Experiment: 8

## Sampling the signal

Scilab code Solution 8.8 sampling the sine signal to verify theorem

1
2 // program f o r s a m p l i n g
3 clc ;
4 clf ;
5 clear all ;
6 t =0:0.01:100;
7 f =0.02;
8 x = sin (2* %pi * f * t ) ;
9 subplot (2 ,2 ,1) ;
10 plot (t , x ) ;
11 title ( ’ c o n t i n i o u s t i m e s i g n a l ’ ) ;
12 // s a m p l i n g f r e q u e n c y f s i s l e s s t h a n t w i c e t h e i n p u t
frequency f
13 // t=n∗ Ts
14 // s i n ( 2 ∗ p i ∗ f ∗n∗ Ts )=s i n ( 2 ∗ p i ∗ f ∗n ∗ ( f s ) )
15 fs1 =0.002;
16 n =0:0.1:100;
17 x1 = sin (2* %pi * f * n / fs1 ) ;
18 subplot (2 ,2 ,2) ;

25
Figure 8.1: sampling the sine signal to verify theorem

19 plot2d3 (n , x1 ) ;
20 title ( ’ t i m e s i g n a l f s 1 <2 f ’ ) ;
21 // s a m p l i n g f r e q u e n c y f s i s l e s s t h a n t w i c e t h e i n p u t
frequency f
22 fs2 =0.04;
23 x2 = sin (2* %pi * f * n / fs2 ) ;
24 subplot (2 ,2 ,3) ;
25 plot2d3 (n , x2 ) ;
26 title ( ’ t i m e s i g n a l f s 2 =2 f ’ ) ;
27 // s a m p l i n g f r e q u e n c y f s i s l e s s t h a n t w i c e t h e i n p u t
frequency f
28 fs3 =0.4;
29 x3 = sin (2* %pi * f * n / fs3 ) ;
30 subplot (2 ,2 ,4) ;
31 plot2d3 (n , x3 ) ;
32 title ( ’ t i m e s i g n a l f s 3 >2 f ’ ) ;

26