how can i quantize the normal distribution using uniform PCM?
Show older comments
sinusoidal signal, there is quantization code for 8 levels and 16 levels using PCM scheme. However, I want to do this for N(0,1) distribution as well. Do I just need to make changes in the "a" variable in this code?
echo on
t=[0:0.01:10];
a=sin(t);
[sqnr8,aquan8,code8]=u_pcm(a,8);
[sqnr16,aquan16,code16]=u_pcm(a, 16);
pause % Press a key to see the SQNR for N = 8.
sqnr8
pause % Press a key to see the SQNR for N = 16.
sqnr16
pause % Press a key to see the plot of the signal and its quantized versions.
plot(t,a,' -',t,aquan8,' - . ',t,aquan16,' -',t,zeros(1,length(t)))
function [sqnr,a_quan,code]=u_pcm(a,n)
%U_PCM uniform PCM encoding of a sequence
% [SQNR,A_QUAN,CODE]=U_PCM(A,N)
% a=input sequence.
% n=number of quantization levels (even).
% sqnr=output SQNR (in dB).
% a_quan=quantized output before encoding.
% code=the encoded output.
amax=max(abs(a));
a_quan=a/amax;
b_quan=a_quan;
d=2/n;
q=d.*[0:n-1];
q=q-((n-1)/2)*d;
for i=1:n
a_quan(find((q(i)-d/2 <= a_quan) & (a_quan <= q(i)+d/2)))=...
q(i).*ones(1,length(find((q(i)-d/2 <= a_quan) & (a_quan <= q(i)+d/2))));
b_quan(find( a_quan==q(i) ))=(i-1).*ones(1,length(find( a_quan==q(i) )));
end
a_quan=a_quan*amax;
nu=ceil(log2(n));
code=zeros(length(a),nu);
for i=1:length(a)
for j=nu:-1:0
if ( fix(b_quan(i)/(2^j)) == 1)
code(i,(nu-j)) = 1;
b_quan(i) = b_quan(i) - 2^j;
end
end
end
sqnr=20*log10(norm(a)/norm(a-a_quan));
Answers (7)
Raikantwar Hrashikesh
on 8 Apr 2022
0 votes
idk
Yashwanth
27 minutes ago
0 votes
%ECE24063
function [quant,qlev] = uni(x,N)
x_max = max(x);
x_min = min(x);
N = 8;
delta = (x_max - x_min)/N;
R = x_min:delta:x_max;
quant = zeros(1,length(x));
qlev = zeros(1,length(x));
for i = 1:length(x)
for j = 1:(length(R)-1)
if x(i) >= R(j) && x(i) <= R(j+1)
quant(i) = R(j)+(delta/2);
qlev(i) = j-1;
end
end
end
end
function 1
Yashwanth
26 minutes ago
0 votes
%ECE24063
function [quant,code] = uniformpcm1(x,N)
x_max = max(x);
x_min = min(x);
N = 8;
delta = (x_max - x_min)/N;
R = x_min:delta:x_max;
quant = zeros(1,length(x));
code = zeros(1,length(x));
for i = 1:length(x)
for j = 1:(length(R)-1)
if x(i) >= R(j) && x(i) <= R(j+1)
quant(i) = R(j)+(delta/2);
qlev(i) = j-1;
end
end
code = dec2bin(qlev);
end
end
2nd func
Yashwanth
26 minutes ago
0 votes
%ECE24063
fs = 1000;
t = 0:1/fs:2;
x = sin(2*pi*5*t);
subplot(2,1,1)
plot(t,x)
title("sine signal")
xlabel("time")
ylabel("amplitude")
N = 8;
[quant,qlev] = uni(x,N);
subplot(2,1,2)
stem(t,quant)
title("Quantization Level")
xlabel("Number of Random Variables")
ylabel("quant")
display(qlev)
Yashwanth
25 minutes ago
0 votes
%ECE24063
fs = 1000;
t = 0:1/fs:2;
x = sin(2*pi*5*t);
subplot(2,1,1)
plot(t,x)
title("sine signal")
xlabel("time")
ylabel("amplitude")
N = 8;
[quant,qlev] = uni(x,N);
[quant,code] = uniformpcm1(x,N);
subplot(2,1,2)
stem(quant)
title("Quantization Level")
xlabel("Number of Random Variables")
ylabel("code")
display(code)
Yashwanth
25 minutes ago
0 votes
fs = 1000;
t = 0:1/fs:2;
x = sin(2*pi*5*t);
N = 8;
[quant,qlev] = uni(x,N);
[quant,code] = uniformpcm1(x,N);
P = mean(x.^2);
D = mean((x-quant).^2);
SQNR = P/D;
SQNR1 = 20*log(SQNR);
display(SQNR)
display(SQNR1)
Yashwanth
24 minutes ago
0 votes
fs = 1000;
t = 0:1/fs:2;
x = sin(2*pi*5*t);
subplot(2,1,1)
stem(t,x)
title("Original signal")
title("t")
xlabel("x")
N = 8;
x_max = max(x);
x_min = min(x);
delta = (x_max - x_min)/N;
[quant,code] = uniformpcm1(x,N);
qlev = bin2dec(code);
q_r = min(x) + ((qlev).*delta) + (delta/2);
subplot(2,1,2)
stem(t,q_r)
title("Reconstructed signal")
title("t")
xlabel("q_r")
Categories
Find more on PCM in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!