Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

python - resampled time using scipy.signal.resample

I have a signal that is not sampled equidistant; for further processing it needs to be. I thought that scipy.signal.resample would do it, but I do not understand its behavior.

The signal is in y, corresponding time in x. The resampled is expected in yy, with all corresponding time in xx. Does anyone know what I do wrong or how to achieve what I need?

This code does not work: xx is not time:

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

x = np.array([0,1,2,3,4,5,6,6.5,7,7.5,8,8.5,9])
y = np.cos(-x**2/4.0)
num=50
z=signal.resample(y, num, x, axis=0, window=None)
yy=z[0]
xx=z[1]
plt.plot(x,y)
plt.plot(xx,yy)
plt.show()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Even when you give the x coordinates (which corresponds to the t argument), resample assumes that the sampling is uniform.

Consider using one of the univariate interpolators in scipy.interpolate.

For example, this script:

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt

x = np.array([0,1,2,3,4,5,6,6.5,7,7.5,8,8.5,9])
y = np.cos(-x**2/4.0)

f = interpolate.interp1d(x, y)

num = 50
xx = np.linspace(x[0], x[-1], num)
yy = f(xx)

plt.plot(x,y, 'bo-')
plt.plot(xx,yy, 'g.-')
plt.show()

generates this plot:

plot

Check the docstring of interp1d for options to control the interpolation, and also check out the other interpolation classes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...