Skip to main content
Home Wireless Communication Modulation MATLAB Beamforming Project Ideas MIMO Computer Networks Lab 🚀

MATLAB Code for Pulse Width Modulation (PWM) and Demodulation

 

 Pulse Width Modulation (PWM)

MATLAB Script

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

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


sawtooth=1.01*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
% Plot the demodulated signal for comparison
subplot(4,1,4);
plot(t, demodulated_signal);
title('Demodulated Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;

Output 


 Copy the code from here

 

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 Rank and Condition Number of a Channel Matrix

To assess the signal strengths of various multipaths between TX and RX and enable communication, the rank and condition numbers of a channel matrix are highly helpful characteristics. Signal multipath propagation is a typical occurrence in wireless communication. Phases shift and the signal weakens during this process. We are discussing signal phases in this context. When numerous multipaths arrive at the receiver, the resulting signal may be additive or destructive because of phase alterations. A channel matrix is referred to as a sparse matrix if it only has a few stronger elements and the majority of the other elements are zero. Finding rank and condition number for sparse matrices is important for numerous reasons. That topic has already been covered in another article [ click here ]. We will just talk about the corresponding MATLAB codes here. MATLAB Code for Rank and Condition Number of a Channel Matrix %Author: Salim Wireless For study materials on wireless %com...

Simulation of ASK, FSK, and PSK using MATLAB Simulink

ASK, FSK & PSK HomePage MATLAB Simulation Simulation of Amplitude Shift Keying (ASK) using MATLAB Simulink      In Simulink, we pick different components/elements from MATLAB Simulink Library. Then we connect the components and perform a particular operation.  Result A sine wave source, a pulse generator, a product block, a mux, and a scope are shown in the diagram above. The pulse generator generates the '1' and '0' bit sequences. Sine wave sources produce a specific amplitude and frequency. The scope displays the modulated signal as well as the original bit sequence created by the pulse generator. Mux is a tool for displaying both modulated and unmodulated signals at the same time. The result section shows that binary '1' is modulated by a certain sine wave amplitude of 1 Volt, and binary '0' is modulated by zero amplitude. Simulation of Frequency Shift Keying (FSK) using MATLAB Simulink   Result The diagram above shows t...

MATLAB Code for QAM (Quadrature Amplitude Modulation)

  One of the best-performing modulation techniques is QAM [↗] . Here, we modulate the symbols by varying the carrier signal's amplitude and phase in response to the variation in the message signal (or voltage variation). So, we may say that QAM is a combination of phase and amplitude modulation. Additionally, it performs better than ASK or PSK [↗] . In fact, any constellation for any type of modulation, signal set (or, symbols) is structured in a way that prevents them from interacting further by being distinct by phase, amplitude, or frequency. MATLAB Script % This code is written by SalimWirelss.Com % This is an example of 4-QAM. Here constellation size is 4 % or total number of symbols/signals is 4 % We need 2 bits once to represent four constellation points % QAM modulation is the combination of Amplitude modulation plus % Phase Modulation. We map the decimal value of the input symbols, i.e., % 00, 01, 10, 11 to 1 + 1i, -1 + 1i, 1 - 1i, and -1 - 1i, respectively. clc;clear all;...

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

Modulation Constellation Diagrams BER vs. SNR BER vs SNR for M-QAM, M-PSK, QPSk, BPSK, ... 1. 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.   2. 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, the SNR for a given communication system is 3dB. So, SNR (in ratio) = 10^{SNR (in dB) / 10} = 2 Therefore, in this instance,...

MATLAB code for BER vs SNR for M-QAM, M-PSK, QPSk, BPSK, ...

Modulation Constellation Diagrams BER vs. SNR MATLAB code for BER vs SNR for M-QAM, M-PSK, QPSk, BPSK, ...   MATLAB Script for  BER vs. SNR for M-QAM, M-PSK, QPSk, BPSK %Written by Salim Wireless %Visit www.salimwireless.com for study materials on wireless communication %or, if you want to learn how to code in MATLAB clc; clear; close all; % Parameters num_symbols = 1e5; % Number of symbols snr_db = -20:2:20; % Range of SNR values in dB % PSK orders to be tested psk_orders = [2, 4, 8, 16, 32]; % QAM orders to be tested qam_orders = [4, 16, 64, 256]; % Initialize BER arrays ber_psk_results = zeros(length(psk_orders), length(snr_db)); ber_qam_results = zeros(length(qam_orders), length(snr_db)); % BER calculation for each PSK order and SNR value for i = 1:length(psk_orders) psk_order = psk_orders(i); for j = 1:length(snr_db) % Generate random symbols data_symbols = randi([0, psk...

HomePage

  (Search any topic) Search any topic on the whole website Modulation Signal Processing Beamforming MATLAB 5G Wireless GATE-ESE-NET Programming Telecommunication Channel Impulse Response Computer Networks MIMO - Multiple Input Multiple Output Filters Millimeter wave Python   Constellation Diagrams BER vs SNR Electronics Industry Fourier Series and Fourier Transform Frequency bands Wireless Communication Q & A ASK FSK PSK Channel Model IoTs UWB pskmod Antenna Applications and Games C Programming Channel Estimation Equalizers Gaussian Random Variable Projects Q & A QAM Transform Fading Microwave News about 5G PAM Python Matrix Operations SSC Exam Web Design WordPress Ionospheric Communication JavaScript MATLAB Simulink Mobile & Accessories OFDM Signal Processing for 5G Analog Circuits Cell Towers Computer Digital Circuits Fourier Series HomePage Information and Coding Theory Laplace Transform MySQL Node.js Search ShareLinkF / Generate QR Z Transform ...

Channel Estimation utilizing Decision Feedback Equalizer (DFE)

  Channel estimation using DFE is a similar process to a non-linear equalization process. In DFE (decision feed equalizer), equalization error bits/symbols between the feedforward tabs and feedback taps are calculated continuously. And equalizer's tap weights tap weights are updated correspondingly.  In plain language, the error between the received bits and known training bits is calculated, and tap weights are updated accordingly. The equalizer estimates the channel impulse response (CIR) .  Once we find the channel impulse response or channel information, we can easily retrieve the original message signal from the noisy data. In the communication process, the whole system is modeled as a linear time-invariant (LTI) system. And  y = h*x + n where, y = received signal            x = transmitted signal           n = additive white Gaussian noise [Read more about the Linear time-invariant (LTI) system and convolu...

MIMO, massive MIMO, and Beamforming

  The term 'MIMO' was originally applied to systems with multiple antennas on both the transmitter (Tx) and receiver (Rx) sides. MIMO is a key component of Wi-Fi 4 and 5, 3G, and 4G cellular networks. This method was introduced to increase the capacity of a channel by sending multiple simultaneous data streams through a single channel. All simultaneous data streams in a MIMO system are encoded orthogonally multiplexed, which reduces interference. Massive MIMO is used extensively in 5G to achieve extremely high capacity and to communicate via  beamforming  or directional transmission. 1. Some essential characteristics of a MIMO system 1.1. Spatial Division Multiplexing Access (SDMA) SDMA is a key feature of MIMO, allowing a base station (BS) to communicate with several devices simultaneously (or even using the same frequency) if they are in different locations. There may be no knowledge of channel information at the transmitter. 1.2. Spatial Multiplexing Another essential ...