Python 中的可逆 STFT 和 ISTFT
是否有任何通用形式的 短时傅里叶变换 以及内置的相应逆变换SciPy 或 NumPy 或其他什么?
matplotlib 中有 pyplot specgram
函数,它调用 ax.specgram()
,它调用 mlab.specgram()
,它调用 _spectral_helper()
:
#检查 y 是否为 x,以便我们可以使用相同的函数 #实现psd()、csd()和spectrogram()的核心,无需执行任何操作 #额外的计算。我们返回未平均的 Pxy、freqs 和 t。
但
这是一个辅助函数,它实现了 204 #psd、csd 和频谱图。这是 不意味着在 mlab 之外使用
但我不确定这是否可以用于执行 STFT 和 ISTFT。还有其他什么吗,或者我应该翻译一些类似这些 MATLAB 函数 的内容吗?
我知道如何编写自己的临时实现;我只是在寻找功能齐全的东西,它可以处理不同的窗口函数(但有一个合理的默认值),并且与 COLA 窗口完全可逆(istft(stft(x))==x
) ,经过多人测试,没有差一错误,处理结束和零填充良好,快速 RFFT 实现真实输入等。
Is there any general-purpose form of short-time Fourier transform with corresponding inverse transform built into SciPy or NumPy or whatever?
There's the pyplot specgram
function in matplotlib, which calls ax.specgram()
, which calls mlab.specgram()
, which calls _spectral_helper()
:
#The checks for if y is x are so that we can use the same function to #implement the core of psd(), csd(), and spectrogram() without doing #extra calculations. We return the unaveraged Pxy, freqs, and t.
but
This is a helper function that implements the commonality between the
204 #psd, csd, and spectrogram. It is
NOT meant to be used outside of mlab
I'm not sure if this can be used to do an STFT and ISTFT, though. Is there anything else, or should I translate something like these MATLAB functions?
I know how to write my own ad-hoc implementation; I'm just looking for something full-featured, which can handle different windowing functions (but has a sane default), is fully invertible with COLA windows (istft(stft(x))==x
), tested by multiple people, no off-by-one errors, handles the ends and zero padding well, fast RFFT implementation for real input, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
上述两个答案对我来说都不起作用。所以我修改了 Steve Tjoa 的。
Neither of the above answers worked well OOTB for me. So I modified Steve Tjoa's.
我还在 GitHub 上找到了这个,但它似乎在管道上操作而不是在普通数组上操作:
http://github.com/ronw/frontend/blob/master/basic.py#LID281
I also found this on GitHub, but it seems to operate on pipelines instead of normal arrays:
http://github.com/ronw/frontend/blob/master/basic.py#LID281
我认为 scipy.signal 有你正在寻找的东西。它具有合理的默认值,支持多种窗口类型等...
http://docs.scipy.org/doc/scipy-0.17.0/reference/ generated/scipy.signal.spectrogram.html
I think scipy.signal has what you are looking for. It has reasonable defaults, supports multiple window types, etc...
http://docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.signal.spectrogram.html
basj 答案的固定版本。
A fixed version of basj's answer.
只需分享我的解决方案
导入
定义正向和反向 fft 函数
玩具示例
计算窗口边缘的
检查结果
Just share my solution
imports
define forward and inverse fft functions
compute toy example
check result on edge of window
如果您有权访问可以执行您想要的操作的 C 二进制库,请使用 http://code.google .com/p/ctypesgen/ 生成该库的 Python 接口。
If you have access to a C binary library that does what you want, then use http://code.google.com/p/ctypesgen/ to generate a Python interface to that library.
这是我的 Python 代码,针对这个答案进行了简化:
注意:
blkproc
。我没有使用for
循环,而是将命令(例如fft
)应用于列表理解中信号的每一帧,然后使用scipy.array 将其转换为二维数组。我用它来制作频谱图、色谱图、MFCC 图等等。
istft
中使用了简单的重叠相加方法。为了重建原始信号,顺序窗函数的总和必须是常数,最好等于单位(1.0)。在本例中,我选择了 Hann(或hanning
)窗口和 50% 的重叠,效果非常好。有关详细信息,请参阅此讨论。测试:
Here is my Python code, simplified for this answer:
Notes:
blkproc
in Matlab. Instead of afor
loop, I apply a command (e.g.,fft
) to each frame of the signal inside a list comprehension, and thenscipy.array
casts it to a 2D-array. I use this to make spectrograms, chromagrams, MFCC-grams, and much more.istft
. In order to reconstruct the original signal the sum of the sequential window functions must be constant, preferably equal to unity (1.0). In this case, I've chosen the Hann (orhanning
) window and a 50% overlap which works perfectly. See this discussion for more information.A test:
这是我使用的 STFT 代码。 STFT + ISTFT 这里提供了完美的重建(即使对于第一帧)。我稍微修改了 Steve Tjoa 给出的代码:这里重建信号的幅度与输入信号的幅度相同。
Here is the STFT code that I use. STFT + ISTFT here gives perfect reconstruction (even for the first frames). I slightly modified the code given here by Steve Tjoa : here the magnitude of the reconstructed signal is the same as that of the input signal.
librosa.core.stft
和istft
看起来非常相似到我正在寻找的东西,尽管它们当时不存在:他们不不过,不能完全颠倒;末端是锥形的。
librosa.core.stft
andistft
look pretty similar to what I was looking for, though they didn't exist at the time:They don't invert exactly, though; the ends are tapered.
我有点晚了,但意识到 scipy 内置了 istft 函数自 0.19.0 起
I'm a little late to this, but realised scipy has inbuilt istft function as of 0.19.0
找到另一个STFT,但没有相应的反函数:
http: //code.google.com/p/pytfd/source/browse/trunk/pytfd/stft.py
Found another STFT, but no corresponding inverse function:
http://code.google.com/p/pytfd/source/browse/trunk/pytfd/stft.py