简体   繁体   English

如何在Python中实现MATLAB函数wavread()?

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

In Matlab: 在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. 由于s中每个元素的值在-1和1之间,因此我们使用其wavread函数导入scikits.audiolab。

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. 但是,当我对相同的输入wav文件进行红色处理时,在Matlab和Python中s的值完全不同。 How could I get the same output of s as in MATLAB? 如何获得与MATLAB中相同的s输出?

ps The wav file is simple 16-bit mono channel file sampled in 44100Hz. ps wav文件是简单的16位单声道文件,以44100Hz采样。

The Matlab wavread() function returns normalised data as default, ie it scales all the data to the range -1 to +1. Matlab wavread()函数默认返回归一化数据,即它将所有数据缩放到-1到+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() . 如果您的音频文件为16位格式,则原始数据值将在-32768至+32767的范围内,该范围应与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): 要将数据标准化为-1到+1范围内,您需要做的就是将数据数组除以具有最大幅度的值(在这种情况下使用numpy模块):

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. 请注意,您也可以在Matlab函数中使用'native'选项来返回未归一化的数据值,如horchler在其评论中所建议的那样。

[s, fs] = wavread(file, 'native');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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