Bug in pfft~

hardcoder's icon

The pfft~ reference states the following:

FFT-size
optional
int (samples)
Specifies the FFT size, in samples, of the overlapped windows which are transformed to and from the spectral domain by the FFT/IFFT. The window size must be a power of 2, and defaults to 512. (Note: The size of the spectral "frames" processed by the pfft~ object's subpatch will be half this size, as the 2nd half of the spectrum is a mirror of the first, and thus redundant.)

That means, if we have a 512 point fft, fft_in~ will output 256 comples fourier coefficients. This is wrong.

A 512 point fft has to output 512/2+1 = 257 fourier coefficients. An N point fft has to output N/2 + 1 fourier coefficients. The DFT is symmetric, yes, but the coefficients X[0] and X[N/2] exist only once.

Take an 8 point fft as example:

The real part is even symmetric: real(X[1]) = real(X[7]), real(X[2]) = real(X[6]), real(X[3]) = real(X[5], real(X[0]), and real(X[4]) are unique.

The imaginary part is odd symmetric: imag(X[1]) = -imag(X[7]), imag(X[2]) = -imag(X[6]), imag(X[3]) = -imag(X[5], imag(X[0]), and imag(X[4]) are unique.

The attached patcher proofs, that there is a bug, when the full-spectrum-flag mode equals zero since perfect reconstruction is not given anymore.

Cycling74, please can you fix this?

STFT_main.maxpat
Max Patch
STFT.maxpat
Max Patch
Roth's icon

I had wondered about that statement from the documentation when at some point in the past, but as I was learning how to do STFTs in C at the time, I did not worry about investigating further.

Max Patch
Copy patch and select New From Clipboard in Max.

Perhaps this example as the main patch makes things more clear?

I would fill out a bug report (https://cycling74.com/max6-bug-form/) also as it seems like not all bugs on the forums get noticed.

hardcoder's icon

Thanks a lot ROTH, I just filled out a bug report.

In my STFT_main.maxpat I want to show, that perfect reconstruction is not possible with full-spectrum-flag==0. Perfect reconstruction means, that the difference of a delay compensated input signal and the output of the STFT-ISTFT must be zero.

Your modification of the patch emphasizes, that the audio signals processed by pfft~ with full-spectrum-flag==0 and full-spectrum-flag==1 differ. For the case that both output signals deliver the same but an incorrect result, you couldn't measure any difference.