Skip to main content

MATLAB code for Pulse Code Modulation (PCM) and Demodulation


MATLAB Code for Pulse Code Modulation

MATLAB Source Code
clc;
close all;
clear all;

fm=input('Enter the message frequency (in Hz): ');
fs=input('Enter the sampling frequency (in Hz): ');
L=input('Enter the number of the quantization levels: ');

n = log2(L);

t=0:1/fs:1; % fs nuber of samples have tobe selected

s=8*sin(2*pi*fm*t);
subplot(3,1,1);
t=0:1/(length(s)-1):1;
plot(t,s);
title('Analog Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
subplot(3,1,2);
stem(t,s);grid on; title('Sampled Sinal'); ylabel('Amplitude--->'); xlabel('Time--->');
 
 % Quantization Process
 vmax=8;
 vmin=-vmax; %to quantize a signal s into L levels between vmin and vmax
 del=(vmax-vmin)/L;
 part=vmin:del:vmax; % level are between vmin and vmax with difference of del
 code=vmin-(del/2):del:vmax+(del/2); % Contaion Quantized valuses 
 [ind,q]=quantiz(s,part,code); % Quantization process
 % ind contain index number and q contain quantized values
 l1=length(ind);
 l2=length(q);
 
 for i=1:l1
 if(ind(i)~=0) % To make index as binary decimal so started from 0 to N
 ind(i)=ind(i)-1;
 end 
 i=i+1;
 end 
 for i=1:l2
 if(q(i)==vmin-(del/2)) % To make quantize value inbetween the levels
 q(i)=vmin+(del/2);
 end
 end 
 subplot(3,1,3);
 stem(t,q);grid on; % Display the Quantize values
 title('Quantized Signal');
 ylabel('Amplitude--->');
 xlabel('Time--->');
 
 % Encoding Process
 figure
 code=de2bi(ind,'left-msb'); % Cnvert the decimal to binary
 k=1;
for i=1:l1
 for j=1:n
 coded(k)=code(i,j); % convert code matrix to a coded row vector
 j=j+1;
 k=k+1;
 end
 i=i+1;
end
 subplot(2,1,1); grid on;
 stairs(coded); % Display the encoded signal
axis([0 100 -2 3]); title('Encoded Signal');
 ylabel('Amplitude--->');
 
 % Demodulation Of PCM signal
 
 qunt=reshape(coded,n,length(coded)/n);
 index=bi2de(qunt','left-msb'); % Getback the index in decimal form
 q=del*index+vmin+(del/2); % getback Quantized values
 subplot(2,1,2); grid on;
 plot(t,q);
 title('demodulated signal without low-pass filter');



% % % Demodulation after applying low-pass filter


figure()
% Low-pass Filter Design
fc = fm; % Cutoff frequency for the low-pass filter
order = 1; % Filter order (first-order Butterworth filter)

% Design the low-pass Butterworth filter
[b, a] = butter(order, fc/(fs/2), 'low');

% Apply the low-pass filter to the signal
filtered_signal = filtfilt(b, a, q);
plot(t,s);
title('demodulated signal after applying low-pass filter') 

Program Output & Results

Enter the message frequency (in Hz): 1
Enter the sampling frequency (in Hz): 10000
Enter the number of the quantization levels: 8
>>

Explore Pulse Modulation Techniques

Access our comprehensive dashboard for more simulators and technical explanations.

PCM Online Simulator › Pulse Modulation Home Page ›

People are good at skipping over material they already know!

View Related Topics to







Contact Us

Name

Email *

Message *

Popular Posts

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...(MATLAB Code + Simulator)

Bit Error Rate (BER) & SNR Guide Analyze communication system performance with our interactive simulators and MATLAB tools. 📘 Theory 🧮 Simulators 💻 MATLAB Code 📚 Resources BER Definition SNR Formula BER Calculator MATLAB Comparison 📂 Explore M-ary QAM, PSK, and QPSK Topics ▼ 🧮 Constellation Simulator: M-ary QAM 🧮 Constellation Simulator: M-ary PSK 🧮 BER calculation for ASK, FSK, and PSK 🧮 Approaches to BER vs SNR What is Bit Error Rate (BER)? The BER indicates how many corrupted bits are received compared to the total number of bits sent. It is the primary figure of merit for a...

Power Spectral Density Calculation Using FFT in MATLAB

📘 Overview 🧮 Steps to calculate the PSD of a signal 🧮 MATLAB Codes 📚 Further Reading Power spectral density (PSD) tells us how the power of a signal is distributed across different frequency components, whereas Fourier Magnitude gives you the amplitude (or strength) of each frequency component in the signal. Steps to calculate the PSD of a signal Firstly, calculate the fast Fourier transform (FFT) of a signal. Then, calculate the Fourier magnitude (absolute value) of the signal. Square the Fourier magnitude to get the power spectrum. To calculate the Power Spectral Density (PSD), divide the squared magnitude by the product of the sampling frequency (fs) and the total number of samples (N). Formula: PSD = |FFT|^2 / (fs * N) Sampling frequency (fs): The rate at which the continuous-time signal is sampled (in ...

Constellation Diagrams of ASK, PSK, and FSK (with MATLAB Code + Simulator)

Constellation Diagrams: ASK, FSK, and PSK Comprehensive guide to signal space representation, including interactive simulators and MATLAB implementations. 📘 Overview 🧮 Simulator ⚖️ Theory 📚 Resources Definitions Constellation Tool Key Points MATLAB Code 📂 Other Topics: M-ary PSK & QAM Diagrams ▼ 🧮 Simulator for M-ary PSK Constellation 🧮 Simulator for M-ary QAM Constellation 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 ...

UGC NET Electronic Science Previous Year Question Papers

Home / Engineering & Other Exams / UGC NET 2022 PYQ 📥 Download UGC NET Electronics PDFs Complete collection of previous year question papers, answer keys and explanations for Subject Code 88. Start Downloading UGC-NET (Electronics Science, Subject code: 88) Subject_Code : 88; Department : Electronic Science; 📂 View All Question Papers UGC Net Electronic Science Question Paper With Answer Key Download Pdf [June 2025] with full explanation UGC Net Electronic Science Question Paper With Answer Key Download Pdf [December 2024] UGC Net Paper 1 With Answer Key Download Pdf [Sep 2024] with full explanation UGC Net Electronic Science Question Paper With Answer Key Download Pdf [Aug 2024] with full explanation UGC Net Paper 1 With Answer Key Download...

MATLAB code for BER vs SNR for M-QAM, M-PSK, QPSk, BPSK, ...(with Online Simulator)

🧮 MATLAB Code for BPSK, M-ary PSK, and M-ary QAM Together 🧮 MATLAB Code for M-ary QAM 🧮 MATLAB Code for M-ary PSK 📚 Further Reading MATLAB Script for BER vs. SNR for M-QAM, M-PSK, QPSK, BPSK % Written by Salim Wireless clc; clear; close all; snr_db = -5:2:25; psk_orders = [2, 4, 8, 16, 32]; qam_orders = [4, 16, 64, 256]; ber_psk_results = zeros(length(psk_orders), length(snr_db)); ber_qam_results = zeros(length(qam_orders), length(snr_db)); for i = 1:length(psk_orders) ber_psk_results(i, :) = berawgn(snr_db, 'psk', psk_orders(i), 'nondiff'); end for i = 1:length(qam_orders) ber_qam_results(i, :) = berawgn(snr_db, 'qam', qam_orders(i)); end figure; semilogy(snr_db, ber_psk_results(1, :), 'o-', 'LineWidth', 1.5, 'DisplayName', 'BPSK'); hold on; for i = 2:length(psk_orders) semilogy(snr_db, ber_psk_results(i, :), 'o-', 'DisplayName', sprintf('%d-PSK', psk_orde...

ASK, FSK, and PSK (with MATLAB + Online Simulator)

📘 ASK Theory 📘 FSK Theory 📘 PSK Theory 📊 Comparison 🧮 MATLAB Codes 🎮 Simulator ASK or OFF ON Keying ASK is a simple (less complex) Digital Modulation Scheme where we vary the modulation signal's amplitude or voltage by the message signal's amplitude or voltage. We select two levels (two different voltage levels) for transmitting modulated message signals. Example: "+5 Volt" (upper level) and "0 Volt" (lower level). To transmit binary bit "1", the transmitter sends "+5 Volts", and for bit "0", it sends no power. The receiver uses filters to detect whether a binary "1" or "0" was transmitted. Fig 1: Output of ASK, FSK, and PSK modulation using MATLAB for a data stream "1 1 0 0 1 0 1 0" ( Get MATLAB Code ) ...

Comparisons among ASK, PSK, and FSK (with MATLAB + Simulator)

📘 Comparisons among ASK, FSK, and PSK 🧮 Online Simulator for calculating Bandwidth of ASK, FSK, and PSK 🧮 MATLAB Code for BER vs. SNR Analysis of ASK, FSK, and PSK 📚 Further Reading 📂 View Other Topics on Comparisons among ASK, PSK, and FSK ... 🧮 Comparisons of Noise Sensitivity, Bandwidth, Complexity, etc. 🧮 MATLAB Code for Constellation Diagrams of ASK, FSK, and PSK 🧮 Online Simulator for ASK, FSK, and PSK Generation 🧮 Online Simulator for ASK, FSK, and PSK Constellation 🧮 Some Questions and Answers 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 Parameters ASK FSK PSK Variable Characteristics Amplitude Frequency ...

OFDM vs SC-OFDM

  The main difference between OFDM and SC-OFDM is that SC-OFDM transmits the signal using a single carrier, while OFDM uses multiple subcarriers. However, in SC-OFDM, the signal is generated with different sub-bands, but it is transmitted through a single carrier (more technically, through a wideband carrier signal). Block Diagram of OFDM: Data → Modulation → Serial-to-Parallel → IFFT → Add CP → Transmit Received Signal → Remove CP → FFT → Parallel-to-Serial → Demodulation → Data Block Diagram of SC-OFDM: Data → Modulation → DFT → IFFT → Add CP → Transmit Received Signal → Remove CP → FFT → Demodulation → Data    In the case of OFDM, the input modulated data is converted from a serial stream to parallel streams, and different subcarriers are assigned to each chunk. Then, IFFT is applied to these chunks, and a cyclic prefix is added to each one. Each chunk is technically referred to as an OFDM symbol . Unlike OFDM, SC-OFDM does not perform serial-to-parallel conversion o...