## Displaying FFT Results

Depending on the application, there are many ways to display spectral data, the result of the (forward) FFT function.

#### Real and Imaginary Components

The most direct way is to plot the real and imaginary parts of the spectrum as a function of frequency index or as a function of the corresponding frequencies. The following figure displays the real and imaginary parts of the spectrum v(m) of the sampled signal u(k) for frequencies from -(N/2 - 1)/(N * d) to (N/2)/(d) cycles per second.

Enter the following commands at the IDL prompt to create the plot:

```;Compute time sequence data:
@sigprc01
; Compute spectrum v:
V = FFT(U)
M = (INDGEN(N)-(N/2-1))
; Frequencies corresponding to m in cycles/second:
F = M / (N*delt)
; Set up for two plots in window:

!P.MULTI = [0, 1, 2]
PLOT, F, FLOAT(SHIFT(V,N/2-1)), \$
YTITLE='real part of spectrum', \$
XTITLE='Frequency in cycles / second', \$
XRANGE=[-1,1]/(2*delt), XSTYLE=1, \$
TITLE='Spectrum of u(k)'
PLOT, F, IMAGINARY(SHIFT(V,N/2-1)), \$
YTITLE='imaginary part of spectrum', \$
XTITLE='Frequency in cycles / second', \$
XRANGE=[-1,1]/(2*delt), XSTYLE=1

!P.MULTI = 0
```

Alternately, you can run the following batch file to create the plot:

```@sigprc03
```

See Running the Example Code if IDL does not find the batch file.

IDL's FFT function always returns a single- or double-precision complex array with the same dimensions as the input argument. In the case of a forward FFT performed on a one-dimensional vector of N real values, the result is an N-element vector of complex quantities, which takes 2N real values to represent. It would seem that there is twice as much information in the spectral data as there is in the time sequence data. This is not the case. For a real valued time sequence, half of the information in the frequency sequence is redundant. Specifically:

```; 1 redundant value:
IMAGINARY(v(0)) = 0.0
; 1 redundant value:
IMAGINARY(v(N/2)) = 0.0
```

and

```; for m=1 to N/2-1, N-2 redundant values:
v(N-m) = CONJ(v(m))
```

so that exactly N of the single- or double-precision values used to represent the frequency spectrum are redundant. This redundancy is evident in the previous figure. Notice that the real part of the spectrum is an even function (symmetric about zero), and the imaginary part of the spectrum is an odd function (anti-symmetric about zero). This is always the case for the spectra of real-valued time sequences.

Because of the redundancy in such spectra, it is common to display only half of the spectrum of a real time sequence. That is, only the spectral values with frequency indices from 0 to N/2, which correspond to frequencies from 0 to 1/(2 * d), the Nyquist frequency. This vector of positive frequencies is generated in IDL with the following command:

```; f = [0.0, 1.0/(N*delt), ... , 1.0/(2.0*delt)]
F = FINDGEN(N/2+1)/(N*delt)
```

#### Magnitude and Phase

It is also common to display the magnitude and phase of the spectrum, which have physical significance, as opposed to the real and imaginary parts of the spectrum, which do not have physical significance. Since there is a one-to-one correspondence between a complex number and its magnitude and phase, no information is lost in the transformation from a complex spectrum to its magnitude and phase. In IDL, the magnitude is easily determined with the absolute value (ABS) function, and the phase with the arc-tangent (ATAN) function. By one widely used convention, the magnitude of the spectrum is plotted in decibels (dB) and the phase is plotted in degrees, against frequency on a logarithmic scale. The magnitude and phase of our sample signal are plotted in the figure below.

Enter the following commands at the IDL prompt to create the plot:

```; Compute time sequence data:
@sigprc01
; Compute spectrum v:
V = FFT(U)
; F = [0.0, 1.0/(N*delt), ... , 1.0/(2.0*delt)]:
F = FINDGEN(N/2+1) / (N*delt)
; Magnitude of first half of v:
mag = ABS(V(0:N/2))
; Phase of first half of v:
phi = ATAN(V(0:N/2), /PHASE)
; Set up for two plots in window:

!P.MULTI = [0, 1, 2]
; Create log plots of magnitude in dB and phase in degrees:
PLOT, F, 20*ALOG10(mag), YTITLE='Magnitude in dB', \$
XTITLE='Frequency in cycles / second', /XLOG, \$
XRANGE=[1.0,1.0/(2.0*delt)], XSTYLE=1, \$
TITLE='Spectrum of u(k)'
PLOT, F, phi/!DTOR, YTITLE='Phase in degrees', \$
YRANGE=[-180,180], YSTYLE=1, YTICKS=4, YMINOR=3, \$
XTITLE='Frequency in cycles / second', /XLOG, \$
XRANGE=[1.0,1.0/(2.0*delt)], XSTYLE=1

!P.MULTI = 0
```

Alternately, you can run the following batch file to create the plot:

```@sigprc04
```

See Running the Example Code if IDL does not find the batch file.

Using a logarithmic scale for the frequency axis has the advantage of spreading out the lower frequencies, while higher frequencies are crowded together. Note that the spectrum at zero frequency (DC) is lost completely on a semi-logarithmic plot.

The previous figure shows the strong frequency components at 2.8, 6.25, and 11.0 cycles/second as peaks in the magnitude plot, and as discontinuities in the phase plot. The magnitude peak at 6.25 cycles/second is a narrow spike, as would be expected from the pure sine wave component at that frequency in the time data sequence. The peaks at 2.8 and 11.0 cycles/second are more spread out, due to an effect known as smearing or leakage. This effect is a direct result of the definition of the DFT and is not due to any inaccuracy in the FFT. Smearing is reduced by increasing the length of the time sequence, or by choosing a sample size which includes an integral number of cycles of the frequency component of interest. There are an integral number of cycles of the 6.25 cycles/second component in the time sequence used for this example, which is why the peak at that frequency is sharper.

The apparent discontinuity in the phase plot at around 7.45 cycles/second is an anomaly known as phase wrapping. It is a result of resolving the phase from the real and imaginary parts of the spectrum with the arctangent function (ATAN), which returns principal values between -180 and +180 degrees.

#### Power Spectrum

Finally, for many applications, the phase information is not useful. For these, it is often customary to plot the power spectrum, which is the square of the magnitude of the complex spectrum. The resulting plot is shown in the figure below.

Enter the following commands at the IDL prompt to create the plot:

```; Compute time sequence data.
@sigprc01
; Compute spectrum v:
V = FFT(U)
; F = [0.0, 1.0/(N*delt), ... , 1.0/(2.0*delt)]:
F = FINDGEN(N/2+1) / (N*delt)
; Create log-log plot of power spectrum:
PLOT, F, ABS(V(0:N/2))^2, YTITLE='Power Spectrum of u(k)', \$
/YLOG, XTITLE='Frequency in cycles / second', /XLOG, \$
XRANGE=[1.0,1.0/(2.0*delt)], XSTYLE=1
```

Alternately, you can run the following batch file to create the plot:

```@sigprc05
```

See Running the Example Code if IDL does not find the batch file.