1
$\begingroup$

I am new at signal processing and was trying to understand the relation of the result of the FFT to the original signal. I generated a signal with a sampling rate of 100 Hz within 1 second ($N=100$), consisting of a constant component and two sinusoids with a frequency of 5 Hz and 10 Hz, then applied FFT to it. For FFT I used the C# alglib library. As you can see from the code below, the signal consists of:

  • Constant component equal to $1$
  • 5 Hz sinusoid with amplitude equal to $1$ and initial phase equal to $\pi/6$
  • 10 Hz sinusoid with amplitude equal to $3$ and initial phase equal to $0$

After converting this signal (FFT), I got an array of complex numbers fftRes, consisting of $N=100$ elements and consisting of complex conjugate values ​​symmetrically relative to ($\frac{N}{2}=50$). After that, I decided to extract from it the values ​​of the amplitude of the constant component, as well as the amplitude and phase of the harmonics 5 Hz and 10 Hz. To do this, I used the following formulas: $$ Ampl_i=\frac{\sqrt{Re[fftRes_i]^2+Im[fftRes_i]^2}}{N} $$ $$ Phase_i=arctg(\frac{Im[fftRes_i]}{Re[fftRes_i]}) $$

The obtained values ​​of the phase and amplitude of the harmonics I wrote in the corresponding arrays amplitudes and phases.

For the constant component (zero element of both arrays), I got the expected values ​​​​of amplitude equal to $1$ and phase equal to $0$.

For harmonics, I got unexpected values.

  • For 5 Hz harmonic amplitudes[5]==amplitudes[95]$=0.5$ instead of $1$. And phases[5]==-phases[95]$-\frac{\pi}{3}$ instead of $\frac{\pi}{6}$.
  • For 10 Hz harmonic amplitudes[10]==amplitudes[90]$=1.5$ instead of $3$. And phases[10]==-phases[90]$-\frac{\pi}{2}$ instead of $0$.

So, I know that FFT array values are interpreted as follows:

  • fftRes[0] represents constant (DC) frequency component
  • Next $N/2-1$ terms are positive frequency components with fftRes[N/2] being the Nyquist frequency
  • Next $N/2-1$ terms are negative frequency components (can be omitted).

Because of this, it only makes sense to consider the first $N/2=50$ elements of my arrays.

I have some questions:

  1. Would it be correct to multiply the amplitude values ​​I got by $2$ due to the fact that the FFT splits the magnitude values ​​between positive and negative frequencies?
  2. Where did the $-\Pi / 2$ phase shift come from in my results?
  3. Will it be correct to add the value $\Pi/2$ to the results (phases) obtained by me for constructing the phase spectrum?
int N = 100; 
var sample = new double[N];
//generates N elements for 1 second: sample rate=100Hz
for (int i = 0; i < N; i++)
{
    sample[i] = 1 + Math.Sin(2 * Math.PI * i / 20 + Math.PI / 6) + 3 * Math.Sin(2 * Math.PI * i / 10);
}
var fftRes = new alglib.complex[N];
alglib.fftr1d(sample, out fftRes);

var amplitudes = new double[N];
var phases = new double[N];
for (int i = 0; i < N; i++)
{
    amplitudes[i] = Math.Sqrt(Math.Pow(fftRes[i].x, 2) + Math.Pow(fftRes[i].y, 2)) / N;
    phases[i] = Math.Atan2(fftRes[i].y, fftRes[i].x);
}
$\endgroup$

1 Answer 1

1
$\begingroup$

The DFT represents a time domain signal or frequency domain signal as the superposition of an orthogonal set of base functions which are complex exponentials and vice versa, i.e.

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

The key here is that the basis functions are complex exponentials and NOT $\sin()$ or $\cos()$ functions. They are closely related through Euler's formula

$$e^{jx} = \cos(x) + j\cdot \sin(x)\\ \cos(x) = \frac{1}{2}\left(e^{jx}+e^{-jx}\right) \\ \sin(x) = \frac{1}{2j}\left(e^{jx}-e^{-jx}\right)$$

Would it be correct to multiply the amplitude values ​​I got by 2 due to the fact that the FFT splits the magnitude values ​​between positive and negative frequencies?

Yes. A cosine is the some of two complex exponential of half the magnitude, so if you want to reconstruct the original amplitude you need to multiply with 2

Where did the −Π/2 phase shift come from in my results?

You did use a sine instead of cosine. There is a multiplier of $ \frac{1}{2j}$ in Euler's formula and that adds a phase shift of $-$pi/2$.

Will it be correct to add the value Π/2 to the results (phases) obtained by me for constructing the phase spectrum?

No. Sine and cosines have different FFT phases. The result that you got is actually correct, you just have to interpret it as a cosine, not as a sine.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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