Change sample density
Convert a periodic waveform to a different integer sample count for analysis or visualization.
SciPy · Function
Resamples an array to a requested number of samples using Fourier-domain processing. It supports arbitrary integer upsampling or downsampling, optional spectral windowing, and optional output sample positions.
The function transforms the signal with an FFT, shortens or zero-pads its spectrum to the requested length, and transforms it back. It can also return sample positions when equally spaced input times are supplied; the Fourier approach treats the signal as periodic.
scipy.signal.resample(x, num, t=None, axis=0, window=None, domain='time')| Name | Type | Description |
|---|---|---|
x | array_like | Input signal or array to resample. |
num | int | Number of samples in the output signal; it may be greater or less than the input length. |
t | array_like, optional | Optional equally spaced sample positions associated with x. |
axis | int, optional | Axis of x that is resampled; default is 0. |
window | array_like, callable, string, float, or tuple, optional | Optional window applied in the Fourier domain. |
domain | {'time', 'freq'}, optional | Input domain, either time or frequency; default is 'time'. |
resampled_x, or (resampled_x, resampled_t) when t is supplied.
import numpy as np
from scipy.signal import resample
x = np.sin(2 * np.pi * 5 * np.arange(100) / 100)
y = resample(x, 200)
print(y.shape)This doubles a periodic signal's sample count using Fourier-domain resampling.
Convert a periodic waveform to a different integer sample count for analysis or visualization.
Create a uniformly sized signal representation from periodic sequences with differing lengths.
Apply a Fourier-domain window while changing the signal's sample count.
It uses Fourier-domain resampling: the FFT is shortened for downsampling or zero-padded for upsampling, then an inverse transform produces the output. This supports arbitrary integer output lengths and an ideal antialiasing filter.
Follow-up: What assumption can invalidate the result? The method assumes periodic data, so endpoint discontinuities may introduce ringing or other artifacts.
The axis parameter selects the dimension to resample and defaults to axis 0. The other dimensions retain their corresponding sizes and values are processed independently along the selected axis.
Follow-up: How would you resample each row of a 2-D array? Pass axis=1 so the columns, rather than rows, define the resampled dimension.
With t supplied, t must represent equally spaced sample positions associated with x. The function returns both the resampled data and corresponding output positions; without t, it returns only the data array.
Follow-up: What determines the new position spacing? It is the original spacing multiplied by len(x) / num.
scipy.signal.resample_polyscipy.signal.upfirdnscipy.fft.fftscipy.fft.ifft