In certain applications, such as speech processing, it is common to use a logarithm computation, called a compressor, before quantizing. The inverse operation of a compressor is called an expander. The combination of a compressor and expander is called a compander.
The compand
function supports two kinds
of companders: µ-law and A-law companders. Its reference page
lists both compressor laws.
Set the mu-law parameter.
mu = 255;
Create an exponential signal and calculate its maximum value.
sig = exp(-4:0.1:4); V = max(sig);
Quantize the signal by using equal-length intervals. Set partition and codebook values, assuming 6-bit quantization. Calculate the mean square distortion.
partition = 0:2^6 - 1; codebook = 0:2^6; [~,~,distortion] = quantiz(sig,partition,codebook);
Compress the signal by using the compand
function. Apply quantization and expand the quantized signal. Calculate the mean square distortion of the companded signal.
compsig = compand(sig,mu,V,'mu/compressor'); [~,quants] = quantiz(compsig,partition,codebook); newsig = compand(quants,mu,max(quants),'mu/expander'); distortion2 = sum((newsig - sig).^2)/length(sig);
Compare the mean square distortions. The distortion is smaller when you perform companding. This difference is because equal-length intervals are well suited to the logarithm of the exponential signal but not well suited to exponential signal itself.
[distortion, distortion2]
ans = 1×2
0.5348 0.0397
Plot the original exponential signal and the companded signal.
plot([sig' compsig']); title('Comparison between Original signal and Companded Signal'); xlabel('Interval'); ylabel('Apmlitude'); legend('Original','Companded','location','nw');