You are on page 1of 5

Matlab experiments: Communication system

Experiment 1A: Evaluate performance of quantizer when quantizing uniformly


distributed samples.
%Uniform quantization
%Part 1- Uniform distribution of noise
close all;
clear all;
clc;
N = 100;
L1 = 4;
L2 = 16;
F = 1;
n = linspace(-1,1,N);
x = -F + 2*F*rand(1,N);
e1 = 0;
e2 = 0;
s = 1:N;
for i=0:N-1
for j=0:L1-1
a = -F + j*2*F/L1;
b = -F + (j+1)*2*F/L1;
if (x(i+1) < -F)
Sq1(i+1) = -F + F/L1;
end
if (x(i+1) > F)
Sq1(i+1) = F - F/L1;
end
if(x(i+1) >=a && x(i+1) <b)
Sq1(i+1) = (a+b)/2;
end
end
end
for i=0:N-1

end

for j=0:L2-1
a = -F + j*2*F/L2;
b = -F + (j+1)*2*F/L2;
if (x(i+1) < -F)
Sq2(i+1) = -F + F/L2;
end
if (x(i+1) > F)
Sq2(i+1) = F - F/L2;
end
if(x(i+1) >=a && x(i+1) <b)
Sq2(i+1) = (a+b)/2;
end
end

for i = 0:N-1
e1 = e1 + (x(i+1) - Sq1(i+1))^2;
e2 = e2 + (x(i+1) - Sq2(i+1))^2;
end
e1 = sqrt(e1/N)
e2 = sqrt(e2/N)

subplot(3,1,1)
plot(s,x); xlabel('n'); ylabel('original signal');
subplot(3,1,2)
stairs(s,Sq1); xlabel('n'); ylabel('4 level quantizer');
subplot(3,1,3)
stairs(s,Sq2); xlabel('n'); ylabel('16 level quantizer');

Result:

Experiment 1B: Evaluate performance of uniform quantizer while quantizing


Gaussian distributed samples.
%Uniform quantization
%Part 2: Gaussian distribution of noise
%Uniform quantization
%Part 1- Uniform distribution of noise
close all;
clear all;
clc;
N = 100;
L1 = 4;
L2 = 16;
F = 5;
n = linspace(-1,1,N);
x = randn(1,N);
e1 = 0;
e2 = 0;

s = 1:N;
for i=0:N-1
for j=0:L1-1
a = -F + j*2*F/L1;
b = -F + (j+1)*2*F/L1;
if (x(i+1) < -F)
Sq1(i+1) = -F + F/L1;
end
if (x(i+1) > F)
Sq1(i+1) = F - F/L1;
end
if(x(i+1) >=a && x(i+1) <b)
Sq1(i+1) = (a+b)/2;
end
end
end
for i=0:N-1
for j=0:L2-1
a = -F + j*2*F/L2;
b = -F + (j+1)*2*F/L2;
if (x(i+1) < -F)
Sq2(i+1) = -F + F/L2;
end
if (x(i+1) > F)
Sq2(i+1) = F - F/L2;
end
if(x(i+1) >=a && x(i+1) <b)
Sq2(i+1) = (a+b)/2;
end
end
end
for i = 0:N-1
e1 = e1 + (x(i+1) - Sq1(i+1))^2;
e2 = e2 + (x(i+1) - Sq2(i+1))^2;
end
e1 = sqrt(e1/N)
e2 = sqrt(e2/N)
subplot(3,1,1)
plot(s,x); xlabel('n'); ylabel('original signal');
subplot(3,1,2)
stairs(s,Sq1); xlabel('n'); ylabel('4 level quantizer');
subplot(3,1,3)
stairs(s,Sq2); xlabel('n'); ylabel('16 level quantizer');

Result:

Experiment 2: Evaluate performance of a non uniform quantizer when used to


evaluate a speech sample.
close all;
clear all;
clc;
[X,Fs,bps] = wavread('hello.wav');
V =
A =
N =
s =
for

max(max(X));
42;
length(X);
1:N;
i=1:N
Z(i) = X(i,1);
%Z(i,2) = X(i,2);

end
wavwrite(Z,Fs,'test');
for (i = 1:N)
x = abs(X(i,1));
if(X(i,1) > 0)
if ( (x >= 0) && (x <= V/A))
Y(i) = A*x/(1 + log(A));
end
if ( (x > V/A) && (x <= V))
Y(i) = V*(1 + log(A*x/V))/(1 + log(A));
end
else
if ( x >= 0 && x <= V/A)
Y(i) = -A*x/(1 + log(A));
end
if ( (x > V/A) && (x <= V))
Y(i) = -V*(1 + log(A*x/V))/(1 + log(A));
end
end
end
wavwrite(Y,Fs,'hello_Acomp');
V1 = max(Y);

for i=1:N
if ( abs(Y(i)) <= V/(1+log(A)))
Xr(i) = Y(i)*(1 + log(A))/A;
end
if ( (abs(Y(i)) >= V/(1+log(A))) && (abs(Y(i)) <= V))
if (Y(i) > 0)
Xr(i) = exp(abs(Y(i))*(1 + log(A))/V - 1)*V/A;
else
Xr(i) = -exp(abs(Y(i))*(1 + log(A))/V - 1)*V/A;
end
end
end
wavwrite(Xr,Fs,'hello_Aexp');
subplot(3,1,1)
plot(s,Z); xlabel('n'); ylabel('original signal');
subplot(3,1,2)
stairs(s,Y); xlabel('n'); ylabel('Compressed signal');
subplot(3,1,3)
stairs(s,Xr); xlabel('n'); ylabel('Expanded signal at receiver');

Result:

You might also like