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()
The resampled signal starts at the same value as x but is sampled with a spacing of len(x) / num * (spacing of x) . Because a Fourier method is used, the signal is assumed to be periodic. The data to be resampled. The number of samples in the resampled signal.
To resample a signal by a rational factor p / q , resample calls upfirdn , which conceptually performs these steps: Insert zeros to upsample the signal by p . Apply an FIR antialiasing filter to the upsampled signal. Discard samples to downsample the filtered signal by q .
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:
Check the docstring of interp1d
for options to control the interpolation, and also check out the other interpolation classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With