# Code for PWM (Pulse Width Modulation) generation in MATLAB

PWM (Pulse Width Modulation) can be easily generated in MATLAB using simple MATLAB functions. We have already discussed about PWM generator circuit using 741 Op-amp in previous posts. In PWM, width of the pulses are varied according to the amplitude of AF message signal. PWM is basically an analog pulse modulation technique. It does modulation of the duty cycle of the pulse.
In addition to its use in communication systems, PWM is also used in voltage regulators such as Switched Mode Power Supplies (SMPS) to control the power delivered to the load. PWM can be generated using a comparator to which the modulating signal and a reference ramp (sawtooth) waveform are fed.The code for PWM simulation in MATLAB is shown below. The use of particular line of MATLAB code is also given as comments (%Comment field). The given MATLAB program accepts two input frequencies from the user.

## PWM MATLAB Codes

 123456789101112131415161718192021222324252627282930313233343536 clc; clear all; close all; F2=input(‘Message frequency=’); F1=input(‘Carrier Sawtooth frequency=’); A=5; t=0:0.001:1; c=A.*sawtooth(2*pi*F1*t);%Carrier sawtooth subplot(3,1,1); plot(t,c); xlabel(‘time’); ylabel(‘Amplitude’); title(‘Carrier sawtooth wave’); grid on; m=0.75*A.*sin(2*pi*F2*t);%Message amplitude must be less than Sawtooth subplot(3,1,2); plot(t,m); xlabel(‘Time’); ylabel(‘Amplitude’); title(‘Message Signal’); grid on; n=length(c);%Length of carrier sawtooth is stored to ‘n’ for i=1:n%Comparing Message and Sawtooth amplitudes if (m(i)>=c(i))     pwm(i)=1; else     pwm(i)=0; end end subplot(3,1,3); plot(t,pwm); xlabel(‘Time’); ylabel(‘Amplitude’); title(‘plot of PWM’); axis([0 1 0 2]);%X-Axis varies from 0 to 1 & Y-Axis from 0 to 2 grid on;

## Generated PWM Signal

Inputs and Observation:
Message frequency=1
Carrier Saw tooth frequency=10

## 12 thoughts on “Code for PWM (Pulse Width Modulation) generation in MATLAB”

1. Dilip says:

If i want to replace the normal sine wave by a superposed sine wave(ie an additive resultant of 2 or more sine waves of different phase), how can i do it?

• Jaseem vp says:

Hi Dilip,
First generate the superposed sine wave by adding individual sine waves. Then instead of variable ‘m’ use the superposed sine wave and proceed.

2. michael says:

i made this sg3524 circuit ,it worked out very well but the output voltage is very low say 120v with a 12 0 12 5kv transformer , sir where on the circut can i adjust this

3. akhil says:

sir,
when am executing the command
” c=A.*sawtooth(2*pi*F1*t);%Carrier sawtooth ”
after typing above one and pressing enter it is showing that
” ??? Error using ==> mtimes
Inner matrix dimensions must agree. ”
what does this mean..??
and what can i do..???

4. khalifa enbia says:

could you give me a code to generate PPM Pulse Position Modulation

5. trupti says:

How to generate PWM waveform from using pulse waveform and Duty cycle as inputs ?

6. bhate says:

Would share the how to generate the PWM with Pulse waveform as carrier and duty cycle

7. s.rajamamatha says:

send me how to write programs for pwm generation with different amplitude in a single wave

8. shweta says:

sir,
what can i do ..????not available

9. shweta says:

send me comparator applications in pwm

10. student says: