Shortcuts

SpectralCentroid

class torchaudio.transforms.SpectralCentroid(sample_rate: int, n_fft: int = 400, win_length: ~typing.Optional[int] = None, hop_length: ~typing.Optional[int] = None, pad: int = 0, window_fn: ~typing.Callable[[...], ~torch.Tensor] = <built-in method hann_window of type object>, wkwargs: ~typing.Optional[dict] = None)[source]

Compute the spectral centroid for each channel along the time axis.

This feature supports the following devices: CPU, CUDA This API supports the following properties: Autograd, TorchScript

The spectral centroid is defined as the weighted average of the frequency values, weighted by their magnitude.

Parameters:
  • sample_rate (int) – Sample rate of audio signal.

  • n_fft (int, optional) – Size of FFT, creates n_fft // 2 + 1 bins. (Default: 400)

  • win_length (int or None, optional) – Window size. (Default: n_fft)

  • hop_length (int or None, optional) – Length of hop between STFT windows. (Default: win_length // 2)

  • pad (int, optional) – Two sided padding of signal. (Default: 0)

  • window_fn (Callable[..., Tensor], optional) – A function to create a window tensor that is applied/multiplied to each frame/window. (Default: torch.hann_window)

  • wkwargs (dict or None, optional) – Arguments for window function. (Default: None)

Example
>>> waveform, sample_rate = torchaudio.load("test.wav", normalize=True)
>>> transform = transforms.SpectralCentroid(sample_rate)
>>> spectral_centroid = transform(waveform)  # (channel, time)
forward(waveform: Tensor) Tensor[source]
Parameters:

waveform (Tensor) – Tensor of audio of dimension (…, time).

Returns:

Spectral Centroid of size (…, time).

Return type:

Tensor

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources