Skip to main content

MATLAB Code for Pulse Width Modulation (PWM) and Demodulation

 

 Pulse Width Modulation (PWM)

MATLAB Script

clc;
clear all;
close all;
fs=30; %frequency of the sawtooth signal
fm=3; %frequency of the message signal
sampling_frequency = 10e3;
a=0.5; % amplitide

t=0:(1/sampling_frequency):1; %sampling rate of 10kHz


sawtooth=2*a.*sawtooth(2*pi*fs*t); %generating a sawtooth wave


subplot(4,1,1);
plot(t,sawtooth); % plotting the sawtooth wave
title('Comparator Wave');

msg=a.*sin(2*pi*fm*t); %generating message wave

subplot(4,1,2);
plot(t,msg); %plotting the sine message wave
title('Message Signal');


for i=1:length(sawtooth)
if (msg(i)>=sawtooth(i))
pwm(i)=1; %is message signal amplitude at i th sample is greater than
%sawtooth wave amplitude at i th sample
else
pwm(i)=0;
end
end

subplot(4,1,3);
plot(t,pwm,'r');
title('PWM');
axis([0 1 0 1.1]); %to keep the pwm visible during plotting.

%% Demodulation
% Demodulation: Measure the pulse width to reconstruct the signal
demodulated_signal = zeros(size(msg));

for i = 1:length(pwm)-1
if pwm(i) == 1
% Measure the time until the next falling edge
j = i + 1;
while pwm(j) == 1 && j < length(pwm)
j = j + 1;
end
% Reconstruct the analog value based on pulse width
demodulated_signal(i) = mean(msg(i:j-1));
end
end

% Low-Pass Filter Design
Fs = 1 / (t(2) - t(1)); % Sampling frequency
Fc = 5; % Cutoff frequency (adjust based on your signal)
[b, a] = butter(4, Fc / (Fs / 2), 'low'); % 4th-order Butterworth filter

% Apply the Low-Pass Filter
filtered_signal = filtfilt(b, a, demodulated_signal);

% Plot the demodulated and filtered signal for comparison
subplot(4,1,4);
plot(t, filtered_signal, 'r', 'LineWidth', 1.5); % Filtered signal in red
title('Demodulated Signal (Filtered)');
xlabel('Time');
ylabel('Amplitude');
grid on;
 

Output 






 Copy the code from here

 

 

 

Further Reading

People are good at skipping over material they already know!

View Related Topics to







Admin & Author: Salim

profile

  Website: www.salimwireless.com
  Interests: Signal Processing, Telecommunication, 5G Technology, Present & Future Wireless Technologies, Digital Signal Processing, Computer Networks, Millimeter Wave Band Channel, Web Development
  Seeking an opportunity in the Teaching or Electronics & Telecommunication domains.
  Possess M.Tech in Electronic Communication Systems.


Contact Us

Name

Email *

Message *

Popular Posts

MATLAB Code for BER performance of QPSK with BPSK, 4-QAM, 16-QAM, 64-QAM, 256-QAM, etc

๐Ÿ“˜ Overview ๐Ÿงฎ MATLAB Codes ๐Ÿงฎ Theory ๐Ÿงฎ Are QPSK and 4-PSK same? ๐Ÿ“š Further Reading   QPSK offers double the data rate of BPSK while maintaining a similar bit error rate at low SNR when Gray coding is used. It shares spectral efficiency with 4-QAM and can outperform 4-QAM or 16-QAM in very noisy channels. QPSK is widely used in practical wireless systems, often alongside QAM in adaptive modulation schemes [Read more...]   MATLAB Code clear all; close all; % Set parameters for QAM snr_dB = -20:2:20; % SNR values in dB qam_orders = [4, 16, 64, 256]; % QAM modulation orders % Loop through each QAM order and calculate theoretical BER figure; for qam_order = qam_orders     % Calculate theoretical BER using berawgn for QAM     ber_qam = berawgn(snr_dB, 'qam', qam_order);     % Plot the results for QAM     semilogy(snr_dB, ber_qam, 'o-', 'DisplayName', sprintf('%d-QAM', qam_order));  ...

MATLAB code for MSK

๐Ÿ“˜ Overview ๐Ÿงฎ MATLAB Codes ๐Ÿงฎ Theory ๐Ÿงฎ Simulator for MSK ๐Ÿ“š Further Reading  Copy the MATLAB Code from here % The code is developed by SalimWireless.com clc; clear; close all; % Define a bit sequence bitSeq = [0, 1, 0, 0, 1, 1, 1, 0, 0, 1]; % Perform MSK modulation [modSignal, timeVec] = modulateMSK(bitSeq, 10, 10, 10000); % Plot the modulated signal subplot(2,1,1); samples = 1:numel(bitSeq); stem(samples, bitSeq); title('Original message signal'); xlabel('Time (s)'); ylabel('Amplitude'); % Plot the modulated signal subplot(2,1,2); samples = 1:10000; plot(samples / 10000, modSignal(1:10000)); title('MSK modulated signal'); xlabel('Time (s)'); ylabel('Amplitude'); % Perform MSK demodulation demodBits = demodMSK(modSignal, 10, 10, 10000); % Function to perform MSK modulation function [signal, timeVec] = modulateMSK(bits, carrierFreq, baudRate, sampleFreq) % Converts a binary bit sequence in...

Constellation Diagrams of ASK, PSK, and FSK

๐Ÿ“˜ Overview ๐Ÿงฎ Simulator for constellation diagrams of ASK, FSK, and PSK ๐Ÿงฎ Theory ๐Ÿงฎ MATLAB Codes ๐Ÿ“š Further Reading BASK (Binary ASK) Modulation: Transmits one of two signals: 0 or -√Eb, where Eb​ is the energy per bit. These signals represent binary 0 and 1.    BFSK (Binary FSK) Modulation: Transmits one of two signals: +√Eb​ ( On the y-axis, the phase shift of 90 degrees with respect to the x-axis, which is also termed phase offset ) or √Eb (on x-axis), where Eb​ is the energy per bit. These signals represent binary 0 and 1.  BPSK (Binary PSK) Modulation: Transmits one of two signals: +√Eb​ or -√Eb (they differ by 180 degree phase shift), where Eb​ is the energy per bit. These signals represent binary 0 and 1.    Simulator for BASK, BPSK, and BFSK Constellation Diagrams SNR (dB): 15 Add A...

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...

๐Ÿ“˜ Overview of BER and SNR ๐Ÿงฎ Simulator for m-ary QAM and m-ary PSK ๐Ÿงฎ MATLAB Codes ๐Ÿ“š Further Reading Modulation Constellation Diagrams BER vs. SNR BER vs SNR for M-QAM, M-PSK, QPSk, BPSK, ... What is Bit Error Rate (BER)? The abbreviation BER stands for bit error rate, which indicates how many corrupted bits are received (after the demodulation process) compared to the total number of bits sent in a communication process. It is defined as,  In mathematics, BER = (number of bits received in error / total number of transmitted bits)  On the other hand, SNR refers to the signal-to-noise power ratio. For ease of calculation, we commonly convert it to dB or decibels.   What is Signal the signal-to-noise ratio (SNR)? SNR = signal power/noise power (SNR is a ratio of signal power to noise power) SNR (in dB) = 10*log(signal power / noise power) [base 10] For instance,...

Theoretical and simulated BER vs. SNR for ASK, FSK, and PSK

๐Ÿ“˜ Overview ๐Ÿงฎ Simulator for calculating BER ๐Ÿงฎ MATLAB Codes for calculating theoretical BER ๐Ÿงฎ MATLAB Codes for calculating simulated BER ๐Ÿ“š Further Reading   BER vs. SNR denotes how many bits in error are received in a communication process for a particular Signal-to-noise (SNR) ratio. In most cases, SNR is measured in decibel (dB). For a typical communication system, a signal is often affected by two types of noises 1. Additive White Gaussian Noise (AWGN) 2. Rayleigh Fading In the case of additive white Gaussian noise (AWGN), random magnitude is added to the transmitted signal. On the other hand, Rayleigh fading (due to multipath) attenuates the different frequency components of a signal differently. A good signal-to-noise ratio tries to mitigate the effect of noise.  Simulator for calculating BER vs SNR for binary ASK, FSK, and PSK Calculate BER for Binary ASK Modulation The theoretical BER for binary ASK (BASK) in an AWGN channel is...

Differences between Baseband and Passband Modulation Techniques

๐Ÿ“˜ Overview ๐Ÿงฎ Difference betwen baseband and passband ๐Ÿงฎ Baseband modulation techniques ๐Ÿงฎ Passband modulation techniques ๐Ÿ“š Further Reading   1. Frequency Translation Baseband Modulation: The signal occupies the lower end of the frequency spectrum, close to DC (0 Hz). Noise at these frequencies (such as 1/f noise or flicker noise) can significantly impact the signal.  Passband Modulation: The signal is shifted to a higher frequency range by modulating it with a carrier frequency. This translation can help to avoid low-frequency noise and interference, which are often more prevalent and stronger in the baseband. 2. Bandpass Filtering Baseband Modulation: The filtering of baseband signals is often limited by the need to preserve the low-frequency components of the signal. This makes it difficult to filter out low-frequency noise effectively. Passband Modulation: The modulated signal can be passed through a bandpass filter centered around t...

Comparisons among ASK, PSK, and FSK | And the definitions of each

๐Ÿ“˜ Overview ๐Ÿงฎ Simulator ๐Ÿงฎ Noise Sensitivity, Bandwidth, Complexity, etc. ๐Ÿงฎ MATLAB Codes ๐Ÿงฎ Some Questions and Answers ๐Ÿ“š Further Reading Modulation ASK, FSK & PSK Constellation MATLAB Simulink MATLAB Code Comparisons among ASK, PSK, and FSK    Comparisons among ASK, PSK, and FSK   Simulator for Calculating Bandwidth of ASK, FSK, and PSK The baud rate represents the number of symbols transmitted per second. Both baud rate and bit rate are same for binary ASK, FSK, and PSK. Select Modulation Type: ASK FSK PSK Baud Rate or Bit Rate (bps): Frequency Deviation (Hz) for FSK: Calculate Bandwidth Comparison among ASK,  FSK, and PSK Performance Comparison: 1. Noise Sensitivity:    - ASK is the most sensitive to noise due to its r...