lupy.signalutils.resample

class lupy.signalutils.resample.ResamplePolyParams(up: int, down: int, n_in: int, n_out: int, h: Float1dArray, result_slice: tuple[slice, ...])[source]

Bases: NamedTuple

Parameters for polyphase resampling

up: int

Upsampling factor

down: int

Downsampling factor

n_in: int

Number of input samples

n_out: int

Number of output samples

h: ndarray[tuple[int], dtype[float64]]

The padded FIR filter window

result_slice: tuple[slice, ...]

Slice object to extract the valid output samples from the polyphase upfirdn filtering step.

lupy.signalutils.resample.calc_tp_fir_win(upsample_factor: int) ndarray[tuple[int], dtype[float64]][source]

Calculate an appropriate low-pass FIR filter for over-sampling

The method matches what is done in scipy.signal.resample_poly(), but with the following adjustments:

  • The downsampling factor is fixed as 1 (no downsampling) since only upsampling is required for True Peak detection.

  • The FIR filter length is 8 * upsample_factor + 1 instead of 10 * upsample_factor + 1. This is a minimal length that still performs well for true peak detection, but is much more efficient than the default length used by scipy.signal.resample_poly().

lupy.signalutils.resample.calc_resample_poly_params(up: int, down: int, n_samples: int, window: ndarray[tuple[int], dtype[float64]]) ResamplePolyParams[source]

Calculate parameters for polyphase resampling

The parameters match those of scipy.signal.resample_poly() but assume that the window is already provided.

Also assumes 2D input with the filter applied along the last axis.

class lupy.signalutils.resample.ResamplePoly(up: int, down: int, num_channels: NumChannelsT, window: ndarray[tuple[int], dtype[float64]], num_input_samples: int = 1024)[source]

Bases: Generic[NumChannelsT]

Polyphase resampler using FIR window

Precomputes parameters for efficient repeated resampling.

Parameters:
  • up (int) – Upsampling factor

  • down (int) – Downsampling factor

  • num_channels (NumChannelsT) – Number of channels

  • window (Float1dArray) – FIR filter window

  • num_input_samples (int) – Number of input samples. If not specified, defaults to 1024.

property num_channels: NumChannelsT

Number of channels

property num_input_samples: int

Number of input samples per call to apply()

If this is changed, internal parameters are recalculated.

property num_output_samples: int

Number of output samples

property input_shape: tuple[NumChannelsT, int]

Expected input shape for apply()

property output_shape: tuple[NumChannelsT, int]

Output shape for apply()

apply(x: ndarray[tuple[int, int], dtype[float64]]) ndarray[tuple[int, int], dtype[float64]][source]

Resample the input array using polyphase filtering

The input array must be 2D with shape (num_channels, num_input_samples).

If the number of input samples differs from num_input_samples, internal parameters are recalculated.

class lupy.signalutils.resample._UpFIRDn(h: ndarray[tuple[int], dtype[float64]], up: int, down: int, input_shape: tuple[NumChannelsT, int])[source]

Bases: Generic[NumChannelsT]

Helper for resampling.

apply_filter(x: ndarray[tuple[int, int], dtype[float64]]) ndarray[tuple[int, int], dtype[float64]][source]

Apply the prepared filter to the specified axis of N-D signal x.