DSPRelated.com
Code

WAV Quantizer with Error Output

Ron April 1, 2011 Coded in Matlab

A script that quantizes an input WAV file and outputs a WAV and error WAV. Truncate represents the number of bits to lower the resolution by.

ex. if the original file is a 16 bit WAV and truncate = 6, the output will be a 10 bit WAV

truncate = 6;
[f, fs, nbits] = wavread('in.wav');
f_int = int16(f*2^(nbits-1-truncate));
f_back_to_float = (double(f_int))/2^(nbits-1-truncate);
f_diff = f - f_back_to_float;
wavwrite(f_back_to_float, fs, nbits, 'out.wav');
wavwrite(f_diff, fs, nbits, 'error.wav');