Lab 2
Lab 2
Some examples will serve here to show how simple commands can readily be used to produce useful
signals and plots.
» 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.
» 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
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
>> 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.