% EXPERIMENT 4
% PULSE WIDTH MODULATION AND DEMODULATION
clc;
clear all;
close all;
fm = 1000; % Message frequency
fs = 10000; % Sampling frequency
Fs = 200000; % Sampling rate
t = 0:1/Fs:0.01;
Ts = 1/fs;
% Message signal (2 Vpp)
m = sin(2*pi*fm*t);
N = round(Ts*Fs);
pwm = zeros(size(t));
decoded = zeros(size(t));
% PWM Generation and Demodulation
for k = 0:floor(length(t)/N)-1
index = k*N + 1;
% Duty cycle variation
duty = 0.5 + 0.4*m(index);
% Pulse width
width = round(duty*N);
finish = min(index+width-1,length(t));
pwm(index:finish) = 1;
% Demodulation
recovered = (width/N - 0.5)/0.4;
decoded(index:min(index+N-1,length(t))) = recovered;
end
% FFT based Low Pass Filter
L = length(t);
Y = fftshift(fft(decoded));
f = ((0:L-1)-floor(L/2))*(Fs/L);
cutoff = 2000;
Y(abs(f)>cutoff) = 0;
demodulated = real(ifft(ifftshift(Y)));
% Plotting
figure(1);
subplot(3,1,1);
plot(t,m,'b','LineWidth',1.5);
title('Original Message Signal');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid on;
subplot(3,1,2);
plot(t,pwm,'r');
title('PWM Modulated Signal');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid on;
subplot(3,1,3);
plot(t,demodulated,'g','LineWidth',1.5);
title('PWM Demodulated Signal');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid on;
disp('Experiment 4 Completed Successfully');
To embed this project on your website, copy the following code and paste it into your website's HTML: