| Communications Toolbox | ![]() |
The code below quantizes an exponential signal in two ways and compares the resulting mean square distortions. First, it uses the quantiz function with a partition consisting of length-one intervals. In the second trial, compand implements a µ-law compressor, quantiz quantizes the compressed data, and finally compand expands the quantized data. The output shows that the distortion is smaller for the second scheme. This is because equal-length intervals are well suited to the logarithm of sig, but not well suited to sig. The figure shows how the compander changes sig.
Mu = 255; % Parameter for mu-law compander
sig = -4:.1:4;
sig = exp(sig); % Exponential signal to quantize
V = max(sig);
% 1. Quantize using equal-length intervals and no compander.
[index,quants,distor] = quantiz(sig,0:floor(V),0:ceil(V));
% 2. Use same partition and codebook, but compress
% before quantizing and expand afterwards.
compsig = compand(sig,Mu,V,'mu/compressor');
[index,quants] = quantiz(compsig,0:floor(V),0:ceil(V));
newsig = compand(quants,Mu,max(quants),'mu/expander');
distor2 = sum((newsig-sig).^2)/length(sig);
[distor, distor2] % Display both mean square distortions.
plot(sig); % Plot original signal.
hold on;
plot(compsig,'r--'); % Plot companded signal.
legend('Original','Companded','Location','NorthWest')The output and figure are below.
ans =
0.5348 0.0397

| Companding a Signal | Huffman Coding | ![]() |
© 1994-2005 The MathWorks, Inc.