user3602255
user3602255

Reputation: 21

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?

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

Upvotes: 2

Views: 3261

Answers (1)

user3596308
user3596308

Reputation:

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');

Upvotes: 4

Related Questions