我正在尝试使用以下代码将生成的信号从256样本重新采样为20样本:

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()
哪个返回此图(显然是正确的):
python - 使用scipy.signal.resample重采样信号-LMLPHP
但是,可以注意到,第一个样本的估计值很差。我相信resample会计算属于等距样本组的样本的平均值,在这种情况下,为了估计第一个输出样本,似乎在开始时样本的第一子组中填充了零。
因此,我认为可以通过告诉resample函数我不想用零填充第一子组来成功估计第一个样本。
有人可以帮助我实现此信号的正确重采样吗?
提前致谢。

最佳答案

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

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()

python - 使用scipy.signal.resample重采样信号-LMLPHP

关心!但是,此方法似乎执行了一些不必要的低通滤波。
x = np.linspace(0, 10, 16, endpoint=False)
y = np.random.RandomState(seed=1).rand(len(x))
yre = scipy.signal.resample(y, 18)
xre = np.linspace(0, 10, len(yre), endpoint=False)

yre_polyphase = scipy.signal.resample_poly(y, 18, 16)
yre_interpolation = resample_by_interpolation(y, 16, 18)

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()

python - 使用scipy.signal.resample重采样信号-LMLPHP

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

关于python - 使用scipy.signal.resample重采样信号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51420923/

10-08 21:53