;  $Id: //depot/idl/IDL_71/idldir/examples/doc/signal/sigprc07#1 $

;  Copyright (c) 2005-2009, ITT Visual Information Solutions. All
;       rights reserved.
; 
; This batch file creates a plot of the power spectrum of
; the simulated signal after a Hamming window
; has been applied.

; compute time data sequence u
N = 1024

delt = 0.02d
u = -0.3 $
   + 1.0 * SIN(2 * !DPI * 2.8d * delt * DINDGEN(N)) $
   + 1.0 * SIN(2 * !DPI * 6.25d * delt * DINDGEN(N)) $
   + 1.0 * SIN(2 * !DPI * 11.0d * delt * DINDGEN(N))
time = DINDGEN(N)*delt

hanWindow = HANNING(n, ALPHA=0.54, /DOUBLE)
; freq = [0.0, 1.0/(N*delt), ... , 1.0/(2.0*delt)]:
freq = FINDGEN(N/2+1) / (N*delt)

; Fourier power spectrum with Hamming.
v_n = FFT(hanWindow*U)
powerV = ABS(v_n[0:N/2])^2

; Fourier power spectrum without Hamming.
fftU = FFT(U)
powerU = ABS(fftU[0:N/2])^2

PRINT, 'Variance ratio = ', TOTAL(ABS(v_n)^2)/TOTAL(ABS(fftU)^2)

IPLOT, time, hanWindow*u, VIEW_GRID=[1,2], $
   TITLE='Power Spectrum of u(k) with Hamming Window (solid) ' $
   +' and Without (dashed)', $
   XTITLE='Time (seconds)', YTITLE='u(k)', $
   YRANGE=[-4,4]
IPLOT, time, hanWindow, /OVERPLOT
IPLOT, time, -hanWindow, /OVERPLOT

; Create log-log plot of power spectrum:
IPLOT, freq, powerV, /VIEW_NEXT, YTITLE='Power Spectrum', $
   /Y_LOG, XTITLE='Frequency in cycles / second', $
   /X_LOG

; Overplot without window:
iPLOT, freq, powerU, LINESTYLE=2, /OVERPLOT
