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.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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

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?

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.

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

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..???

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