librosa.interp_time#
- librosa.interp_time(x, *, target_times, times=None, sr=22050, hop_length=512, kind='linear', fill_value=0.0, axis=-1, assume_sorted=False)[source]#
Interpolate an array along a time axis.
Given an input array representing time-series features (such as an STFT, spectrogram, novelty curve, or chromagram), this function interpolates the values along the specified time axis onto a new grid of target times.
Times can be provided explicitly via
times, or inferred automatically fromsrandhop_length(usingtimes_like).- Parameters:
- xnp.ndarray
The input array to interpolate.
- target_timesnp.ndarray
The target time coordinates (in seconds) at which to evaluate the interpolator. If 1-dimensional, all slices along other dimensions are evaluated at these times. If multi-dimensional, the shape along
axismust match the target length, and other dimensions must be broadcast-compatible withx.- timesnp.ndarray, optional
The time coordinates corresponding to elements of
xalongaxis. If not provided, times are inferred using times_like(x, sr=sr, hop_length=hop_length, axis=axis). Can be 1-dimensional with lengthx.shape[axis], or multi-dimensional with shape matching or broadcastable tox.- srnumber > 0 [scalar]
Audio sampling rate. Only used if
times is None.- hop_lengthint > 0 [scalar]
Number of samples between successive frames. Only used if
times is None.- kindstr
Interpolation type. See
scipy.interpolate.interp1d. Common choices:'linear','nearest','cubic'.- fill_valuefloat, tuple of float, or ‘extrapolate’
Value to fill when evaluating outside the observed time bounds. Defaults to 0.0.
- axisint
The axis along which to interpolate time. Defaults to -1.
- assume_sortedbool
If True, the source time coordinates are assumed to be monotonically increasing. Defaults to False. If
times is None, this is automatically treated as True.
- Returns:
- x_interpnp.ndarray
The interpolated array. Dimension
axiswill have length matching the target time dimension.
Examples
Resample a spectrogram in time to a fixed 100 Hz frame rate:
>>> y, sr = librosa.loadx("trumpet", duration=1.0) >>> S = np.abs(librosa.stft(y)) >>> times = librosa.times_like(S, sr=sr) >>> target_times = np.arange(0, times[-1], 0.01) >>> S_resampled = librosa.interp_time(S, target_times=target_times, sr=sr) >>> S_resampled.shape (1025, 96)
Resample with reassigned times onto a regular time grid:
>>> freqs, times, mags = librosa.reassigned_spectrogram(y, sr=sr) >>> t_grid = np.linspace(0, 1.0, 100) >>> S_locked = librosa.interp_time(mags, target_times=t_grid, times=times) >>> S_locked.shape (1025, 100)