You are on page 1of 5

COMMUNICATION SYSTEM LAB NO.

LAB MANUAL
Objective:

 Derivation in Matlab
 Integration in Matlab
o Numerical computation
o Symbolic/ general computation
 Square wave synthesis
o From built in function
o By coding
 Fourier series

Derivation in Matlab:

You must first give MATLAB a list of the variable and function names that
will appear in the symbolic expressions you will be working with. Do this using
the command syms
>>syms x y z a;
This command basically tells the software that you will be using the symbols x,
y, z, and f in forthcoming symbolic expressions.

Code:
clc
clear all
close all
syms x;
f = x^2 + sin(x);
diff(f,x)
diff(f,x,2) %will give 2nd order derivative
diff(f,x,3) % 3rd order derivative
limit(f,x,0); % to take the limit of f as x goes to zero.

Integration in Matlab:

Integration is an important operation In signals and systems employed in several analysis techniques.
These techniques include calculating the energy, power, trigonometric and exponential Fourier series
and Fourier transform.

 Symbolic/ general computation:


clc
clear all
close all
syms x;
f = x^2 + sin(x);
int(f,x)

subs(f,{x,y},{1,pi/2}) % If you would like to substitute numerical values into a symbolic function, use

LAB INSTRUCTOR: Eng. SUNDAS FAROOQ KHAN / Eng. WASEEM RAZA


2K12 5TH SEMESTER
COMMUNICATION SYSTEM LAB NO. 4

the function subs as in the following way

int(f,x,1,a) % to get the definite integral from 1 to a.

To do double or triple integrals you need to manually use the int function multiple times.
 Numerical computation
Different methods through formulas
 quad command which uses an adaptive Simpson’s method
 trapz which uses Trapezoidal integration

“quad “command which uses an adaptive Simpson’s method

quad(’exp(-x.ˆ2)’,1,2)

ans =
0.1353

“trapz” which uses Trapezoidal integration

Code:
t = -1:0.01:3; %time
u_t=t>=0;
u_t_1=t>=2;
f=u_t-u_t_1;
I1 = trapz(t,f)

t = 0:0.01:pi/2; %time
g = sin(2*t); %g(t)=sin(2t)
I2 = trapz(t,g)

To synthesis a square wave

Graph a 1 KHz pulse wave of 5 volts peak amplitude. Choose the number of points as

power of two, i.e. 128, 256, 512. This selection for the number of points will help speed up

the computation of signal’s Fourier Transform.

MATLAB Program:

V=5; %the peak voltage

LAB INSTRUCTOR: Eng. SUNDAS FAROOQ KHAN / Eng. WASEEM RAZA


2K12 5TH SEMESTER
COMMUNICATION SYSTEM LAB NO. 4

f0 = 1000; % Fundamental frequency in Hertz

w0 = 2*pi*f0; % Fundamental frequency in radians

T=1/f0; % the period of the square wave

D=0.5; % pulse duty ratio

% plotting a square wave

dT=T/512; % incremental time,

t1=0:dT:D*T-dT; % Positive half period

t2=D*T:dT:T-dT; % Negative half period

t=[t1 t2]; %One complete period

v=V*[ones(size(t1)) -ones(size(t2))]; % square wave signal over a period

plot(t,v)

axis([0 T -10 10]) %Change the scale on the figure x-axis: 0 to T, y-axis: -10

Plotting square wave by using a built-in MATLAB function

v=V*square(w0*t); % keyword to generate a square wave in MATLAB

plot(t,v)

axis([0 T -10 10])

Fourier series /Synthesizing a square wave from harmonics

Synthesize the square wave from harmonics: (Use the Fourier Table for calculating harmonic
amplitudes)
(a) Fundamental.
(b) Fundamental and 3rd harmonic.
(c) Fundamental, 3rd and 5th harmonics.

LAB INSTRUCTOR: Eng. SUNDAS FAROOQ KHAN / Eng. WASEEM RAZA


2K12 5TH SEMESTER
COMMUNICATION SYSTEM LAB NO. 4

MATLAB Code for Harmonic Synthesis:


% harmonic amplitudes
V0=0;
V1= 4*V/pi;
V3= 4*V/(3*pi);
V5= 4*V/(5*pi);
V7= 4*V/(7*pi);
V9= 4*V/(9*pi);
v1 = V0+V1*sin(w0*t); % First (fundamental) harmonic
v13 = v1+V3*sin(3*w0*t); % First + Third harmonic
v135 =v13+V5*sin(5*w0*t); % First +Third +Fifth harmonic
subplot (3,2,1)
plot (t,v,t,v1)
axis ([ 0 .001 -15 15])
subplot(3,2,2)
plot(t,v,t,v13)
axis([0 .001 -15 15])
subplot(3,2,3)
plot(t,v,t,v135)

LAB TASK:
Q1:Take 1st , 2nd , 3rd , order derivative of function
f(x)= x2cos(x)+xsin(x)2
Q2:Find anti derivative for the function
f(x)=xsin(x)2
Q3:Evaluate integral
2
1 ∫sin(x)2dx
Q4:
(a) Write the code to add the 7th harmonic to obtain the waveform.
(b) Write the instructions to add the 9th harmonic to obtain the waveform.

LAB INSTRUCTOR: Eng. SUNDAS FAROOQ KHAN / Eng. WASEEM RAZA


2K12 5TH SEMESTER
COMMUNICATION SYSTEM LAB NO. 4

(c) ) Write a program to add Fundamental to 99th harmonic and display the resulting
waveform.
HINT: Add following lines to your MATLAB program before subplot instructions:
v99=0; %synthesized square wave % add only odd harmonics.
for n=1:2:99
v99 = v99+(4*V/(n*pi))*sin(n*w0*t);
end
subplot(3, 2, 6)
plot(t,v99)
axis([0 .001 -15 15])
The plots are shown below

LAB INSTRUCTOR: Eng. SUNDAS FAROOQ KHAN / Eng. WASEEM RAZA


2K12 5TH SEMESTER

You might also like