You are on page 1of 10

EXPERIMENT 1

I n t r o d u c t i o n t o M AT L A B
Questions:

p=3 sin ( x 2 ) +2 cos ( y 3 )

Question 1) Add the functions

and

q=3 cos ( xy ) +2 y 2 .

y=0.05 x +2.01

x is in the range 0 to 5 with an increment of 0.01.

in all cases. Plot all

three functions p,q, and z=p+q. Verify the correctness of your plot from the figures below.
Write your code on the space provided.
Note:
When a dot precedes an operator, as using .* for multiplication, it implies that each element
in the vector (matrix) results from applying that operator to corresponding elements in the
first and second vectors (matrices). For example, dot multiplication of two m x 1 vectors
results in an m x 1 vector where each element is the product of the corresponding elements
in the first and second vectors. This type of multiplication requires that the vectors
(matrices) must be of the same size and is called pointwise, rather than vector or matrix,
multiplication.
Write your code here:
clf;
x=[0:0.01:5];
y=0.05.*x+2.01;
p=3*sin(x.^2)+2*cos(y.^3);
q=3*cos(x.*y)+2*y.^2;
z=p+q;
figure(1),plot(p);
figure(2),plot(q);
figure(3),plot(z);
4
3
2
1
0
-1
-2
-3
-4
-5

100

200

300

400

500

Figure 1.2 Plot of p

600

13
12
11
10
9
8
7
6
5

100

200

300

400

500

600

Figure 1.3 Plot of q


14

12

10

100

200

300

400

500

600

Figure 1.4 Plot of z

y (t )=( 1e 2.2 t ) cos ( 60 t )

Question 2) Plot the function

Use t from 0 to 0.25 in 0.001

increments.
Answer: (Sketch or attach the resulting figure below)
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8

50

100

150

200

250

300

Question 3) A polynomial function has roots at -2, 2, -2+3j, -2-3j.


a) Determine the polynomial and plot the four roots in the complex plane. You may
wish to use help to look up the functions poly, plot, grid, and axis. Verify the
correctness of your own plot to be similar to the one shown below.
Write your code on the space provided:
clf;
r=[-2 2 -2+3j -2-3j]
p=poly(r)
plot(r,'k*');
axis([-4 4 -4 4]);
grid;
%optional

The polynomial is

x4+4x3+9x2-16x-52
4
3
2
1
0
-1
-2
-3
-4
-4

-3

-2

-1

b) Plot the polynomial function for the range of x from -5 to 5. You may wish to use
help to look up the functions polyval, and linspace to obtain a plot similar to the
one shown below .
1400
1200
1000
800
600
400
200
0
-200
-5

-4

-3

-2

-1

Plot of the given polynomial


Write your code below:
clf;
r=[-2 2 -2+3j -2-3j];
p=poly(r);
x=linspace(-5,5);
y=polyval(p,x);
plot(x,y);
Question 4) The complex function f(t) has the form:

f ( t )=3 e j 2 t + /4

Plot the real and imaginary parts as a function of time from 0 to 3 seconds in 0.01second increments. Also plot the magnitude and phase of f as a function of time. All
four plots must be contained in one graphics window. You may wish to look up the
functions subplot, title, and plot to see how to generate more than one plot in the
graphics window at the same time. You will also need to look up the functions abs

and angle. Verify the correctness of your own plot to be similar to the one shown
below.

Re(f(t))

8
6

-2

-2

-4

-4

-6
-8

Im(f(t))

-6
0

0.5

1.5

2.5

Magnitude of f(t)

10

-8

0.5

2.5

2.5

Phase of f(t)

1.5

6
0
4
-2

2
0

0.5

1.5

2.5

-4

0.5

1.5

Write your code below:


clf;
t=0:0.01:3;
f=3*exp(-j*2*pi*t+pi/4);
zr=real(f);
subplot(2,2,1);
plot(t,zr);axis([0 3 -8 8]);
title('Re(f(t))');
zi=imag(f);
subplot(2,2,2);
plot(t,zi);axis([0 3 -8 8]);
title('Im(f(t))');
mag=abs(f);
subplot(2,2,3);
plot(t,mag);axis([0 3 0 10]);
title('Magnitude of f(t)');
a=angle(f);
subplot(2,2,4);
plot(t,a);
title('Phase of f(t)');
EXPERIMENT 2
Discrete Time Signal
Generation
% Exp2_1
% Generation of a Unit Sample Sequence
clf;
% Generate a vector from -10 to 20
n = -10:20;
% Generate the unit sample sequence
s = [zeros(1,10) 1 zeros(1,20)];
% Plot the unit sample sequence
stem(n,s);
xlabel('Time index n'); ylabel('Amplitude');
title('Unit Sample Sequence');
axis([-10 20 0 1.2]);
STEP 1

Create an m-file for the program above and then execute. Draw the figure generated.

Unit Sample Sequence

Amplitude

0.8

0.6

0.4

0.2

0
-10

The
The
The
The
The

-5

purpose
purpose
purpose
purpose
purpose

5
Time index n

of
of
of
of
of

10

15

20

clf command is - clear the current figure


axis command is - control axis scaling and appearance
title command is add a title to graph or an axis and specify text properties
xlabel command is add a label to the x-axis and specify text properties
ylabel command is add label to the y-axis and specify the text properties

STEP 2 Modify Program EXP2_1 to generate a delayed unit sample sequence sd[n] with a delay of
11 samples by completing the code below:
% Generation of a Delayed Unit Sample Sequence
clf;
n = -10:20;
sd = [zeros(1,21) 1 zeros(1,9)];
%or
sd =[zeros(1,10) zeros(1,11) 1 zeros(1,9)];
stem(n,sd);
xlabel('Time index n'); ylabel('Amplitude');
title('Delayed Unit Sample Sequence');
axis([-10 20 0 1.2]);
STEP 3

Modify Program Exp2_1 to generate a unit step sequence u[n] similar to the plot shown.
Write your code below.
Unit Step Sequence

% Generation of a Unit Step


Sequence
clf;
n = -10:20;
u = [zeros(1,10)
ones(1,21)];
stem(n,u);
xlabel('Time index n');
ylabel('Amplitude');
title('Unit Step
Sequence');
axis([-10 20 0 1.2]);

Amplitude

0.8

0.6

0.4

0.2

0
-10

-5

5
Time index n

10

15

20

STEP 4
Modify the Program
Exp2_1 to generate a unit step
sequence ua[n] with an advance
of 7 samples by completing the
code below:

% Generation of a Unit Step Sequence with an advance of 7 samples


clf;
n = -10:20;
ua = [zeros(1,3) ones(1,28)];
stem(n,ua);
xlabel('Time index n'); ylabel('Amplitude');
title(' Unit Step Sequence with an advance of 7 samples');

axis([-10 20 0 1.2]);

% Program Exp2_2
% Generation of a complex exponential sequence
clf;
c = -(1/12)+(pi/6)*i;
K = 2;
n = 0:40;
x = K*exp(c*n);
subplot(2,1,1);
stem(n,real(x));
xlabel('Time index n');ylabel('Amplitude');
title('Real part');
subplot(2,1,2);
stem(n,imag(x));
xlabel('Time index n');ylabel('Amplitude');
title('Imaginary part');
STEP 5

Create an m-file for the program above and then execute.

Answer the

following questions:
Which of the figures below is generated? Both were generated

The parameter controlling the rate of growth or decay of this sequence is the real
part of c.
The parameter controlling the amplitude of this sequence is the K parameter.
The result of changing the parameter c to (1/12)+(pi/6)*i is the envelope of the signal will
grow with n
The purpose of the operator real is to extract the real component of a complex number or
matrix.
The purpose of the operator imag is to extract the imaginary component of the complex
number or matrix.
The purpose of the command subplot is to plot more than one graph in the same Matlab
figure.
STEP 6
The following code can be used to generate a real-valued exponential
sequence:
% Program Exp2_3
% Generation of a real exponential sequence
clf;
n = 0:35; a = 1.2; K = 0.2;
x = K*a.^n;
stem(n,x);
xlabel('Time index n');ylabel('Amplitude');
Execute the given code and observe the figure. Which of the following figures is
produced? Figure(a)

The parameter controlling the rate of growth or decay of this sequence is the a parameter
The parameter controlling the amplitude of this sequence is the K parameter
The
difference
between
the
arithmetic
operators
^
and
.^
is
_________________________________.
^ raises a square matrix to a power using matrix multiplication.
.^ raises the
elements of a matrix or vector to a power (pointwise operation)
STEP 7 Draw the sequence generated by running Program Exp2_3 with the parameter a changed
to 0.9 and the parameter K changed to 20.
20
18
16

Amplitude

14
12
10
8
6
4
2
0

10

15
20
Time index n

25

30

35

The length of this sequence is 36


It is controlled by the following MATLAB command line: n=0:35;
It can be changed to generate sequences with different lengths as follows (give an
example command line and the corresponding length): n=0:99; this gives a length of 100
You can use the MATLAB command sum(s.*s) to compute the energy of a real
sequence s[n] stored as a vector s. Evaluate the energy of the real-valued
exponential sequences x[n] generated when
a) a = 1.2; K = 0.2;
Energy: 4.5673e+004
b) a = 0.9; K = 20; Energy: 2.1042e+003
Sinusoidal sequences
The following code can be used to generate a real sinusoidal sequence. Such a sequence
can be generated using the trigonometric operators cos and sin.
% Program Exp2_4
% Generation of a sinusoidal sequence
n = 0:40;
f = 0.1;
phase = 0;
A = 1.5;
arg = 2*pi*f*n + phase;
x = A*cos(arg);
clf;
stem(n,x);
axis([0 40 -2 2]);
grid;

title('Sinusoidal Sequence');
xlabel('Time index n');
ylabel('Amplitude');
axis;
STEP 8

Draw the sinusoidal sequence generated by running Program Exp2_4


Sinusoidal Sequence

2
1.5
1

Amplitude

0.5
0
-0.5
-1
-1.5
-2

10

15

20
25
Time index n

30

35

40

The frequency of this sequence is f=0.1 cycles/sample


The frequency is controlled by the following MATLAB command line f=0.1;
A sequence with new frequency 0.7 can be generated by the following command line:
f=0.7;
The parameter controlling the phase of this sequence is phase
The parameter controlling the amplitude of this sequence is A
The period of this sequence is 10
The length of this sequence is 41
The length is controlled by the following MATLAB command line: n=0:40;
A sequence with new length 50 can be generated by the following command line: n=0:49;
The average power of the generated sinusoidal sequence is
sum(x(1:10).*x(1:10))/10=1.1250
The purpose of axis command is to set the range of the x-axis to [0,40] and the range of
the y-axis to [-2,2].
The purpose of grid command is to turn on the drawing of grid lines on the graph.
STEP 9 Create a sinusoidal sequence of length 50, frequency 0.08, amplitude 2.5, and phase shift
of 90 degrees by modifying Program Exp2_4 and write the code below: The period of this
sequence is 25.
STEP 10
What happens when the stem command in Program Exp2_4 is replaced with
the plot command? the plot command connects the points with straight line
segments, which approximates the graph of a continuous-time cosine signal.
STEP 11
What happens when the stem command in Program Exp2_4 is replaced with the
stairs command? stairs command produces a stairstep plot.

EXPERIMENT 3
Other Sig na l Wa veform
Generation
% Generation of a sinusoidal sequence
n = 0:40;
f = 0.1;
phase = 0;
A = 1.5;
arg = 2*pi*f*n + phase;
x = A*cos(arg);
clf;
stem(n,x);
axis([0 40 -2 2]);
grid;
title('Sinusoidal Sequence');

xlabel('Time index n');


ylabel('Amplitude');
axis;

MATLAB CODES
%Generationofthesquarewave
n=0:30;
f=0.1;
phase=0;
duty=60;
A=2.5;
arg=2*pi*f*n+phase;
x=A*square(arg,duty);
clf;%Clearoldgraph
stem(n,x);%Plotthegeneratedsequence
axis([03033]);
grid;
title('SquareWaveSequence');
xlabel('Timeindexn');
ylabel('Amplitude');
axis;

%Generationofthesawtoothwave1

n=0:50;
f=0.05;
phase=0;
peak=1;
A=2.0;
arg=2*pi*f*n+phase;
x=A*sawtooth(arg,peak);
clf;%Clearoldgraph
stem(n,x);%Plotthegeneratedsequence
axis([05022]);
grid;
title('SawtoothWave1');
xlabel('Timeindexn');
ylabel('Amplitude');
axis;

%Generationofthesawtoothwave2
n=0:50;
f=0.05;
phase=0;
peak=0.5;
A=2.0;
arg=2*pi*f*n+phase;
x=A*sawtooth(arg,peak);
clf;%Clearoldgraph
stem(n,x);%Plotthegeneratedsequence
axis([05022]);
grid;
title('SawtoothWave2');
xlabel('Timeindexn');
ylabel('Amplitude');
axis;

You might also like