0
$\begingroup$

I am trying to apply FFT on a time domain signal in C++, however I am having trouble with the imaginary part i.e square root of -1. In the FFT equation we have,

$$X =\frac{1}{2\pi}\int_a^b x(t) \ e^{-j\omega t} \ dt$$

we know that: $$ e^{-j\omega t} = \cos(\omega t) - j\sin(\omega t) \tag{2} $$

we also know that:

$$ \ j = \sqrt{-1}\ $$

Problem : I am trying to apply the fft equation on a signal and the problem of implementation arises when coding the equation(2) in C++. As, we know the value of $ \ j = \sqrt{-1}\ $ is infinite or if tried to solve using a calculator, we get a Math Error*. And so is my program giving an error and I'd like to get suggestions and advises on how to solve this issue and apply the FFT equation in C++.

$\endgroup$
  • $\begingroup$ Use a 2 element vector for complex numbers, store the imaginary component as a real number in the 2nd element (without any sqrt(-1)), and read up on complex arithmetic, as you can't use C scaler arithmetic operations on complex numbers (2 element vectors) and get the correct results. $\endgroup$ – hotpaw2 Mar 18 '15 at 18:50
3
$\begingroup$

In computer programs, complex numbers are usually represented by an array, field or vector containing two values: one value represents the real part, the other represents the imaginary part of the complex number. The imaginary unit itself is usually not represented at all, because this information would be redundant.

An alternative representation is to store the magnitude and phase angle of a complex number.

Specifically in C++, there is the class complex<T> from the STL library that takes care of storing two values of type T for every complex value. You should use that, it's part of the standard libraries.

| improve this answer | |
$\endgroup$

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.