2
$\begingroup$

The DFT formula is $$X(k) = \sum x(n)e^{-i2πkn/N}$$

Is it possible to obtain absolute values from the DFT without having to calculate the imaginary components? I only need the signal amplitude values, not phase. I'm trying to create a spectrogram so I'm wondering if there's a shortcut or if I have to program complex math into the programming language

$\endgroup$
  • $\begingroup$ What programming language? You should generally not implement basic numerical algorithms yourself. That's an incredibly bad idea, and without wanting to be disrespectful, especially since you don't seem to have an overly deep insight in what the FFT does. $\endgroup$ – Marcus Müller Apr 23 '17 at 17:34
  • 1
    $\begingroup$ The whole point of this project is to implement a simple DFT for a spectrogram. I did not mention anything about FFT nor am I trying to implement it at the moment. I am trying to use DFT and if that is computationally slow, I will look into implementing FFT algorithms. By using a library I am negating the whole point of this $\endgroup$ – user4757174 Apr 23 '17 at 18:00
  • 1
    $\begingroup$ You did mention the FFT! See your tags. But, you're contradicting yourself: if the point of the project is to write a simple DFT, you'll have to write a simple DFT, i.e. complex. If the point is however to write a simple spectrogram, you should simply use an existing FFT impl in your language! $\endgroup$ – Marcus Müller Apr 23 '17 at 18:03
  • 1
    $\begingroup$ @hotpaw2 indeed, as your answer explains very nicely! Point is that it's not any easier than the complex arithmetic. $\endgroup$ – Marcus Müller Apr 23 '17 at 18:23
  • 1
    $\begingroup$ I already created this program in MATLAB, I just want to recreate it in another language, this time implementing it myself instead of relying on matlab's built in functions. The program just spits out a 2d array, which is the image of the spectrogram. Speed isn't too much of a concern unless it is excessively slow $\endgroup$ – user4757174 Apr 23 '17 at 19:42
2
$\begingroup$

Being orthogonal, and although fast algorithms exist, Fourier techniques ultimately resorts to least-squares spectral analysis, i.e. finding a sum of terms in the shape of

$$ a_k \cos(\cdot) + b_k \sin(\cdot) $$

which can be solved by linear algebra, with pure real computations. Just remember that, at the time of Fourier (1768-1830), complex arithmetic was not fully developed. Carl F. Gauss (1777-1855) is sometimes credited of the first uses of fast techniques to solve it.

So yes, you can avoid complex numbers, but $ a \cos(\phi) + b \sin(\phi) $, being real with $a$, $b$, $\phi$ reals, is just completely equivalent to some complex number. And complex numbers are just fundamental to the analysis of linear time-invariant systems, because they form their invariant eigenvectors, or allow to diagonalize them.

| improve this answer | |
$\endgroup$
2
$\begingroup$

Yes. You can do the calculation. But, no, it's not a shortcut.

If you want to obtain your DFT magnitudes fast (e.g. using the O(NlogN) FFT algorithm), then you will need the complex math (or its computational equivalent). This is because the cosine and sine forms of the DFT basis vectors are orthogonal, and you need both to completely be able to represent arbitrary input waveforms. The sine portions of the basis vectors are represented by the imaginary components of an FFT's complex result. So the full complex result of an FFT needs to be computed to end up with enough information for a magnitude or amplitude result.

If a slow DFT computation is acceptable, then you could just compute the cosine components using strictly real arithmetic and then separately compute the sine components also using reals. Then compute the hypotenuse of the real triangle for the amplitude. No complex numbers needed. But this method is O(N^2), which would take a lot longer.

For just magnitude results (computed slowly), N Goertzel filters is another possibility, and may save in trig function calls.

| improve this answer | |
$\endgroup$
  • $\begingroup$ What about discrete cosine transform? Would that be a better option, in terms of computation speed, since it is only real output and I'm making a spectrogram of amplitudes? Or is it still just as slow? $\endgroup$ – user4757174 Apr 23 '17 at 17:29
  • $\begingroup$ … you could also just return a vector of zeros, that would, for a bounded input, have a bounded error, and be incredibly fast. What is appropriate depends on the data you have, and what exactly you need, and under which constraints. $\endgroup$ – Marcus Müller Apr 23 '17 at 17:35
  • $\begingroup$ There are already other answers here regarding using a DCT for spectrograms. See: dsp.stackexchange.com/questions/93/… , dsp.stackexchange.com/questions/28583/… $\endgroup$ – hotpaw2 Apr 23 '17 at 17:42
  • $\begingroup$ Hey there, I have implemented the FFT algorithm without using complex math by calculating the real and im components separately like you said, but I used cooley tukey algorithm so its NlogN not N^2. I essentially made a 2xN array where X[N][0] is the real component and X[N][1] is the imaginary component. So to add to your answer, yes, you can do the calculation without imaginary numbers even with the NlogN algorithm $\endgroup$ – user4757174 May 13 '17 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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