You are on page 1of 22

MATLAB

Experiment No. 1 AIM: To plotting two dimensional plots using MATLAB. SOFTWARE: MATLAB 7 THEORY: MATLAB can produce two-dimensional plots. The primary command for this is plot. The plot command creates linear xy plots; if x and y are vectors of the same length, the command plot(x,y) opens a graphics window and draws an xy plot of the elements of y versus the elements of x. The graphs can be given titles, axes labeled, and text placed within the graph with the following commands, which take a string as an argument. title : graph title xlabel : x-axis label ylabel : y-axis label gtext : place text on graph using the mouse text : position text at specified coordinates

The command subplot(m,n,p) partitions a single figure into an m-by-n array of panes, and makes pane p the current plot. The panes are numbered left to right. A subplot can span + We can draw multiple plots using MATLAB and can assign Line types, marker types, colors etc to graph. The program should be written and edited on editor window and should be run on command window.

Editor window

Command window

Here the example illustrating the function of these above commands.

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

PROGRAM: To plot y = sin (x): x = (0:.01:2*pi)'; y = [sin(x)]; plot(x, y); xlabel('X Label'); ylabel('Y Label') title(' Plot of y = Sin(x)') This program will display output on graphics window as:
Plot of y = Sin(x) 1 0.8 0.6 0.4 0.2

Y Label

0 -0.2 -0.4 -0.6 -0.8 -1

3 X Label

To draw multiple plots on same graph we use commands as: x = 0:.01:2*pi; y1 = sin(x) ; y2 = sin(2*x) ; y3 = sin(4*x) ; plot(x, y1, x, y2, x, y3) And then the output will be:
Plot of y = Sin(x) 1 0.8 0.6 0.4 0.2

Y Label

0 -0.2 -0.4 -0.6 -0.8 -1

3 X Label

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

We can override the default line types, marker types, and colors as: x = 0:.01:2*pi ; y1 = sin(x) ; y2 = sin(2*x) ; y3 = sin(4*x) ; plot(x,y1, '--', x,y2, ':', x,y3, 'o') xlabel('X Label'); ylabel('Y Label') title(' Plot of y = Sin(x)') This renders a dashed line and dotted line for the first two graphs, whereas for the third the symbol o is placed at each node. Similarly Colors can be specified for the line and marker types. The output will be:
Plot of y = Sin(x) 1 0.8 0.6 0.4 0.2

Y Label

0 -0.2 -0.4 -0.6 -0.8 -1

3 X Label

Now we can put text in graph using gtext command as: x = 0:.01:2*pi ; y1 = sin(x) ; y2 = sin(2*x) ; y3 = sin(4*x) ; plot(x,y1, '--', x,y2, ':', x,y3, 'o') xlabel('X Label'); ylabel('Y Label'); title(' Plot of y = Sin(x)'); 1 gtext ('y1=sin(x)'); 0.8 gtext ('y2=sin(2*x)'); y1=sin(x) 0.6 gtext ('y3=sin(4*x)'); y2=sin(2*x)
0.4

Plot of y = Sin(x)

This displays output as:


Y Label

0.2 0 -0.2 -0.4 -0.6 -0.8 -1

y3=sin(4*x)

3 X Label

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

To draw same graph separately using subplots we have use commands as: x = 0:.01:2*pi ; y1 = sin(x) ; y2 = sin(2*x) ; y3 = sin(4*x) ; subplot(2,2,1) plot(x,y1, '--') xlabel('X Label'); ylabel('Y Label'); title(' Plot of y1 = Sin(x)'); gtext ('y1=sin(x)'); subplot(2,2,2) plot(x,y2, ':') xlabel('X Label'); ylabel('Y Label'); title(' Plot of y2 = Sin(x)'); gtext ('y2=sin(2*x)'); subplot(2,2,[3 4]) plot(x,y3, 'o') xlabel('X Label'); ylabel('Y Label'); title(' Plot of y3 = Sin(x)'); gtext ('y3=sin(4*x)'); Then the output is:
Plot of y1 = Sin(x) 1 0.5 y1=sin(x) 1 0.5 y2=sin(2*x) Plot of y2 = Sin(x)

Y Label

0 -0.5 -1

Y Label
0 2 4 X Label 6 8

0 -0.5 -1

4 X Label

Plot of y3 = Sin(x) 1 0.5 y3=sin(4*x)

Y Label

0 -0.5 -1

3 X Label

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

Experiment No. 2 AIM: To plotting three dimensional plots using MATLAB. SOFTWARE: MATLAB 7 THEORY: MATLAB can also produce three-dimensional plots. The primary command for this is plot3. The command plot3 produces curves in three-dimensional space. If x, y, and z are three vectors of the same size, then the command plot3(x,y,z) produces a perspective plot of the piecewise linear curve in three space passing through the points whose coordinates are the respective elements of x, y, and z. PROGRAM: To Plot 3D plot between XYZ: t = .01:.01:20*pi ; x = cos(t) ; y = sin(t) ; z = t.^3 ; plot3(x, y, z); xlabel('X Label'); ylabel('Y Label'); zlabel('Z Label'); title(' 3D plot for xyz'); This gives output as:

3D plot for xyz


5

x 10 2.5 2 1.5 1 0.5 0 1

Z Label

0.5 0 -0.5 Y Label -1 -1 -0.5 X Label 0.5 0

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

And for following program: t = .01:.01:20*pi ; x = cos(t) ; y = sin(t) ; z = t.^3 ; plot3(x, y, z, 'g+'); xlabel('X Label'); ylabel('Y Label'); zlabel('Z Label'); title(' 3D plot for xyz'); The output will be:
3D plot for xyz
5

x 10 2.5 2 1.5 1 0.5 0 1

Z Label

0.5 0 -0.5 Y Label -1 -1 -0.5 X Label 0.5 0

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

Experiment No. 3 AIM: To generate different types of signals used in signal processing using MATLAB. SOFTWARE: MATLAB 7 THEORY: In signal processing following signal sequences are used: unit impulse, unit step, ramp, exponential, sinusoidal and cosine sequences

The generations of these above sequences using MATLAB are as follows. In MATLAB to plot any discrete sequence the basic command is stem.

PROGRAM:

1. Program for the generation of unit impulse signal: t=-2:1:2; y=[zeros(1,2),ones(1,1),zeros(1,2)]; subplot(2,2,1); stem(t,y); ylabel('Amplitude --->'); xlabel('(a) n --->'); title('Unit Impulse Signal');

2.

Program for the generation of unit step sequences: n=7; t=0:1:n-1; y1=ones(1,n); subplot(2,2,2); stem(t,y1); ylabel('Amplitude --->'); xlabel('(b) n --->'); title('Unit Step Sequence');

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

3. Program for the generation of ramp sequences: n1=7; t=0:1:n1; subplot(2,2,3); stem(t,t); ylabel('Amplitude --->'); xlabel('(c) n --->'); title('Ramp Sequence');

4. Program for the generation exponential sequences: n2=5; t=0:1:n2; y2=exp(t); subplot(2,2,4); stem(t,y2); ylabel('Amplitude --->'); xlabel('(d) n --->'); title('Exponential Sequence');

5. Program for the generation sine sequences: t=0:.01:pi; y=sin(2*pi*t); figure (2); subplot(2,1,1); plot(t,y); ylabel('Amplitude --->'); xlabel('(a) n --->'); title('Sine Sequence');

6. Program for the generation cosine sequences: t=0:.01:pi; y=cos(2*pi*t); figure (2); subplot(2,1,2); plot(t,y); ylabel('Amplitude --->'); xlabel('(b) n --->'); title('Cosine Sequence');

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

Output for all these sequences are as shown by graphics window:

Unit Impulse Signal 1 1

Unit Step Sequence

Amplitude --->

0.5

Amplitude --->
-1 0 1 (a) n ---> Ramp Sequence 2

0.5

0 -2

2 4 (b) n ---> Exponential Sequence

150

Amplitude --->

6 4 2 0

Amplitude --->
0 2 (c) 4 6 n ---> 8

100

50

2 (d)

4 n --->

Sine Sequence 1

Amplitude --->

0.5 0 -0.5 -1

0.5

1.5 2 (a) n ---> Cosine Sequence

2.5

3.5

Amplitude --->

0.5 0 -0.5 -1

0.5

1.5 (b)

2 n --->

2.5

3.5

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

Experiment No. 4 AIM: To study the comparison between discrete and continuous ramp signal using MATLAB. SOFTWARE: MATLAB 7. THEORY: A unit ramp signal can be defined as a continuous time or discrete time signal. A continuous time ramp signal is denoted by R (t) and it increases linearly with time. Mathematically it is expressed as, R (t) = {1 0 for t0 for t<0};

A discrete time ramp is denoted by Ur (n). Its value increases linearly with sample number n and is given as, Ur (n) = {1 0 1 for n0 for n>0};

PROGRAM:

n=8; x = [0:n]; y = [2*x]; subplot (2,1,1); stem (x,y); xlabel('(a) n samples--->') ylabel('Amplitude --->') title ('Discrete Ramp Signal') subplot (2,1,2) x=[0:n]; y=[2*x]; plot (x,y) xlabel('(b) time --->') ylabel('Amplitude --->') title ('Continuous Ramp Signal')

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

The output of above program is:

Discrete Ramp Signal 20


Amplitude --->

15 10 5 0

3 4 5 (a) n samples---> Continuous Ramp Signal

20
Amplitude --->

15 10 5 0

4 (b) time --->

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

Experiment No. 5 AIM: Write a program for computing the linear convolution of two discrete sequences using MATLAB. SOFTWARE: MATLAB 7. THEORY: Convolution is defined as the mathematical operation to correlate two sequences and it gives the response of LT system as a function of input sequence x(n) and unit impulse sequence h(n). Let two signals be x(n)and h(n) Then their convolution is given by

The convolution of two sequences can be computed using MATLAB which is illustrated as below. PROGRAM: Program for linear convolution of the sequence x= [1, 2] and h= [1, 2, 4]. x=[1 2]; h=[1 2 4]; y=conv(x,h); subplot(3,1,1); stem(x); ylabel('Amplitude --->'); xlabel('(a) n --->'); title(' x Input'); subplot(3,1,2); stem(h); ylabel('Amplitude --->'); xlabel('(b) n --->'); title(' h Input'); subplot(3,1,3); stem(y); ylabel('Amplitude --->'); xlabel('(c) n --->'); title(' Y Output(convolution of x and h');

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

The output of this program is:

Amplitude --->

x Input 2 1 0 1 1.1 1.2 1.3 1.4 1.5 1.6 (a) n ---> h Input 1.7 1.8 1.9 2

Amplitude --->

4 2 0 1 1.2 1.4 1.6 2 2.2 2.4 (b) n ---> Y Output(convolution of x and h) 1.8 2.6 2.8 3

Amplitude --->

10 5 0 1 1.5 2 2.5 (c) n ---> 3 3.5 4

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

Experiment No. 6 AIM: Write a program that illustrates folding of sequence using MATLAB. SOFTWARE: MATLAB 7. THEORY: In the folding operation each sample of x(n) sequence is flipped around n = 0 to obtain folded sequence y(n) This is expressed as: ( ) Folding of signal is also called time reversal. PROGRAM: Program for folding the input sequence X= [1 2 3 4 5] t=-2:1:2; x=[1 2 3 4 5]; subplot(2,1,1); stem(t,x); title('input signal'); ylabel('ampli---->'); xlabel('n--->'); t1=t.*-1; subplot(2,1,2); stem(t1,x); title('folded signal'); ylabel('ampli--->'); xlabel('n---->'); Output of this program is:
input signal 6

ampli---->

0 -2

-1.5

-1

-0.5

0 0.5 n---> folded signal

1.5

ampli--->

0 -2

-1.5

-1

-0.5

0 n---->

0.5

1.5

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

Experiment No. 7 AIM: Write a program that illustrates time shifting of sequence using MATLAB. SOFTWARE: MATLAB 7. THEORY: In the shifting operation each sample of x(n) is shifted by an amount of k to obtain a shifted sequence y(n) Here the y(n) is expressed as: ( ) ( )

The time shifting operation is carried out in MATLAB is as following. PROGRAM: Program for shifting the input sequence: t=-2:1:2; x=[1 2 3 4 5]; subplot(2,1,1); stem(t,x); title('input sgnal'); ylabel('ampli--->'); xlabel('n---->'); n=2; t1=t+n; subplot(2,1,2); stem(t1,x); title('shifting'); ylabel('ampli---->'); xlabel('n----->'); Output Window Shows:
input sgnal 5 4

ampli--->

3 2 1 0 -2 -1.5 -1 -0.5 0 n----> shifting 0.5 1 1.5 2

ampli---->

0.5

1.5

2 n----->

2.5

3.5

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

Experiment No. 8 AIM: To study basic commands for image processing in MATLAB. SOFTWARE: MATLAB 7. THEORY: The purpose of this experiment is for us to understand some fundamental concepts of image processing using MATLAB. In this, we will learn some basic MATLAB commands for image processing. When working with images in Matlab, there are many things to keep in mind such as loading an image, using the right format, saving the data as different data types, how to display an image, conversion between different image formats, etc. Reading and Writing an image in MATLAB: Operation Reading the image Writing the image Saving and loading an image: Operation saving the image loading the image Displaying an image: Operation Displaying an image Displaying an image as matrix X Zoom in (using the left and right mouse button). Turn off the zoom function. command imshow(X) imagesc(X) Zoom in Zoom off command Save X variable Load X variable command imread( ) Inwrite( , )

Various commands provide information about a variable image. size(p) whos(p) The figure command can be used to create a new current figure for the display: figure, imshow(p); The command iminfo can be used to retrieve information about image files: iminfo('filename')

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

The example illustrating all these above commands is explained in next part of this experiment. PRAGRAM: Here the program for all basic commands mentioned in this section over the image named cell.png at location c:\cell.png P=imread('c:\cell.png'); save P; clc; after this command everything is clears from command window load P; imshow(P); Output after these commands is:

whos; Name P Size 512x512x3 Bytes 786432 Class uint8 Attributes

Now for reducing the image i=1:256; j=1:256; Pred(i,j)=P(i,j); whos; Name Size P Pred i j
Sarvjeet Singh Sohal

Bytes

Class uint8 uint8 double double

Attributes

512x512x3 256x256 1x256 1x256

786432 65536 2048 2048

Regd. No.: 1269922

MATLAB

imshow(Pred);

The reduced image is:

Convert between double and uint8 Q=im2double(P); whos; Name Size P Pred Q i j 512x512x3 256x256 512x512x3 1x256 1x256

Bytes 786432 65536 6291456 2048 2048

Class uint8 uint8 double double double

Attributes

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

Experiment No. 9 AIM: Write a MATLAB program to corrupt the image with impulse noise and also study the image format conversion and image histogram. SOFTWARE: MATLAB 7. THEORY: Image processing involves changing the nature of an image in order to either Improve its pictorial information for human interpretation. Render it more suitable for machine perception.

Image histogram: An image histogram is a chart that shows the distribution of intensities in an image. Each color level is represented as a point on x-axis and on y-axis is the number instance a color level repeats in the image. Histogram may be view with imhist command. To balance the brightness level, we carry out an image processing operation termed histogram equalization. Command for image equalization is histeq().

The image can corrupted using the command imnoise. The noise is added to the image using this command.

Image format conversion: The following table shows how to convert between the different formats:

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

PROGRAM: I=imread('D:\Picture1.png'); imshow(I);

size(I) ans = 511 511 3 igrey=rgb2gray(I); figure; imshow(igrey);

figure; imhist(igrey); i2=histeq(igrey); figure; imshow(i2);


3000 2500 2000 1500 1000 500 0 0 50 100 150 200 250

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

figure; imhist(i2);
5000 4500 4000 3500 3000 2500 2000 1500 1000 500 0 0 50 100 150 200 250

i3=imnoise(i2); imshow(i3);

Sarvjeet Singh Sohal

Regd. No.: 1269922

MATLAB

Experiment No. 9 AIM: To develop program for designing IIR filter. APPARATUS: PC having MATLAB software. PROGRAM:
%prog for designing of FIR low pass % filters using rectangular window clc;clear all;close all; rp =input('Enter the pass band ripple '); rs =input('Enter the stop band ripple '); fp =input('Enter the pass band freq '); fs =input('Enter the stop band freq '); f =input('Enter the sampling freq '); wp = 2*fp/f; ws = 2*fs/f; num = -20*log10(sqrt(rp*rs))-13; dem = 14.6*(fs-fp)/f; n = ceil(num/dem); n1 = n+1; if (rem(n,2)~=0) n1 =n; n = n-1; end y = boxcar(n1); % low pass filter b = fir1(n,wp,y); [h,o] = freqz(b,1,256); m = 20*log(abs(h)); subplot(2,2,1);plot(o/pi,m); ylabel('Gain in dB ------>'); xlabel('(a) Normalised freq ---->');

Sarvjeet Singh Sohal

Regd. No.: 1269922

You might also like