Complex arrays & the FFT
Packed complex arrays, and a Fourier transform that works at every length and gives identical output in every engine. Run a cell to see its output beside the expected one.
A complex array
czeros allocates; indexing gives a complex number, and storing a real promotes it.
A literal with a complex in it
One complex element makes the whole literal a packed complex array; the reals promote.
Real, imaginary, magnitude
re, im and abs over a complex array give a REAL array of the same shape. abs is what you plot as a spectrum.
A delta transforms to all ones
A unit impulse at index 0 transforms to all ones, which also confirms that fft does not scale its output.
A pure tone shows up in two bins
A cosine at frequency 3 on 16 points puts N/2 = 8 into bins 3 and 13. The test checks that each of the other 14 bins is below 1e-12.
ifft inverts fft
ifft carries the 1/N, so the round trip returns what you started with — the same convention as numpy and FFTW.
Any length, not just powers of two
Seven is prime. Lengths that are not a power of two go through Bluestein's algorithm, so they cost N log N too and nothing about the call changes.
A real array is not a complex one
fft refuses a real array and says what to do instead. The two are different types and never mix silently.
expected error: `fft` needs a complex array…