Compand a Signal
In certain applications, such as speech processing, it is common to use a logarithm
computation, called a compressor, before quantizing a signal. The inverse
operation of a compressor is called an expander. The combination of a
compressor and expander is called a compander. You can use the compand
function to implement a compander.
Compress and Expand Data Sequence Using Mu-Law
This example implements a compander by using a mu-law compressor and expander.
Generate a data sequence.
data = 2:2:12
data = 1×6
2 4 6 8 10 12
Compress the data sequence by using a mu-law compressor. Set the value for mu to 255. The compressed data sequence now ranges between 8.1 and 12.
compressed = compand(data,255,max(data),'mu/compressor')
compressed = 1×6
8.1644 9.6394 10.5084 11.1268 11.6071 12.0000
Expand the compressed data sequence by using a mu-law expander. The expanded data sequence is nearly identical to the original data sequence.
expanded = compand(compressed,255,max(data),'mu/expander')
expanded = 1×6
2.0000 4.0000 6.0000 8.0000 10.0000 12.0000
Calculate the difference between the original data sequence and the expanded sequence.
diffvalue = expanded - data
diffvalue = 1×6
10-14 ×
-0.0444 0.1776 0.0888 0.1776 0.1776 -0.3553
Compress and Expand Data Sequence Using A-Law
This example implements a compander by using an A-law compressor and expander.
Generate a data sequence.
data = 1:5;
Compress the data sequence by using an A-law compressor. Set the value for A to 87.6. The compressed data sequence now ranges between 3.5 and 5.
compressed = compand(data,87.6,max(data),'A/compressor')
compressed = 1×5
3.5296 4.1629 4.5333 4.7961 5.0000
Expand the compressed data sequence by using an A-law expander. The expanded data sequence is nearly identical to the original data sequence.
expanded = compand(compressed,87.6,max(data),'A/expander')
expanded = 1×5
1.0000 2.0000 3.0000 4.0000 5.0000
Calculate the difference between the original data sequence and the expanded sequence.
diffvalue = expanded - data
diffvalue = 1×5
10-14 ×
0 0 0.1332 0.0888 0.0888