0
$\begingroup$

I'm having difficulties understanding some basics regarding FFT. I was hoping you could answer a few questions I have. These questions will probably seem silly to you, sorry for that. I'm a complete beginner in DSP.

Let's consider the following script:

#!/usr/bin/env python3

import matplotlib.pyplot as plt 
import numpy as np

x = np.linspace(0, np.pi * 2, 256)
y = np.sin(x) + np.sin(2*x + 1)
Y = np.fft.fft(y)

freq = np.fft.fftfreq(x.shape[-1])
plt.plot(freq, Y.real)
plt.savefig('Y_real.png')
plt.close()

plt.plot(freq, Y.imag)
plt.savefig('Y_imag.png')
plt.close()

y2 = np.fft.ifft(Y)
plt.plot(x, y2.real)
plt.savefig('y2_real.png')
plt.close()

The signal in time domain (y2_real.png):

enter image description here

The signal in frequency domain, real part (Y_real.png):

enter image description here

Imaginary part (Y_imag.png):

enter image description here

I was expecting that the real part will represent amplitudes of two sinusoidal signals and the imaginary part will represent a phase shift of them. However the amplitude 109 doesn't make much sense to me, nor does the phase shift of +/-128.

Where did these numbers came from? Also I'm not sure whether everything is right with OX axises in the frequency domain. How exactly can I figure out which frequency represents one peak or another?

$\endgroup$
1
  • 3
    $\begingroup$ The magnitude and phase of a signal are $|X(f)|$ and $\angle X(f)$, not the real and imaginary parts of $X(f)$. $\endgroup$
    – MBaz
    Commented Jun 19, 2018 at 15:15

1 Answer 1

1
$\begingroup$

These questions will probably seem silly to you, sorry for that.

No question is "silly". My perception is that the questions are trivial and can be answered by looking things up in a textbook but if this is to get you out of a "difficult corner", here it goes:

...I was expecting that the real part will represent amplitudes of two sinusoidal signals and the imaginary part will represent a phase shift of them.

The real and imaginary part of the complex Discrete Fourier Transform are one number which carries many pieces of information. Specifically, the magnitude $|X[k]|$ of some $k$ complex sinusoid returns information about its amplitude.

$$|X[k]| = \sqrt{\mathcal{R}(X[k])^2 + \mathcal{I}(X[k])^2}$$

Or in more practical terms Y_amp = np.abs(np.fft.fft(y)) following the signals established in your example.

The "phase shift" is the angle $\angle X[k]$ of a given $k$ complex sinusoid.

$$\angle X[k] = \arctan\left(\frac{\mathcal{I}(X[k])}{\mathcal{R}(X[k])}\right)$$

Or in more practical terms Y_phs = np.angle(np.fft.fft(y)).

Where $\mathcal{I},\mathcal{R}$ denote the imaginary and real part of a complex sinusoid at discrete frequency $k$.

However the amplitude 109 doesn't make much sense to me, nor does the phase shift of +/-128.

That's because of conventions used in the evaluation of the DFT integral. So, if you look at the documentation of the FFT, you will notice that there is a $\frac{1}{N}$ term (where $N$ is the length of the signal) that can be applied either on $x[n]$ before the transform, or on the $\sum$ at the inverse transform. In this case, the sum is evaluated without scaling in the forward transform and with scaling when performing the inverse transform. If you divide your amplitude spectrum values by 256, your strengths across the spectrum will be adjusted to the values you probably expect. Again, in practical terms that would be: Y_amp_normed = np.abs(np.fft.fft(y))/256.

And since we are on the topic of phase, sooner or later you are going to need unwrap to deal with phase wraping.

How exactly can I figure out which frequency represents one peak or another?

$k$ denotes discrete frequency. To convert between discrete frequency and natural frequency use:

$$f = \frac{k}{N_{FFT}} \cdot Fs$$

For more information about this you can have a quick look at this link

Hope this helps.

$\endgroup$
1
  • $\begingroup$ @AleksanderAlekseev Glad to hear it was helpful, good luck with your project. All the best. $\endgroup$
    – A_A
    Commented Jun 20, 2018 at 12:40

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.