-
Notifications
You must be signed in to change notification settings - Fork 6
/
mo_corr.f90
1364 lines (1089 loc) · 43.8 KB
/
mo_corr.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
MODULE mo_corr
! This module provides auto- and crosscorrelation function calculations
! Note routines work with optional ddof, i.e. "Delta Degrees of Freedom":
! the divisor used in the calculations is n-ddof, where n represents the number of (non-masked) elements.
! By default ddof is zero.
! Literature
! Corr, FFT
! WH Press, SA Teukolsky, WT Vetterling, BP Flannery,
! Numerical Recipes in Fortran 90 - The Art of Parallel Scientific Computing, 2nd Edition
! Volume 2 of Fortran Numerical Recipes, Cambridge University Press, UK, 1996
! Written March 2011, Matthias Cuntz
! Modified May 2016, Matthias Cuntz - ddof
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2011-2016 Matthias Cuntz - mc (at) macu (dot) de
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
! Note on Numerical Recipes License
! ---------------------------------
! Be aware that some code is under the Numerical Recipes License 3rd
! edition <http://www.nr.com/aboutNR3license.html>
! The Numerical Recipes Personal Single-User License lets you personally
! use Numerical Recipes code ("the code") on any number of computers,
! but only one computer at a time. You are not permitted to allow anyone
! else to access or use the code. You may, under this license, transfer
! precompiled, executable applications incorporating the code to other,
! unlicensed, persons, providing that (i) the application is
! noncommercial (i.e., does not involve the selling or licensing of the
! application for a fee), and (ii) the application was first developed,
! compiled, and successfully run by you, and (iii) the code is bound
! into the application in such a manner that it cannot be accessed as
! individual routines and cannot practicably be unbound and used in
! other programs. That is, under this license, your application user
! must not be able to use Numerical Recipes code as part of a program
! library or "mix and match" workbench.
! Businesses and organizations that purchase the disk or code download,
! and that thus acquire one or more Numerical Recipes Personal
! Single-User Licenses, may permanently assign those licenses, in the
! number acquired, to individual employees. Such an assignment must be
! made before the code is first used and, once made, it is irrevocable
! and can not be transferred.
! If you do not hold a Numerical Recipes License, this code is only for
! informational and educational purposes but cannot be used.
USE mo_kind, ONLY: i4, sp, dp, spc, dpc
USE mo_constants, ONLY: TWOPI_sp, TWOPI_dp, PI_dp
USE mo_utils, ONLY: swap
Implicit NONE
PUBLIC :: autocoeffk ! coeff_k so that autocorr = coeff_k/coeff_0
PUBLIC :: autocorr ! Autocorrelation coefficient at lag k = autocoeffk(k)/autocoeffk(0)
PUBLIC :: corr ! Correlation (function) with optional highpass filtering: covariance=correlation(1)/n
PUBLIC :: crosscoeffk ! coeff_k so that crosscorr = coeff_k/coeff_0, crosscoeffk(0) = covariance
PUBLIC :: crosscorr ! Crosscorrelation coefficient at lag k = crosscoeffk(k)/crosscoeffk(0)
! ------------------------------------------------------------------
! NAME
! autocoeffk
! PURPOSE
! Coefficient at lag k so that autocorrelation coefficient at lag k
! is autocoeffk(x,k)/autocoeffk(x,0).
!
! If an optinal mask is given, the calculations are only over those locations that correspond
! to true values in the mask.
! x can be single or double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! ak = autocoeffk(x, k, mask=mask, ddof=ddof)
! INTENT(IN)
! real(sp/dp) :: x(:) Time series
! integer(i4) :: k[(:)] Lag for autocorrelation
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: ak[(:)] coefficient so that ak/autocoeffk(x,0) is the autocorrelation coefficient at lag k
! INTENT(IN), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(vec).
! If present, only those locations in vec corresponding to the true values in mask are used.
! integer(i4) :: ddof Delta Degrees of Freedom. The divisor used in calculations is n-ddof,
! where n represents the number of (non-masked) elements. By default ddof is zero.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! ! Variance = autocoeffk(0)
! var = autocoeffk(x,0)
! -> see also example in test directory
! LITERATURE
! WH Press, SA Teukolsky, WT Vetterling, BP Flannery,
! Numerical Recipes in Fortran 90 - The Art of Parallel Scientific Computing, 2nd Edition
! Volume 2 of Fortran Numerical Recipes, Cambridge University Press, UK, 1996
! HISTORY
! Written, Matthias Cuntz, Nov 2011
! Modified, Stephan Thober, Nov 2012 - added 1d version
INTERFACE autocoeffk
MODULE PROCEDURE autocoeffk_sp, autocoeffk_dp, &
autocoeffk_1d_dp, autocoeffk_1d_sp
END INTERFACE autocoeffk
! ------------------------------------------------------------------
! NAME
! autocorr
! PURPOSE
! Element at lag k of autocorrelation function
! autocorr(x,k) = autocoeffk(x,k)/autocoeffk(x,0).
!
! If an optinal mask is given, the calculations are only over those locations that correspond
! to true values in the mask.
! x can be single or double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! ak = autocorr(x, k, mask=mask)
! INTENT(IN)
! real(sp/dp) :: x(:) Time series
! integer(i4) :: k[(:)] Lag for autocorrelation
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: ak[(:)] Coefficient of autocorrelation function at lag k
! INTENT(IN), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(vec).
! If present, only those locations in vec corresponding to the true values in mask are used.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! ! Last autocorrelation element
! acorr = autocorr(x,size(x)/2)
! -> see also example in test directory
! LITERATURE
! WH Press, SA Teukolsky, WT Vetterling, BP Flannery,
! Numerical Recipes in Fortran 90 - The Art of Parallel Scientific Computing, 2nd Edition
! Volume 2 of Fortran Numerical Recipes, Cambridge University Press, UK, 1996
! HISTORY
! Written, Matthias Cuntz, Nov 2011
! Modified, Stephan Thober, Nov 2012 - added 1d version
INTERFACE autocorr
MODULE PROCEDURE autocorr_sp, autocorr_dp, &
autocorr_1d_sp, autocorr_1d_dp
END INTERFACE autocorr
! ------------------------------------------------------------------
! NAME
! corr
! PURPOSE
! Computes the correlation of two real data sets data1 and data2 of length N (includ-
! ing any user-supplied zero padding) with Fast Fourier Transform (FFT). N must be an integer
! power of 2 for the FFT routine. Corr takes only the elements up to the last power of 2 and
! returns the number of elements used in nadjust.
! The answer is returned as the function corr, an array of length N (nadjust).
! The answer is stored in wrap-around order, i.e., correlations at increasingly negative lags
! are in corr(N) on down to corr(N/2+1), while correlations at increasingly positive lags are
! in correl(1) (zero lag) on up to correl(N/2). Sign convention of this routine: if data1 lags
! data2, i.e., is shifted to the right of it, then correl will show a peak at positive lags.
!
! Optional high-pass filtering of the time series in Fourier space is implemented.
!
! Note covariance(x,y) = corr(x,y)/(n-ddof)
! CALLING SEQUENCE
! cfunc = corr(data1,data2,nadjust=nadjust,nhigh=nhigh,nwin=nwin)
! INTENT(IN)
! real(sp/dp) :: data1(:) 1st time series
! real(sp/dp) :: data2(:) 2nd time series
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: corr(size(data1)) Correlation function between data1 and data2 in wrap-around order
! INTENT(IN), OPTIONAL
! integer(i4) :: nhigh If >0 then nhigh upper frequencies are filtered. nwin then defines
! the used window function for filtering. (default: 0)
! integer(i4) :: nwin Window function for highpass filtering (default: 1)
! 0: no filtering
! 1: ideal highpass, i.e. cut out the nhigh upper frequencies
! 2: linear interpolation of 0 to 1 from highest to highest-nhigh
! frequency; similar Bartlett window
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! integer(i4) :: nadjust Actual used number of elements.
! RESTRICTIONS
! None
! EXAMPLE
! ! Covariance function: covariance(x,y) = corr(x,y)/n
! wT = corr(w, T, nadjust=nwT)
! wT(1:nwT) = wT(1:nwT) / real(nwT,dp)
! -> see also example in test directory
! LITERATURE
! WH Press, SA Teukolsky, WT Vetterling, BP Flannery,
! Numerical Recipes in Fortran 90 - The Art of Parallel Scientific Computing, 2nd Edition
! Volume 2 of Fortran Numerical Recipes, Cambridge University Press, UK, 1996
! HISTORY
! Written, Matthias Cuntz, Nov 2011
INTERFACE corr
MODULE PROCEDURE corr_sp, corr_dp
END INTERFACE corr
! ------------------------------------------------------------------
! NAME
! crosscoeffk
! PURPOSE
! Coefficient at lag k so that crosscorrelation coefficient at lag k
! is crosscoeffk(x,y,k)/crosscoeffk(x,y,0).
! -> crosscoeffk(x,y,0) = covariance(x,y)
!
! If an optinal mask is given, the calculations are only over those locations that correspond
! to true values in the mask.
! x can be single or double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! ck = crosscoeffk(x, y, k, mask=mask, ddof=ddof)
! INTENT(IN)
! real(sp/dp) :: x(:) 1st time series
! real(sp/dp) :: y(:) 2nd time series
! integer(i4) :: k Lag for crosscorrelation
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: ck coefficient so that ck/crosscoeffk(x,0) is the crosscorrelation coefficient at lag k
! INTENT(IN), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(vec).
! If present, only those locations in vec corresponding to the true values in mask are used.
! integer(i4) :: ddof Delta Degrees of Freedom. The divisor used in calculations is n-ddof,
! where n represents the number of (non-masked) elements. By default ddof is zero.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! ! covariance = crosscoeffk(0)
! cov = crosscoeffk(x,y,0)
! -> see also example in test directory
! LITERATURE
! WH Press, SA Teukolsky, WT Vetterling, BP Flannery,
! Numerical Recipes in Fortran 90 - The Art of Parallel Scientific Computing, 2nd Edition
! Volume 2 of Fortran Numerical Recipes, Cambridge University Press, UK, 1996
! HISTORY
! Written, Matthias Cuntz, Nov 2011
INTERFACE crosscoeffk
MODULE PROCEDURE crosscoeffk_sp, crosscoeffk_dp
END INTERFACE crosscoeffk
! ------------------------------------------------------------------
! NAME
! crosscorr
! PURPOSE
! Element at lag k of crosscorrelation function
! crosscorr(x,y,k) = crosscoeffk(x,y,k)/crosscoeffk(x,y,0).
!
! If an optinal mask is given, the calculations are only over those locations that correspond
! to true values in the mask.
! x can be single or double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! ck = crosscorr(x, y, k, mask=mask)
! INTENT(IN)
! real(sp/dp) :: x(:) 1st time series
! real(sp/dp) :: y(:) 2nd time series
! integer(i4) :: k Lag for crosscorrelation
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: ck Coefficient of crosscorrelation function at lag k
! INTENT(IN), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(vec).
! If present, only those locations in vec corresponding to the true values in mask are used.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! ! Last crosscorrelation element
! ccorr = crosscorr(x,y,size(x)/2)
! -> see also example in test directory
! LITERATURE
! WH Press, SA Teukolsky, WT Vetterling, BP Flannery,
! Numerical Recipes in Fortran 90 - The Art of Parallel Scientific Computing, 2nd Edition
! Volume 2 of Fortran Numerical Recipes, Cambridge University Press, UK, 1996
! HISTORY
! Written, Matthias Cuntz, Nov 2011
INTERFACE crosscorr
MODULE PROCEDURE crosscorr_sp, crosscorr_dp
END INTERFACE crosscorr
! ------------------------------------------------------------------
PRIVATE
! ------------------------------------------------------------------
! Private routines, mostly from numerical recipes
INTERFACE arth
MODULE PROCEDURE arth_sp, arth_dp, arth_i4
END INTERFACE arth
INTERFACE four1
MODULE PROCEDURE four1_sp, four1_dp
END INTERFACE four1
INTERFACE fourrow
MODULE PROCEDURE fourrow_sp, fourrow_dp
END INTERFACE fourrow
INTERFACE realft
MODULE PROCEDURE realft_sp, realft_dp
END INTERFACE realft
! INTERFACE zroots_unity
! MODULE PROCEDURE zroots_unity_sp, zroots_unity_dp
! END INTERFACE zroots_unity
INTEGER(i4), PARAMETER :: NPAR_ARTH=16, NPAR2_ARTH=8
! ------------------------------------------------------------------
CONTAINS
! ------------------------------------------------------------------
! From numerical recipes documentation
! Returns an array of length n containing an arithmetic progression whose
! first value is first and whose increment is increment. If first and
! increment have rank greater than zero, returns an array of one larger rank,
! with the last subscript having size n and indexing the progressions.
FUNCTION arth_sp(first,increment,n)
IMPLICIT NONE
REAL(sp), INTENT(IN) :: first, increment
INTEGER(i4), INTENT(IN) :: n
REAL(sp), DIMENSION(n) :: arth_sp
INTEGER(i4) :: k, k2
REAL(sp) :: temp
if (n > 0) arth_sp(1)=first
if (n <= NPAR_ARTH) then
do k=2, n
arth_sp(k) = arth_sp(k-1)+increment
end do
else
do k=2, NPAR2_ARTH
arth_sp(k) = arth_sp(k-1)+increment
end do
temp = increment*NPAR2_ARTH
k = NPAR2_ARTH
do
if (k >= n) exit
k2 = k+k
arth_sp(k+1:min(k2,n)) = temp+arth_sp(1:min(k,n-k))
temp = temp+temp
k = k2
end do
end if
END FUNCTION arth_sp
FUNCTION arth_dp(first,increment,n)
IMPLICIT NONE
REAL(dp), INTENT(IN) :: first,increment
INTEGER(i4), INTENT(IN) :: n
REAL(dp), DIMENSION(n) :: arth_dp
INTEGER(i4) :: k, k2
REAL(dp) :: temp
if (n > 0) arth_dp(1)=first
if (n <= NPAR_ARTH) then
do k=2, n
arth_dp(k) = arth_dp(k-1)+increment
end do
else
do k=2, NPAR2_ARTH
arth_dp(k) = arth_dp(k-1)+increment
end do
temp = increment*NPAR2_ARTH
k = NPAR2_ARTH
do
if (k >= n) exit
k2 = k+k
arth_dp(k+1:min(k2,n)) = temp+arth_dp(1:min(k,n-k))
temp = temp+temp
k = k2
end do
end if
END FUNCTION arth_dp
FUNCTION arth_i4(first,increment,n)
IMPLICIT NONE
INTEGER(i4), INTENT(IN) :: first, increment, n
INTEGER(i4), DIMENSION(n) :: arth_i4
INTEGER(i4) :: k, k2, temp
if (n > 0) arth_i4(1)=first
if (n <= NPAR_ARTH) then
do k=2, n
arth_i4(k) = arth_i4(k-1)+increment
end do
else
do k=2, NPAR2_ARTH
arth_i4(k) = arth_i4(k-1)+increment
end do
temp = increment*NPAR2_ARTH
k = NPAR2_ARTH
do
if (k >= n) exit
k2 = k+k
arth_i4(k+1:min(k2,n)) = temp+arth_i4(1:min(k,n-k))
temp = temp+temp
k = k2
end do
end if
END FUNCTION arth_i4
! ------------------------------------------------------------------
FUNCTION autocoeffk_dp(x, k, mask, ddof)
IMPLICIT NONE
REAL(dp), DIMENSION(:), INTENT(IN) :: x
INTEGER(i4), INTENT(IN) :: k
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
INTEGER(i4), OPTIONAL, INTENT(IN) :: ddof
REAL(dp) :: autocoeffk_dp
autocoeffk_dp = crosscoeffk(x, x, k, mask, ddof)
END FUNCTION autocoeffk_dp
FUNCTION autocoeffk_sp(x, k, mask, ddof)
IMPLICIT NONE
REAL(sp), DIMENSION(:), INTENT(IN) :: x
INTEGER(i4), INTENT(IN) :: k
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
INTEGER(i4), OPTIONAL, INTENT(IN) :: ddof
REAL(sp) :: autocoeffk_sp
autocoeffk_sp = crosscoeffk(x, x, k, mask, ddof)
END FUNCTION autocoeffk_sp
FUNCTION autocoeffk_1d_dp(x, k, mask, ddof) result(acf)
IMPLICIT NONE
REAL(dp), DIMENSION(:), INTENT(IN) :: x
INTEGER(i4), DIMENSION(:), INTENT(IN) :: k
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
INTEGER(i4), OPTIONAL, INTENT(IN) :: ddof
INTEGER(i4) :: i
REAL(dp), DIMENSION(size(k)) :: acf
do i = 1, size(k)
acf(i) = crosscoeffk(x, x, k(i), mask, ddof)
end do
END FUNCTION autocoeffk_1d_dp
FUNCTION autocoeffk_1d_sp(x, k, mask, ddof) result(acf)
IMPLICIT NONE
REAL(sp), DIMENSION(:), INTENT(IN) :: x
INTEGER(i4), DIMENSION(:), INTENT(IN) :: k
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
INTEGER(i4), OPTIONAL, INTENT(IN) :: ddof
INTEGER(i4) :: i
REAL(sp), DIMENSION(size(k)) :: acf
do i = 1, size(k)
acf(i) = crosscoeffk(x, x, k(i), mask, ddof)
end do
END FUNCTION autocoeffk_1d_sp
! ------------------------------------------------------------------
FUNCTION autocorr_dp(x, k, mask)
IMPLICIT NONE
REAL(dp), DIMENSION(:), INTENT(IN) :: x
INTEGER(i4), INTENT(IN) :: k
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
REAL(dp) :: autocorr_dp
autocorr_dp = crosscoeffk(x, x, k, mask) / crosscoeffk(x, x, 0, mask)
END FUNCTION autocorr_dp
FUNCTION autocorr_sp(x, k, mask)
IMPLICIT NONE
REAL(sp), DIMENSION(:), INTENT(IN) :: x
INTEGER(i4), INTENT(IN) :: k
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
REAL(sp) :: autocorr_sp
autocorr_sp = crosscoeffk(x, x, k, mask) / crosscoeffk(x, x, 0, mask)
END FUNCTION autocorr_sp
FUNCTION autocorr_1d_dp(x, k, mask) result(acf)
IMPLICIT NONE
REAL(dp), DIMENSION(:), INTENT(IN) :: x
INTEGER(i4),DIMENSION(:), INTENT(IN) :: k
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
INTEGER(i4) :: i
REAL(dp), DIMENSION(size(k)) :: acf
REAL(dp) :: c0
c0 = crosscoeffk(x, x, 0, mask)
do i = 1, size(k)
acf(i) = crosscoeffk(x, x, k(i), mask) / c0
end do
END FUNCTION autocorr_1d_dp
FUNCTION autocorr_1d_sp(x, k, mask) result(acf)
IMPLICIT NONE
REAL(sp), DIMENSION(:), INTENT(IN) :: x
INTEGER(i4),DIMENSION(:), INTENT(IN) :: k
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
INTEGER(i4) :: i
REAL(sp), DIMENSION(size(k)) :: acf
REAL(sp) :: c0
c0 = crosscoeffk(x, x, 0, mask)
do i = 1, size(k)
acf(i) = crosscoeffk(x, x, k(i), mask) / c0
end do
END FUNCTION autocorr_1d_sp
! ------------------------------------------------------------------
FUNCTION corr_dp(data1,data2,nadjust,nhigh,nwin)
IMPLICIT NONE
REAL(dp), DIMENSION(:), INTENT(IN) :: data1, data2
INTEGER(i4), OPTIONAL, INTENT(OUT) :: nadjust
INTEGER(i4), OPTIONAL, INTENT(IN) :: nhigh
INTEGER(i4), OPTIONAL, INTENT(IN) :: nwin
REAL(dp), DIMENSION(size(data1)) :: corr_dp
REAL(dp), DIMENSION(:), ALLOCATABLE :: dat1, dat2, corrout
COMPLEX(dpc), DIMENSION(:), ALLOCATABLE :: cdat1, cdat2
REAL(dp), DIMENSION(:), ALLOCATABLE :: win1
!COMPLEX(dpc), DIMENSION(:), ALLOCATABLE :: cwin1
INTEGER(i4) :: i, n, no2, iwin, ihigh
REAL(dp) :: no2_1, ihigh1
n = size(data1)
if (size(data2) /= n) stop 'Error corr_dp: size(data1) /= size(data2)'
if (present(nwin)) then
iwin = nwin
else
iwin = 1
endif
if (present(nhigh)) then
ihigh = nhigh
else
ihigh = 0
endif
if (iand(n,n-1) /= 0) then
if (present(nadjust)) then
n = 2**floor(log(real(n,dp))/log(2.0_dp))
nadjust = n
else
stop 'Error corr_dp: size(data1) must be a power of 2'
endif
else
if (present(nadjust)) then
nadjust = n
endif
endif
allocate(dat1(n))
allocate(dat2(n))
dat1 = data1(1:n)
dat2 = data2(1:n)
no2 = n/2
no2_1 = 1.0_dp / real(no2,dp)
allocate(cdat1(no2))
allocate(cdat2(no2))
allocate(corrout(n))
! FFT
call realft(dat1,1,cdat1)
call realft(dat2,1,cdat2)
! Highpass
if (ihigh > 0) then
! FxH
allocate(win1(no2))
!allocate(cwin1(no2))
select case(iwin)
case(0) ! no window
win1(1:no2) = 1.0_dp
case(1) ! ideal high pass filter
win1(1:ihigh) = 0.0_dp
win1(ihigh+1:no2) = 1.0_dp
case(2) ! similar Bartlett window
ihigh1 = 1.0_dp / real(ihigh,dp)
forall(i=1:ihigh) win1(i) = real(i-1,dp) * ihigh1
win1(ihigh+1:no2) = 1.0_dp
case default
stop 'Unimplemented window option in corr_dp'
end select
!cwin1 = cmplx(win1, win1, kind=dpc)
! low pass
! cdat1(1:no2) = cdat1(1:no2)*cwin1(1:no2)
! cdat2(1:no2) = cdat2(1:no2)*cwin1(1:no2)
cdat1(1:no2) = cdat1(1:no2) * win1(1:no2)
cdat2(1:no2) = cdat2(1:no2) * win1(1:no2)
endif
! FxF*
cdat1(1) = cmplx(real(cdat1(1))*real(cdat2(1))*no2_1, &
aimag(cdat1(1))*aimag(cdat2(1))*no2_1, kind=dpc)
cdat1(2:) = cdat1(2:)*conjg(cdat2(2:))*no2_1
! IFFT
call realft(corrout,-1,cdat1)
corr_dp(1:n) = corrout(1:n)
if (size(corr_dp) > n) corr_dp(n+1:) = 0.0_dp
deallocate(dat1)
deallocate(dat2)
deallocate(cdat1)
deallocate(cdat2)
deallocate(corrout)
if (ihigh > 0) then
deallocate(win1)
!deallocate(cwin1)
endif
END FUNCTION corr_dp
FUNCTION corr_sp(data1,data2,nadjust,nhigh,nwin)
IMPLICIT NONE
REAL(sp), DIMENSION(:), INTENT(IN) :: data1, data2
INTEGER(i4), OPTIONAL, INTENT(OUT) :: nadjust
INTEGER(i4), OPTIONAL, INTENT(IN) :: nhigh
INTEGER(i4), OPTIONAL, INTENT(IN) :: nwin
REAL(sp), DIMENSION(size(data1)) :: corr_sp
REAL(sp), DIMENSION(:), ALLOCATABLE :: dat1, dat2, corrout
COMPLEX(spc), DIMENSION(:), ALLOCATABLE :: cdat1, cdat2
REAL(sp), DIMENSION(:), ALLOCATABLE :: win1
!COMPLEX(spc), DIMENSION(:), ALLOCATABLE :: cwin1
INTEGER(i4) :: i, n, no2, iwin, ihigh
REAL(sp) :: no2_1, ihigh1
n = size(data1)
if (size(data2) /= n) stop 'Error corr_sp: size(data1) /= size(data2)'
if (present(nwin)) then
iwin = nwin
else
iwin = 1
endif
if (present(nhigh)) then
ihigh = nhigh
else
ihigh = 0
endif
if (iand(n,n-1) /= 0) then
if (present(nadjust)) then
n = 2**floor(log(real(n,sp))/log(2.0_sp))
nadjust = n
else
stop 'Error corr_sp: size(data1) must be a power of 2'
endif
else
if (present(nadjust)) then
nadjust = n
endif
endif
allocate(dat1(n))
allocate(dat2(n))
dat1 = data1(1:n)
dat2 = data2(1:n)
no2 = n/2
no2_1 = 1.0_sp / real(no2,sp)
allocate(cdat1(no2))
allocate(cdat2(no2))
allocate(corrout(n))
! FFT
call realft(dat1,1,cdat1)
call realft(dat2,1,cdat2)
! Highpass
if (ihigh > 0) then
! FxH
allocate(win1(no2))
!allocate(cwin1(no2))
select case(iwin)
case(0) ! no window
win1(1:no2) = 1.0_sp
case(1) ! ideal high pass filter
win1(1:ihigh) = 0.0_sp
win1(ihigh+1:no2) = 1.0_sp
case(2) ! similar Bartlett window
ihigh1 = 1.0_sp / real(ihigh,sp)
forall(i=1:ihigh) win1(i) = real(i-1,sp) * ihigh1
win1(ihigh+1:no2) = 1.0_sp
case default
stop 'Unimplemented window option in corr_sp'
end select
!cwin1 = cmplx(win1, win1, kind=spc)
! low pass
! cdat1(1:no2) = cdat1(1:no2)*cwin1(1:no2)
! cdat2(1:no2) = cdat2(1:no2)*cwin1(1:no2)
cdat1(1:no2) = cdat1(1:no2) * win1(1:no2)
cdat2(1:no2) = cdat2(1:no2) * win1(1:no2)
endif
! FxF*
cdat1(1) = cmplx(real(cdat1(1))*real(cdat2(1))*no2_1, &
aimag(cdat1(1))*aimag(cdat2(1))*no2_1, kind=spc)
cdat1(2:) = cdat1(2:)*conjg(cdat2(2:))*no2_1
! IFFT
call realft(corrout,-1,cdat1)
corr_sp(1:n) = corrout(1:n)
if (size(corr_sp) > n) corr_sp(n+1:) = 0.0_sp
deallocate(dat1)
deallocate(dat2)
deallocate(cdat1)
deallocate(cdat2)
deallocate(corrout)
if (ihigh > 0) then
deallocate(win1)
!deallocate(cwin1)
endif
END FUNCTION corr_sp
! ------------------------------------------------------------------
FUNCTION crosscoeffk_dp(x, y, k, mask, ddof)
IMPLICIT NONE
REAL(dp), DIMENSION(:), INTENT(IN) :: x
REAL(dp), DIMENSION(:), INTENT(IN) :: y
INTEGER(i4), INTENT(IN) :: k
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
INTEGER(i4), OPTIONAL, INTENT(IN) :: ddof
REAL(dp) :: crosscoeffk_dp
INTEGER(i4) :: nn ! number of true values in mask
INTEGER(i4) :: nnn ! number of true values in mask .and. shifted mask by lag k
REAL(dp) :: n ! real of nn or nnn
INTEGER(i4) :: kk ! absolute value of lag k
REAL(dp) :: ave
REAL(dp), DIMENSION(size(x)) :: xdash
REAL(dp), DIMENSION(size(x)) :: ydash
LOGICAL, DIMENSION(size(x)) :: maske
maske(:) = .true.
if (present(mask)) then
if (size(x) /= size(y)) stop 'Error crosscoeffk_dp: size(x) /= size(y)'
if (size(mask) /= size(x)) stop 'Error crosscoeffk_dp: size(mask) /= size(x)'
maske = mask
endif
nn = count(maske)
n = real(nn,dp)
! crosscoeffk(x, y, k) = crosscoeffk(y, x, -k)
if (k >= 0) then
ave = sum(x(:), mask=maske)/n
xdash = x - ave
ave = sum(y(:), mask=maske)/n
ydash = y - ave
else
ave = sum(y(:), mask=maske)/n
xdash = y - ave
ave = sum(x(:), mask=maske)/n
ydash = x - ave
endif
kk = abs(k)
nnn = size(x,1)
n = real(count(maske(1:nnn-kk).and.maske(1+kk:nnn)),dp)
if (present(ddof)) n = n-real(ddof,dp)
crosscoeffk_dp = sum(xdash(1:nnn-kk)*ydash(1+kk:nnn), mask=(maske(1:nnn-kk).and.maske(1+kk:nnn))) / n
END FUNCTION crosscoeffk_dp
FUNCTION crosscoeffk_sp(x, y, k, mask, ddof)
IMPLICIT NONE
REAL(sp), DIMENSION(:), INTENT(IN) :: x
REAL(sp), DIMENSION(:), INTENT(IN) :: y
INTEGER(i4), INTENT(IN) :: k
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
INTEGER(i4), OPTIONAL, INTENT(IN) :: ddof
REAL(sp) :: crosscoeffk_sp
INTEGER(i4) :: nn ! number of true values in mask
INTEGER(i4) :: nnn ! number of true values in mask .and. shifted mask by lag k
REAL(sp) :: n ! real of nn or nnn
INTEGER(i4) :: kk ! absolute value of lag k
REAL(sp) :: ave
REAL(sp), DIMENSION(size(x)) :: xdash
REAL(sp), DIMENSION(size(x)) :: ydash
LOGICAL, DIMENSION(size(x)) :: maske
maske(:) = .true.
if (present(mask)) then
if (size(x) /= size(y)) stop 'Error crosscoeffk_sp: size(x) /= size(y)'
if (size(mask) /= size(x)) stop 'Error crosscoeffk_sp: size(mask) /= size(x)'
maske = mask
endif
nn = count(maske)
n = real(nn,sp)
! crosscoeffk(x, y, k) = crosscoeffk(y, x, -k)
if (k >= 0) then
ave = sum(x(:), mask=maske)/n
xdash = x - ave
ave = sum(y(:), mask=maske)/n
ydash = y - ave
else
ave = sum(y(:), mask=maske)/n
xdash = y - ave
ave = sum(x(:), mask=maske)/n
ydash = x - ave
endif
kk = abs(k)
nnn = size(x,1)
n = real(count(maske(1:nnn-kk).and.maske(1+kk:nnn)),sp)
if (present(ddof)) n = n-real(ddof,sp)
crosscoeffk_sp = sum(xdash(1:nnn-kk)*ydash(1+kk:nnn), mask=(maske(1:nnn-kk).and.maske(1+kk:nnn))) / n
END FUNCTION crosscoeffk_sp
! ------------------------------------------------------------------
FUNCTION crosscorr_dp(x, y, k, mask)
IMPLICIT NONE
REAL(dp), DIMENSION(:), INTENT(IN) :: x
REAL(dp), DIMENSION(:), INTENT(IN) :: y
INTEGER(i4), INTENT(IN) :: k
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
REAL(dp) :: crosscorr_dp
crosscorr_dp = crosscoeffk(x, y, k, mask) / crosscoeffk(x, y, 0, mask)
END FUNCTION crosscorr_dp
FUNCTION crosscorr_sp(x, y, k, mask)
IMPLICIT NONE
REAL(sp), DIMENSION(:), INTENT(IN) :: x
REAL(sp), DIMENSION(:), INTENT(IN) :: y
INTEGER(i4), INTENT(IN) :: k
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
REAL(sp) :: crosscorr_sp
crosscorr_sp = crosscoeffk(x, y, k, mask) / crosscoeffk(x, y, 0, mask)
END FUNCTION crosscorr_sp
! ------------------------------------------------------------------