-
Notifications
You must be signed in to change notification settings - Fork 0
/
prcorr2.c
429 lines (397 loc) · 14.1 KB
/
prcorr2.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
/* MEX-file (Plugin) for MATLAB
Created with: ELEMENT ITERATOR Version 1.0 Beta (C) 2001-2002 Peter Rydesäter
Implements function: prcorr2
*/
#include "mex.h"
#include <math.h>
#include <string.h>
#define MATLAB_COMMENT(X) /* Dummy macro for matlab_comments */
#define _deftype_ double
#define MAX(A,B) ((A)>(B)?(A):(B))
#define MIN(A,B) ((A)<(B)?(A):(B))
#define LIMIT(X,A,B) MIN(MAX(X,B),A)
#define double(X) (double)(X)
#define single(X) (float)(X)
#define char(X) uint16(X)
#define int(X) int32(X)
#define int8(X) (char)(X)
#define int16(X) (short int)(X)
#define int32(X) (long int)(X)
#define int64(X) (long long)(X)
#define uint8(X) (unsigned char)(X)
#define uint16(X) (unsigned short int)(X)
#define uint32(X) (unsigned long int)(X)
#define uint64(X) (unsigned long long)(X)
#define pointer(X) (&(X))
#define error(X) ErrStr=(X)
#define disp(X) fprintf(stderr,"At element %d:%s = %.15g\n",_iter_i_+1, #X , (double)(X))
#define dispstr(X) fprintf(stderr,"At element %d: %s\n",_iter_i_+1,(X))
#define mod(X,Y) ((X)%(Y))
#define round(X) (floor((X)-0.5))
#define power(X,Y) pow(X,Y)
#define prod_size(X) ( _ ## X ## _elements_ )
#define _iter_elements_ _iter_end_
#define size(X,D) (((int)(D)> ( _ ## X ## _no_dims_ ) || (int)(D)<1 )?1:_ ## X ## _dims_[(int)(D)-1])
#define length(X) MAX( MAX(size(X,1),size(X,2)) , size(X,3) )
#define ndims(X) ( _ ## X ## _no_dims_ )
#define INDEXERR(X,mi,ma) ((X)<(mi)?((mi)+0*(int)(ErrStr="Index out of range")):((X)>=(ma)? ((ma)+0*((int)(ErrStr="Index out of range"))):(X)))
#define _ERROR_FF_CALL(ERRSTR) \
mexErrMsgTxt(ERRSTR)
volatile const char *ErrStr=NULL;
const int ones_dims[]={1,1};
/* FUNCTION that combine two arrays of dimention sizes and allocate an array from it*/
mxArray *mk_iterator_NumericArray(int ndims1, const int *dims1,int ndims2,const int *dims2,mxClassID class)
{
int ndims=0;
int dims[512];
if(ndims1+ndims2>512) mexErrMsgTxt("To many dimentions in array to create");
memcpy(&dims[ndims],dims1,ndims1*sizeof(int));
ndims+=ndims1;
memcpy(&dims[ndims],dims2,ndims2*sizeof(int));
ndims+=ndims2;
return mxCreateNumericArray(ndims,dims,class,mxREAL);
}
int myGetNumberOfDimensions(const mxArray *ptr)
{
const int *dims;
int ndims=mxGetNumberOfDimensions(ptr);
if(ndims>2) return ndims;
dims=mxGetDimensions(ptr);
if(dims[1]>1) return 2;
if(dims[0]>1) return 1;
return 0;
}
const double pi=3.141592653589793115997963468544185161590576171875;
void mexFunction(int nlhs_IN,mxArray *plhs_IN[],int nrhs_IN, const mxArray *prhs_IN[])
{
/* Variables */
mxArray *plhs[51],*prhs[51];
int nlhs=0,nrhs=0;
int _iter_start_=0, _iter_stop_=0, _iter_end_=0,no_arg_in=0,no_arg_ret=0;
int _iter_no_dims_=2;
const int *_iter_dims_=NULL;
int _iter_make_dims[6]={1,1,1,1,1,1};
/* Input and Return Variables ToFrom MATLAB WS */
const double _def_A_[]={0};
const double *_A_=_def_A_;
int _A_no_dims_ =0;
int const *_A_dims_ =ones_dims;
int _A_elements_=1;
const double _def_B_[]={0};
const double *_B_=_def_B_;
int _B_no_dims_ =0;
int const *_B_dims_ =ones_dims;
int _B_elements_=1;
double _def_corr_[]={0};
double *_corr_=_def_corr_;
int _corr_no_dims_ =0;
int const *_corr_dims_ =ones_dims;
int _corr_elements_=1;
/*----Argument init section---*/
{
int idx=0;
ErrStr=NULL;
if(nrhs_IN==0 && nlhs_IN==1){plhs_IN[0]=mxCreateString(
"ELEMENT ITERATOR Version 1.0 Beta (C) 2001-2002 Peter Rydesäter -pipeline -force -fastmath -typecast [double corr]=prcorr2( A, B) init: elements=prod_size(iter) iter: suma=suma+A sumb=sumb+B post: ma=suma/elements mb=sumb/elements itersec: a=(A)-ma b=(B)-mb sab+=a*b saa+=a*a sbb+=b*b postsec: corr=sab/(sqrt(saa*sbb)) ");return;}
do{
/* Start of breakable section */
if( nrhs_IN>0)
prhs[nrhs++]=(mxArray *)prhs_IN[0];
else
break;
if( nrhs_IN>1)
prhs[nrhs++]=(mxArray *)prhs_IN[1];
else
break;
}while(0); /* End of breakable section */
nlhs=MAX(nlhs_IN,0);
if(nrhs_IN>2)
mexWarnMsgTxt( "Too many arguments to function prcorr2. Argument(s) ignored.");
/*==============GET DIMENSIONS SIZE FOR ITERATOR============*/
/* If iterator dimensions not know, get it from first arguments shape/size */
if(nrhs>idx+0 && _iter_dims_==NULL){
_iter_no_dims_=myGetNumberOfDimensions(prhs[idx+0])-(0);
_iter_dims_=mxGetDimensions(prhs[idx+0]);
_iter_end_= mxGetNumberOfElements(prhs[idx+0])/(1);
}
/* If iterator dimensions not know, Compose it....*/
if(_iter_dims_==NULL){
_iter_dims_=_iter_make_dims;
_iter_end_=1;
_iter_no_dims_=0;
}
if(_iter_no_dims_<=0){_iter_no_dims_=1; _iter_dims_=ones_dims;}
/*==============END GET DIMS========================*/
do{
/*========GET ARGUMENTS======================*/
const mxArray *ptr=NULL;
/* Get argument: A */
if(nrhs<=idx) break; else no_arg_in=1;
ptr=prhs[idx++];
if(mxGetClassID(ptr)!=mxDOUBLE_CLASS){
mxArray *cast_plhs[1]={ NULL };
mexCallMATLAB(1,cast_plhs,1,(mxArray **)&ptr,"double");
ptr=cast_plhs[0];
}
if(mxGetClassID(ptr)!=mxDOUBLE_CLASS)
_ERROR_FF_CALL("Datatype double expected as input argument \"A\" (1)");
if(mxIsComplex(ptr))
_ERROR_FF_CALL("Expected REAL number as input argument \"A\" (1)");
_A_ =(double *)mxGetPr(ptr);
_A_no_dims_=myGetNumberOfDimensions(ptr);
_A_elements_=mxGetNumberOfElements(ptr);
_A_dims_=mxGetDimensions(ptr);
/* Remove leading iterator dimensions */
{ int n=0,e=1;
for(n=0; ;n++){
if(e==_iter_end_ && _A_no_dims_-n==0)
break;
if( (n>=_A_no_dims_) || (e * _A_dims_[n]>_iter_end_) )
_ERROR_FF_CALL("Can not match size of dimensions for A to iterator");
e *=_A_dims_[n];
}
_A_elements_/=e;
_A_no_dims_-=n;
_A_dims_=&_A_dims_[n];
}
if( _A_elements_!=1 )
_ERROR_FF_CALL("Expected 1 elements as iterator window dims as input argument \"A\" (1)");
/* Get argument: B */
if(nrhs<=idx) break; else no_arg_in=2;
ptr=prhs[idx++];
if(mxGetClassID(ptr)!=mxDOUBLE_CLASS){
mxArray *cast_plhs[1]={ NULL };
mexCallMATLAB(1,cast_plhs,1,(mxArray **)&ptr,"double");
ptr=cast_plhs[0];
}
if(mxGetClassID(ptr)!=mxDOUBLE_CLASS)
_ERROR_FF_CALL("Datatype double expected as input argument \"B\" (2)");
if(mxIsComplex(ptr))
_ERROR_FF_CALL("Expected REAL number as input argument \"B\" (2)");
_B_ =(double *)mxGetPr(ptr);
_B_no_dims_=myGetNumberOfDimensions(ptr);
_B_elements_=mxGetNumberOfElements(ptr);
_B_dims_=mxGetDimensions(ptr);
/* Remove leading iterator dimensions */
{ int n=0,e=1;
for(n=0; ;n++){
if(e==_iter_end_ && _B_no_dims_-n==0)
break;
if( (n>=_B_no_dims_) || (e * _B_dims_[n]>_iter_end_) )
_ERROR_FF_CALL("Can not match size of dimensions for B to iterator");
e *=_B_dims_[n];
}
_B_elements_/=e;
_B_no_dims_-=n;
_B_dims_=&_B_dims_[n];
}
if( _B_elements_!=1 )
_ERROR_FF_CALL("Expected 1 elements as iterator window dims as input argument \"B\" (2)");
}while(0);
do{
/*========CREATE RETURN ARGUMENTS============*/
/* Create return argument: corr */
no_arg_ret=1;
{int di[]={1,1};plhs[0]=mxCreateNumericArray(2,di,mxDOUBLE_CLASS,mxREAL);}
_corr_=(double *)mxGetPr(plhs[0]);
_corr_no_dims_=myGetNumberOfDimensions(plhs[0]);
_corr_elements_=mxGetNumberOfElements(plhs[0]);
_corr_dims_=mxGetDimensions(plhs[0]);
}while(0);
if(no_arg_in!=2 ) mexErrMsgTxt("Wrong number of input variables");
if(no_arg_ret!=1) mexErrMsgTxt("Wrong number of return arguments");
_iter_stop_=_iter_end_;
} /* End argument Init section */
/* THE ELEMENT ITERATOR LOOP 2 1 */
{
register int _iter_i_=0;
#define A _A_[_iter_i_]
#define B _B_[_iter_i_]
#define corr _corr_[0]
#define _continue_this_loop_ _continue_this_loop_2_1_
double elements=0;
double suma=0;
double sumb=0;
double ma=0;
double mb=0;
double a=0;
double b=0;
double sab=0;
double saa=0;
double sbb=0;
elements = prod_size(iter);
for( _iter_i_=_iter_start_; _iter_i_<_iter_stop_; _iter_i_++){
if(_iter_i_+16>=_iter_stop_){
suma = suma+A;
sumb = sumb+B;
}else{
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
_iter_i_++;
suma = suma+A;
sumb = sumb+B;
}
}
_iter_i_=_iter_stop_-1; /*Back to last element for post processing */
ma = suma/elements;
mb = sumb/elements;
for( _iter_i_=_iter_start_; _iter_i_<_iter_stop_; _iter_i_++){
if(_iter_i_+16>=_iter_stop_){
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
}else{
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
_iter_i_++;
a = (A)-ma;
b = (B)-mb;
sab += a*b;
saa += a*a;
sbb += b*b;
}
}
_iter_i_=_iter_stop_-1; /*Back to last element for post processing */
corr = sab/(sqrt(saa*sbb));
#undef _continue_this_loop_
#undef A
#undef B
#undef corr
}
do{ /* Start of breakable section */
if(1)
plhs_IN[0]=plhs[0];
}while(0); /* End of breakable section */
if(ErrStr) mexErrMsgTxt((const char *)ErrStr);
}