Sie sind auf Seite 1von 6

EXPERIMENT NO: 04 Introduction to different operations on Sequences & Plots

Sample Summation and Sample Product


It adds all sample values of x[n] between n1 and n2. It is implemented by sum(x(n1:n2)). Sample multiplication multiplies all sample values of x[n] between n1 and n2. It is implemented by prod(x(n1:n2)). MATLAB CODE FOR SUMMATION: x=[1 2 3 4 5 6]; n1=x(1,3); n2=x(1,5); sum(x(n1:n2)) RESULT: ans =12 MATLAB CODE FOR PRODUCT: x=[1 2 3 4 5 6]; n1=x(1,3); n2=x(1,5);

prod(x(n1:n2)) RESULT: ans =60

Signal Energy
Energy of a finite duration sequence x[n] can be computed in MATLAB using >>Ex=sum(x.*conj(x)); or >>Ex=sum(abs(x).^2); MATLAB CODE: x=[1 2 3]; Ex=sum(abs(x).^2) RESULT: Ex =14

Signal multiplication
In MATLAB, two signals are multiplied sample by sample using array operator *. Similar instructions regarding position vector imply for multiplication as for addition. Practice: Write a MATLAB function simult to multiply two signals x1[n] and x2[n], where x1[n] and x2[n] may have different durations. Call this function to multiply any two signals.

x1[n] = [1 3 4 8 9 10] x2[n] = [2 5 7 2 3 11] y = x1* x2

Random Signals
Many practical signals cannot be created by mathematical expressions like those above. These signals are called random or stochastic signals. A random signal of length N with samples uniformly distributed in the interval (0,1) can be generated by using the MATLAB command x = rand(1,N); Above mentioned functions can be transformed to generate other random sequences. EXAMPLE: >> rand(1,5) RESULT: ans = 0.0975 0.2785 0.5469 0.9575 0.9649

Multiple Plots
In MATLAB, it is possible to plot multiple functions on the same graph by including more than one set of (x,y) values in the plot function. Example: We want to plot f(x) = sin2x and its derivative on the same plot. The derivative of f(x) = 2cos2x.

Matlab Code: x=0:pi/100:2*pi; y1= sin(2*x); y2= 2* cos (2*x); plot(x,y1,x,y2); Result:
Check your result on Matlab.

Multiple Plots on Same Axes


Normally , a new plot is created each time that a plot command is issued, and the previous data are lost. This behaviour can be modified with the hold command. After hold on command is issued, all additional plots will be laid on top of the previously existing plots. A hold off switches plotting behavior back to default situation, in which a new plot replaces the previous one.

EXAMPLE: Following commands will plot sinx and cosx on the same axes. x = -pi:pi/20;pi; y1 = sin(x); y2 = cos(x); plot(x,y1, b-);% () Is apostrophe hold on; plot(x,y2, k--); hold off; legend (sin x, cos x);

SubPlots
It is possible to place more than one set of axes on a single figure, creating multiple subplots. Subplots are created with a subplot command of the form Subplot (m,n,p) This command divides the current figure into m*n equal sized regions, arranged in m rows and n columns, and creates a set of axes at position p to receive aldl current plotting commands. EXAMPLE:

Subplot (2, 3, 4) would divide the current figure into six regions arranged in 2 rows and 3 columns, and creates an axis in position 4 ( the lower left one) to accept new plot data.

MATLAB Code:
Subplot (2,1,1); x = -pi:pi/20:pi; y = sin(x); plot (x,y); title ( subplot 1 title); Subplot (2,1,2); x = -pi:pi/20:pi; y = cos(x); plot (x,y); title ( subplot 2 title);

Das könnte Ihnen auch gefallen