Skip to main content

MATLAB Code for Pulse Amplitude Modulation (PAM) and Demodulation

 

Pulse Amplitude Modulation (PAM) & Demodulation

MATLAB Script

clc;

clear all;

close all;

fm= 10; % frequency of the message signal

fc= 100; % frequency of the carrier signal

fs=1000*fm; % (=100KHz) sampling frequency (where 1000 is the upsampling factor)

t=0:1/fs:1; % sampling rate of (1/fs = 100 kHz)

m=1*cos(2*pi*fm*t); % Message signal with period 2*pi*fm (sinusoidal wave signal)

c=0.5*square(2*pi*fc*t)+0.5; % square wave with period 2*pi*fc

s=m.*c; % modulated signal (multiplication of element by element)

subplot(4,1,1);

plot(t,m);

title('Message signal');

xlabel ('Time');

ylabel('Amplitude');

subplot(4,1,2);

plot(t,c);

title('Carrier signal');

xlabel('Time');

ylabel('Amplitude');

subplot(4,1,3);

plot(t,s);

title('Modulated signal');

xlabel('Time');

ylabel('Amplitude');

%demdulated

d=s.*c; % At receiver, received signal is multiplied by carrier signal

filter=fir1(200,fm/fs,'low'); % low-pass FIR filter which order is 200

% here fm is the cut-off frequency and the fs is the sampling frequency

original_t_signal=conv(filter,d); % convolution of demodulated signal with filter %coefficient

t1=0:1/(length(original_t_signal)-1):1;

subplot(4,1,4);

plot(t1,original_t_signal);

title('demodulated signal');

xlabel('time');

ylabel('amplitude');

 

 Output

 

Copy the code from here

 


Another Code for Pulse Amplitude Modulation

MATLAB Script

 clc;
clear;
close all;

% Parameters
messageFrequency = 2;   % Message frequency in Hz
carrierFrequency = 20;  % Carrier frequency in Hz
samplingFrequency = 1000; % Sampling frequency in Hz
duration = 1;           % Signal duration in seconds
A = 1;                  % Amplitude of the signals

% Time vector
t = 0:1/samplingFrequency:duration;

% Message signal (sinusoidal)
messageSignal = A * sin(2 * pi * messageFrequency * t);

% Carrier signal (square wave)
carrierSignal = A * square(2 * pi * carrierFrequency * t);

% PAM signal
pamSignal = messageSignal .* (carrierSignal > 0);

% Plotting
figure;
subplot(3,1,1);
plot(t, messageSignal);
title('Message Signal');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(3,1,2);
plot(t, carrierSignal);
title('Carrier Signal');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(3,1,3);
plot(t, pamSignal);
title('PAM Signal');
xlabel('Time (s)');
ylabel('Amplitude');

Copy the Code from here

 

Pulse Amplitude Modulation (PAM) & Demodulation for Digital Data

% The code is written by SalimWireless.Com
clc;
clear;
close all;


% PAM Modulation and Demodulation Example


% Parameters
M = 8; % PAM order (8-PAM)
numSymbols = 100; % Number of symbols to transmit
Fs = 1000; % Sampling frequency
T = 1; % Symbol duration


% Generate random data
data = randi([0 M-1], 1, numSymbols); % Random data symbols


% PAM Modulation
% Map the data symbols to PAM levels
pamLevels = linspace(-M + 1, M - 1, M); % PAM levels
modulatedSignal = pamLevels(data + 1); % Map data to PAM levels


% Create a time vector
t = 0:1/Fs:T*numSymbols-1/Fs;


% Upsample and create PAM signal
upsampledSignal = zeros(1, length(t));
for i = 1:numSymbols
upsampledSignal((i-1)*Fs+1:i*Fs) = modulatedSignal(i);
end


% Add some noise
snr = 20; % Signal-to-noise ratio
noisySignal = awgn(upsampledSignal, snr, 'measured');


% PAM Demodulation
% Sample the noisy signal at symbol rate
receivedSymbols = noisySignal(1:Fs:end);


% Map received symbols to nearest PAM level
demodulatedData = zeros(1, numSymbols);
for i = 1:numSymbols
[~, demodulatedData(i)] = min(abs(receivedSymbols(i) - pamLevels));
end


% Plotting
figure;
subplot(4,1,1);
stem(data);
title('Original Data');
xlabel('Time (s)');
ylabel('Amplitude');


subplot(4,1,2);
plot(t, upsampledSignal);
title('Transmitted PAM Signal');
xlabel('Time (s)');
ylabel('Amplitude');


subplot(4,1,3);
plot(t, noisySignal);
title('Received Noisy PAM Signal');
xlabel('Time (s)');
ylabel('Amplitude');


subplot(4,1,4);
stem(demodulatedData);
title('Demodulated Data');
xlabel('Symbol Index');
ylabel('PAM Level');
grid on;


% Display results
disp('Original Data:');
disp(data);
disp('Demodulated Data:');
disp(demodulatedData);

Output






Copy the MATLAB Code from here



Also read about

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

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, ... 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, the SNR for a given communication system is 3dB. So, SNR (in ratio) = 10^{SNR (in dB) / 10} = 2 Therefore, in this instance, the s...

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:leng...

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

  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.  Calculate BER for Binary ASK Modulation The theoretical BER for binary ASK (BASK) in an AWGN channel is given by: BER  = (1/2) * erfc(0.5 * sqrt(SNR_ask));   Enter SNR (dB): Calculate BER BER vs. SNR curves for ASK, FSK, and PSK Calculate BER for Binary FSK Modulation The theoretical BER for binary FSK (BFSK) in a...

Antenna Gain-Combining Methods - EGC, MRC, SC, and RMSGC

 There are different antenna gain-combining methods. They are as follows. 1. Equal gain combining (EGC) 2. Maximum ratio combining (MRC) 3. Selective combining (SC) 4. Root mean square gain combining (RMSGC) 1. Equal gain combining method We add the correlated data streams from different antennas in the equal gain combining method. Then we multiply the resultant data with (1/(number of antennas)) For example, for two antenna gain-combining  If the received symbols are y1 and y2, then  Equal combing gain, y_egc = 0.5 * (y1 + y2) 2. Maximum ratio combining method We multiply the individual data streams with weights in the maximum ratio combining method. More weightage is multiplied by those data streams with maximum {|h|^2}, where h denotes the channel impulse response. And less weightage is multiplied by those data streams with corresponding small value of  {|h|^2}.  Then we sum the data streams to improve SNR. In the case of Maximum Ratio Combining, if y1 an...

High Level and Low Level Modulation

High Level and Low Level Modulation You know for wireless communication is suitable for long distance communication. In wireless, for data transmission modulation become essential to avoid interference and to reduce antenna size significantly. Especially, in modulation process, we translate the low frequency baseband signal to higher frequency by modulating with high frequency carrier signal. For a typical communication system we generate the high frequency (carrier) signal by using local oscillator. Source signal or message signal is modulated with local oscillator. Then modulated signal is transmitted thru antenna.  Low Level Modulation In low level modulation, message signal is modulated with local  oscillator  that produces high frequency. Then the frequency of message signal is translated to much higher frequency. Then the modulated signal passes thru wideband amplifier. High Level Modulation In high level modulation, source or message signal is passed thru wideband ...

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

Modulation ASK, FSK & PSK Constellation MATLAB Simulink MATLAB Code Comparisons among ASK, PSK, and FSK    Comparisons among ASK, PSK, and FSK Comparison among ASK,  FSK, and PSK Performance Comparison: 1. Noise Sensitivity:    - ASK is the most sensitive to noise due to its reliance on amplitude variations.    - PSK is less sensitive to noise compared to ASK.    - FSK is relatively more robust against noise, making it suitable for noisy environments. 2. Bandwidth Efficiency:    - PSK is the most bandwidth-efficient, requiring less bandwidth than FSK for the same data rate.    - FSK requires wider bandwidth compared to PSK.    - ASK's bandwidth efficiency lies between FSK and PSK. Bandwidth Calculator for ASK, FSK, and PSK The baud rate represents the number of symbols transmitted per second Select Modulation Type: ASK...

Constellation Diagrams of ASK, PSK, and FSK

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.  Key Points For Binary Amplitude Shift Keying (BASK), binary bit '0' can be represented as lower level voltage or no signal and bit '1' as higher level voltage.  For Binary Frequency Shift Keying (BFSK), you can map binary bit '0' to 'j' and bit '1' to '1'. So, signals are in phase.  A phase shift of 0 degrees could represent a binary '1...