%Spring 99 ECOMMS Class Demo
%S. Mandayam, Rowan University
%PCM: sampling, quantization and encoding
close all;
clear;

%original time domain signal
t1=(0.01:0.1:1.25*pi);
x=7*sin(t1);

%Sampling: PAM signal
t2=(0.01:0.2:1.25*pi);
xpam=7*sin(t2);

%mid-tread quantization - 8 levels
partition=[-6:2:6];
y1=quantiz(xpam,partition);
y=(y1-3.5)*2;

figure(1);
plot(t1,x);
hold;
bar(t2,xpam,0.5,'c');
bar(t2,y,0.2,'r');
grid on;
legend('Analog','PAM','Quantized PAM');
xlabel('time');
title('PCM: Sampling (PAM) and Uniform Quantization (Midrise)');

pause;

%Encoding
%Diplaying code range
xrange=-7:7;
yrange=quantiz(xrange,partition);
code=fliplr(de2bi(yrange))

%encoding the sine signal
xpcm=fliplr(de2bi(y1));
bitstream=reshape(xpcm',prod(size(xpcm)),1);

figure(2);
subplot 211
plot(t1,x);
hold;
bar(t2,y,0.2,'r');
grid on;
legend('Analog','Quantized PAM');
xlabel('time');
title('PCM: Sampling & Uniform Quantization (Midrise)');


subplot 212
bar(bitstream);
axis([0 length(bitstream) -2 2]);
title('Encoded Unipolar R-Z');










