Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 1000
frits Avatar asked Jan 02 '14 18:01

frits


People also ask

How does Scipy signal resample work?

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.

How do you resample a 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 .


1 Answers

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.

like image 75
Warren Weckesser Avatar answered Oct 08 '22 18:10

Warren Weckesser