You are on page 1of 6

Text Book : Basic Simulation Lab with MATLAB

Authors : Dr Bhanu Bhaskara, Siddhartha Bhaskara


Publisher : Tata McGrawHill, 1st Edition, July 2010

Chapter 3: Generation of Signals and Sequences

S.No MATLAB Code Explanation


1. t = 0 : 0.001 : 1; Generate a vector of 1001 samples for t
with a value between 0 & 1 with an
increment of 0.001
2. y = 0.5 * t; Generate a linear ramp with a slope of 0.5

3. plot( t , y ); Plot the graph

4. xlabel ('Time Index t (sec.)'); Label the X-axis

5. ylabel ('Amplitude'); Label the Y-axis

6. title ('Ramp Signal Sequence'); Add an appropriate title to the graph

Table 3.1 : MATLAB code for Ramp Signal Generation

S.No MATLAB Code Explanation


1. t = 0 : 0.0001 : 0.1; Generate a vector of 10001 samples
for t with a value between 0 & 0.1
with an increment of 0.0001
2. y = square ( 2 * pi * 50 * t); Generate a square wave of frequency
50 Hz, by calling the MATLAB in-
built function ‘square’.
3. plot (t , y); axis ( [ 0 0.1 -2 2 ] ); Plot the graph. Adjust the X and Y
coordinates to view a few cycles of the
square wave.
4. xlabel ('Time Index t (sec.)'); Label the X and Y axes
ylabel ('Amplitude');

5. title ('Square Wave Signal Sequence'); Add an appropriate title to the graph

Table 3.2 : MATLAB code for Square Wave Signal Generation


Text Book : Basic Simulation Lab with MATLAB
Authors : Dr Bhanu Bhaskara, Siddhartha Bhaskara
Publisher : Tata McGrawHill, 1st Edition, July 2010

S.No MATLAB Code Explanation


1. fs = 10000; Set the sampling frequency fs as
10000.
2. t = 0 : 1/fs : 1.5; Generate a vector of 15001 samples
for t with a value between 0 & 1.5
with an increment of 0.0001
3. x = sawtooth (2 * pi * 100 * t); Generate a Saw Tooth wave of
frequency 100 Hz, by calling the
MATLAB built-in function
‘sawtooth’.
4. plot( t , x ); axis ( [ 0 0.05 -1 1 ] ); Plot the graph. Adjust the X and Y
coordinates to view a few cycles of
the Saw Tooth wave.
5. xlabel ('Time Index t (sec.)'); Label the X and Y axes
ylabel ('Amplitude');

6. title ('Sawtooth Wave Signal Sequence'); Add an appropriate title to the


graph

Table 3.3 : MATLAB code for Saw Tooth Wave Generation


Text Book : Basic Simulation Lab with MATLAB
Authors : Dr Bhanu Bhaskara, Siddhartha Bhaskara
Publisher : Tata McGrawHill, 1st Edition, July 2010

S.No MATLAB Code Explanation


1. fs = 10000; Set the sampling frequency fs as
10000.
2. t = 0 : 1/fs : 1.5; Generate a vector of 15001 samples
for t with a value between 0 & 1.5
with an increment of 0.0001
3. x = sawtooth (2 * pi * 50 * t , 0.5); Generate a Triangular wave of
frequency 50 Hz, by calling the
MATLAB built-in function
‘sawtooth’ with 0.5 as the duty cycle
parameter.
4. plot(t,x); axis ( [ 0 0.1 -1 1 ] ); Plot the graph. Adjust the X and Y
coordinates to view a few cycles of
the triangular wave.
5. xlabel ('Time Index t (sec.)'); Label the X and Y axes
ylabel ('Amplitude');

6. title ('Triangular Wave Signal Sequence'); Add an appropriate title to the graph

Table 3.4 : MATLAB Code for Triangular Wave Generation


Text Book : Basic Simulation Lab with MATLAB
Authors : Dr Bhanu Bhaskara, Siddhartha Bhaskara
Publisher : Tata McGrawHill, 1st Edition, July 2010

S.No MATLAB Code Explanation


1. N=20; Set a limit 20 for X-axis coordinate
‘time’.
2. t = -N : .0001 : N; Generate a vector of 400001 samples
for t with a value between -20 & 20
with a step size of 0.0001. The X-
axis range is from -20 to +20.
3. theta = input('The phase angle is '); Input the desired Phase Angle for the
variable called ‘theta’
4. x = sin (2 * pi * 50 * t + theta); Generate a Sine Wave of frequency
50 Hz and desired phase angle
‘theta’, by calling the MATLAB
built-in function ‘sin’.
5. plot( t , x ); axis( [-.04 .04 -1 1] ); Plot the graph. Adjust the X and Y
coordinates to view a few cycles of
the Sine wave.
6. grid; Insert horizontal and vertical grid in
the graph for easy measurement of
parameters.
7. xlabel ('Time Index t (sec.)'); Label the X and Y axes
ylabel ('Amplitude');

8. title ('Sinusoidal Signal of Frequency 50 Hz Add an appropriate title to the graph


and Desired Phase');

Table 3.5 : MATLAB Code for Sine Wave Signal Generation

S.No MATLAB Code Explanation


1. t = linspace(-5 , 5); Generate a vector of 100 linearly
spaced samples for t with a value
between -5 & 5
2. y = sinc(t); Call the built-in 'Sinc' function to
generate the Sinc signal of 100
samples
3. plot( t , y ); Plot the graph.
4. xlabel ('Time Index t (sec.)'); Label the X and Y axes
ylabel ('Amplitude');
5. title ('Sinc Signal Sequence'); Add an appropriate title to the graph

Table 3.6 : MATLAB Code for Sinc Signal Generation


Text Book : Basic Simulation Lab with MATLAB
Authors : Dr Bhanu Bhaskara, Siddhartha Bhaskara
Publisher : Tata McGrawHill, 1st Edition, July 2010

S.No MATLAB Code Explanation


1. clc; Clear Command Window for a clean
view
2. clear all; Clear all the previously defined
variables
3. m1 = input ( ' Please input a NEGATIVE Set a limit for negative X-axis
number for “m1” ');
4. m2 = input ( ' Please input a POSITIVE Set a limit for positive X-axis
number for “m2” ');
5. m = [m1 : m2]; Define the range for X-axis
6. x = zeros(1,m2-m1+1); Create a row vector of zeros
7. m0 = input ( ' Type 0 for Unit Impulse, Define the place of the single unit
negative/ positive no. for delayed/ advanced impulse on the X-axis
signal ');
8. x(m0-m1+1) = 1; Set the amplitude of the unit impulse
as ‘1’ at the defined place
9. stem( m , x ); axis( [ m1 m2 -1.5 1.5 ] ); Plot the graph as stems. Adjust the X
and Y coordinates to view a nice
graph.
10. xlabel ('Time Index m (sec.)'); Label the X and Y axes
ylabel ('Amplitude');
11. title ('Unit Impulse Signal Sequence'); Add an appropriate title to the graph

Table 3.7 : MATLAB Code for Unit Impulse Signal Generation


Text Book : Basic Simulation Lab with MATLAB
Authors : Dr Bhanu Bhaskara, Siddhartha Bhaskara
Publisher : Tata McGrawHill, 1st Edition, July 2010

S.No MATLAB Code Explanation


1. clc; clear all; Clear Command Window and all the
previously defined variables for a
clean view
2. k1 = input ( ' Please input a NEGATIVE Set a limit for negative X-axis
number for “k1” ');
3. k2 = input ( ' Please input a POSITIVE Set a limit for positive X-axis
number for “k2” ');
4. k0 = input ( ' Type 0 for normal Unit Step, a Decide on the type of Unit Step
negative or positive no. for a shifted one '); desired and accordingly feed an input
5. k = [k1 : k2]; Define the range for X-axis
6. x = zeros(1,k2-k1+1); Create a row vector of zeros
7. x( k0-k1+1 : end ) = 1; Set the amplitude of the unit step as
‘1’ from the place of starting until
end.
8. stem( k , x ); Plot the graph as stems. Adjust the X
axis ( [ k1 k2 -1.5 1.5 ] ); and Y coordinates to view a decent
looking graph.
9. xlabel ('Time Index k (sec.)'); Label the X and Y axes
ylabel ('Amplitude');
10. title ('Unit Step Signal Sequence'); Add an appropriate title to the graph

Table 3.8 : MATLAB Code for Unit Step Signal Generation

You might also like