RESAMPLE
This function changes the number of samples in a signal using an FFT-based resampling method. The method interprets the signal as periodic, modifies its spectrum, and then reconstructs a uniformly resampled sequence.
If a signal has N input samples and is converted to M output samples, the sample spacing changes in proportion to:
\Delta t_{\text{new}} = \Delta t_{\text{old}} \cdot \frac{N}{M}
The Fourier approach is especially useful for band-limited periodic signals. When sample positions t are supplied, SciPy also returns the resampled coordinate vector corresponding to the new spacing.
Excel Usage
=RESAMPLE(x, num, t, window, resample_domain)
x(list[list], required): The data to be resampled.num(int, required): The number of samples in the resampled signal.t(list[list], optional, default: null): Optional equally spaced sample positions (Excel range).window(str, optional, default: null): Specifies the window applied in the Fourier domain (defaults to None).resample_domain(str, optional, default: “time”): A string indicating the domain of the input x.
Returns (list[list]): A 2D array where the first row is resampled x, and the second row is resampled t (if t was provided).
Example 1: Basic Resample
Inputs:
| x | num | ||||
|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 10 |
Excel formula:
=RESAMPLE({1,2,3,4,5}, 10)
Expected output:
| Result | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| 1 | 0.763932 | 2 | 3 | 3 | 3 | 4 | 5.23607 | 5 | 3 |
Example 2: Resample with t
Inputs:
| x | num | t | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 10 | 0 | 1 | 2 | 3 | 4 |
Excel formula:
=RESAMPLE({1,2,3,4,5}, 10, {0,1,2,3,4})
Expected output:
| Result | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| 1 | 0.763932 | 2 | 3 | 3 | 3 | 4 | 5.23607 | 5 | 3 |
| 0 | 0.5 | 1 | 1.5 | 2 | 2.5 | 3 | 3.5 | 4 | 4.5 |
Example 3: Frequency-domain resample preserves sample count request
Inputs:
| x | num | resample_domain | |||||
|---|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 0 | 0 | 0 | 3 | freq |
Excel formula:
=RESAMPLE({1,0,0,0,0,0}, 3, "freq")
Expected output:
| Result | ||
|---|---|---|
| 0.166667 | 0.166667 | 0.166667 |
Example 4: Resample using a Hann Fourier window
Inputs:
| x | num | window | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 0 | -1 | 0 | 1 | 0 | -1 | 12 | hann |
Excel formula:
=RESAMPLE({0,1,0,-1,0,1,0,-1}, 12, "hann")
Expected output:
| Result | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0.433013 | 0.433013 | 0 | -0.433013 | -0.433013 | 0 | 0.433013 | 0.433013 | 0 | -0.433013 | -0.433013 |
Python Code
Show Code
import numpy as np
from scipy.signal import resample as scipy_resample
def resample(x, num, t=None, window=None, resample_domain='time'):
"""
Resample x to num samples using Fourier method.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.resample.html
This example function is provided as-is without any representation of accuracy.
Args:
x (list[list]): The data to be resampled.
num (int): The number of samples in the resampled signal.
t (list[list], optional): Optional equally spaced sample positions (Excel range). Default is None.
window (str, optional): Specifies the window applied in the Fourier domain (defaults to None). Default is None.
resample_domain (str, optional): A string indicating the domain of the input x. Valid options: Time, Frequency. Default is 'time'.
Returns:
list[list]: A 2D array where the first row is resampled x, and the second row is resampled t (if t was provided).
"""
try:
def to_1d(v):
if v is None: return None
if isinstance(v, list):
return np.array([float(x) for row in v for x in row])
return np.array([float(v)])
x_arr = to_1d(x)
t_arr = to_1d(t)
# Handle result structure
result = scipy_resample(
x_arr,
int(num),
t=t_arr,
window=window,
domain=resample_domain
)
if t_arr is not None:
res_x, res_t = result
return [res_x.tolist(), res_t.tolist()]
else:
return [result.tolist()]
except Exception as e:
return f"Error: {str(e)}"