def subsampInterp_regular(ts, c, axis=-1): """Makes a subsample sinc interpolation on an N-D array in a given axis. This uses sinc interpolation to return ts(t-a), where a is related to the factor c as described below. ts : the N-D array with a time series in "axis" c : the fraction of the sampling interval such that a = c*dt -- note that c is only useful in the interval (0, 1] axis : specifies the axis of the time dimension """ To = ts.shape[axis] # put time series in the last dimension if axis != -1: ts = np.swapaxes(ts, axis, -1) ## ts_buf = circularize(ts) ts_buf = ts.copy() T = ts_buf.shape[-1] Fn = T / 2 + 1 # make sure the interpolating filter's dtype is complex! filter_dtype = np.dtype(ts.dtype.char.upper()) phs_shift = np.empty((T,), filter_dtype) phs_shift[:Fn] = np.exp(-2.0j * np.pi * c * np.arange(Fn) / float(T)) phs_shift[Fn:] = np.conjugate(reverse(phs_shift[1 : T - (Fn - 1)])) fft(ts_buf, shift=False, inplace=True) np.multiply(ts_buf, phs_shift, ts_buf) ifft(ts_buf, shift=False, inplace=True) ## ts[:] = ts_buf[...,To-1:2*To-1] ts[:] = ts_buf[:] del ts_buf if axis != -1: ts = np.swapaxes(ts, axis, -1)
def subsampInterp_regular(ts, c, axis=-1): """Makes a subsample sinc interpolation on an N-D array in a given axis. This uses sinc interpolation to return ts(t-a), where a is related to the factor c as described below. ts : the N-D array with a time series in "axis" c : the fraction of the sampling interval such that a = c*dt -- note that c is only useful in the interval (0, 1] axis : specifies the axis of the time dimension """ To = ts.shape[axis] # put time series in the last dimension if axis != -1: ts = np.swapaxes(ts, axis, -1) ## ts_buf = circularize(ts) ts_buf = ts.copy() T = ts_buf.shape[-1] Fn = T / 2 + 1 # make sure the interpolating filter's dtype is complex! filter_dtype = np.dtype(ts.dtype.char.upper()) phs_shift = np.empty((T, ), filter_dtype) phs_shift[:Fn] = np.exp(-2.j * np.pi * c * np.arange(Fn) / float(T)) phs_shift[Fn:] = np.conjugate(reverse(phs_shift[1:T - (Fn - 1)])) fft(ts_buf, shift=False, inplace=True) np.multiply(ts_buf, phs_shift, ts_buf) ifft(ts_buf, shift=False, inplace=True) ## ts[:] = ts_buf[...,To-1:2*To-1] ts[:] = ts_buf[:] del ts_buf if axis != -1: ts = np.swapaxes(ts, axis, -1)
def subsampInterp(ts, c, axis=-1): """Makes a subsample sinc interpolation on an N-D array in a given axis. This uses sinc interpolation to return ts(t-a), where a is related to the factor c as described below. ts : the N-D array with a time series in "axis" c : the fraction of the sampling interval such that a = c*dt -- note that c is only useful in the interval (0, 1] axis : specifies the axis of the time dimension """ To = ts.shape[axis] # find ramps and subtract them ramps = np.empty_like(ts) rax_shape = [1] * len(ts.shape) rax_shape[axis] = To rax = np.arange(To) rax.shape = rax_shape (mre, b, r) = lin_regression(ts.real, axis=axis) ramps.real[:] = rax * mre if ts.dtype.type in np.sctypes["complex"]: (mim, b, r) = lin_regression(ts.imag, axis=axis) ramps.imag[:] = rax * mim np.subtract(ts, ramps, ts) # find biases and subtract them ts_mean = ts.mean(axis=axis) if len(ts_mean.shape): mean_shape = list(ts.shape) mean_shape[axis] = 1 ts_mean.shape = tuple(mean_shape) np.subtract(ts, ts_mean, ts) # put time series in the last dimension if axis != -1: ts = np.swapaxes(ts, axis, -1) ts_buf = circularize(ts) ## ts_buf = ts.copy() T = ts_buf.shape[-1] Fn = T / 2 + 1 # make sure the interpolating filter's dtype is complex! filter_dtype = np.dtype(ts.dtype.char.upper()) phs_shift = np.empty((T,), filter_dtype) phs_shift[:Fn] = np.exp(-2.0j * np.pi * c * np.arange(Fn) / float(T)) phs_shift[Fn:] = np.conjugate(reverse(phs_shift[1 : T - (Fn - 1)])) fft(ts_buf, shift=False, inplace=True) np.multiply(ts_buf, phs_shift, ts_buf) ifft(ts_buf, shift=False, inplace=True) ts[:] = ts_buf[..., To - 1 : 2 * To - 1] ## ts[:] = ts_buf[:] del ts_buf if axis != -1: ts = np.swapaxes(ts, axis, -1) # add back biases and analytically interpolated ramps np.subtract(rax, c, rax) # np.add(rax, c, rax) ramps.real[:] = rax * mre if ts.dtype.type in np.sctypes["complex"]: ramps.imag[:] = rax * mim np.add(ts, ramps, ts) np.add(ts, ts_mean, ts)
def subsampInterp(ts, c, axis=-1): """Makes a subsample sinc interpolation on an N-D array in a given axis. This uses sinc interpolation to return ts(t-a), where a is related to the factor c as described below. ts : the N-D array with a time series in "axis" c : the fraction of the sampling interval such that a = c*dt -- note that c is only useful in the interval (0, 1] axis : specifies the axis of the time dimension """ To = ts.shape[axis] # find ramps and subtract them ramps = np.empty_like(ts) rax_shape = [1] * len(ts.shape) rax_shape[axis] = To rax = np.arange(To) rax.shape = rax_shape (mre, b, r) = lin_regression(ts.real, axis=axis) ramps.real[:] = rax * mre if ts.dtype.type in np.sctypes['complex']: (mim, b, r) = lin_regression(ts.imag, axis=axis) ramps.imag[:] = (rax * mim) np.subtract(ts, ramps, ts) # find biases and subtract them ts_mean = ts.mean(axis=axis) if len(ts_mean.shape): mean_shape = list(ts.shape) mean_shape[axis] = 1 ts_mean.shape = tuple(mean_shape) np.subtract(ts, ts_mean, ts) # put time series in the last dimension if axis != -1: ts = np.swapaxes(ts, axis, -1) ts_buf = circularize(ts) ## ts_buf = ts.copy() T = ts_buf.shape[-1] Fn = T / 2 + 1 # make sure the interpolating filter's dtype is complex! filter_dtype = np.dtype(ts.dtype.char.upper()) phs_shift = np.empty((T, ), filter_dtype) phs_shift[:Fn] = np.exp(-2.j * np.pi * c * np.arange(Fn) / float(T)) phs_shift[Fn:] = np.conjugate(reverse(phs_shift[1:T - (Fn - 1)])) fft(ts_buf, shift=False, inplace=True) np.multiply(ts_buf, phs_shift, ts_buf) ifft(ts_buf, shift=False, inplace=True) ts[:] = ts_buf[..., To - 1:2 * To - 1] ## ts[:] = ts_buf[:] del ts_buf if axis != -1: ts = np.swapaxes(ts, axis, -1) # add back biases and analytically interpolated ramps np.subtract(rax, c, rax) #np.add(rax, c, rax) ramps.real[:] = rax * mre if ts.dtype.type in np.sctypes['complex']: ramps.imag[:] = rax * mim np.add(ts, ramps, ts) np.add(ts, ts_mean, ts)