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

Lab 2

Uploaded by

mohsin89
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab 2

Uploaded by

mohsin89
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Signal Processing Lab 2

1 Continuous Time Signals:


1.1 Generation and Plotting of Continuous Time Signals:
A principle advantage of MATLAB in signal Processing is the simplicity with which signals of all types
can be generated and plotted. MATLAB supports a large variety of functions to visualize numerical data,
both as 2D and 3D graphs. The following basic features are often needed in our context of signal
processing:

 The function PLOT is the basic method for plotting 2D graphs.


 A single figure window can be split into several graphs with the SUBPLOT command.

Some examples will serve here to show how simple commands can readily be used to produce useful
signals and plots.

1.1.1. Sine Wave Generation:


The following commands will generate and plot 31 samples of a unit sine wave with a period of
6.28 samples.

» x=0:1.0:30;
» y=sin(2*pi*x/(2*pi));
» plot(y); grid on; title('Sine wave with period = 2pi');
» xlabel('Sample number'); ylabel('Amplitude')

The first command establishes the vector = The next command generates the sine
wave vector with the same number of elements as . Note that the first index of is 1, not 0, with period
of and a total of 31 samples, consists of 4.75 cycles of the sine wave.
The Plot command, along with grid, title, xlabel and ylabel, causes the plot shown in the above figure.

With simple commands like these, one can usually create process and plot signals rapidly and efficiently.
Here are few more examples to generate and plot different signals.

1.1.2. Cosine wave:

» x=0:1.0:30;
» y=cos(2*pi*x/(2*pi));
» plot(y); grid on; title('cosine wave with period = 2pi');
» xlabel('Sample number'); ylabel('Amplitude')
Signal Processing Lab 2

1.2. Simultaneous Plot:

MATLAB provides facility to plot more than one signal on the same axis. One can plot multiple signals by
using a single Plot command or by holding the first signal plot before plotting the second signal. Hold on
and Hold off commands is used to hold the plot. Different colors and different line types can be used to plot
multiple signals on the same axis.

>> x=0:0.1:20;
>> y=sin(x);
>> z=cos(x);
>> plot(x,y,x,z)

Following sequence of commands also generate the same plot. This method holds first signal before
plotting the second signal.

>> plot(x,y)
>> hold on
>> plot(x,z,'g')
>> hold off

1.3. Sub plotting:


A single figure window can be split into several graphs with the SUBPLOT command. Here is an
example to get an idea about sub plotting.

>> t=linspace(0,20,300); %generates 300 equally spaced points between 0 and 20

>> x=sin(t);
>> y=cos(t);
>> z=tan(t);

>> subplot(2,2,1);
Signal Processing Lab 2

>> plot(t,x)
>> title('sine wave')
>> xlabel('time')
>> ylabel('Amplitude')

>> subplot(2,2,2);
>> plot(t,y)
>> title('cosine wave')
>> xlabel('time')
>> ylabel('Amplitude')

>> subplot(2,2,3);
>> plot(t,z)
>> title('tan wave')
>> xlabel('time')
>> ylabel('Amplitude')

Exercise 1:
Task#1: Investigate all the arguments (line type and line color) of Plot command by using MATLAB help.
Try to change the color and line type of the plot in the above examples.
Task#2: Investigate subplot command by using MATLAB help to understand the above example.
Task#3: Generate a decaying sinusoid with 1001 samples and spacing . Plot the result with
reference to tome. Try to change the color and line type of the plot. Switch on the grid. Use xlabel,
ylabel and title commands to name the X-axis, Y-axis and the plot.
Task#4: Make a function to solve task#3 and call it from the workspace to get the plot.
Task#5: Write a function y=u(t) to plot the unit step function, defined as zero for input argument value less
than zero and one for input argument values greater than and equal to zero.
Task#6: Write a function y=rect(t) to plot the rectangular function, Uses the definition of rectangle
function in terms of unit step function.
Task#7: Write a function y=tri(t) to plot a triangular function, defined as 1-|t| for |t| ≤1 and zero otherwise.

You might also like