# How do you calculate spectral flatness from an FFT?

Ok, the spectral flatness (also called Wiener entropy) is defined as the ratio of the geometric mean of a spectrum to its arithmetic mean.

Wikipedia and other references say the power spectrum. Isn't that the square of the Fourier transform? The FFT produces an "amplitude spectrum" and then you square that to get a "power spectrum"?

Basically what I want to know is, if spectrum = abs(fft(signal)), which of these is correct?

• spectral_flatness = gmean(spectrum)/mean(spectrum)
• spectral_flatness = gmean(spectrum^2)/mean(spectrum^2)

Wikipedia's definition seems to use the magnitude directly:

$$\mathrm{Flatness} = \frac{\sqrt[N]{\prod_{n=0}^{N-1}x(n)}}{\frac{\sum_{n=0}^{N-1}x(n)}{N}} = \frac{\exp\left(\frac{1}{N}\sum_{n=0}^{N-1} \ln x(n)\right)}{\frac{1}{N} \sum_{n=0}^{N-1}x(n)}$$ where $x(n)$ represents the magnitude of bin number $n$.

SciPy docs define power spectrum as:

When the input a is a time-domain signal and A = fft(a), np.abs(A) is its amplitude spectrum and np.abs(A)**2 is its power spectrum.

This source agrees about the definition of "power spectrum" and calls it $S_{f}(\omega)$:

We can define $F_{T}(\omega)$ which is the fourier transform of the signal in period T, and define the power spectrum as the following: $\displaystyle S_{f}(\omega) = \lim_{T \rightarrow \infty} \frac{1}{T}{\mid F_{T}(\omega)\mid}^2.$

This source defines Wiener entropy in terms of $S(f)$.

But I don't see the squaring in equations like this, which seems to be based on the magnitude spectrum:

$$S_{flatness} = \frac{\exp\left(\frac{1}{N} \sum_k \log (a_k)\right)}{\frac{1}{N} \sum_k a_k}$$

Likewise, another source defines the spectral flatness in terms of the power spectrum, but then uses the magnitude of the FFT bins directly, which would seem to conflict with the above definition of "power spectrum".

Does "power spectrum" mean different things to different people?

-

If the definition of the flatness dictates that you use a power spectrum, then yes, you should square the magnitudes as the reference from the SciPy documentation indicates. In the equation that you referenced where you didn't see a squaring, I don't think you can read much into it; it says that

$$S_{flatness} = \frac{\exp\left(\frac{1}{N} \sum_k \log (a_k)\right)}{\frac{1}{N} \sum_k a_k}$$

but I don't see a definition for $a_k$ anywhere. If you want the spectrum to be proportional to the power in each bin, you need to square.

-
I guess this is a question about what the definition actually is, then – endolith Apr 13 '12 at 20:19

Definitions vary, don't they? The first thing that has to be settled is whether we agree that the power spectral density is equivalent to the power spectrum, or else define what we mean by both. Proakis and Salehi use them synonymously. Moving on, I think the discrepancies are due to differing definitions, for signals that have one, of the power spectrum. The usual definition of that is the magnitude squared of the Fourier transformed data. The Wiener-Khinchin theorem provides another route to the power spectrum for WSS signals through the Fourier transform of the autocorrelation. Depending on whether or not you define the power spectrum with a square, you get a square in the spectral flatness.

Others use the magnitude of the Fourier transform. Some call this the "power spectrum", and reserve the name "power spectrum density" for the derivative of the "power spectrum" while others reserve the term "power spectrum" for the integral of the Fourier transform of the autocorrelation (what others call the power spectrum). As you can see, definitions abound; feel free to invent you own :) Or stick to the Wiener-Khinchin standard.

-
That says "power spectrum", too. – endolith Apr 13 '12 at 21:24
ಠ_ಠ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ – endolith May 16 '12 at 16:17

Its a good question, one that I was wondering myself as well. The spectral flatness (also known as Weiner Entropy) is simply a measure of the 'peakiness' of a vector.

This source seems to indicate that the vector under consideration is the power spectral density, in which case you have to square. If you square the magnitude spectrum, you are accentuating peaks over the case where you don't square obviously, and I think this also makes more intuitive sense.

-