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.