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.

dimension issue spectral_flatness

See original GitHub issue

Hi Folks,

I’m not 100 sure I understand the logic so maybe it issues on my end. Input (80, 513), here 80 filters are used to generate mel.

Now let’s say, I pass that spectral_flatness, according to the doc it should accept that. So I can pass spectrogram input.

Now if I don’t flatten input I don’t have any issue since it already has 80 filters.

librosa.feature.spectral_flatness(y=mel_numpy,
                                                       n_fft=1024,
                                                       hop_length=self.hop_length,
                                                       win_length=self.win_length,
                                                       center=True)

Now If pass spectral_flatness input I am already in a spectral form MEL. In a shape (80, 513) dim. (80 MEL channels, win length 1024, and hop length 256)

UserWarning: n_fft=1024 is too small for input signal of length=513
  return f(*args, **kwargs)

-> so if it is already _spectrogram do you need to pass it again or is it no-op?

    S, n_fft = _spectrogram(
        y=y,
        S=S,
        n_fft=n_fft,
        hop_length=hop_length,
        power=1.0,
        win_length=win_length,
        window=window,
        center=center,
        pad_mode=pad_mode,
    )

Now, I tried to understand why you doing axis=-2 i.e n what dim you computing that ratio and what relation input signal.

    S_thresh = np.maximum(amin, S ** power)
    gmean = np.exp(np.mean(np.log(S_thresh), axis=-2, keepdims=True))
    amean = np.mean(S_thresh, axis=-2, keepdims=True)
    return gmean / amean

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
lostanlencommented, May 31, 2022

It’s unclear what you’re asking. If you want the spectral flatnesss of an audio file, the simplest approach is to compute it directly from the waveform, by passing y=audioIn. This will be STFT-based spectral flatness. And it will depend on time, the same way that STFT depends on time.

If you insist on having a mel-based spectral flatness, you can always write your own function. Spectral flatness is a very simple algorithm, with 3-4 lines of Python code. It is a ratio of geometric to arithmetic mean. See librosa code here: https://librosa.org/doc/main/_modules/librosa/feature/spectral.html#spectral_flatness

The utility function librosa.mel_frequencies can help you compute these arithmetic and geometric means.

1reaction
bmcfeecommented, Jun 1, 2022

@spyroot you can absolutely provide a magnitude or power spectrogram as input to spectral flatness by using the S= argument instead of y. The examples in the documentation for this function should be enough to explain how to use it.

The utility function librosa.mel_frequencies can help you compute these arithmetic and geometric means.

This is a subtle point, and perhaps worth discussing a bit. Our flatness implementation assumes linear frequency spacing, which for the purposes of flatness calculation, amounts to a uniform weighting in the arithmetic and geometric means (measured against a linear spacing). If you provide a different frequency scale (mel, cqt, whatever), it will still apply a uniform weighting, so the resulting flatness measurement may be very different. It’s a bit more subtle than this actually - different transforms will have variable filter bandwidths, which is really what will matter when it comes to weighting things properly for a flatness calculation.

Now, maybe this is all fine. But I wonder if it’s worth considering extending the API to support frequency axis weighting here if the user wants to override the default assumptions? It would take a bit of thought, and I’m not sure there’s much demand as flatness is not very often used anymore, but it’s worth considering. (FWIW, many of our other feature transforms support overriding the frequency axis for similar reasons, allowing for stft/mel/cqt/etc inputs to be supported.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spectral Flatness - an overview | ScienceDirect Topics
The spectral flatness is the ratio of the geometric and the arithmetic mean of a subband in the power spectrum [103]. The same...
Read more >
Note on measures for spectral flatness - ResearchGate
Spectral flatness [24] is computed as the ratio of the geometric to arithmetic mean of spectrum values in a given frequency band, and...
Read more >
Generalization of Spectral Flatness Measure for Non ...
This measure is formulated in terms of the rate of growth of multi- information for every new signal sample of the signal that...
Read more >
Understanding why spectral flatness cannot be computed
To do so, I start by taking its discrete Fourier transform with fft = scipy.fft.rfft(x) and then I compute the power spectrum with...
Read more >
Spectral Descriptors - MATLAB & Simulink - MathWorks
Spectral flatness is an indication of the peakiness of the spectrum. A higher spectral flatness indicates noise, while a lower spectral flatness indicates ......
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