2

In Matlab:

[s, fs] = wavread(file);  

Since the value of each element in s is between -1 and 1, so we import scikits.audiolab using its wavread function.

from scikits.audiolab import wavread
s, fs, enc = wavread(filename)

But when I red the same input wav file, the value of s in Matlab and Python were totally different. How could I get the same output of s as in MATLAB?

p.s. The wav file is simple 16-bit mono channel file sampled in 44100Hz.

user3602255
  • 21
  • 1
  • 3
  • Well, what is the datatype of `s`, i.e., `type(s)`? How about if you compare using the `'native'` option in Matlab's `wavread`? – horchler May 04 '14 at 22:03

1 Answers1

4

The Matlab wavread() function returns normalised data as default, i.e. it scales all the data to the range -1 to +1. If your audio file is in 16-bit format then the raw data values will be in the range -32768 to +32767, which should match the range returned by scikits.audiolab.wavread().

To normalise this data to within the range -1 to +1 all you need to do is divide the data array by the value with the maximum magnitude (using the numpy module in this case):

from scikits.audiolab import wavread
import numpy

s, fs, enc = wavread(filename)  # s in range -32768 to +32767
s = numpy.array(s)
s = s / max(abs(s))             # s in range -1 to +1

Note that you can also use the 'native' option with the Matlab function to return un-normalised data values, as suggested by horchler in his comment.

[s, fs] = wavread(file, 'native');
  • I can no longer test things in MATLAB, but I think it does not scale values quite as you say. I think it scales max and min integer values (rather than max and min sampled values) to -1 and +1, respectively. So rather than dividing by max(abs(s)), you might instead divide by (2**15 - 1) for 16-bit samples. I'm not yet sure how to look up the max normalizing value dynamically in case of encountering other bit-depths. This is particularly important if you're loading different files and need the levels to be comparable. – Andrew Schwartz Sep 15 '15 at 17:55
  • You can get the maximum value of a data type by `numpy.iinfo(s).max`. – Rob May 31 '16 at 09:12
  • Note: for some applications it seems more common to divide by 2^(n_bits - 1) instead of the max value. See http://stackoverflow.com/questions/2060628/reading-wav-files-in-python – Quetzalcoatl Mar 25 '17 at 18:53