-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathCorr.pas
1544 lines (1280 loc) · 50.3 KB
/
Corr.pas
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
// ###################################################################
// #### This file is part of the mathematics library project, and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2017, Michael R. . All rights reserved.
// ####
// #### Unless required by applicable law or agreed to in writing, software
// #### distributed under the License is distributed on an "AS IS" BASIS,
// #### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// #### See the License for the specific language governing permissions and
// #### limitations under the License.
// ###################################################################
unit Corr;
interface
uses SysUtils, Math, MatrixConst, Matrix, BaseMathPersistence, Types;
{$I 'mrMath_CPU.inc'}
// standard simple linear correlation method:
type
ECorrelateException = class(EBaseMatrixException);
type
TCorrelation = class(TMatrixClass)
protected
function InternalCorrelateArr( w1, w2 : PDouble; len : integer) : Double;
function InternalCorrelate(w1, w2: IMatrix; const VarianceRatioThreshold: double = 0; canRaiseException : boolean = True): double;
function InternalWeightedCorrelate( w1, w2 : IMatrix; weights : IMatrix ) : double;
public
function Correlate(x, y : IMatrix; const VarianceRatioThreshold: double = 0; canRaiseException : boolean = True) : double; overload; // correlation coefficient between t, r (must have the same length)
function Correlate(x, y : IMatrix; var prob, z : double) : double; overload; // pearson correlation with Fishers's z and probobality
function Correlate(x, y : PDouble; len : integer) : double; overload; // pearson correlation but with pointers
function CorrelateWeighted(x, y, w : IMatrix) : double; overload; // weighted correlation
function CorrelateWeighted(x, y, w : IMatrix; var prob, z : double) : double; overload; // weighted correlation
// spearman correlation. X, Y need to be vectors
class function CorrelateSpearman( x, y : IMatrix; var zd, probd, rs, probrs : double ) : double;
class function Covariance(x, y : IMatrix; Unbiased : boolean = True) : IMatrix; overload; // covariance matrix
class function Covariance(A : IMatrix; Unbiased : boolean = True) : IMatrix; overload; // covariance matrix of matrix A
end;
// see: https://en.wikipedia.org/wiki/Dynamic_time_warping
// based on Timothy Felty's matlab script (google timothy felty dynamic time warping)
// -> enhanced with maximum search window
// -> enhanced with different distance methods
// Added the recursive fast dtw method based on
// FastDTW: Toward Accurate Dynamic Time Warping in Linear Time and Space
type
TDynamicTimeWarpDistMethod = (dtwSquared, dtwAbsolute, dtwSymKullbackLeibler);
TDynamicTimeWarepReduceFunc = procedure (X : PConstDoubleArr; inLen, inOffset : NativeInt; out newLen, newOffset : NativeInt) of object;
TDynamicTimeWarp = class(TCorrelation)
private
type
TCoordRec = record
i, j : integer;
end;
TDistRec = record
next : TCoordRec;
curr : TCoordRec;
dist : double;
end;
private
fd : TDoubleDynArray;
fSearchBoundaryLeft : TIntegerDynArray;
fSearchBoundaryRight : TIntegerDynArray;
fAccDist : TDoubleDynArray;
fMinLastRow : TDoubleDynArray;
fW1, fW2 : IMatrix;
fW1Arr, fW2Arr : TDoubleDynArray;
fNumW : integer;
fMaxSearchWin : integer;
fMethod : TDynamicTimeWarpDistMethod;
fMemX : IMatrix; // holds the aligned memory
fMemY : IMatrix;
fX : PConstDoubleArr; // fast access to fMemx and fMemY (sse aligned)
fY : PConstDoubleArr;
fWindow : Array of TCoordRec; // pairs of x and y indices. Used in dtw and fastdtw
fDistIdx : Array of TDistRec;
fPath : Array of TCoordRec; // i, j that build up the path
fNumPath : Integer;
fMaxPathLen : integer;
fMaxWinLen : integer;
fReduceByHalf : TDynamicTimeWarepReduceFunc;
fEvalFullMtx: boolean;
// fastdtw implementation based on the python package on: https://pypi.python.org/pypi/fastdtw
// and: Stan Salvador, and Philip Chan. “FastDTW: Toward accurate dynamic time warping in linear time and space.” Intelligent Data Analysis 11.5 (2007): 561-580.
procedure ReduceByHalfPas(X : PConstDoubleArr; inLen, inOffset : NativeInt; out newLen, newOffset : NativeInt);
{$IFNDEF MRMATH_NOASM}
procedure ReduceByHalfSSE(X : PConstDoubleArr; inLen, inOffset : NativeInt; out newLen, newOffset : NativeInt);
{$ENDIF}
function ExpandWindow(inXLen, inYLen : integer; radius : integer) : Integer;
procedure InternalFastDTW(inXOffset, inXLen, inYOffset, inYLen : integer; radius : integer; var dist : double);
function InternalDTW(inXOffset, inXLen, inYOffset, inYLen : integer; window : integer) : double;
procedure DictNewCoords(var i, j, MaxDictIdx: integer); {$IFNDEF FPC} {$IF CompilerVersion >= 17.0} inline; {$IFEND} {$ENDIF}
function DictValue(i, j, MaxDictIdx: integer): double; {$IFNDEF FPC} {$IF CompilerVersion >= 17.0} inline; {$IFEND} {$ENDIF}
function GetPathByIndex(index: integer): TCoordRec;
procedure InitXY(x, y : IMatrix); overload;
procedure InitXY(x, y : TDoubleDynArray); overload;
public
// setup
class var UseSSE : boolean;
property W1 : IMatrix read fW1; // stores the last result (warped vector)
property W2 : IMatrix read fW2;
property W1Arr : TDoubleDynArray read fW1Arr;
property W2Arr : TDoubleDynArray read fW2Arr;
// if set to false the distance calculation only calculates a strip instead of the full matrix (default is True)
property EvaluateFullDistanceMtx : boolean read fEvalFullMtx write fEvalFullMtx;
property Path[index : integer] : TCoordRec read GetPathByIndex;
property PathLen : integer read fNumPath;
property MaxPathLen : integer read fMaxPathLen;
property MaxWinLen : integer read fMaxWinLen;
function DTW(t, r : IMatrix; var dist : double; MaxSearchWin : integer = 0) : IMatrix; overload;
function DTWCorr(t, r : IMatrix; MaxSearchWin : integer = 0) : double; overload; // calculate the correlation coefficient between both warped vectors
function FastDTW(x, y : IMatrix; var dist : double; Radius : integer = 1) : IMatrix; overload; // applies fastdtw
function FastDTWCorr(t, r : IMatrix; Radius : integer = 1) : double; overload; // calculate the correlation coefficient between both warped vectors
function FastDTWCorr(t, r : IMatrix; var dist : double; Radius : integer = 1) : double; overload;
// same Methods but with dynamic arrays
function DTW(t, r : TDoubleDynArray; var dist : double; MaxSearchWin : integer = 0) : TDoubleDynArray; overload;
function DTWCorr(t, r : TDoubleDynArray; MaxSearchWin : integer = 0) : double; overload;
function FastDTW(x, y : TDoubleDynArray; var dist : double; Radius : integer = 1) : TDoubleDynArray; overload;
function FastDTWCorr(t, r : TDoubleDynArray; Radius : integer = 1) : double; overload;
function FastDTWCorr(t, r : TDoubleDynArray; var dist : double; Radius : integer = 1) : double; overload;
constructor Create(DistMethod : TDynamicTimeWarpDistMethod = dtwSquared); // -> 0 = infinity
destructor Destroy; override;
end;
// spearman rank-order correlation coefficient
// sum squared rank difference as D, number of standard deviations by which D deviates fro mits nullnypothesis as zd, two sided
// significance level of this deviation as probd, spearmans rank correlation as rs, and the two -sided significance level of its
// deviation from zero as probrs.
procedure spearmanCorr( data1, data2 : PConstDoubleArr; n : integer; var d, zd, probd, rs, probrs : double);
implementation
uses MatrixASMStubSwitch, MathUtilFunc, Statistics;
const cLocDivBy2 : Array[0..1] of double = (0.5, 0.5);
cTiny : double = 1e-20;
// disable Range check and overflow checks (speed up is necessary)
{$R-}{$Q-}
// ###########################################
// #### Correlation (base implementation)
// ###########################################
function TCorrelation.Correlate(x, y: IMatrix; var prob, z: double): double;
var df : integer;
t : double;
begin
Result := Correlate(x, y);
df := (x.Width*x.Height - 2);
t := Result * sqrt( df/( (1 - Result + cTiny)*(1 + Result + cTiny) ) );
z := 0.5*ln( (1 + (Result) + cTiny)/(1 - Result + cTiny)); // Fishers z transformation
prob := betaI( 0.5*df, 0.5, df/(df + sqr(t)) ); // probability
end;
// note: afterwards w1 and w2 are mean normalized and w2 width and height is changed!
function TCorrelation.Correlate(x, y: IMatrix; const VarianceRatioThreshold: double = 0; canRaiseException : boolean = True): double;
var w1, w2 : IMatrix;
begin
w1 := x;
if x.Height <> 1 then
w1 := x.Reshape(x.Width*x.Height, 1);
w2 := y;
if y.Height <> 1 then
w2 := y.Reshape(y.Width*y.Height, 1);
assert(w1.Width = w2.Width, 'Dimension error');
Result := InternalCorrelate(w1, w2, VarianceRatioThreshold, canRaiseException);
end;
class function TCorrelation.Covariance(x, y: IMatrix; Unbiased : boolean = True): IMatrix;
var xc, tmp : IMatrix;
begin
if x.Width*x.Height <> y.Width*y.Height then
raise Exception.Create('Error length of x and y must be the same');
xc := TDoubleMatrixClass(x.GetObjRef.ClassType).Create(2, x.Width*x.Height);
// build matrix with 2 columns
if x.Width = 1
then
tmp := x
else
tmp := x.Reshape(1, x.Width*x.Height, True);
xc.SetColumn(0, tmp);
if y.Width = 1
then
tmp := y
else
tmp := y.Reshape(1, x.Width*x.Height, True);
xc.SetColumn(1, tmp);
Result := Covariance(xc, Unbiased);
end;
// each row is an observation, each column a variable
class function TCorrelation.Covariance(A: IMatrix; Unbiased: boolean): IMatrix;
var aMean : IMatrix;
ac : IMatrix;
tmp : IMatrix;
m : Integer;
begin
aMean := A.Mean(False);
ac := A.SubVec( aMean, True );
m := ac.Height;
tmp := ac.Transpose;
ac := tmp.Mult(ac);
if Unbiased then
dec(m);
if m > 0 then
ac.ScaleInPlace(1/m);
Result := ac;
end;
function TCorrelation.InternalCorrelate(w1, w2: IMatrix; const VarianceRatioThreshold: double = 0; canRaiseException : boolean = true): double;
var meanVar1 : TMeanVarRec;
meanVar2 : TMeanVarRec;
begin
// note the routine avoids memory allocations thus it runs on the raw optimized functions:
// calc: 1/(n-1)/(var_w1*var_w2) sum_i=0_n (w1_i - mean_w1)*(w2_i - mean_w2)
MatrixMeanVar( @meanVar1, 2*sizeof(double), w1.StartElement, w1.LineWidth, w1.Width, 1, True, True);
MatrixMeanVar( @meanVar2, 2*sizeof(double), w2.StartElement, w2.LineWidth, w2.Width, 1, True, True);
// Optional: If signal has a MUCH smaller (eg: factor 3) variance
// than the reference signal -> don't scale it up
if (VarianceRatioThreshold <> 0) and (meanVar2.aVar*VarianceRatioThreshold < meanVar1.aVar) then
meanVar2.aVar := meanVar1.aVar;
// For norming the variance may not be zero
if SameValue(meanVar1.aVar, 0, cDefEpsilon) or SameValue(meanVar2.aVar, 0, cDefEpsilon) then
begin
if canRaiseException
then
raise ECorrelateException.Create('Correlation Error: variance of an input vector is zero')
else
begin
Result := 0;
exit;
end;
end;
w1.AddInPlace( -meanVar1.aMean );
w2.AddInPlace( -meanVar2.aMean );
Result := MatrixVecDotMult( w1.StartElement, sizeof(double), w2.StartElement, sizeof(double), w1.Width);
Result := Result/sqrt(meanVar1.aVar*meanVar2.aVar)/(w1.Width - 1);
end;
function TCorrelation.InternalCorrelateArr(w1, w2: PDouble;
len: integer): Double;
var meanVar1 : TMeanVarRec;
meanVar2 : TMeanVarRec;
begin
// calc: 1/(n-1)/(var_w1*var_w2) sum_i=0_n (w1_i - mean_w1)*(w2_i - mean_w2)
MatrixMeanVar( @meanVar1, 2*sizeof(double), w1, len*sizeof(double), len, 1, True, True);
MatrixMeanVar( @meanVar2, 2*sizeof(double), w2, len*sizeof(double), len, 1, True, True);
MatrixAddAndScale( w1, len*sizeof(double), len, 1, -meanVar1.aMean, 1 );
MatrixAddAndScale( w2, len*sizeof(double), len, 1, -meanVar2.aMean, 1 );
// dot product:
Result := MatrixVecDotMult( w1, sizeof(double), w2, sizeof(double), len );
Result := Result/sqrt(meanVar1.aVar*meanVar2.aVar)/(len - 1);
end;
function TCorrelation.InternalWeightedCorrelate(w1, w2,
weights: IMatrix): double;
var sumweights : double;
wMean1, wMean2 : double;
weightedW1, weightedW2 : IMatrix;
t : IMatrix;
ww : IMatrix;
sxx, syy : double;
begin
// calculated correlation according to
// r = S_xy/sqrt(S_xx*S_yy)
// where S_xy = sum_i=0_n weights[i]*( x[i] - M(x, w) )*( y[i] - M(y, w))
// S_xx = sum_i=0_n weights[i]*( x[i] - M(x, w) )^2
// S_yy = sum_i=0_n weights[i]*( y[i] - M(y, w) )^2
// M(x, w) = sum_i=0_n w[i]*x[i] / sum w
// M(y, w) = sum_i=0_n w[i]*y[i] / sum w
// normalize weighted mean
MatrixSum( @sumweights, sizeof(double), weights.StartElement, weights.LineWidth, weights.Width, 1, True );
if SameValue(sumweights, 0, eps(1)) then
raise ECorrelateException.Create('Weighting vector sums to zero');
ww := weights;
if not sameValue(sumweights, 1, eps( 1 ) ) then
ww := weights.Scale( 1/sumweights );
if sumweights > cDefEpsilon then
begin
wMean1 := 0;
wMean2 := 0;
MatrixMtxVecMult( @wMean1, sizeof(double), w1.StartElement, ww.StartElement, w1.LineWidth, sizeof(double), w1.Width, 1, 1, 0 );
MatrixMtxVecMult( @wMean2, sizeof(double), w2.StartElement, ww.StartElement, w2.LineWidth, sizeof(double), w2.Width, 1, 1, 0 );
weightedW1 := w1.ScaleAndAdd(-wMean1, 1);
weightedW2 := w2.ScaleAndAdd(-wMean2, 1);
t := weightedW1.ElementWiseMult(weightedw2);
Result := 0;
MatrixMtxVecMult( @Result, sizeof(double), t.StartElement, ww.StartElement, t.LineWidth, sizeof(double), t.Width, 1, 1, 0);
weightedw1.ElementWiseMultInPlace(weightedw1);
weightedw2.ElementWiseMultInPlace(weightedw2);
sxx := 0;
syy := 0;
MatrixMtxVecMult( @sxx, sizeof(double), weightedW1.StartElement, ww.StartElement, weightedW1.LineWidth, sizeof(double), weightedW1.Width, 1, 1, 0);
MatrixMtxVecMult( @syy, sizeof(double), weightedW2.StartElement, ww.StartElement, weightedW2.LineWidth, sizeof(double), weightedW2.Width, 1, 1, 0);
sxx := sqrt(sxx*syy);
if sxx < eps(Result) then
raise ECorrelateException.Create('Variances close too machine precission - out of bounds');
Result := Result/sxx;
end
else
raise ECorrelateException.Create('Sum of weights <= 0');
end;
// ###########################################
// #### Dynamic time warping
// ###########################################
{ TDynamicTimeWarp }
constructor TDynamicTimeWarp.Create(DistMethod : TDynamicTimeWarpDistMethod = dtwSquared);
begin
fMethod := DistMethod;
fEvalFullMtx := True;
{$IFNDEF MRMATH_NOASM}
if UseSSE
then
fReduceByHalf := {$IFDEF FPC}@{$ENDIF}ReduceByHalfSSE
else
{$ENDIF}
fReduceByHalf := {$IFDEF FPC}@{$ENDIF}ReduceByHalfPas;
inherited Create;
end;
// ###########################################
// #### Base DTW algorithm
// ###########################################
function TDynamicTimeWarp.DTW(t, r: IMatrix; var dist: double; MaxSearchWin : integer = 0): IMatrix;
begin
DTW( t.SubMatrix, r.SubMatrix, dist, MaxSearchWin);
fW1 := TDoubleMatrix.Create( Copy( fW1Arr, 0, fNumW ), fNumW, 1);
fW2 := TDoubleMatrix.Create( Copy( fW2Arr, 0, fNumW ), fNumW, 1);
Result := fw1;
end;
function TDynamicTimeWarp.DTWCorr(t, r: IMatrix; MaxSearchWin : integer = 0): double;
var dist : double;
begin
// ###########################################
// #### Create time warping vectors -> stored in fw1, fw2
DTW(t, r, dist, MaxSearchWin);
// ###########################################
// #### Calculate correlation
Result := InternalCorrelate(fw1, fw2);
end;
// ###########################################
// #### Fast DTW
// ###########################################
function TDynamicTimeWarp.FastDTW(x, y: IMatrix; var dist: double; Radius : integer = 1): IMatrix;
var counter: Integer;
begin
dist := 0;
radius := Max(1, radius);
// ###########################################
// #### Preparation
fMaxPathLen := 0;
fMaxWinLen := 0;
InitXY(x, y);
// prepare memory
if Length(fWindow) < Max(x.Width, y.Width)*2 then
begin
SetLength(fWindow, (radius*4 + 4)*Max(x.Width, y.Width));
SetLength(fPath, 3*Max(x.Width, y.Width));
SetLength(fDistIdx, Length(fWindow));
end;
// ###########################################
// #### Find optimal path
fNumPath := 0;
InternalFastDTW(0, x.Width, 0, y.Width, Max(1, Radius), dist);
// ###########################################
// #### Build result
if not Assigned(fW1) then
begin
fW1 := MatrixClass.Create(fNumPath, 1);
fW2 := MatrixClass.Create(fNumPath, 1);
end;
fW1.UseFullMatrix;
fW2.UseFullMatrix;
if fW1.Width < fNumPath then
begin
fW1.SetWidthHeight(fNumPath, 1);
fW2.SetWidthHeight(fNumPath, 1);
end;
fW1.SetSubMatrix(0, 0, fNumPath, 1);
fW2.SetSubMatrix(0, 0, fNumPath, 1);
for counter := 0 to fNumPath - 1 do
begin
fW1.Vec[counter] := fX^[fPath[counter].i];
fW2.Vec[counter] := fY^[fPath[counter].j];
end;
Result := FW1;
end;
function TDynamicTimeWarp.FastDTW(x, y: TDoubleDynArray; var dist: double;
Radius: integer): TDoubleDynArray;
var counter: Integer;
begin
dist := 0;
radius := Max(1, radius);
// ###########################################
// #### Preparation
fMaxPathLen := 0;
fMaxWinLen := 0;
InitXY(x, y);
// prepare memory
if Length(fWindow) < Max(Length(x), Length(y))*2 then
begin
SetLength(fWindow, (radius*4 + 4)*Max(Length(x), Length(y)));
SetLength(fPath, 3*Max(Length(x), Length(y)));
SetLength(fDistIdx, Length(fWindow));
end;
// ###########################################
// #### Find optimal path
fNumPath := 0;
InternalFastDTW(0, Length(x), 0, Length(y), Max(1, Radius), dist);
// ###########################################
// #### Build result
if not Assigned(fW1) then
begin
fW1 := TDoubleMatrix.Create(fNumPath, 1);
fW2 := TDoubleMatrix.Create(fNumPath, 1);
end;
fW1.UseFullMatrix;
fW2.UseFullMatrix;
if fW1.Width < fNumPath then
begin
fW1.SetWidthHeight(fNumPath, 1);
fW2.SetWidthHeight(fNumPath, 1);
end;
fW1.SetSubMatrix(0, 0, fNumPath, 1);
fW2.SetSubMatrix(0, 0, fNumPath, 1);
for counter := 0 to fNumPath - 1 do
begin
fW1.Vec[counter] := fX^[fPath[counter].i];
fW2.Vec[counter] := fY^[fPath[counter].j];
end;
Result := FW1.SubMatrix;
end;
function TDynamicTimeWarp.FastDTWCorr(t, r: IMatrix; var dist: double;
Radius: integer): double;
begin
// ###########################################
// #### Create time warping vectors -> stored in fw1, fw2
FastDTW(t, r, dist, radius);
// ###########################################
// #### Calculate correlation
Result := InternalCorrelate(fw1, fw2);
end;
function TDynamicTimeWarp.FastDTWCorr(t, r: TDoubleDynArray;
Radius: integer): double;
var dist : double;
begin
Result := FastDTWCorr(t, r, dist, radius);
end;
function TDynamicTimeWarp.FastDTWCorr(t, r: TDoubleDynArray; var dist: double;
Radius: integer): double;
begin
// ###########################################
// #### Create time warping vectors -> stored in fw1, fw2
FastDTW(t, r, dist, radius);
// ###########################################
// #### Calculate correlation
Result := InternalCorrelate(fw1, fw2);
end;
function TDynamicTimeWarp.GetPathByIndex(index: integer): TCoordRec;
begin
Result := fPath[index];
end;
function TDynamicTimeWarp.FastDTWCorr(t, r: IMatrix; Radius : integer = 1): double;
var dist : double;
begin
// ###########################################
// #### Create time warping vectors -> stored in fw1, fw2
FastDTW(t, r, dist, radius);
// ###########################################
// #### Calculate correlation
Result := InternalCorrelate(fw1, fw2);
end;
// ###########################################
// #### path functions
// ###########################################
function TDynamicTimeWarp.DictValue(i, j : integer; MaxDictIdx : integer) : double;
var cnt : integer;
begin
Result := MaxDouble;
for cnt := MaxDictIDx - 1 downto 0 do
begin
if (i = fDistIdx[cnt].curr.i) and (j = fDistIdx[cnt].curr.j) then
begin
Result := fDistIdx[cnt].dist;
break;
end;
end;
end;
destructor TDynamicTimeWarp.Destroy;
begin
fW1 := nil;
fW2 := nil;
fAccDist := nil;
fW1Arr := nil;
fW2Arr := nil;
inherited;
end;
procedure TDynamicTimeWarp.DictNewCoords(var i, j : integer; var MaxDictIdx : integer);
begin
dec(MaxDictIdx);
while (MaxDictIdx >= 0) and ((fDistIdx[MaxDictIdx].curr.i <> i) or (fDistIdx[maxDictIdx].curr.j <> j)) do
dec(MaxDictIdx);
if MaxDictIdx >= 0 then
begin
i := fDistIdx[MaxDictIdx].next.i;
j := fDistIdx[MaxDictIdx].next.j;
end;
end;
// ###########################################
// #### private fast dtw functions
// ###########################################
function TDynamicTimeWarp.InternalDTW(inXOffset, inXLen, inYOffset, inYLen : integer; window : integer) : double;
var i, j : Integer;
cnt : integer;
dt : double;
dIdx : integer;
dist0, dist1, dist2 : double;
tmp : TCoordRec;
begin
// perform a full inXLen*inYLen coordinate space
if window = 0 then
begin
if Length(fWindow) < inXLen*inYLen then
SetLength(fWindow, inXLen*inYLen);
for i := 1 to inXLen do
begin
for j := 1 to inYLen do
begin
fWindow[window].i := i;
fWindow[window].j := j;
inc(window);
end;
end;
end;
fMaxWinLen := Max(fMaxWinLen, window);
if Length( fDistIdx ) < (inXLen*inYLen) then
SetLength( fDistIdx, inXLen*inYLen );
// ###########################################
// #### Prepare a path list through the given coordinate list
// first we take a forward step through a given set of coordinates
// and then search back for the shortest path
fDistIdx[0].dist := 0;
fDistIdx[0].next.i := 0;
fDistIdx[0].next.j := 0;
fDistIdx[0].curr.i := 0;
fDistIdx[0].curr.j := 0;
dIdx := 1;
for cnt := 0 to window - 1 do
begin
i := fWindow[cnt].i;
j := fWindow[cnt].j;
case fMethod of
dtwSquared: dt := sqr( fX^[inXOffset + i - 1] - fY^[inYOffset + j - 1]);
dtwAbsolute: dt := abs( fX^[inXOffset + i - 1] - fY^[inYOffset + j - 1] );
dtwSymKullbackLeibler: dt := fX^[inXOffset + i - 1] - fY^[inYOffset + j - 1]*(ln(fX^[inXOffset + i - 1]) - ln(fY^[inYOffset + j - 1]));
else
dt := abs( fX^[inXOffset + i - 1] - fY^[inYOffset + j - 1] );
end;
dist0 := DictValue(i - 1, j, dIdx);
dist1 := DictValue(i, j - 1, dIdx);
dist2 := DictValue(i - 1, j - 1, dIdx);
if (dist0 = cMaxDouble) and (dist1 = cMaxDouble) and (dist2 = cMaxDouble) then
continue;
fDistIdx[dIdx].curr.i := i;
fDistIdx[dIdx].curr.j := j;
// according to the distance measure store the path coordinates for the next step
if dist2 <= Min(dist1, dist0) then
begin
fDistIdx[dIdx].dist := dist2 + dt;
fDistIdx[dIdx].next.i := i - 1;
fDistIdx[dIdx].next.j := j - 1;
end
else if dist0 < Min(dist1, dist2) then
begin
fDistIdx[dIdx].dist := dist0 + dt;
fDistIdx[dIdx].next.i := i - 1;
fDistIdx[dIdx].next.j := j;
end
else
begin
fDistIdx[dIdx].dist := dist1 + dt;
fDistIdx[dIdx].next.i := i;
fDistIdx[dIdx].next.j := j - 1;
end;
inc(dIdx);
end;
// ###########################################
// #### Build path (backwards)
Result := fDistIdx[dIdx - 1].dist;
i := inXLen;
j := inYLen;
fNumPath := 0;
while ( (i > 0) and (j > 0) ) and (dIdx >= 0) do
begin
fPath[fNumPath].i := i - 1;
fPath[fNumPath].j := j - 1;
inc(fNumPath);
DictNewCoords(i, j, dIdx);
end;
// reverse path
i := 0;
j := fNumPath - 1;
while i < j do
begin
tmp := fPath[i];
fPath[i] := fPath[j];
fPath[j] := tmp;
inc(i);
dec(j);
end;
fMaxPathLen := Max(fMaxPathLen, fNumPath);
end;
procedure TDynamicTimeWarp.InitXY(x, y: IMatrix);
begin
if not Assigned(fMemX) or (fMemX.Width < 2* (x.Width + x.Width and 1)) then
begin
fMemx := MatrixClass.Create( 2* (x.Width + x.Width and 1), 1);
fX := PConstDoubleArr(fMemX.StartElement);
end;
Move(x.StartElement^, fx^[0], sizeof(double)*x.Width);
if not Assigned(fMemY) or (fMemY.Width < 2* (y.Width + y.Width and 1)) then
begin
fMemY := MatrixClass.Create( 2* (y.Width + y.Width and 1), 1);
fY := PConstDoubleArr(fMemY.StartElement);
end;
Move(y.StartElement^, fy^[0], sizeof(double)*y.Width);
end;
procedure TDynamicTimeWarp.InitXY(x, y: TDoubleDynArray);
var lenX, lenY : integer;
begin
lenX := Length(x);
lenY := Length(y);
if not Assigned(fMemX) or (fMemX.Width < 2* (lenX + lenX and 1)) then
begin
fMemx := MatrixClass.Create( 2* (lenX + lenX and 1), 1);
fX := PConstDoubleArr(fMemX.StartElement);
end;
Move(x[0], fx^[0], sizeof(double)*lenX);
if not Assigned(fMemY) or (fMemY.Width < 2* (lenY + lenY and 1)) then
begin
fMemY := MatrixClass.Create( 2* (lenY + lenY and 1), 1);
fY := PConstDoubleArr(fMemY.StartElement);
end;
Move(y[0], fy^[0], sizeof(double)*lenY);
end;
procedure TDynamicTimeWarp.InternalFastDTW(inXOffset, inXLen, inYOffset, inYLen : integer; radius : integer; var dist : double);
var minTimeSize : Integer;
newXOffset, newXLen : NativeInt;
newYOffset, newYLen : NativeInt;
windowCnt : integer;
begin
minTimeSize := radius + 2;
// check for break condition
if (inXLen < minTimeSize) or (inYLen < minTimeSize) then
begin
dist := InternalDTW(inXOffset, inXLen, inYOffset, inYLen, 0);
exit;
end;
// reduce by half recursively
fReduceByHalf(fX, inXLen, inXOffset, newXLen, newXOffset);
fReduceByHalf(fY, inYLen, inYOffset, newYLen, newYOffset);
InternalFastDTW(newXOffset, newXLen, newYOffset, newYLen, radius, dist);
// rebuild
windowCnt := ExpandWindow(inXLen, inYLen, radius);
dist := InternalDTW(inXOffset, inXLen, inYOffset, inYLen, windowCnt);
end;
procedure TDynamicTimeWarp.ReduceByHalfPas(X : PConstDoubleArr; inLen, inOffset : NativeInt; out newLen, newOffset : NativeInt);
var counter: NativeInt;
idx : NativeInt;
begin
newOffset := inOffset + inLen;
newLen := inLen div 2;
idx := inOffset;
for counter := newOffset to newOffset + newLen - 1 do
begin
X^[counter] := 0.5*(x^[idx] + X^[idx + 1]);
inc(idx, 2);
end;
end;
{$IFNDEF MRMATH_NOASM}
{$IFDEF x64}
// 64bit version
procedure TDynamicTimeWarp.ReduceByHalfSSE(X : PConstDoubleArr; inLen,
inOffset: NativeInt; out newLen, newOffset: NativeInt);
{$IFDEF FPC}
begin
{$ENDIF}
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
// rcx self, rdx X, r8 inLen, r9 = inOffset
// newOffset := inOffset + inLen;
// newOffset := newOffset + newOffset and 1;
mov r10, newOffset;
mov r11, r8;
add r11, r9;
inc r11; // make sure the last bit is 0
and r11, $FFFFFFFFFFFFFFFE; // clear the lowest bit (ensure SSE alignment)
mov [r10], r11;
// newLen := inLen div 2;
shr r8, 1;
mov r10, newLen;
mov [r10], r8;
movupd xmm3, [rip + cLocDivBy2];
// test if we have enough elements to handle 2 elements at once
cmp r8, 2;
jl @lastElem;
@loop1:
// loop handles two elements in one step
movapd xmm0, [rdx + 8*r9];
movapd xmm1, [rdx + 8*r9 + 16];
// perform xmm0 = 0.5( [rcx + 8] + [rcx + 16]), 0.5*([rcx + 16] + [rcx + 32])
haddpd xmm0, xmm1;
mulpd xmm0, xmm3;
// write back two element
movapd [rdx + 8*r11], xmm0;
add r11, 2;
add r9, 4;
sub r8, 2;
jg @loop1;
test r8, r8;
je @exit;
@lastElem:
// last element
movsd xmm0, [rcx + 8*r9];
movsd xmm1, [rcx + 8*r9 + 8];
addsd xmm0, xmm1;
mulsd xmm0, xmm3;
movsd [rdx + 8*r11], xmm0;
@exit:
end;
{$IFDEF FPC}
end;
{$ENDIF}
{$ELSE}
// 32 bit version
procedure TDynamicTimeWarp.ReduceByHalfSSE(X : PConstDoubleArr; inLen,
inOffset: NativeInt; out newLen, newOffset: NativeInt);
{$IFDEF FPC}
begin
{$ENDIF}
asm
// edx: X, ecx : inlen
push ebx;
// newOffset := inOffset + inLen;
mov ebx, newOffset;
mov eax, inOffset;
//add eax, inLen;
add eax, ecx;
inc eax; // make sure the last bit is 0
and eax, $FFFFFFFE; // clear the lowest bit (ensure SSE alignment)
mov [ebx], eax;
// newLen := inLen div 2;
//mov ecx, inLen;
shr ecx, 1;
mov ebx, newLen;
mov [ebx], ecx;
movupd xmm3, cLocDivBy2;
// eax := counter for x[idx]
// edx := counter for x[counter];
//mov edx, X;
mov ebx, inOffset;
// test if we have enough elements to handle 2 elements at once
cmp ecx, 2;
jl @lastElem;
@loop1:
// loop handles two elements in one step
movapd xmm0, [edx + 8*ebx];
movapd xmm1, [edx + 8*ebx + 16];
// perform xmm0 = 0.5( [edx + 8] + [edx + 16]), 0.5*([edx + 16] + [edx + 32])
haddpd xmm0, xmm1;
mulpd xmm0, xmm3;
// write back two element
movapd [edx + 8*eax], xmm0;
add eax, 2;
add ebx, 4;
sub ecx, 2;
jg @loop1;
test ecx, ecx;
je @exit;
@lastElem:
// last element
movsd xmm0, [edx + 8*ebx];
movsd xmm1, [edx + 8*ebx + 8];
addsd xmm0, xmm1;
mulsd xmm0, xmm3;
movsd [edx + 8*eax], xmm0;
@exit:
pop ebx;
end;
{$IFDEF FPC}
end;
{$ENDIF}
{$ENDIF}
{$ENDIF}
function TDynamicTimeWarp.ExpandWindow(inXLen, inYLen, radius: integer): Integer;
var cnt : integer;
baseI, baseJ : integer;
minJ, maxJ : integer;
pathCnt : integer;
prevRadiusPathIdx : integer;
nextRadiusPathIdx : integer;
i, j : integer;
base : integer;
numItems : integer;
begin
Result := 0;
prevRadiusPathIdx := 0;
nextRadiusPathIdx := 0;
base := fNumPath - 1;
// handle last element
for cnt := 1 to radius do
begin
fPath[fNumPath].i := fPath[base].i + cnt;
fPath[fNumPath].j := fPath[base].j;
inc(fNumPath);
end;
baseI := -1;