q-bio0309023/coh.pro
1: ;+
2: ; NAME:
3: ;	COH
4: ;
5: ; PURPOSE:
6: ;	This procedure calculates the coherence between two time series, TS1 and
7: ;	TS2 using the slepian data tapers, TAPERS. 
8: ;
9: ; CATEGORY:
10: ;	Spectral Analysis
11: ;
12: ; CALLING SEQUENCE:
13: ;	COH, ts1, ts2, tapers, coh, NF=nf, FK=fk, TRANS=trans
14: ;
15: ; INPUTS:
16: ;	TS1:	An n_element vector of type integer, float or double.
17: ;
18: ;	TS2:	An n_element vector of type integer, float or double.
19: ;
20: ;	TAPERS:	An n-by-k element vector of type float that provides
21: ;		the prolate spheroidal sequences to be used in the 
22: ;		spectral calculation.
23: ;
24: ; OUTPUTS:
25: ;	COH:	An FK-element vector of type complex containing the coherence
26: ;		estimate as a function of frequency.
27: ;
28: ; KEYWORDS:
29: ;	NF:	Integer containing the number of frequency bins
30: ;		to calculate the spectral quantities at.  Default is 4*N
31: ;
32: ;	FK:	An integer giving the number of frequency bins to keep.
33: ;		Default is NF/2
34: ;
35: ;	TRANS:	An FK-element complexarr containing the Transfer function as a 
36: ;		function of frequency.
37: ;
38: ; EXAMPLE:  	[To be inserted here]
39: ;
40: ; MODIFICATION HISTORY:
41: ;	Written by:	Bijan Pesaran and Partha Mitra, Bell Labs, 1996
42: ;
43: ;-
44: 
45: 
46: 
47: 
48: PRO coh, ts1, ts2, tapers, coh, NF=nf, FK=fk, TRANS=trans
49: 
50: vdp=tapers
51: nt=n_elements(ts1)
52: nd=n_elements(tapers(*,0))
53: k=n_elements(tapers(0,*))
54: if nt ne nd then goto, error
55: n=nd
56: trans_chk=0
57: if not keyword_set(nf) then nf = 4.*N
58: if not keyword_set(fk) then fk = nf/2.
59: if keyword_set(trans) then trans_chk=1
60: 
61: tsf1=fltarr(nf)
62: tsf2=tsf1
63: tsvar1=fltarr(nf)
64: tsvar2=fltarr(nf)
65: 
66: ts1=ts1-total(ts1)/float(nt)
67: ts2=ts2-total(ts2)/float(nt)
68: cspec=complexarr(nf)
69: for j=0,k-1 do begin 
70: tsf1(0:nt-1)=ts1*vdp(*,j)
71: tsf2(0:nt-1)=ts2*vdp(*,j)
72: tmpf1=fft(tsf1,-1)
73: tmpf2=fft(tsf2,-1)
74: cspec=cspec+conj(tmpf1)*tmpf2
75: tsvar1=tsvar1+abs(tmpf1)^2
76: tsvar2=tsvar2+abs(tmpf2)^2
77: endfor
78: coh=cspec(0:fk-1)/sqrt(tsvar1(0:fk-1)*tsvar2(0:fk-1))
79: cspec=cspec(0:fk-1)
80: if trans_chk eq 1 then trans=cspec(0:fk-1)/tsvar2(0:fk-1)
81: error: if nt ne nd then print,'lengths incompatible'
82: end
83: