question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SpectralCentroid transform

See original GitHub issue

🚀 Feature

torchaudio.transforms.SpectralCentroid

Motivation

This is a common audio transform, included in Librosa.

Pitch

A differentiable SpectralCentroid would be useful for audio neural networks.

Alternatives

Here is the librosa implementation.

Additional context

Here is a simplified implementation I have made, based upon the librosa implementation:

def spectral_centroid(y, sr, n_fft, hop_length=512):
    S = torchaudio.transforms.Spectrogram(n_fft=n_fft, hop_length=hop_length, power=1.0)(y)

#    freq = fft_frequencies(sr=sr, n_fft=n_fft)
    freq = torch.linspace(0,
                       float(sr) / 2,
                       int(1 + n_fft//2))
                       #endpoint=True)

    if freq.ndim == 1:
        freq = freq.reshape((-1, 1))

    def tl1norm(S):
        return S / torch.sum(torch.abs(S), axis=0)
        
    # Column-normalize S
    return torch.sum(freq * tl1norm(S),
                  axis=0, keepdims=True)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jacobjwebbercommented, Jan 13, 2021

Excellent, thanks @mthrok

I will make a new merge request with optional frequency limiting, probably this weekend.

1reaction
NicolasHugcommented, Jan 7, 2021

I’ll give this a shot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spectral centroid
The spectral centroid is a measure used in digital signal processing to characterise a spectrum. It indicates where the center of mass of...
Read more >
Spectral Centroid - an overview
The discrete Fourier transform is a powerful tool that is widely used to transform a time domain signal into a frequency domain audio...
Read more >
Spectral Centroid
transform's frequency and magnitude information. The individual centroid of a spectral frame is defined as the average frequency weighted by amplitudes, ...
Read more >
SPECTRAL CENTROID
Goal: Use spectral centroid to control FM synthesis parameters. • What's a spectral centroid? • Example code. ICM Week 5 ... Discrete Fourier...
Read more >
(PDF) Fast computation of spectral centroids
A spectral centroid provides a noise-robust estimate of how the dominant frequency of a signal changes over time. As such, spectral centroids ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found