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$. Andphases[5]==-phases[95]
$-\frac{\pi}{3}$ instead of $\frac{\pi}{6}$. - For 10 Hz harmonic
amplitudes[10]==amplitudes[90]
$=1.5$ instead of $3$. Andphases[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:
- 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?
- Where did the $-\Pi / 2$ phase shift come from in my results?
- 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);
}