%Spring 99 ECOMMS Class Demo
%S. Mandayam, Rowan University
%PCM: sampling, quantization and encoding with
%A-law companding
clear;

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

%A-law companded analog signal
x_mu=compand(x,87.6,7,'A/compressor');

%Sampling: PAM signal
t2=(0.01:0.2:1.25*pi);
xpam=compand(7*sin(t2),87.6,7,'A/compressor');

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

figure;
plot(t1,x);
hold;
plot(t1,x_mu,'r');
bar(t2,xpam,0.5,'c');
bar(t2,y,0.2,'r');
grid on;
legend('Analog','Companded: A=87.6','PAM','Quantized PAM');
xlabel('time');
title('PCM: Sampling (PAM) and A-law Quantization (A=87.6)');

pause;

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

figure;
subplot 211
plot(t1,x);
hold;
bar(t2,y,0.2,'r');
grid on;
legend('Analog','Quantized PAM');
xlabel('time');
title('PCM: Sampling & A-law Quantization (A=87.6)');

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










