在python浮点中刷新为零和/或有效数字

2024-06-28 20:56:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用音频文件(.wavs)执行ICA应用程序。在使用scipywavfile时,我从cprofile中注意到,我的load_wav函数执行得非常差。以4.5GHz的CPU速度处理44100立体声.wav文件需要30分钟。请记住,.wav文件的持续时间只有10秒。以下是我的参考功能:

def load_wav(filename,samplerate=44100):

    # load file
    rate, data = wavfile.read(filename)

    # convert stereo to mono
    if len(data.shape) > 1:
        data = data[:,0]/2 + data[:,1]/2

    # re-interpolate samplerate    
    ratio = float(samplerate) / float(rate)
    data = resample(data, len(data) * ratio)

    return samplerate, data.astype(np.int16)

主要是重新插值部分需要花费很长时间。我研究了我所能做的,似乎并不是所有的计算机都擅长处理大量的浮动。有些浮动可能接近于零,但不完全是零。在我的音频示例中,这可能是一个人讲话停顿的结果,也可能发生在文件的开头或结尾,然后列表继续。在

所以说实话,有人能在Python中分享类似c++的解决方法吗?他们称这个问题为“非规范化”,并建议将其归零。在

http://blog.audio-tk.com/2016/09/20/audio-toolkit-handling-denormals/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+eifelle%2FCPPV+%28Matt%27s+blog%29

如果知道零是否是唯一值得警惕的数字,也会让人心安。也许,我应该为所有数字设置有效数字?这会对音频应用产生影响吗?如果是这样,在Python中会是什么样子?显然,我们可能希望避免越界,比如只使用整数,这可能会导致大量的声音退化。在

不过,我还是要在计算速度的范围内,考虑一下我的问题。在

如果可以请帮忙

谢谢你

在 进口如下:

^{pr2}$

此外,如果您想查看jupyter笔记本以查看整个ICA应用程序,可以在此处执行此操作: http://web.archive.org/web/20150314223948/http://shogun-toolbox.org/static/notebook/current/bss_audio.html

**该链接使用Python2.7,我使用的是Python3.5,因此情况可能略有不同。在

--编辑

嗯。很抱歉给你添麻烦了。为什么X的形状改变的答案是因为这个例子笔记本对点积的处理方式与我以前使用的方式不同。他们使用S=(np.c_[s1,s2,s3]).T,然后使用np.dot(A,S)。我通常使用S=np.c_[s1,s2,s3],然后使用np.dot(S,A.T)。所以我所要做的就是改变一些东西,以获得所需的X的形状。典型的例子是在我跳跃之前不看。我错了,把造成那个形状错误的原因归咎于{}。事实证明它可以处理素数,保证数据是偶数。我将删除我的一些不必要的评论,以恢复我的帖子的清晰度。在

就音频处理代码效率而言,判决结果仍然是零/有效数字阈值。我希望我的问题的这一特定方面很快会被重新讨论。在此之前,我将使用np.delete(data,-1)作为处理大/素数的快速而肮脏的解决方案。在


Tags: 文件应用程序httpdataratenploadfilename
1条回答
网友
1楼 · 发布于 2024-06-28 20:56:11

正如AndyG已经评论的,the documentation for ^{}警告您

... resample uses FFT transformations, which can be very slow if the number of input samples is large and prime, see scipy.fftpack.fft.

441263既大又好。在

如果您有scipy>;=0.18.0的版本,您可以改为尝试^{}

This polyphase method will likely be faster than the Fourier method in scipy.signal.resample when the number of samples is large and prime, or when the number of samples is large and up and down share a large greatest common denominator.

例如:

import numpy
from scipy.signal import resample, resample_poly

x = np.random.randn(441263)

%timeit resample(x, x.shape[0] // 4)
# 1 loop, best of 3: 2min 18s per loop

%timeit resample_poly(x, 10, 40)
# 100 loops, best of 3: 9.05 ms per loop

相关问题 更多 >