5
$\begingroup$

I feel like I am having a brainfart over here and can't seem to remember what's going on with STFT outputs.

Consider these two lines of code from the Python library Librosa:

# Window the time series.
y_frames = util.frame(y, frame_length=n_fft, hop_length=hop_length)

# Pre-allocate the STFT matrix
stft_matrix = np.empty((int(1 + n_fft // 2), y_frames.shape[1]),
                       dtype=dtype,
                       order='F')

In the first line, we have a function that creates a matrix with a window length of n_fft (2048).

Then in the next line, we pre-allocate our STFT, but our window length is now 1025 instead of 1024 as dictated by the 1+n_fft // 2? Where does this extra frequency bin come from? Why is not just 1024?

$\endgroup$
1

1 Answer 1

4
$\begingroup$

The continuous Fourier transform possesses symmetries when computed on real signals (Hermitian symmetry). The discrete version, an FFT (of even length) possesses a slighty twisted symmetry.

The DC coefficient ($F(0)$) is real, as well as the Nyquist one ($F(N/2)$). In between, you get $\frac{2048-2}{2}=1023$ "complex" coefficients, "duplicated" in positive and negative frequencies.

So for real signal, each STFT frame can be represented by $1023+2$ frequency bins, the remaining 1023 being recovered by Hermitian symmetry.

You can get complementary information at waybackmachine version of Pure Real Sequences broken FFT of :

As a result, we can see that if N is even, both $F(0)$ and $F(N/2)$ must be real. Given these two values and the complex values $F(1)...F(N/2-1)$, (I.E. N numbers in total) the sequence is completely characterized.

$\endgroup$
3
  • $\begingroup$ Hello, the link is broken, anybody has complementary information for this? Thanks $\endgroup$
    – Tom
    Jan 7, 2023 at 18:12
  • 1
    $\begingroup$ You can get a look using Waybackmachine. web.archive.org/web/20180202152348/http://… I'll try to find another link $\endgroup$ Jan 8, 2023 at 8:55
  • 1
    $\begingroup$ Thanks, I did not know that trick. $\endgroup$
    – Tom
    Jan 12, 2023 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.