matab no4

23
MATLAB graphics Designed by : Dawar Awan [email protected] March July 2012 CECOS College of Engineering and IT Lab No.04

Upload: moeen-khan-afridi

Post on 08-Jul-2015

48 views

Category:

Education


1 download

TRANSCRIPT

Page 1: matab no4

MATLAB graphics

Designed by : Dawar [email protected]

March – July 2012CECOS College of Engineering and IT

Lab No.04

Page 2: matab no4

March – July 2012CECOS College of Engineering and IT

Plot the discrete signal x = [1 2 3 4] in MATLAB

using stem(x) plots the signal starting from index number 1

Discrete signals

1

2

3

4

-1 0 1 2

Page 3: matab no4

Plot the following discrete time sequences

x[n]=[1 1 1 1 2 1 1 1 1]

x[n]=[5 4 3 2 1 0 1 2 3 4 5]

Practice

March – July 2012CECOS College of Engineering and IT

Page 4: matab no4

March – July 2012CECOS College of Engineering and IT

Continuous plot

>> plot(n,x)

Shows a continuous plot of x

Actually the discrete points are only connected with

straight lines.

Page 5: matab no4

March – July 2012CECOS College of Engineering and IT

Labeling the plot

Example:

Page 6: matab no4

March – July 2012CECOS College of Engineering and IT

Labeling the plot

Page 7: matab no4

March – July 2012CECOS College of Engineering and IT

Multiple plots

x = 0:0.01:5;

y1=sin(x);

y2=sin(2*x);

y3=sin(4*x);

plot(x,y1,x,y2,x,y3)

Using single “plot” instruction

Example:

Page 8: matab no4

March – July 2012CECOS College of Engineering and IT

Multiple plots

Page 9: matab no4

March – July 2012CECOS College of Engineering and IT

Multiple plots

x = 0:0.01:5;

y1=sin(x);

y2=sin(2*x);

y3=sin(4*x);

plot(x,y1);

hold on;

plot(x,y2);

plot(x,y3);

Using multiple “plot” instructions

Example:

Page 10: matab no4

March – July 2012CECOS College of Engineering and IT

Multiple plots

Page 11: matab no4

March – July 2012CECOS College of Engineering and IT

Changing default plot settings

x = 0:0.1:5;

y1=sin(x);

y2=sin(2*x);

y3=sin(4*x);

plot(x,y1,’+’,x,y2,':',x,y3,‘--');

Example:

Page 12: matab no4

March – July 2012CECOS College of Engineering and IT

Changing default plot settings

Page 13: matab no4

March – July 2012CECOS College of Engineering and IT

Changing default plot settings

line types and mark types

Check the help of “plot” for more effects.

Linetypes : solid (-), dashed (--), dotted (:), dashdot (-.)

Marktypes : point (.), plus (+), star (*), circle (o), x-mark (x)

Page 14: matab no4

March – July 2012CECOS College of Engineering and IT

Axis command

Used to manually zoom axis

axis([xmin xmax ymin ymax])

Example:

Page 15: matab no4

March – July 2012CECOS College of Engineering and IT

Axis command

Without axis command

Page 16: matab no4

March – July 2012CECOS College of Engineering and IT

Axis command

With axis command

Page 17: matab no4

March – July 2012CECOS College of Engineering and IT

SUBPLOT

x = 0:1:10;

y = x.^2;

z = 10*x;

subplot (1,2,1)

plot(x,y)

subplot (1,2,2)

plot(x,z)

Example:

subplot(m,n,p) : makes m rows and n columns of figures in figure windowand p shows the location of the current plot i.e the plot instruction just below the subplot

Page 18: matab no4

March – July 2012CECOS College of Engineering and IT

SUBPLOT

Page 19: matab no4

March – July 2012CECOS College of Engineering and IT

Generating a sine wave

Example: creating a 5Hz sine wave, sampled at 8000Hz.

f=5;

fs=8000;

t=[0:1/fs:1];

y=sin(2*pi*f*t);

plot(t,y);

Page 20: matab no4

March – July 2012CECOS College of Engineering and IT

Generating a sine wave

Observe 5 cycles per second.

Page 21: matab no4

March – July 2012CECOS College of Engineering and IT

Generating a sine wave

Example: Creating a 1000Hz sine wave, sampled at

8000Hz, and routing it to computer’s soundcard.

f=1000;

fs=8000;

t=[0:1/fs:3];

y=sin(2*pi*f*t);

sound(y,fs);

The sound will be heard for 3 seconds, as dictated by the code

Page 22: matab no4

March – July 2012CECOS College of Engineering and IT

Task1. Plot the two curves y1 = x and y2 = 2x on the same graph using

different plot styles. Label the plot properly

2. Write a MATLAB program that adds the following two signals,

Plot the original signals as well as their sum.

x1[n] = [2 2 2 2 2]

x2[n] = [2 2 2 2 0 0]

3. Scale the amplitude of the following signal by a factor of 2 and ½ and show the scaled signals along with the original.

x(t) = sin(t) + 1/3sin(3t) , t=0:0.01:10

2

Page 23: matab no4

March – July 2012CECOS College of Engineering and IT

Tasks

4. Demonstrate the use of the function ”plot3” to plot a straight 3D line