gpt4 book ai didi

python - 使用 scipy.signal.resample 对信号进行重采样

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:29 27 4
gpt4 key购买 nike

我尝试使用以下代码将生成的信号从 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()

返回此图(显然是正确的): enter image description here

但是,可以注意到,第一个样本的近似值很差。我相信 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()

enter image description here

关心!然而,这种方法似乎执行了一些不需要的低通滤波。

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

enter image description here

不过,这是我得到的最好的结果,但我希望有人能提供更好的结果。

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com