-
Notifications
You must be signed in to change notification settings - Fork 9
/
normconv.c
1971 lines (1783 loc) · 49.7 KB
/
normconv.c
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
#include "mex.h"
#define DEBUG 0
/*
* normconv.c
*
* Perform normalized convolution. Only real images and filters are
* supported, but at arbitrary dimensionality. Perform normalized
* convolution. Complex signals and basis functions are supported, at
* arbitrary dimensionality. The method is described in chapter 3 of
* Gunnar Farnebäck's thesis "Polynomial Expansion for Orientation and
* Motion Estimation".
*
* Call:
* result = normconv(signal, certainty, basis, applicability)
* or
* result = normconv(signal, certainty, basis, applicability,
* region_of_interest)
*
* signal - signal values
* certainty - signal certainty (pointwise)
* basis - subspace basis
* applicability - applicability function for the basis
* region_of_interest - where to compute the normalized convolution
* result - local basis coefficients
*
* Formats: (Also see note 2 for special cases.)
* signal is an n-dimensional array.
* certainty is the same size as signal.
* basis is an (n+1)-dimensional array. The first n dimensions correspond
* to the signal dimensions. The size of the last dimension gives
* the number of basis functions.
* applicability is an n-dimensional array, with the same size as the first
* n dimensions of basis.
* region_of_interest is either an nx2 matrix, where each row gives start
* and end indices for each dimension, or a sparse matrix of the
* same size as signal.
* result is an (n+1)-dimensional array. The size of the first n dimensions
* are the same as for signal, unless region_of_interest is
* specified. The size of the last dimension equals the number of
* basis functions.
*
* Note 1: Only double, nonsparse arrays are currently supported.
*
* Note 2: Trailing singleton dimensions cannot exist in matlab 5.
* If there is a mismatch in the number of dimensions of the
* parameters it will be assumed that there are additional
* singleton dimensions, if that turns out to make sense.
* Particularly, in the case of a single basis function, basis
* will normally be n-dimensional instead of (n+1)-dimensional.
* The same goes for result.
*
* In the case of column vectors, the second, singleton, dimension
* will be ignored, although it is included by matlab 5.
*
* Note 3: The special cases of 1-3 dimensions and/or a single basis
* function have been optimized for speed. To use the general
* algorithm also in these cases, add as a final parameter in
* the call the string 'general'. This will certainly be slower
* but can be used to verify that the optimized code works as
* intended. The results should not differ by more than
* numerical deviations. The general algorithm does not work with
* the sparse form of region_of_interest.
*
* Note 4: The above mentioned sparse region of interest has not been
* implemented, as it turned out that matlab only supports
* sparse matrices in two dimensions.
*
* Author: Gunnar Farnebäck
* Computer Vision Laboratory
* Linköping University, Sweden
*/
#define NO_ROI 0
#define MASK_ROI 1
#define BOX_ROI 2
/* Set up matrices needed for computation of output certainty. */
static void
init_cout_matrices(const double *basis_r, const double *basis_i,
const double *applicability,
int basis_size, int modeldimprod,
int *cout_num_arguments, mxArray **cout_arguments,
mxArray *G_matrix, mxArray *G0_matrix,
mxArray *h_vector, mxArray *cout_data_array,
int is_real)
{
int k1, k2;
double p_r, p_i;
double ip_r, ip_i;
int model_index;
double *G0_r;
double *G0_i;
if (is_real)
{
G0_matrix = mxCreateDoubleMatrix(basis_size, basis_size, mxREAL);
G0_r = mxGetPr(G0_matrix);
G0_i = NULL;
}
else
{
G0_matrix = mxCreateDoubleMatrix(basis_size, basis_size, mxCOMPLEX);
G0_r = mxGetPr(G0_matrix);
G0_i = mxGetPi(G0_matrix);
}
/* Double loop over the basis functions to compute inner
* products between basis functions for certainty identically one.
*/
for (k1 = 0; k1 < basis_size; k1++)
{
for (k2 = k1; k2 < basis_size; k2++)
{
/* Reset the accumulated inner product. */
ip_r = 0.0;
ip_i = 0.0;
/* Loop over the dimensions for the basis functions. */
for (model_index = 0;
model_index < modeldimprod;
model_index++)
{
if (is_real)
{
p_r = basis_r[model_index + modeldimprod * k2];
p_r *= applicability[model_index];
p_r *= basis_r[model_index + modeldimprod * k1];
ip_r += p_r;
}
else
{
double b_r, b_i;
p_r = basis_r[model_index + modeldimprod * k2];
p_i = basis_i[model_index + modeldimprod * k2];
p_r *= applicability[model_index];
p_i *= applicability[model_index];
b_r = basis_r[model_index + modeldimprod * k1];
b_i = basis_i[model_index + modeldimprod * k1];
ip_r += p_r * b_r + p_i * b_i;
ip_i += p_i * b_r - p_r * b_i;
}
}
if (is_real)
{
G0_r[k1 + k2 * basis_size] = ip_r;
G0_r[k2 + k1 * basis_size] = ip_r;
}
else
{
G0_r[k1 + k2 * basis_size] = ip_r;
G0_i[k1 + k2 * basis_size] = ip_i;
G0_r[k2 + k1 * basis_size] = ip_r;
G0_i[k2 + k1 * basis_size] = -ip_i;
}
}
}
*cout_num_arguments = 4;
cout_arguments[0] = G_matrix;
cout_arguments[1] = G0_matrix;
cout_arguments[2] = h_vector;
if (cout_data_array)
{
cout_arguments[4] = cout_data_array;
*cout_num_arguments = 5;
}
}
/* One signal dimension, multiple basis functions. */
static void
normconv1(const double *signal_r,
const double *signal_i,
const double *certainty,
const double *basis_r,
const double *basis_i,
const double *applicability,
double *result_r,
double *result_i,
double *cout,
const int *signal_dimensions,
const int *model_dimensions,
const int *result_dimensions,
int basis_size,
const int *start_indices,
const int *stop_indices,
const double *roi,
const char *cout_func_name,
mxArray *cout_data_array,
int cout_elements,
int is_real)
{
mxArray *arguments[2];
mxArray *G_matrix;
mxArray *h_vector;
mxArray *x_vector;
double *G_r;
double *G_i;
double *h_r;
double *h_i;
double *x_r;
double *x_i;
mxArray *cout_arguments[5];
mxArray *G0_matrix;
mxArray *this_cout_array;
double *G0_r;
double *G0_i;
double *this_cout;
int cout_num_arguments;
int result_index;
int model_index;
int signal_index;
int displacement;
int resultindex;
int modeldimprod; /* Product of the first n dimensions. */
int resultdimprod; /* Product of the first n dimensions. */
double p_r;
double p_i;
double ip_r;
double ip_i;
int k, k1, k2;
if (is_real)
{
G_matrix = mxCreateDoubleMatrix(basis_size, basis_size, mxREAL);
h_vector = mxCreateDoubleMatrix(basis_size, 1, mxREAL);
G_r = mxGetPr(G_matrix);
G_i = NULL;
h_r = mxGetPr(h_vector);
h_i = NULL;
}
else
{
G_matrix = mxCreateDoubleMatrix(basis_size, basis_size, mxCOMPLEX);
h_vector = mxCreateDoubleMatrix(basis_size, 1, mxCOMPLEX);
G_r = mxGetPr(G_matrix);
G_i = mxGetPi(G_matrix);
h_r = mxGetPr(h_vector);
h_i = mxGetPi(h_vector);
}
arguments[0] = G_matrix;
arguments[1] = h_vector;
/* Initialize the first result index and the model index. */
result_index = start_indices[0];
model_index = 0;
/* The centers of the basis functions are supposed to be local
* origins. Compute the corresponding offset. Note that this is
* integer division.
*/
displacement = (model_dimensions[0] - 1) / 2;
/* Compute the product of the first n dimensions of the model and
* result arrays respectively.
*/
modeldimprod = model_dimensions[0];
resultdimprod = result_dimensions[0];
if (cout_func_name)
{
init_cout_matrices(basis_r, basis_i, applicability, basis_size,
modeldimprod, &cout_num_arguments, cout_arguments,
G_matrix, G0_matrix, h_vector, cout_data_array,
is_real);
}
/* Loop over the signal dimensions. */
for (result_index = start_indices[0];
result_index <= stop_indices[0];
result_index++)
{
/* If we have a mask region of interest and this is zero, skip
* this point. (The result array is initialized to zero so we
* don't need to put anything there ourselves.)
*/
if (roi && roi[result_index] == 0.0)
continue;
/* Double loop over the basis functions to compute inner
* products. Inner products between basis functions and signal
* are computed when k2 == basis_size.
*/
for (k1 = 0; k1 < basis_size; k1++)
{
for (k2 = k1; k2 <= basis_size; k2++)
{
/* Reset the accumulated inner product. */
ip_r = 0.0;
ip_i = 0.0;
/* Loop over the dimensions for the basis functions. */
for (model_index = 0;
model_index < model_dimensions[0];
model_index++)
{
/* Compute the signal index corresponding to the
* current result index and model index.
*/
signal_index = (result_index
+ model_index
- displacement);
/* Check if we are outside the signal boundary. It
* is implied that the certainty is zero then.
*/
if (signal_index < 0
|| signal_index >= signal_dimensions[0])
continue;
if (is_real)
{
if (k2 == basis_size)
p_r = signal_r[signal_index];
else
p_r = basis_r[model_index + modeldimprod * k2];
p_r *= certainty[signal_index];
p_r *= applicability[model_index];
p_r *= basis_r[model_index + modeldimprod * k1];
ip_r += p_r;
}
else
{
double ca, b_r, b_i;
if (k2 == basis_size)
{
p_r = signal_r[signal_index];
p_i = signal_i[signal_index];
}
else
{
p_r = basis_r[model_index + modeldimprod * k2];
p_i = basis_i[model_index + modeldimprod * k2];
}
ca = (certainty[signal_index] * applicability[model_index]);
p_r *= ca;
p_i *= ca;
b_r = basis_r[model_index + modeldimprod * k1];
b_i = basis_i[model_index + modeldimprod * k1];
/* Notice that this is multiplication with the
* conjugate of b.
*/
ip_r += p_r * b_r + p_i * b_i;
ip_i += p_i * b_r - p_r * b_i;
}
}
if (is_real)
{
if (k2 == basis_size)
h_r[k1] = ip_r;
else
{
G_r[k1 + k2 * basis_size] = ip_r;
G_r[k2 + k1 * basis_size] = ip_r;
}
}
else
{
if (k2 == basis_size)
{
h_r[k1] = ip_r;
h_i[k1] = ip_i;
}
else
{
G_r[k1 + k2 * basis_size] = ip_r;
G_i[k1 + k2 * basis_size] = ip_i;
G_r[k2 + k1 * basis_size] = ip_r;
G_i[k2 + k1 * basis_size] = -ip_i;
}
}
}
}
resultindex = (result_index - start_indices[0]);
if (basis_size == 1 && !cout_func_name)
{
if (is_real)
result_r[resultindex] = h_r[0] / G_r[0];
else
{
double w = 1.0 / (G_r[0] * G_r[0] + G_i[0] * G_i[0]);
result_r[resultindex] = w * (h_r[0] * G_r[0] + h_i[0] * G_i[0]);
result_i[resultindex] = w * (h_i[0] * G_r[0] - h_r[0] * G_i[0]);
}
}
else
{
mexCallMATLAB(1, &x_vector, 2, arguments, "\\");
if (is_real)
{
x_r = mxGetPr(x_vector);
for (k = 0; k < basis_size; k++)
result_r[resultindex + resultdimprod * k] = x_r[k];
}
else
{
x_r = mxGetPr(x_vector);
x_i = mxGetPi(x_vector);
for (k = 0; k < basis_size; k++)
{
result_r[resultindex + resultdimprod * k] = x_r[k];
if (x_i != NULL)
result_i[resultindex + resultdimprod * k] = x_i[k];
else /* Not sure whether this can happen. */
result_i[resultindex + resultdimprod * k] = 0.0;
}
}
}
if (cout_func_name)
{
cout_arguments[3] = x_vector;
mexCallMATLAB(1, &this_cout_array, cout_num_arguments, cout_arguments,
cout_func_name);
this_cout = mxGetPr(this_cout_array);
for (k = 0; k < cout_elements; k++)
cout[resultindex + resultdimprod * k] = this_cout[k];
mxDestroyArray(this_cout_array);
}
if (basis_size > 1 || cout_func_name)
mxDestroyArray(x_vector);
}
}
/* Two signal dimensions, multiple basis functions. */
static void
normconv2(const double *signal_r,
const double *signal_i,
const double *certainty,
const double *basis_r,
const double *basis_i,
const double *applicability,
double *result_r,
double *result_i,
double *cout,
const int *signal_dimensions,
const int *model_dimensions,
const int *result_dimensions,
int basis_size,
const int *start_indices,
const int *stop_indices,
const double *roi,
const char *cout_func_name,
mxArray *cout_data_array,
int cout_elements,
int is_real)
{
mxArray *arguments[2];
mxArray *G_matrix;
mxArray *h_vector;
mxArray *x_vector;
double *G_r;
double *G_i;
double *h_r;
double *h_i;
double *x_r;
double *x_i;
mxArray *cout_arguments[5];
mxArray *G0_matrix;
mxArray *this_cout_array;
double *G0_r;
double *G0_i;
double *this_cout;
int cout_num_arguments;
int result_indices[2];
int model_indices[2];
int signal_indices[2];
int displacements[2];
int signalindex;
int modelindex;
int resultindex;
int modeldimprod; /* Product of the first n dimensions. */
int resultdimprod; /* Product of the first n dimensions. */
double p_r;
double p_i;
double ip_r;
double ip_i;
int i, k, k1, k2;
if (is_real)
{
G_matrix = mxCreateDoubleMatrix(basis_size, basis_size, mxREAL);
h_vector = mxCreateDoubleMatrix(basis_size, 1, mxREAL);
G_r = mxGetPr(G_matrix);
G_i = NULL;
h_r = mxGetPr(h_vector);
h_i = NULL;
}
else
{
G_matrix = mxCreateDoubleMatrix(basis_size, basis_size, mxCOMPLEX);
h_vector = mxCreateDoubleMatrix(basis_size, 1, mxCOMPLEX);
G_r = mxGetPr(G_matrix);
G_i = mxGetPi(G_matrix);
h_r = mxGetPr(h_vector);
h_i = mxGetPi(h_vector);
}
arguments[0] = G_matrix;
arguments[1] = h_vector;
/* Initialize the first 2 result indices and the model indices. */
for (i = 0; i < 2; i++)
{
result_indices[i] = start_indices[i];
model_indices[i] = 0;
}
/* The centers of the basis functions are supposed to be local
* origins. Compute the corresponding offsets.
*/
for (i = 0; i < 2; i++)
{
/* Note that this is integer division. */
displacements[i] = (model_dimensions[i] - 1) / 2;
}
/* Compute the product of the first n dimensions of the model and
* result arrays respectively.
*/
modeldimprod = 1;
resultdimprod = 1;
for (i = 0; i < 2; i++)
{
modeldimprod *= model_dimensions[i];
resultdimprod *= result_dimensions[i];
}
if (cout_func_name)
{
init_cout_matrices(basis_r, basis_i, applicability, basis_size,
modeldimprod, &cout_num_arguments, cout_arguments,
G_matrix, G0_matrix, h_vector, cout_data_array,
is_real);
}
/* Loop over the signal dimensions. */
for (result_indices[1] = start_indices[1];
result_indices[1] <= stop_indices[1];
result_indices[1]++)
{
for (result_indices[0] = start_indices[0];
result_indices[0] <= stop_indices[0];
result_indices[0]++)
{
/* If we have a mask region of interest and this is zero, skip
* this point. (The result array is initialized to zero so we
* don't need to put anything there ourselves.)
*/
if (roi)
{
int roi_index = (result_indices[1] * signal_dimensions[0]
+ result_indices[0]);
if (roi[roi_index] == 0.0)
continue;
}
/* Double loop over the basis functions to compute inner
* products. Inner products between basis functions and
* signal are computed when k2 == basis_size.
*/
for (k1 = 0; k1 < basis_size; k1++)
{
for (k2 = k1; k2 <= basis_size; k2++)
{
/* Reset the accumulated inner product. */
ip_r = 0.0;
ip_i = 0.0;
/* Loop over the dimensions for the basis functions. */
for (model_indices[1] = 0;
model_indices[1] < model_dimensions[1];
model_indices[1]++)
{
/* Compute the signal index corresponding to
* the current result index and model index.
*/
signal_indices[1] = (result_indices[1]
+ model_indices[1]
- displacements[1]);
/* Check if we are outside the signal
* boundary. It is implied that the certainty
* is zero then.
*/
if (signal_indices[1] < 0
|| signal_indices[1] >= signal_dimensions[1])
continue;
/* Loop over the dimensions for the basis functions. */
for (model_indices[0] = 0;
model_indices[0] < model_dimensions[0];
model_indices[0]++)
{
signal_indices[0] = (result_indices[0]
+ model_indices[0]
- displacements[0]);
if (signal_indices[0] < 0
|| signal_indices[0] >= signal_dimensions[0])
continue;
signalindex = (signal_indices[1]
* signal_dimensions[0]
+ signal_indices[0]);
modelindex = (model_indices[1]
* model_dimensions[0]
+ model_indices[0]);
if (is_real)
{
if (k2 == basis_size)
p_r = signal_r[signalindex];
else
p_r = basis_r[modelindex + modeldimprod * k2];
p_r *= certainty[signalindex];
p_r *= applicability[modelindex];
p_r *= basis_r[modelindex + modeldimprod * k1];
ip_r += p_r;
}
else
{
double ca, b_r, b_i;
if (k2 == basis_size)
{
p_r = signal_r[signalindex];
p_i = signal_i[signalindex];
}
else
{
p_r = basis_r[modelindex + modeldimprod * k2];
p_i = basis_i[modelindex + modeldimprod * k2];
}
ca = (certainty[signalindex]
* applicability[modelindex]);
p_r *= ca;
p_i *= ca;
b_r = basis_r[modelindex + modeldimprod * k1];
b_i = basis_i[modelindex + modeldimprod * k1];
/* Notice that this is multiplication with the
* conjugate of b.
*/
ip_r += p_r * b_r + p_i * b_i;
ip_i += p_i * b_r - p_r * b_i;
}
}
}
if (is_real)
{
if (k2 == basis_size)
h_r[k1] = ip_r;
else
{
G_r[k1 + k2 * basis_size] = ip_r;
G_r[k2 + k1 * basis_size] = ip_r;
}
}
else
{
if (k2 == basis_size)
{
h_r[k1] = ip_r;
h_i[k1] = ip_i;
}
else
{
G_r[k1 + k2 * basis_size] = ip_r;
G_i[k1 + k2 * basis_size] = ip_i;
G_r[k2 + k1 * basis_size] = ip_r;
G_i[k2 + k1 * basis_size] = -ip_i;
}
}
}
}
resultindex = ((result_indices[1] - start_indices[1])
* result_dimensions[0]
+ (result_indices[0] - start_indices[0]));
if (basis_size == 1 && !cout_func_name)
{
if (is_real)
result_r[resultindex] = h_r[0] / G_r[0];
else
{
double w = 1./(G_r[0] * G_r[0] + G_i[0] * G_i[0]);
result_r[resultindex] = w * (h_r[0] * G_r[0] + h_i[0] * G_i[0]);
result_i[resultindex] = w * (h_i[0] * G_r[0] - h_r[0] * G_i[0]);
}
}
else
{
mexCallMATLAB(1, &x_vector, 2, arguments, "\\");
if (is_real)
{
x_r = mxGetPr(x_vector);
for (k = 0; k < basis_size; k++)
result_r[resultindex + resultdimprod * k] = x_r[k];
}
else
{
x_r = mxGetPr(x_vector);
x_i = mxGetPi(x_vector);
for (k = 0; k < basis_size; k++)
{
result_r[resultindex + resultdimprod * k] = x_r[k];
if (x_i != NULL)
result_i[resultindex + resultdimprod * k] = x_i[k];
else /* Not sure whether this can happen. */
result_i[resultindex + resultdimprod * k] = 0.0;
}
}
}
if (cout_func_name)
{
cout_arguments[3] = x_vector;
mexCallMATLAB(1, &this_cout_array, cout_num_arguments, cout_arguments,
cout_func_name);
this_cout = mxGetPr(this_cout_array);
for (k = 0; k < cout_elements; k++)
cout[resultindex + resultdimprod * k] = this_cout[k];
mxDestroyArray(this_cout_array);
}
if (basis_size > 1 || cout_func_name)
mxDestroyArray(x_vector);
}
}
}
/* Three signal dimensions, multiple basis functions. */
static void
normconv3(const double *signal_r,
const double *signal_i,
const double *certainty,
const double *basis_r,
const double *basis_i,
const double *applicability,
double *result_r,
double *result_i,
double *cout,
const int *signal_dimensions,
const int *model_dimensions,
const int *result_dimensions,
int basis_size,
const int *start_indices,
const int *stop_indices,
const double *roi,
const char *cout_func_name,
mxArray *cout_data_array,
int cout_elements,
int is_real)
{
mxArray *arguments[2];
mxArray *G_matrix;
mxArray *h_vector;
mxArray *x_vector;
double *G_r;
double *G_i;
double *h_r;
double *h_i;
double *x_r;
double *x_i;
mxArray *cout_arguments[5];
mxArray *G0_matrix;
mxArray *this_cout_array;
double *G0_r;
double *G0_i;
double *this_cout;
int cout_num_arguments;
int result_indices[3];
int model_indices[3];
int signal_indices[3];
int displacements[3];
int signalindex;
int modelindex;
int resultindex;
int modeldimprod; /* Product of the first n dimensions. */
int resultdimprod; /* Product of the first n dimensions. */
double p_r;
double p_i;
double ip_r;
double ip_i;
int i, k, k1, k2;
if (is_real)
{
G_matrix = mxCreateDoubleMatrix(basis_size, basis_size, mxREAL);
h_vector = mxCreateDoubleMatrix(basis_size, 1, mxREAL);
G_r = mxGetPr(G_matrix);
G_i = NULL;
h_r = mxGetPr(h_vector);
h_i = NULL;
}
else
{
G_matrix = mxCreateDoubleMatrix(basis_size, basis_size, mxCOMPLEX);
h_vector = mxCreateDoubleMatrix(basis_size, 1, mxCOMPLEX);
G_r = mxGetPr(G_matrix);
G_i = mxGetPi(G_matrix);
h_r = mxGetPr(h_vector);
h_i = mxGetPi(h_vector);
}
arguments[0] = G_matrix;
arguments[1] = h_vector;
/* Initialize the first 3 result indices and the model indices. */
for (i = 0; i < 3; i++)
{
result_indices[i] = start_indices[i];
model_indices[i] = 0;
}
/* The centers of the basis functions are supposed to be local
* origins. Compute the corresponding offsets.
*/
for (i = 0; i < 3; i++)
{
/* Note that this is integer division. */
displacements[i] = (model_dimensions[i] - 1) / 2;
}
/* Compute the product of the first n dimensions of the model and
* result arrays respectively.
*/
modeldimprod = 1;
resultdimprod = 1;
for (i = 0; i < 3; i++)
{
modeldimprod *= model_dimensions[i];
resultdimprod *= result_dimensions[i];
}
if (cout_func_name)
{
init_cout_matrices(basis_r, basis_i, applicability, basis_size,
modeldimprod, &cout_num_arguments, cout_arguments,
G_matrix, G0_matrix, h_vector, cout_data_array,
is_real);
}
/* Loop over the signal dimensions */
for (result_indices[2] = start_indices[2];
result_indices[2] <= stop_indices[2];
result_indices[2]++)
{
for (result_indices[1] = start_indices[1];
result_indices[1] <= stop_indices[1];
result_indices[1]++)
{
for (result_indices[0] = start_indices[0];
result_indices[0] <= stop_indices[0];
result_indices[0]++)
{
/* If we have a mask region of interest and this is
* zero, skip this point. (The result array is
* initialized to zero so we don't need to put
* anything there ourselves.)
*/
if (roi)
{
int roi_index = ((result_indices[2]
* signal_dimensions[1]
+ result_indices[1])
* signal_dimensions[0]
+ result_indices[0]);
if (roi[roi_index] == 0.0)
continue;
}
/* Double loop over the basis functions to compute
* inner products. Inner products between basis
* functions and signal are computed when k2 ==
* basis_size.
*/
for (k1 = 0; k1 < basis_size; k1++)
{
for (k2 = k1; k2 <= basis_size; k2++)
{
/* Reset the accumulated inner product. */
ip_r = 0.0;
ip_i = 0.0;
/* Loop over the dimensions for the basis functions. */
for (model_indices[2] = 0;
model_indices[2] < model_dimensions[2];
model_indices[2]++)
{
/* Compute the signal index corresponding
* to the current result index and model
* index.
*/
signal_indices[2] = (result_indices[2]
+ model_indices[2]
- displacements[2]);
/* Check if we are outside the signal
* boundary. It is implied that the
* certainty is zero then.
*/
if (signal_indices[2] < 0
|| signal_indices[2] >= signal_dimensions[2])
continue;
for (model_indices[1] = 0;
model_indices[1] < model_dimensions[1];
model_indices[1]++)
{
signal_indices[1] = (result_indices[1]
+ model_indices[1]
- displacements[1]);
if (signal_indices[1] < 0
|| (signal_indices[1]
>= signal_dimensions[1]))
continue;
for (model_indices[0] = 0;
model_indices[0] < model_dimensions[0];
model_indices[0]++)
{
signal_indices[0] = (result_indices[0]
+ model_indices[0]
- displacements[0]);
if (signal_indices[0] < 0
|| (signal_indices[0]
>= signal_dimensions[0]))
continue;
signalindex = ((signal_indices[2]
* signal_dimensions[1]
+ signal_indices[1])
* signal_dimensions[0]
+ signal_indices[0]);
modelindex = ((model_indices[2]
* model_dimensions[1]
+ model_indices[1])
* model_dimensions[0]
+ model_indices[0]);
if (is_real)
{
if (k2 == basis_size)
p_r = signal_r[signalindex];
else
p_r = basis_r[modelindex + modeldimprod * k2];
p_r *= certainty[signalindex];
p_r *= applicability[modelindex];
p_r *= basis_r[modelindex + modeldimprod * k1];
ip_r += p_r;
}
else
{
double ca, b_r, b_i;
if (k2 == basis_size)
{
p_r = signal_r[signalindex];
p_i = signal_i[signalindex];
}
else
{
p_r = basis_r[modelindex + modeldimprod * k2];
p_i = basis_i[modelindex + modeldimprod * k2];
}