sigenc is a library which adds to LME functions for encoding and decoding scalar signals. It implements quantization, DPCM (differential pulse code modulation), and companders used in telephony.
The following statement makes available functions defined in sigenc:
use sigenc
A-law compressor.
use sigenc output = alawcompress(input) output = alawcompress(input, a)
alawcompress(input,a) compresses signal input with A-law method using parameter a. The signal is assumed to be in [-1,1]; values outside this range are clipped. input can be a real array of any size and dimension. The default value of a is 87.6.
The compressor and its inverse, the expander, are static, nonlinear filters used to improve the signal-noise ratio of quantized signals. The compressor should be used before quantization (or on a signal represented with a higher precision).
A-law expander.
use sigenc output = alawexpand(input) output = alawexpand(input, a)
alawexpand(input,a) expands signal input with A-law method using parameter a. input can be a real array of any size and dimension. The default value of a is 87.6.
Differential pulse code modulation decoding.
use sigenc output = dpcmdeco(i, codebook, predictor)
dpcmdeco(i,codebook,predictor) reconstructs a signal encoded with differential pulse code modulation. It performs the opposite of dpcmenco.
Differential pulse code modulation encoding.
use sigenc i = dpcmenco(input, codebook, partition, predictor)
dpcmenco(input,codebook,partition,predictor) quantizes the signal in vector input with differential pulse code modulation. It predicts the future response with the finite-impulse response filter given by polynomial predictor, and it quantizes the residual error with codebook and partition like quantiz. The output i is an array of codes with the same size and dimension as input.
The prediction
y*(k) = sum predictor(i+1) * yq(k-i) for i = 1,2,...,length(predictor)-1
where
use sigenc t = 0:0.1:10; x = sin(t); codebook = -.1:.01:.1; partition = -.0:.01:.09; predictor = [0, 1]; i = dpcmenco(x, codebook, partition, predictor); y = dpcmdeco(i, codebook, predictor);
Differential pulse code modulation decoding.
use sigenc (predictor, codebook, partition) = dpcmopt(in, order, n) (predictor, codebook, partition) = dpcmopt(in, order, codebook0) (predictor, codebook, partition) = dpcmopt(in, predictor, ...) (predictor, codebook, partition) = dpcmopt(..., tol) predictor = dpcmopt(in, order)
dpcmopt(in,order,n) gives the optimal predictor of order order, codebook of size n and partition to encode the signal in vector in with differential pulse code modulation. The result can be used with dpcmenco to encode signals with similar properties. If the second input argument is a vector, it is used as the predictor and not optimized further; its first element must be zero. If the third input argument is a vector, it is used as an initial guess for the codebook, which has the same length. An optional fourth input argument provides the tolerance (the default is 1e-7).
If only the predictor is required, only the input and the predictor order must be supplied as input arguments.
Optimal quantization.
use sigenc (partition, codebook) = lloyds(input, n) (partition, codebook) = lloyds(input, codebook0) (partition, codebook) = lloyds(..., tol)
lloyds(input,n) computes the optimal partition and codebook for quantizing signal input with n codes, using the Lloyds algorithm.
If the second input argument is a vector, lloyds(input,codebook0) uses codebook0 as an initial guess for the codebook. The result has the same length.
A third argument can be used to specify the tolerance used as the stopping criterion of the optimization loop. The default is 1e-7.
We start from a suboptimal partition and compute the distortion:
use sigenc partition = [-1, 0, 1]; codebook = [-2, -0.5, 0.5, 2]; in = -5:0.6:3; (i, out, dist) = quantiz(in, partition, codebook); dist 2.1421
The partition is optimized with lloyds, and the same signal is quantized again. The distortion is reduced.
(partition_opt, codebook_opt) = lloyds(in, codebook) partition_opt = -2.9 -0.5 1.3 codebook_opt = -4.1 -1.7 0.4 2.2 (i, out, dist) = quantiz(in, partition_opt, codebook_opt); dist 1.0543
Table-based signal quantization.
use sigenc i = quantiz(input, partition) (i, output, distortion) = quantiz(input, partition, codebook)
quantiz(input,partition) quantizes signal input
using partition as boundaries between different ranges. Range
from
quantiz(input,partition,codebook) uses codebook as a look-up table to convert back from codes to signal. It should be a vector with one more element than partition. With a second output argument, quantiz gives codebook(i).
With a third output argument, quantiz computes the distortion between input and codebook(i), i.e. the mean of the squared error.
use sigenc partition = [-1, 0, 1]; codebook = [-2, -0.5, 0.5, 2]; in = randn(1, 5) in = 0.1799 -9.7676e-2 -1.1431 -0.4986 1.0445 (i, out, dist) = quantiz(in, partition, codebook) i = 2 1 0 1 2 out = 0.5 -0.5 -2 -0.5 0.5 dist = 0.259
mu-law compressor.
use sigenc output = ulawcompress(input) output = ulawcompress(input, mu)
ulawcompress(input,mu) compresses signal input with mu-law method using parameter mu. input can be a real array of any size and dimension. The default value of mu is 255.
The compressor and its inverse, the expander, are static, nonlinear filters used to improve the signal-noise ratio of quantized signals. The compressor should be used before quantization (or on a signal represented with a higher precision).
mu-law expander.
use sigenc output = ulawexpand(input) output = ulawexpand(input, mu)
ulawexpand(input,mu) expands signal input with mu-law method using parameter a. input can be a real array of any size and dimension. The default value of mu is 255.