Routines (alphabetical) > Routines: F > FFT_POWERSPECTRUM

# FFT_POWERSPECTRUM

The FFT_POWERSPECTRUM function computes the Fourier power spectrum of an array.

For a given input signal array, the power spectrum computes the portion of a signal's power (energy per unit time) falling within given frequency bins. Power is calculated as the average of the squared signal. In the frequency domain, this is the square of the FFT's magnitude.

This routine is written in the IDL language. Its source code can be found in the file `fft_powerspectrum.pro` in the `lib` subdirectory of the IDL distribution.

## Example

This example plots the power spectrum of a noisy sinusoidal signal with two specified frequency components.

; Specify the time interval

dt = 1/16000.

; Create an array of time indices

t = [0:3:dt]

; Create a signal with two frequencies

freq1 = 2100

freq2 = 6400

x = cos(2*!pi*t * freq1)+ sin(2*!pi*t * freq2)

; Add some random noise

x += randomn(seed, t.length)

; Compute the power spectrum of the signal

f = FFT_PowerSpectrum(x, dt, FREQ=freq)

; Plot the results

p = plot(freq/1000, f, /YLOG, XTITLE='Frequency (kHz)')

IDL produces the following plot:

## Syntax

Result = FFT_POWERSPECTRUM(Input [, dt])

## Return Value

If Input is an N-element array, Result is a vector of length N/2 + 1.

If Input is a double, Result is a double vector containing the computed values. Otherwise Result is a float vector.

## Arguments

### Input

A vector of time series values, of length N.

### dt

The time interval between measurements. If not supplied a default value of 1.0 is used.

## Keywords

### AMP

If set to a named variable, this keyword will contain a vector of length N/2 + 1 containing the Fourier amplitude.

### BANDWIDTH

If set to a named variable, this keyword will contain the width of the Tukey smoothing filter, in units of frequency. If TUKEY is not set, this keyword has no effect.

### DOF

If set to a named variable, this keyword will contain the degrees of freedom for the chi-square distribution for the significances. If TUKEY smoothing is not used, this value is 2.0. If TUKEY smoothing is used, the value is 2*N*BANDWIDTH.

### FRACTION

If this keyword is set, return the fractional power spectral density; calculated as Result/TOTAL(Result).

### FREQ

If set to a named variable, this keyword will contain a vector of the frequencies at which the power spectral density is evaluated.

### LAG

The autocorrelation lag value used when calculating significance values. If not supplied a default value of 0.0 is used. If SIGNIFICANCE is not used, this keyword has no effect.

### PERIOD

If set to a named variable, this keyword will contain a vector of the periodic components at which the power spectral density is evaluated. Period[n] = 1/Freq[n]

### PHASE

If set to a named variable, this keyword will contain a vector of length N/2 + 1 containing the Fourier phase.

### SIG_LEVEL

The significance level used when calculating significance values. If not supplied a default value of 0.05 (5%) is used. If SIGNIFICANCE is not used, this keyword has no effect.

### SIGNIFICANCE

If set to a named variable, this keyword will contain a vector of length N/2 + 1 containing the significance levels as a function of FREQ. The default 5% significance level (95% confidence level) implies that for a random time series, 5% of the points will, by chance, be above that threshold.

### TUKEY

If this keyword is set, smooth the result using a Tukey filter. The default is no smoothing.

### WIDTH

The effective width of the Tukey filter, as a fraction of the number of points N. If not supplied a default value of 0.02 is used. If TUKEY is not set, this keyword has no effect.

## Version History

8.4 | Introduced