Routines (alphabetical) > Routines: H > HANNING

# HANNING

The HANNING function is used to create a “window” for Fourier Transform filtering. It can be used to create both Hanning and Hamming windows.

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

The Hanning window is defined as:

w(k) = alpha - (1-alpha)*cos(2 pi k/ N), k = 0,1,...,N-1

where alpha=0.5 for the Hanning, and alpha=0.54 for the Hamming window.

Note: Because of the factor of 1/N (rather than 1/(N-1)) in the above equation, the Hanning filter is not exactly symmetric, and does not go to zero at the last point. The factor of 1/N is chosen to give the best behavior for spectral estimation of discrete data.

## Examples

; Construct a 1024-element time series composed of three sine waves.

n = 1024

dt = 0.02

w = 2 * !CONST.pi * dt * DINDGEN(n)

x = -0.3d + SIN(2.8d*w) + SIN(6.25d*w) + SIN(11.0d*w)

; Find the power spectrum with and without the Hanning window.

hwindow = HANNING(n, /DOUBLE)

pwrspec = ABS(fft(x))^2

pwrspec_windowed = ABS(FFT(hwindow*x))^2

; Calculate frequency vector.

freq = FINDGEN(n)/(n*dt)

; Plot the results.

p = PLOT(freq, pwrspec_windowed, /XLOG, /YLOG, $

XRANGE=[1,1./(2*dt)], XSTYLE=1, $

TITLE='Power spectrum with Hanning (black) and without (red)')

q = PLOT(freq, pwrspec, COLOR='red', /OVERPLOT)

## Syntax

Result = HANNING( N1 [, N2] [, ALPHA=value{0.5 to 1.0}] [, /DOUBLE] )

## Return Value

If only N1 is specified, this function returns an array of dimensions [N1]. If both N1 and N2 are specified, this function returns an array of dimensions [N1, N2]. If any of the inputs are double-precision or if the DOUBLE keyword is set, the result is double-precision, otherwise the result is single-precision.

## Arguments

### N1

The number of columns in the resulting array.

### N2

The number of rows in the resulting array.

## Keywords

### ALPHA

Set this keyword equal to the width parameter of a generalized Hamming window. ALPHA must be in the range of 0.5 to 1.0. If ALPHA = 0.5 (the default) the function is called a “Hanning” window. If ALPHA = 0.54, the result is called a “Hamming” window.

### DOUBLE

Set this keyword to force the computations to be done in double-precision arithmetic.

## Version History

Original | Introduced

## See Also

ABS, DINDGEN, FINDGEN, FFT, PLOT, SIN