使用scipy.signal.resample重采样信号

时间:2018-07-19 10:58:49

标签: python python-3.x scipy

我正在尝试使用以下代码将生成的信号从 toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); toolbar.setTitle(performer_name); toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp); 个样本重采样为256个样本:

20

哪个返回此图(显然是正确的): output

但是,可以注意到,第一个样本的近似值很差。我相信import scipy.signal import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 256, endpoint=False) y = np.cos(-x**2/6.0) yre = signal.resample(y,20) xre = np.linspace(0, 10, len(yre), endpoint=False) plt.plot(x,y,'b', xre,yre,'or-') plt.show() 会计算属于等距样本组的样本的平均值,并且在这种情况下,为了估计第一个输出样本,似乎在样本的第一个子组的开头填充了零。

因此,我认为可以通过告诉resample函数我不想在第一个子组中填充零来成功估计第一个样本。

有人可以帮助我实现对该信号的正确重采样吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

我有类似的问题。在网上找到的解决方案似乎也比pasteSheet.Cells(1, columns.count).End(xlToLeft).Offset(0, 1) https://github.com/nwhitehead/swmixer/blob/master/swmixer.py)快。它基于scipy.signal.resample函数。还添加了np.interp进行比较(在这种情况下,效果不是很好)。

scipy.signal.resample_poly

enter image description here

小心!但是,此方法似乎执行了一些不需要的低通滤波。

import scipy.signal 
import matplotlib.pyplot as plt
import numpy as np

# DISCLAIMER: This function is copied from https://github.com/nwhitehead/swmixer/blob/master/swmixer.py, 
#             which was released under LGPL. 
def resample_by_interpolation(signal, input_fs, output_fs):

    scale = output_fs / input_fs
    # calculate new length of sample
    n = round(len(signal) * scale)

    # use linear interpolation
    # endpoint keyword means than linspace doesn't go all the way to 1.0
    # If it did, there are some off-by-one errors
    # e.g. scale=2.0, [1,2,3] should go to [1,1.5,2,2.5,3,3]
    # but with endpoint=True, we get [1,1.4,1.8,2.2,2.6,3]
    # Both are OK, but since resampling will often involve
    # exact ratios (i.e. for 44100 to 22050 or vice versa)
    # using endpoint=False gets less noise in the resampled sound
    resampled_signal = np.interp(
        np.linspace(0.0, 1.0, n, endpoint=False),  # where to interpret
        np.linspace(0.0, 1.0, len(signal), endpoint=False),  # known positions
        signal,  # known data points
    )
    return resampled_signal

x = np.linspace(0, 10, 256, endpoint=False)
y = np.cos(-x**2/6.0)
yre = scipy.signal.resample(y,20)
xre = np.linspace(0, 10, len(yre), endpoint=False)

yre_polyphase = scipy.signal.resample_poly(y, 20, 256)
yre_interpolation = resample_by_interpolation(y, 256, 20)

plt.figure(figsize=(10, 6))
plt.plot(x,y,'b', xre,yre,'or-')
plt.plot(xre, yre_polyphase, 'og-')
plt.plot(xre, yre_interpolation, 'ok-')
plt.legend(['original signal', 'scipy.signal.resample', 'scipy.signal.resample_poly', 'interpolation method'], loc='lower left')
plt.show()

enter image description here

尽管如此,这是我获得的最好结果,但我希望有人能提供更好的东西。

答案 1 :(得分:1)

作为scipy.signal.resample状态的参考页,它使用FFT方法执行重采样。

https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.resample.html

副作用之一是隐式假设(由于底层FFT),信号是周期性的;因此,如果从x [0]到x [-1]的步长很大,则重采样将很难使它们满足:FFT认为类似时间的轴不是直线,而是圆。

FFT是一个功能强大的工具,但它是一个功能强大的工具,它的锋利边缘可以割伤您。