clc;
close all;
clear all;
n=input('Enter n value for n-bit PCM system : ');
n1=input('Enter number of samples in a period : ');
L=2^n;
% % Signal Generation
% x=0:1/100:4*pi;
% y=8*sin(x); % Amplitude Of signal is 8v
% subplot(2,2,1);
% plot(x,y);grid on;
% Sampling Operation
x=0:2*pi/n1:4*pi; % n1 nuber of samples have tobe selected
s=8*sin(x);
subplot(3,1,1);
plot(s);
title('Analog Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
subplot(3,1,2);
stem(s);grid on; title('Sampled Sinal'); ylabel('Amplitude--->');
xlabel('Time--->');
% Quantization Process
vmax=8;
vmin=-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(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--->');
xlabel('Time--->');
% 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(q); % Plot
Demodulated signal
title('Demodulated Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
https://www.mathworks.com/matlabcentral/fileexchange/28416-pulse-code-modulation?
focused=5158615&tab=function
In this program, MSE, Step size, Bit rate, Quantization Noise are also calculated
function [y Bitrate MSE Stepsize QNoise]=pcm(A,fm,fs,n)
%A=amplitute of cosine signal
%fm=frequency of cosine signal
%fs=sampling frequency
%n= number of bits per sample
%MSE=Mean Squar error, QNoise=Quantization Noise
%Example [y Bitrate MSE Stepsize QNoise]=pcm(2,3,20,3)
%If you have any problem or feedback please contact me @
%%===============================================
% NIKESH BAJAJ
% Asst. Prof., Lovely Professional University, India
% Almameter: Aligarh Muslim University, India
% +919915522564,
[email protected]%%===============================================
t=0:1/(100*fm):1;
x=A*cos(2*pi*fm*t);
%---Sampling-----
ts=0:1/fs:1;
xs=A*cos(2*pi*fm*ts);
%xs Sampled signal
%--Quantization---
x1=xs+A;
x1=x1/(2*A);
L=(-1+2^n); % Levels
x1=L*x1;
xq=round(x1);
r=xq/L;
r=2*A*r;
r=r-A;
%r quantized signal
%----Encoding---
y=[];
for i=1:length(xq)
d=dec2bin(xq(i),n);
y=[y double(d)-48];
end
%Calculations
MSE=sum((xs-r).^2)/length(x);
Bitrate=n*fs;
Stepsize=2*A/L;
QNoise=((Stepsize)^2)/12;
figure(1)
plot(t,x,'linewidth',2)
title('Sampling')
ylabel('Amplitute')
xlabel('Time t(in sec)')
hold on
stem(ts,xs,'r','linewidth',2)
hold off
legend('Original Signal','Sampled Signal');
figure(2)
stem(ts,x1,'linewidth',2)
title('Quantization')
ylabel('Levels L')
hold on
stem(ts,xq,'r','linewidth',2)
plot(ts,xq,'--r')
plot(t,(x+A)*L/(2*A),'--b')
grid
hold off
legend('Sampled Signal','Quantized Signal');
figure(3)
stairs([y y(length(y))],'linewidth',2)
title('Encoding')
ylabel('Binary Signal')
xlabel('bits')
axis([0 length(y) -1 2])
grid
http://www.mathworks.com/matlabcentral/fileexchange/30377-pulse-code-modulation?
focused=5186416&tab=function
Pulse Code Modulation (PCM) Using
MATLAB
PCM is a type of source coding.
Invented by Alec Reeves, it is the standard form of digital audio, CDs, telephony & other digital audio applications. A
PCM stream is a digital representation of an analog signal, in which the magnitude of the analogue signal is sampled
regularly at uniform intervals, with each sample being quantized to the nearest value within a range of digital steps.
A PCM stream has two basic properties that determine the stream's fidelity to the original analog signal: the sampling
rate, which is the number of times per second that samples are taken; and the bit depth, which determines the
number of possible digital values that can be used to represent each sample.
PCM Modulated Sine Wave (Courtesy : Wikipedia)
In the diagram above, a sine wave (red curve) is sampled and quantized for PCM. The sine wave is sampled at
regular intervals, shown as vertical lines. For each sample, one of the available values (on the y-axis) is chosen by
some algorithm. This produces a fully discrete representation of the input signal (blue points) that can be easily
encoded as digital data for storage or manipulation. For the sine wave example at right, we can verify that the
quantized values at the sampling moments are 8, 9, 11, 13, 14, 15, 15, 15, 14, etc. Encoding these values as binary
numbers would result in the following set of nibbles: 1000 (2 3×1+22×0+21×0+20×0=8+0+0+0=8), 1001, 1011, 1101,
1110, 1111, 1111, 1111, 1110, etc. These digital values could then be further processed or analyzed by a digital
signal processor.
MATLAB Implementation (Simulation) Of Pulse Code Modulation (PCM)
clc
close all
clear all
t = 0:0.0001:20; %sampling at niquist rate
c=input('Enter Bit Depth Of PCM Coding:');
part = -1:0.1:1;%A quantization partition defines several contiguous, nonoverlapping ranges
%of values within the set of real numbers.
codebook = -1:0.1:1.1;%A codebook tells the quantizer which common value to assign to inputs that
%fall into each range of the partition.
msg = cos(t);
[~,quants] = quantiz(msg,part,codebook);%returns a vector that tells which interval each input is in
subplot(3,1,1);
plot(t,msg);
title('Message Signal');
subplot(3,1,2);
plot(t,quants);
title('Quantized Signal');
y = uencode(quants,c);
ybin=dec2bin(y,c); %converting it to final binary form to make it transmit ready
subplot(3,1,3);
plot(t,y);
title('PCM PLOT');
Some Concepts Improtant for Coding Implementation:
Two parameters that determine a quantization: a partition and a codebook.
A quantization partition defines several contiguous, nonoverlapping ranges of values within the set of real numbers.
For example, if the partition separates the real number line into the four sets
{x: x ≤ 0}
{x: 0< x ≤ 1}
{x: 1 < x ≤ 3}
{x: 3 < x}
then you can represent the partition as the three-element vector
partition = [0,1,3];
A codebook tells the quantizer which common value to assign to inputs that fall into each range of the partition.
Represent a codebook as a vector whose length is the same as the number of partition intervals. For example, the
vector
codebook = [-1, 0.5, 2, 3]; is one possible codebook for the partition [0,1,3].
NOTE: The output PCM Plot & value projection greatly depends on the bit depth you have chosen. More the bit depth
used, more closely it will resemble the message signal while plotting the curve.
Sample Input & Output:
Enter Bit Depth Of PCM Coding:2
PCM (Pulse Code Modulation) in MATLAB 2 bit Coding
Enter Bit Depth Of PCM Coding:8
PCM (Pulse Code Modulation) in MATLAB 8 bit Coding
Hence its also clear by seeing above two implementation that more the bit depth more accurately the message
signal are coded. But the drawback of increasing the bit depth is, more number of bits is now to be transmitted.
Read more: http://www.divilabs.com/2014/12/pulse-code-modulation-pcm-through-matlab.html#ixzz5B9US2vZX
http://www.divilabs.com/2014/12/pulse-code-modulation-pcm-through-matlab.html
Pulse Code Modulation MATLAB
Code
Important to note this that this is a function file not script, you need to
pass parameters to execute this program.
Step 1. Save "pcm.m" in your current directory of MATLAB
Step 2. define parameters A, fm, fs, n as rquired e.g. A=2, fm=3, fs=20, n=3
Step 3. then write [y Bitrate MSE Stepsize QNoise] = pcm(A,fm,fs,n)
Or After step 1 just write [y Bitrate MSE Stepsize QNoise]=pcm(2,3,20,3) in
command window
Explainantion
Explaination of parameters is as follow
A = amplitute of cosine signal
fm = frequency of cosine signal
fs = sampling frequency
n = number of bits per sample
y = binary output
MSE= Mean Squar error
QNoise = Quantization Noise
Example
or y = pcm(2,3,20,3)
[y Bitrate MSE Stepsize QNoise] = pcm(2,3,20,3)
####==========================================
=
function [y Bitrate
MSE Stepsize
QNoise]=pcm(A,fm,fs,
n)
%A=amplitute of cosine signal
%fm=frequency of cosine signal
%fs=sampling frequency
%n= number of bits per sample
%MSE=Mean Squar error, QNoise=Quantization Noise
%Example [y Bitrate MSE Stepsize
QNoise]=pcm(2,3,20,3)
%If you have any problem or feedback please contact
me @
%%===============================================
% NIKESH BAJAJ
% Asst. Prof., Lovely Professional University,
India
% Almameter: Aligarh Muslim University, India
% +919915522564, [email protected]
%%===============================================
t=0:1/(100*fm):1;
x=A*cos(2*pi*fm*t);
%---Sampling-----
ts=0:1/fs:1;
xs=A*cos(2*pi*fm*ts);
%xs Sampled signal
%--Quantization---
x1=xs+A;
x1=x1/(2*A);
L=(-1+2^n); % Levels
x1=L*x1;
xq=round(x1);
r=xq/L;
r=2*A*r;
r=r-A;
%r quantized signal
%----Encoding---
y=[];
for i=1:length(xq)
d=dec2bin(xq(i),n);
y=[y double(d)-48];
end
%Calculations
MSE=sum((xs-r).^2)/length(x);
Bitrate=n*fs;
Stepsize=2*A/L;
QNoise=((Stepsize)^2)/12;
figure(1)
plot(t,x,'linewidth',2)
title('Sampling')
ylabel('Amplitute')
xlabel('Time t(in sec)')
hold on
stem(ts,xs,'r','linewidth',2)
hold off
legend('Original Signal','Sampled Signal');
figure(2)
stem(ts,x1,'linewidth',2)
title('Quantization')
ylabel('Levels L')
hold on
stem(ts,xq,'r','linewidth',2)
plot(ts,xq,'--r')
plot(t,(x+A)*L/(2*A),'--b')
grid
hold off
legend('Sampled Signal','Quantized Signal');
figure(3)
stairs([y y(length(y))],'linewidth',2)
title('Encoding')
ylabel('Binary Signal')
xlabel('bits')
axis([0 length(y) -1 2])
grid
https://github.com/Nikeshbajaj/Pulse-Code-Modulation
Pulse Code Modulation (Matlab 2012a)
Program:
clc;
clear all;
t = 0:0.0005:20;
partition = -1:0.1:1;
codebook = -1:0.1:1.1;
x = sin(t);
[index,quants] = quantiz(x,partition,codebook);
subplot(3,1,1);
plot(t,x);
title('Message Signal');
xlabel('Time(s) ---->')
ylabel('Amplitude(V) ---->')
subplot(3,1,2);
plot(t,quants);
title('Quantized Signal');
xlabel('Time(s) ---->')
ylabel('Amplitude(V) ---->')
y = uencode(quants,3);
subplot(3,1,3);
plot(t,y);
title('PCM Signal');
xlabel('Time(s) ---->');
ylabel('Amplitude(V) ---->')
Output Waveforms:
http://newlinecode.blogspot.com/2012/11/pulse-code-modulation-matlab-2012a.html
pulsecode modulation using matlab/PCM using
Matlab
March 23, 2012
Pulse code Modulation:
By observing the name we think this is a modulation process but in
reality this a one type of A to D conversion mechanism i.e. analog to digital
conversion technique. Beside this technique we have lot of methods like delta
modulation, differential pulse code modulation, adaptive delta modulation,
adaptive differential pulse code modulation etc in digital communications. There is
always a trade of between all these methods we compare all these based on the bit
rate, circuit implementation, and bandwidth. PCM uses lot of operations to convert
analog signal to digital signal so the technique is complex. At the receiver side we
employ sampling, quantizing, encoding. Generally the two process named as
encoding and quantizing are done in the same circuit known as analog to digital
converter i.e. A to D converter. The operations done in the receiver circuit are
regeneration of the received signal, decoder, and reconstruction or regeneration.
All these operations are performed on the quantized samples. All these operations
are done in the same circuit which is digital to analog converter i.e. D to A
converter. We know that in the process of transmission we face lot of problems
with atmospheric noise and distortion and filtering action of the channel. To
combat all these effects we employ regenerating repeaters in the channel to
reconstruct and re transmit the coded pulses which are in the way to the receiver.
This is time to deal all the processes used in the PCM. A PCM stream is a digital
representation of an analog signal, in which the magnitude of the analog signal is sampled regularly
at uniform intervals, with each sample being quantized to the nearest value within a range of digital
steps. . This is the standard form used for digital audio in computers, CD and DVD
formats. PCM has two properties namely sampling rate and bit depth we all know
about sampling rate bit depth refers to the number of digital values that the each
sample can take. Quantization error is the main drawback of PCM.
Sampling:
Digital communications requires sampling which is first step in all A to D
converters. The sampling is done according to the Nyquist criterion. We may define
the process of sampling as a band-limited signal of finite energy, which has no
frequency components higher than W hertz, may be completely recovered from the
knowledge of its samples taken at the rate of 2W per second. The other definition
is a band-limited signal of finite energy, which has no frequency components higher
than W hertz, is completely described by specifying the values of the signal at
instants of time separated by seconds. We use these to definitions at the
receiver and transmitter. The sampling rate 2W is called the Nyquist rate. And its
inverse is called the Nyquist interval. To make the signal as the band limited signal
we incorporate the low pass filter at the beginning of the transmitter known as the
anti-aliasing filter which stops or attenuates the high frequency components which
lie outside the desired frequency band. Then the filtered signal is sampled at a rate
higher than the Nyquist rate. A circuit known as the sample and hold circuit is
used in sampling to produce the flat top sampling. Flat top sampling provides us to
use simple quantizing strategy. Over sampling is used to reduce the distortion in
most of the analog to digital converters.
Quantizing:
Quantization in DSP represents the process which maps the large set
values to a single value. A circuit which performs this operation is called the
quantizer. This process is also introduces some errors in the signal this is called
quantization error or rounding of error. This is a non linear process because this is
many to one mapping function. Quantized sample has discrete nature in both time
and amplitude. Quantizers are of two types namely uniform and non uniform. In
uniform quantization step size is constant while in non uniform quantization step
size is varied according to the amplitude of the signal. To perform non uniform
quantization one needs to pass the signal into the compressor and then into the
uniform quantizer. Non uniform quantization takes care of both high and small
variations in the amplitude levels of the signal. we have two laws to perform the
quantization they are A-law and µ-law. In the receiver we perform the inverse
operation of the quantizer in this we send the received signal into the expander.
The device which can perform both operations of expansion and compression is
called compander.
Encoding:
Encoding is done to transmit the signal obtained by the combined effect
of the sampling and quantizing over the channel which has undesired
characteristics. The signal obtained from the sampling and quantizing is not
suitable for transmission as it is over a telephone line i.e. copper pair or radio or
microwave link. To combat with the channel impairments we use encoding process.
For this we use different types of coding techniques. Encoding process is a one to
one representation. A particular arrangement of symbols used in a code to
represent a single value of the discrete set is called a code word or character. In a
binary code each symbol may be represented by the any two levels of presence of
pulse or absence of pulse. Binary wave is robust to noise and easy to regenerate.
For example in a binary code each sample is represented with the n number of bits
then we can represent the total of 2n discrete levels.
http://kemppro.blogspot.com/2012/03/pulsecode-modulation-using-matlabpcm.html