Digital Sound & Music: Concepts, Applications, & Science, Chapter 5, last updated 6/25/2013

54

in Algorithm 5.2. You can output your files as uncompressed RAW files, import the data into

MATLAB, and graph the error waves, comparing the shape of the error with dither-only and

with noise shaping using various values for c, the scaling factor. The algorithm given is the most

basic kind of noise shaping you can do. Many refinements and variations of this algorithm have

been implemented and distributed commercially.

algorithm noise_shape {

/*Input:

b_orig, the original bit depth

b_new, the new bit depth to which samples are to be quantized

F_in, an array of N digital audio samples that are to be

quantized, dithered, and noise shaped. It’s assumed that these are read

in from a RAW file and are values between

–2^b_orig-1 and (2^b_orig-1)-1.

c, a scaling factor for the noise shaping

Output:

F_out, an array of N digital audio samples quantized to bit

depth b_new using dither and noise shaping*/

s = (2^b_orig)/(2^b_new);

c = 0.8; //Other scaling factors can be tried.*/

e = 0;

for (i = 0; i N; i++) {

/*Get a random number between 1 and 1 from some probability density

function*/

d = pdf();

F_scaled = F_in[i] / s; //Integer division, discarding remainder

F_scaled_plus_dith_and_error = F_scaled + d + c*e;

F_out[i] = floor(F_scaled_plus_dith_and_error);

e = F_scaled – F_out[i];

}

}

Algorithm 5.2

5.3.8 Algorithms for Audio Companding and Compression

5.3.8.1 Mu-law Encoding

Mu-law encoding (also denoted -law) is a nonlinear companding method used in

telecommunications. Companding is a method of compressing a digital signal by reducing the

bit depth before it is transmitted and then expanding it when it is received. The advantage of

mu-law encoding is that it preserves some of the dynamic range that would be lost if a linear

method of reducing the bit depth were used instead. Let’s look more closely at the difference

between linear and nonlinear methods of bit depth reduction.

Picture this scenario. A telephone audio signal is to be transmitted. It is originally 16

bits. To reduce the amount of data that has to be transmitted, the audio data is reduced to 8 bits.

In a linear method of bit depth reduction, the amount of quantization error possible at low

amplitudes is the same as the amount of error possible at high amplitudes. However, this means

that the ratio of the error to the sample value is greater for low amplitude samples than for high.

Let's assume that in converting from 16 bits to 8 bits is done by dividing by the sample value by

256 and rounding down. Consider a 16-bit sample that is originally 32767, the highest positive