You are on page 1of 15

REPORT

 HO CHI MINH UNIVERSITY OF TECHNOLOGY 

 DIGITAL SIGNAL PROCESSING


LABORATORY
 Major Project: Pulse Width Modulation
 Class-date: Thursday, Group: 8
 Nguyễn Thanh Khải Tâm -1651091 No: 11
 Nguyễn Thiện Cường – 1651015 No: 12
 Instructor: Prof. Le Tien Thuong
 Class: TT16DDT03
REPORT

I. Abstract:
Signal modulation is an important technique in the field of
communication and telecommunications also power control. One of
the analog signal modulation techniques was the pulse width
modulation technique. In this report, we mentioned about the theory
of the pulse width modulation and conduct experiment about PWM
application in Digital Signal Processing using MATLAB and kit
TMS320C5515.
II. Introduction to project:
1. Aim:
- To know about Duty cycle, pulse width, pulse width modulation and
how to apply it.

2. Relevant Theories:
Digital Signal: A signal that is being used to represent data as a sequence of
discrete values; at any given time it can only take on one of a finite number
of values.

Analog Signal: Represents continuous values; at any given time it represents


a real number within a continuous range of values.

Duty cycle: Duty cycle is measured in percentage. The percentage duty


cycle specifically describes the percentage of time a digital signal is on over
an interval or period of time. This period is the inverse of the frequency of
the waveform.

2
REPORT

100% duty cycle would be the same as setting the voltage to


5 Volts (high). 0% duty cycle would be the same as
grounding the signal.
Pulse Width: Pulse width is the time when the signal is on in a cycle

Pulse width modulation (PWM), is a modulation technique that


controls the width of an electrical pulse (in time), in the cycle of the pulse,
based on modulated signal information. Although this modulation technique
can be used to encode information for data transmission, its main use is the
control of the electrical energy supplied to electrical equipment, especially
motors.

PWM is not true analog output, however. PWM “fakes” an analog-like


result by applying power in pulses, or short bursts of regulated voltage. (Cite:
Heath, J. (2017). PWM: Pulse Width Modulation: What is it and how does it
work? [online] Analog IC Tips.)

3
REPORT

Pulse width modulation uses a rectangular impulse wave with


modulated pulse width resulting in a change of the average of the waveforms.
If we consider a f (t) pulse waveform, with time T, with a low value ymin, a
high value ymax and a duty cycle D (duty cycle), the average of the waveform
is given by

y = D. ymax+(1-D)ymin .

where ymin = 0, the average y depends on the value of D.

4
REPORT

III. Conduct Experiment:


1. MATLAB based simulation
clear;
clc;
F2 = input('Message frequency = ');
F1 = input('Carrier wave frequency = ');
%D = input('PWM duty cycle within [0;1] = ');
Amplitude = 5;
t = 0:0.001:1;
c = Amplitude.*sawtooth(2*pi*F1*t); % Carrier sawtooth
subplot(3,1,1);
plot(t,c);
xlabel('t'); ylabel('Amplitude'); title('Carrier sawtooth
wave');
grid on;
m=0.75*Amplitude*sin(2*pi*F2*t); % Message signal
subplot(3,1,2);
plot(t,m);
xlabel('t'); ylabel('Amplitude'); title('Message wave');
grid on;
% Ton = D*1/F1; % The time interval in which the pulse is
HIGH
% m = Amplitude.*sawtooth(2*pi*F1*Ton);
% plot(t,m*ones(size(t))); hold off

n = length(c); % Length of carrier sawtooth wave


for i = 1:n
if (m(i) >= c(i)) % All values of t which are less
than Ton will output
pwm(i) = 1; % pwm pulse as HIGH
else
pwm(i) = 0; % the remaining values of t will lie
on Toff interval
end
end
subplot(3,1,3);
plot(t,pwm);
xlabel('t'); ylabel('Amplitude'); title('PWM');
axis([0 1 -1 2]);
grid on;

5
REPORT

With F carrier = 10, F message = 5

With F carrier = 2, F message = 4

6
REPORT

2. Working with TMS320C5515


Below is our 4-step process of working with the TMS320C5515

Create message signal from MATLAB and save as file m1.bin

Read file m1.bin and create carrier signal (Sawtooth wave)

Compare the 2 signals and generate PWM as output then


save as file pwm.bin

Plot the output signal in MATLAB

Step 1: Create message signal from MATLAB and save as file m1.bin

%Generating Message Wave


clc;
clear;
F2 = input('Message frequency = ');
Amplitude = 5;
t = 0:0.001:1;
m=Amplitude*sin(2*pi*F2*t); % Message signal
fid =
fopen('D:\Mup_Projects\MatLab\DSP\Major\m1.bin','wb');
fwrite(fid,m,'float');
fclose(fid);

7
REPORT

8
REPORT

Step 2: Reads file m1.bin and create carrier signal (Sawtooth wave)

//Receive Message wave from MATLAB


ftr = fopen("D:/Mup_Projects/MatLab/DSP/Major/m1.bin","rb");
fread(m,sizeof(float),1001,ftr);
fclose(ftr);
//Generate back to MATLAB for drawing graph
ftr = fopen("D:/Mup_Projects/MatLab/DSP/Major/m.bin","wb");
fwrite(m,sizeof(float),time_elements,ftr);
fclose(ftr);

//Generate values for the first period of sawtooth wave


c[0] = (-1)*ampc;
for(i=0; i<=time_elements; i++)
if ((Tperi - t[i])<0){
c[i-1] = ((float) ampc) - t_div;
j = i-1;
break;
}
b = c[0];
a = -2*b/Tperi;
for(i=1; i<=j; i++)
c[i] = a*t[i]+b;
//Generate remaining values for the other periods of sawtooth wave
for(i=0; i<=j; i++)
for(k=1; k<fc; k++)
c[i+k*(time_elements)/fc]=c[i];
//Generate Carrier wave to MATLAB for drawing graph
ftr = fopen("D:/Mup_Projects/MatLab/DSP/Major/c.bin","wb");
fwrite(c,sizeof(float),time_elements,ftr);
fclose(ftr);

9
REPORT

Step 3: Compare the 2 signals and generate PWM as output then save as file
pwm.bin

//Create PWM for Message wave


for(i=0; i<=time_elements; i++)
if (m[i]>=c[i]){
pwm[i] = 1;
printf("%f\n",pwm[i]);
}
else{
pwm[i] = 0;
printf("%f\n",pwm[i]);
}
//Generate PWM to MATLAB for drawing graph
ftr = fopen("D:/Mup_Projects/MatLab/DSP/Major/pwm.bin","wb");
fwrite(pwm,sizeof(float),time_elements,ftr);
fclose(ftr);

Step 4: Plot the output signal in MATLAB

The file pwm.bin in Step 3 was not created although all the codes were
compiled successfully. However, when we generated codes by
CodeBlocks, the program worked successfully.

10
REPORT

11
REPORT

The full code for all steps is shown below:

//major.c
#include "usbstk5515.h"
#include "usbstk5515_gpio.h"
#include "usbstk5515_i2c.h"
#include "stdio.h"
#include "stdint.h"
#include "stdlib.h"

extern int majordsp(){


FILE *ftr;

float t[1020], c[1020], m[1020], pwm[1020];


int i, j, k, t_max = 1, ampc = 5, fc=10; //t_max = 1 second;
ampc: carrier wave amplitude;
float t_div = 0.001, Tperi = 1/((float)fc); //fc: carrier wave
frequency
int time_elements = t_max/t_div + 2; //total number of time
elements in the interval [0;1] second
float a,b;
12 0s
//Initilize for all arrays with
for(i=0; i<1020; i++){
t[i] = 0;
c[i] = 0;
m[i] = 0;
REPORT

int time_elements = t_max/t_div + 2; //total number of time


elements in the interval [0;1] second
float a,b;
//Initilize for all arrays with 0s
for(i=0; i<1020; i++){
t[i] = 0;
c[i] = 0;
m[i] = 0;
pwm[i] = 0;
}
//Create an array of time from 0 to 1 second with t_div = 0.001
second
for (i=1; i<=time_elements; i++)
t[i] = t[i-1] + t_div + 0.0000001;

//Generate values for the first period of sawtooth wave


c[0] = (-1)*ampc;
for(i=0; i<=time_elements; i++)
if ((Tperi - t[i])<0){
c[i-1] = ((float) ampc) - t_div;
j = i-1;
break;
}
b = c[0];
a = -2*b/Tperi;
for(i=1; i<=j; i++)
c[i] = a*t[i]+b;
//Generate remaining values for the other periods of sawtooth wave
for(i=0; i<=j; i++)
for(k=1; k<fc; k++)
c[i+k*(time_elements)/fc]=c[i];
//Generate Carrier wave to MATLAB for drawing graph
ftr = fopen("D:/Mup_Projects/MatLab/DSP/Major/c.bin","wb");
fwrite(c,sizeof(float),time_elements,ftr);
fclose(ftr);

//Receive Message wave from MATLAB


ftr = fopen("D:/Mup_Projects/MatLab/DSP/Major/m1.bin","rb");
fread(m,sizeof(float),1001,ftr);
fclose(ftr);
//Generate back to MATLAB for drawing graph
13
ftr = fopen("D:/Mup_Projects/MatLab/DSP/Major/m.bin","wb");
fwrite(m,sizeof(float),time_elements,ftr);
fclose(ftr);

//Create PWM for Message wave


REPORT

fread(m,sizeof(float),1001,ftr);
fclose(ftr);
//Generate back to MATLAB for drawing graph
ftr = fopen("D:/Mup_Projects/MatLab/DSP/Major/m.bin","wb");
fwrite(m,sizeof(float),time_elements,ftr);
fclose(ftr);

//Create PWM for Message wave


for(i=0; i<=time_elements; i++)
if (m[i]>=c[i]){
pwm[i] = 1;
printf("%f\n",pwm[i]);
}
else{
pwm[i] = 0;
printf("%f\n",pwm[i]);
}
//Generate PWM to MATLAB for drawing graph
ftr = fopen("D:/Mup_Projects/MatLab/DSP/Major/pwm.bin","wb");
fwrite(pwm,sizeof(float),time_elements,ftr);
fclose(ftr);
return 0;
}

14
REPORT

IV. Conclusion
Through the process of implementing the project, although we
failed to generate the final results but our team has improved the
knowledge of pulse width modulation and added the knowledge of
simulation calculation on MATLAB and programming DSP kit on
Code Composer Studio.
V. References
Mathworks.com. (2019). ReferenceEntities. [online] Available at:
https://www.mathworks.com/help/matlab/.

Ucalgary.ca. (2019). Variable Speed Drives: Sinusoidal PWM. [online] Available


at: http://people.ucalgary.ca/~aknigh/vsd/ssim/vsi/spwm.html [Accessed
21 Nov. 2019].
Heath, J. (2017). PWM: Pulse Width Modulation: What is it and how does it
work? [online] Analog IC Tips. Available at:
https://www.analogictips.com/pulse-width-modulation-pwm/ [Accessed
21 Nov. 2019].
Sparkfun.com. (2019). Pulse Width Modulation - learn.sparkfun.com. [online]
Available at: https://learn.sparkfun.com/tutorials/pulse-width-
modulation/all.

15

You might also like