1
votes

I am attempting to quantize a 16 bit .wav file to a lower bit rate using Matlab. I've opened the file using wavread() but I am unsure of how to proceed from here. I know that somehow I need to "round" each sample value to (for example) a 7 bit number. Here's the code that's reading the file:

[file,rate,bits] = wavread('smb.wav');

file is a 1 column matrix containing the values of each sample. I can loop through each item in that matrix like so:

for i=1 : length(file)
   % not sure what to put here..
end

Could you point me in the right direction to quantize the data?

1

1 Answers

2
votes

If you have int16 data, varying from -32768 to +32767, it can be as simple as

new_data = int8(old_data./2^8);

That won't even require a for loop.

For scaled doubles it would be

new_data = int8(old_data.*2^7);

The wavread documentation suggests that you might even be able retrieve the data in that format to begin with:

[file,rate,bits] = wavread('smb.wav','int8');

EDIT: Changing the bit rate:

After rereading the question, I realize that you also mentioned a lower bit rate which implies reducing the sample rate, not the quantization of the data. If that is the case, you should look at the documentation for downsample, decimate, and/or resample. They are all built in MATLAB functions that change the bit rate.

downsample(file,2)

would half the bit rate, for example.