# How to interpret FFT results – obtaining magnitude and phase information

(15 votes, average: 4.67 out of 5)

In the previous post, Interpretation of frequency bins, frequency axis arrangement (fftshift/ifftshift) for complex DFT were discussed. In this post, I intend to show you how to obtain magnitude and phase information from the FFT results.

## Outline

In this discussion, I will take an arbitrary cosine function of the form $$x(t)= A cos \left(2 \pi f_c t + \phi \right)$$ and proceed step by step as follows

1. Represent the signal $$x(t)$$ in computer (discrete-time) and plot the signal (time domain)
2. Represent the signal in frequency domain using FFT ($$X[k]$$)
3. Extract amplitude and phase information from the FFT result
4. Reconstruct the time domain signal from the frequency domain samples

For more such examples check this ebook : Simulation of Digital Communications using Matlab – by Mathuranathan Viswanathan

## 1. Discrete-time domain representation

Consider a cosine signal of  amplitude $$A=0.5$$, frequency $$f_c=10 Hz$$ and phase $$\phi= \pi/6$$ radians  (or $$30^{\circ}$$ )

$$x(t) = 0.5 cos \left( 2 \pi 10 t + \pi/6 \right)$$

In order to represent the continuous time signal $$x(t)$$ in computer memory, we need to sample the signal at sufficiently high rate (according to Nyquist sampling theorem). I have chosen a oversampling factor of $$32$$ so that the sampling frequency will be $$f_s = 32 \times f_c$$, and that gives $$640$$ samples in a $$2$$ seconds duration of the waveform record.

## Represent the signal in frequency domain using FFT

Lets represent the signal in frequency domain using the FFT function. The FFT function computes $$N$$-point complex DFT. The length of the transformation $$N$$ should cover the signal of interest otherwise we will some loose valuable information in the conversion process to frequency domain. However, we can choose a reasonable length if we know about the nature of the signal.

For example, the cosine signal of our interest is periodic in nature and is of length $640$ samples (for 2 seconds duration signal). We can simply use a lower number $N=256$ for computing the FFT. In this case, only the first $256$ time domain samples will be considered for taking FFT. No need to worry about loss of information in this case, as the 256 samples will have sufficient number of cycles using which we can calculate the frequency information.

In the code above, $$fftshift$$ is used only for obtaining a nice double-sided frequency spectrum that delineates negative frequencies and positive frequencies in order. This transformation is not necessary. A scaling factor $$1/N$$ was used to account for the difference between the FFT implementation in Matlab and the text definition of complex DFT.

## 3a. Extract amplitude of frequency components (amplitude spectrum)

The FFT function computes the complex DFT and the hence the results in a sequence of complex numbers of form $$X_{re} + j X_{im}$$. The amplitude spectrum is obtained
$$|X[k]| = \sqrt{X_{re}^2 + X_{im}^2 }$$

For obtaining a double-sided plot, the ordered frequency axis (result of fftshift) is computed based on the sampling frequency and the amplitude spectrum is plotted.

## 3b. Extract phase of frequency components (phase spectrum)

Extracting the correct phase spectrum is a tricky business. I will show you why it is so. The phase of the spectral components are computed as
$$\angle X[k] = tan^{-1} \left( \frac{X_{im}}{X_{re}} \right)$$

That equation looks naive, but one should be careful when computing the inverse tangents using computers. The obvious choice for implementation seems to be the $$atan$$ function in Matlab. However, usage of $$atan$$ function will prove disastrous unless additional precautions are taken. The $$atan$$ function computes the inverse tangent over two quadrants only, i.e, it will return values only in the $$[-\pi/2 , \pi/2]$$ interval. Therefore, the phase need to be unwrapped properly. We can simply fix this issue by computing the inverse tangent over all the four quadrants using the $$atan2(X_{img},X_{re})$$ function.

Lets compute and plot the phase information using $$atan2$$ function and see how the phase spectrum looks

The phase spectrum is completely noisy. Unexpected !!!. The phase spectrum is noisy due to fact that the inverse tangents are computed from the $$ratio$$ of imaginary part to real part of the FFT result. Even a small floating rounding off error will amplify the result and manifest incorrectly as useful phase information (read how a computer program approximates very small numbers).

To understand, print the first few samples from the FFT result and observe that they are not absolute zeros (they are very small numbers in the order $$10^{-16}$$. Computing inverse tangent will result in incorrect results

The solution is to define a tolerance threshold and ignore all the computed phase values that are below the threshold.

The recomputed phase spectrum is plotted below. The phase spectrum has correctly registered the $$30^{\circ}$$ phase shift at the frequency $$f=10 Hz$$. The phase spectrum is anti-symmetric ($$\phi=-30^{\circ}$$ at $$f=-10 Hz$$ ), which is expected for real-valued signals.

## 4. Reconstruct the time domain signal from the frequency domain samples

Reconstruction of the time domain signal from the frequency domain sample is pretty straightforward

The reconstructed signal has preserved the same initial phase shift and the frequency of the original signal. Note: The length of the reconstructed signal is only 256 sample long (~ 0.8 seconds duration), this is because the size of FFT is considered as $$N=256$$. Since the signal is periodic it is not a concern. For more complicated signals, appropriate FFT length (better to use a value that is larger than the length of the signal) need to be used.