我也有类似的问题。在网上找到的解决方案似乎也比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()
小心!然而,这种方法似乎执行一些不需要的低通滤波。在
^{pr2}$
不过,这是我得到的最好的结果,但我希望有人能提供更好的结果。在