How to interpret FFT results – obtaining magnitude and phase information

(47 votes, average: 4.21 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.

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

Recommended Signal Processing Books

• afshin aghayan

look at the following Matlab function, it can calculate phase spectrum as well as amplitude spectrum with a perfect accuracy:

https://www.mathworks.com/matlabcentral/fileexchange/63847-amplitude-and-phase-spectrum-of-a-signal–fourier-transform-

• Great work !! it will definitely help everyone.

• Benjamin Reboui

Hello Mathuranathan
Thank you very much for this article, I’ve a naive question, why when I try with a sine wave
x=A*sin(2*pi*fc*t+phi);
I get phase spectrum peak at 60 degree (pi/3) instead of 30 degree.
I’m not sure why this is happening do you have any clue?
Thank you

• Nitin

Thanks for the explanation. If I increase N to 1024 or change fs to
20*fc, the magnitude and phase plots changes. Why does this happen? https://uploads.disquscdn.com/images/3b5435a0043d44b9c7682baf64b19d0f1f048f2271b682835bec2d02daa3b3e6.jpg

• It is due to the spectral leakage. If the input signal is not periodic with respect to the length of FFT, it creates artifacts in the input signal that creates all these types of issues.

• Nitin

• orial

This tutorial was amazing for me. Thank you for typing up the extremely clear presentation. I’m trying to understand the phase I’m getting – my data is an almost pure cosine from an RK4 algorithm but the phase has a discontinuity right at the frequency of the solution! https://uploads.disquscdn.com/images/f2af1d75093aad75bb03d8708d5ff80f9210bf52c02359728f17652e3d7e9745.jpg

• is it really a pure cosine function ? is it possible to share the input data ?

• Thanks !!!. Is it really a pure cosine function ? is it possible to share the input data ?

• orial

Thank you for the response. I found the answer – my solution had constant amplitude and frequency but was varying in phase over time, so the FFT accurately described my data.

• Giovanni Piccinni

Hi Mathuranathan, thank you for this article, it’s very helpful. I’d like to extract the phase information from an OFDM signal. How can I do this? Thanks in advance

• In OFDM, the phase estimation should be done after the FFT block in the receiver. Several algorithms/technique available for phase estimation in an OFDM system.

Consider a coherent detection system.
1. For each transmitted OFDM symbol, the FFT output contains say N modulated values (say QAM modulation is used)
2. These values may contain random phase shifts and amplitude variations caused by local oscillator drift, jitter, channel response and other factors.
3. We usually use a channel estimator (can be a pilot/preamble based one) to estimate the reference phases and amplitudes.
4. Once the reference phases and amplitudes are learnt, we can apply them to correct phase/amplitude errors in the actual data transmission.

You can take a look at this patent. I hope it offers more clue on how to use it for your system.

• Giovanni Piccinni

Thank you for your answer. In my system, an OFDM signal is used for extracting the distance between a TX and RX. The signal is composed solely by zadoff-chu pilots. I have extracted a coarse distance measure from the correlation function between the received signal and a refrence signal. Now I’d like to perform a fine estimation exploiting the signal phase estimated in frequency domain but I’m not able to do this.

• ajit mishra

While reconstructing the signal, what happens to the phase information. Since that is not being passed to the ifft(), how is the phase information restored in the time domain?

• Marivn

how can I realize this in java?

• I do not have the code in Java