python scipy.signal_信号重采样scipy.signal.resamp

我也有类似的问题。在网上找到的解决方案似乎也比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}$

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值