Code for PWM
fm=1;
fc=10;
A=5;
t=0:0.001:1;
msg=0.75*A.*sin(2*pi*fm*t);%Message amplitude must be less than Sawtooth
subplot(3,1,1);
plot(t,msg,'Linewidth',4);
xlabel('Time');
ylabel('Amplitude');
title('Message Signal');
carrier=A.*sawtooth(2*pi*fc*t);%Carrier sawtooth
subplot(3,1,2);
plot(t,carrier,':','Linewidth',3);
xlabel('time');
ylabel('Amplitude');
title('Carrier Sawtooth');
n=length(carrier);
for i=1:n%Comparing Message and Sawtooth amplitudes
if (msg(i)>=carrier(i))
pwm(i)=1;
else
pwm(i)=0;
end
end
subplot(3,1,3);
plot(t,pwm,'--','Linewidth',2);
xlabel('time');
ylabel('Amplitude');
title('Pulse Width Modulated Wave');
Waveforms