简体   繁体   中英

How can MATLAB function wavread() be implemented in Python?

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?

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

The Matlab wavread() function returns normalised data as default, ie 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM