SpectrogramInput

class torch_ecg.components.SpectrogramInput(config: InputConfig)[source]

Bases: _SpectralInput

Inputs from the spectro-temporal domain.

One has to set the following parameters for initialization:
  • n_binsint

    The number of frequency bins.

  • fs (or sample_rate)int

    The sample rate of the waveform.

with the following optional parameters with default values:
  • window_sizefloat, default: 1 / 20

    The size of the window in seconds.

  • overlap_sizefloat, default: 1 / 40

    The overlap of the windows in seconds.

  • feature_fsNone or float,

    The sample rate of the features. If specified, the features will be resampled against fs to this sample rate.

  • to1dbool, default False

    Whether to convert the features to 1D. NOTE that if to1d is True, then if the convolutions with groups=1 applied to the input acts on all the bins, which is “global” w.r.t. the bins dimension of the corresponding 2d input.

Examples

>>> from torch_ecg.cfg import DEFAULTS
>>> BATCH_SIZE = 32
>>> N_CHANNELS = 12
>>> N_SAMPLES = 5000
>>> input_config = InputConfig(
...     name="spectrogram",
...     n_channels=N_CHANNELS,
...     n_samples=N_SAMPLES,
...     n_bins=128,
...     fs=500,
...     window_size=1 / 20,
...     overlap_size=1 / 40,
...     feature_fs=100,
...     to1d=True,
... )
>>> inputer = SpectrogramInput(input_config)
>>> waveform = torch.randn(BATCH_SIZE, N_CHANNELS, N_SAMPLES)
>>> spectrogram = inputer(waveform)
>>> spectrogram.shape == inputer.compute_input_shape(waveform.shape)
True
from_waveform(waveform: ndarray | Tensor) Tensor[source]

Converts the input ndarray or Tensor waveform to a Tensor of spectrograms.

Parameters:

waveform (numpy.ndarray or torch.Tensor) – The waveform to be transformed, of shape (batch_size, n_channels, n_samples) or (n_channels, n_samples).

Returns:

The transformed waveform, of shape (batch_size, n_channels, n_bins, n_frames), where

\[n\_frames = (n\_samples - win\_length) // hop\_length + 1\]

Return type:

torch.Tensor

Note

If the input is a 2D tensor, then the batch dimension is added (batch_size = 1).