chatter.utils#

chatter.utils#

Utility functions for spectrogram processing and general helpers.

Functions

chunker(seq, size)

Generate successive fixed-size chunks from a sequence for batch processing.

downsample_spectrogram(spec, target_shape)

Downsample a spectrogram to a target shape using smooth anti-aliased interpolation.

normalize_spec(spec[, noise_floor])

Normalize a spectrogram to the [0, 1] range by clipping to a noise floor and scaling.

pad_center_zero(spec, target_frames)

Pad the time axis of a spectrogram to a target length, centering the content.

suppress_stdout_stderr()

Context manager to suppress stdout and stderr output, including low-level C / C++ writes that bypass Python's sys.stdout/sys.stderr.

chatter.utils.normalize_spec(spec: ndarray[tuple[Any, ...], dtype[floating]], noise_floor: float = -60.0) ndarray[tuple[Any, ...], dtype[floating]][source]#

Normalize a spectrogram to the [0, 1] range by clipping to a noise floor and scaling.

Parameters:
  • spec (np.ndarray) – Input spectrogram in decibel scale. The array may contain NaN or infinite values, which will be replaced with finite values using numpy.nan_to_num.

  • noise_floor (float, optional) – Minimum decibel value used as a noise floor. All values below this threshold are clipped upward to the noise floor before normalization. The default is -60.0.

Returns:

A spectrogram normalized to the [0, 1] range. If the input is constant after clipping, a zero array with the same shape is returned.

Return type:

np.ndarray

chatter.utils.pad_center_zero(spec: ndarray[tuple[Any, ...], dtype[floating]], target_frames: int) ndarray[tuple[Any, ...], dtype[floating]][source]#

Pad the time axis of a spectrogram to a target length, centering the content.

Parameters:
  • spec (np.ndarray) – Input spectrogram with shape (n_mels, time_frames).

  • target_frames (int) – Desired total number of time frames after padding. This value must be greater than or equal to the current number of time frames.

Returns:

Zero-padded spectrogram with shape (n_mels, target_frames), where the original content is centered along the time axis.

Return type:

np.ndarray

chatter.utils.downsample_spectrogram(spec: ndarray[tuple[Any, ...], dtype[floating]], target_shape: Tuple[int, int]) ndarray[tuple[Any, ...], dtype[floating]][source]#

Downsample a spectrogram to a target shape using smooth anti-aliased interpolation.

Parameters:
  • spec (np.ndarray) – Input spectrogram with shape (height, width).

  • target_shape (tuple of int) – Desired output shape given as (target_height, target_width).

Returns:

Downsampled spectrogram with shape equal to target_shape. The input value range is preserved.

Return type:

np.ndarray

chatter.utils.chunker(seq: Sequence[Any], size: int) Generator[Sequence[Any], None, None][source]#

Generate successive fixed-size chunks from a sequence for batch processing.

Parameters:
  • seq (Sequence) – Input sequence to be partitioned into chunks.

  • size (int) – Maximum size of each chunk. The last chunk may be smaller if the length of the sequence is not divisible by the chunk size.

Yields:

Sequence – Successive chunks of the input sequence, each with at most ‘size’ elements.

chatter.utils.suppress_stdout_stderr() Iterator[None][source]#

Context manager to suppress stdout and stderr output, including low-level C / C++ writes that bypass Python’s sys.stdout/sys.stderr.

This is useful for silencing overly verbose third-party libraries (e.g., TensorFlow Lite via birdnetlib) during localized operations.