0
$\begingroup$

I have 64 samples of a signal, two sines, one at 0.1 max freq, another at 0.5. Wrapped in gaussian: enter image description here In my JS version of FFT I get an array of numbers, but the size is double (128 elements) and the second part is mirrored: enter image description here But in Python, numpy's FFT returns an array of 64 complex numbers, and I like it because I can calculate magnitude and frequency!

My question: how can I convert the (kind of) mirrored FFT array of 128 real numbers to 64 complex numbers?

Here you can try this code yourself: https://stackblitz.com/edit/fft-example?file=index.ts

$\endgroup$
2
  • $\begingroup$ How are you calculating the FFT in JS? Please share the code. You should have 128 complex numbers, OR 128 real AND 128 imaginary. Then just take half of this (these) array(s) and compute magnitude/phase just like you do with numpy's FFT $\endgroup$
    – Jdip
    Oct 14, 2022 at 19:08
  • $\begingroup$ Here is a snippet: stackblitz.com/edit/fft-example?file=index.ts $\endgroup$
    – shal
    Oct 15, 2022 at 3:09

1 Answer 1

2
$\begingroup$

The FFT is an implementation of the Discrete Fourier Transform (DFT) which is defined as

$$X[k] = \sum_{n=0}^{N-1}x[n] e^{-j2\pi\frac{nk}{N}}$$

Both $x[n]$ and $X[k]$ are periodic with N. The DFT produces N complex outputs for N complex inputs. For real inputs the DFT has Hermitian symmetry, i.e.

$$X[-k] = X[N-k] = X^*[k]$$

where $*$ is the conjugate complex operator. The magnitude is symmetric and the phase is antisymmetric around $k=0$. This specifically implies that the vales at DC and Nyquist are real, i.e. $X[0],X[N/2] \in \mathbb{R}$

Some FFT implementation take of advantage of this property to save memory and computation time. The information in $X[k]$ in the range $[N/2+1,N-1]$ is redundant.

The output of a real FFT of length N is then two real numbers (DC & Nyquist) and $N/2-1$ complex numbers for a total of N real numbers. This can be packaged in different ways and there is no standard. Some will return a complex array of length $N/2+1$ some return a complex array of length $N/2$ with Nyquist packed into the imaginary part of the DC bin, etc.

If you don't know: READ THE DOCUMENTATION of the function you are using.

$\endgroup$
2
  • $\begingroup$ How can my DFT input be complex? Only real, to my understanding. For example, I have audio input. Or heartbeat. How can I sample heartbeat time domain signal as complex values? $\endgroup$
    – shal
    Oct 15, 2022 at 14:46
  • 1
    $\begingroup$ @shal Audio and heartbeat are input, but their imaginary component will be zero. $\endgroup$
    – Peter K.
    Oct 15, 2022 at 21:04

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.