在 python scipy 版本 1.1 中计算信噪比

Calculate Signal to Noise ratio in python scipy version 1.1

我在网上查了一下,似乎 scipy.stats 中的 signaltonoise ratio 函数已被弃用,并且不是在版本 1.1 中可用。 scipy 包中是否还有其他等效方法,因为我无法在网上找到它。

如果没有 scipy 那么是否有任何其他库推荐用于此类计算?

scipy issue #609 on github 所示,signaltonoise 函数

[...] is not useful except for backwards compatibility. The reason is that there's a Matlab signal-to-noise function http://www.mathworks.com/help/signal/ref/snr.html which means something different. This is not good, because scipy clones the Matlab interface of other signal-related functions, and this incompatibility apparently has no offsetting benefit.

如果您确实需要此功能来实现向后兼容,可以在 history of scipy repository as (reproduced here without the documentation comments, license):

中找到简短的实现
def signaltonoise(a, axis=0, ddof=0):
    a = np.asanyarray(a)
    m = a.mean(axis)
    sd = a.std(axis=axis, ddof=ddof)
    return np.where(sd == 0, 0, m/sd)