-
Notifications
You must be signed in to change notification settings - Fork 2
/
utilah.h
378 lines (304 loc) · 6.63 KB
/
utilah.h
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
#include <stdio.h>
#include <math.h>
/* --- data structures ---- */
typedef struct
{
double** rptr; /* pointer to 2D double array */
int rows;
int cols;
int newmatrix; /* indicates if data array can be delete */
} matx;
/* ---------------- memory management functions -------- */
double** get_array( double* data, int no_rows, int no_cols)
{
int r=0,c=0;
double** rptr = (double**) malloc(no_cols*sizeof(double*));
for ( c=0; c < no_cols; c++)
{
rptr[c] = (double*)&data[r];
r=r+no_rows;
}
return rptr;
}
matx* mx_from_vector( double* data, int no_rows, int no_cols)
{
int r=0,c=0;
double **matP = (double**) malloc(no_cols*sizeof(double*));
matx* m = (matx *) malloc(sizeof(matx));
for ( c=0; c < no_cols; c++)
{
matP[c] = (double*)&data[r];
r=r+no_rows;
}
m->cols=no_cols; m->rows=no_rows;
m->rptr=matP; m->newmatrix=0; /* false, it is old data */
return m;
}
matx* mx_new(int no_rows, int no_cols)
{
double* data = (double*) malloc( no_rows*no_cols*sizeof(double));
matx* m = mx_from_vector( data, no_rows, no_cols);
m->newmatrix=1; /* true, it is a new matrix */
return m;
}
void mx_free( matx* m)
{
if (m->newmatrix == 1) free( m->rptr[0]);
free(m->rptr); m->rptr=NULL;
free( m);
}
matx* mx_clone(matx* a)
{
matx* m = mx_new( a->rows, a->cols);
register int x,y;
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
m->rptr[x][y]=a->rptr[x][y];
}
return m;
}
/* a=b */
void mx_copy( matx* a, matx* b)
{
register int x,y;
if (a->rows != b->rows || a->cols != b->cols)
{
printf("ERROR in copy_Matx: matrix sizes must agree\n");
return;
}
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
a->rptr[x][y]=b->rptr[x][y];
}
}
/* copy region of size a from (rpos and cpos) in matrix b
rpos, cpos denote centre of the matrix
*/
void mx_copy_region( matx* a, matx* b, int xpos, int ypos)
{
register int x,y;
ypos=ypos-(a->rows/2);
xpos=xpos-(a->cols/2);
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
a->rptr[x][y]=b->rptr[xpos+x][ypos+y];
}
}
void mx_find_peak(matx* a, int* xpos, int* ypos)
{
register int x,y;
float max=-1000000;
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
if (a->rptr[x][y] > max)
{
*ypos=y; *xpos=x; max=a->rptr[x][y];
}
}
}
void mx_centroid(matx* a, int* xpos, int* ypos)
{
register int x,y;
double xdx=0.0, ydy=0.0, sum=0.0, val;
for (x=0; x < a->cols; x++)
{
for (y=0; y < a->rows; y++)
{
val=a->rptr[x][y];
xdx=xdx+(x+1)*val;
ydy=ydy+(y+1)*val;
sum=sum+val;
}
}
if (sum==0) { *xpos=a->cols/2; *ypos=a->rows/2;}
else
{
*ypos=(int)((ydy/sum)-0.5); *xpos=(int)((xdx/sum)-0.5);
}
}
void mx_print(matx* m)
{
int x,y;
for (y=0; y < m->rows; y++)
{
for (x=0; x < m->cols; x++)
{
printf(" %f", (float)m->rptr[x][y]);
}
printf("\n");
}
}
/* ------------ simple statistics functions ------------ */
double mx_min(matx* a)
{
register int x,y;
double _min=1000000.0;
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
if (a->rptr[x][y] < _min) _min=a->rptr[x][y];
}
return _min;
}
double mx_max(matx* a)
{
register int x,y;
double _max=-1000000.0;
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
if (a->rptr[x][y] > _max) _max=a->rptr[x][y];
}
return _max;
}
double mx_mean(matx* a)
{
register int x,y;
double sum=0.0;
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
sum=sum+a->rptr[x][y];
}
return sum/(a->rows*a->cols);
}
double mx_std(matx* a)
{
register int x,y;
double sum=0.0;
double m=mx_mean(a);
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
sum=sum+((a->rptr[x][y]-m)*(a->rptr[x][y]-m));
}
return sqrt(sum/(a->rows*a->cols));
}
void mx_abs(matx* a)
{
register int x,y;
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
a->rptr[x][y] = fabs( a->rptr[x][y]);
}
void mx_positive(matx* a)
{
register int x,y;
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
if (a->rptr[x][y] < 0) a->rptr[x][y]=0;
}
/* normalise between 0..1 */
void mx_normalise(matx* a)
{
register int x,y;
double _max=mx_max(a), _min=mx_min(a);
double s= 1/(_max-_min);
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
a->rptr[x][y]=(a->rptr[x][y]+_min)*s;
}
}
double mx_find(matx* a, double value)
{
register int x,y;
double sum=0.0;
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
if (a->rptr[x][y]==value) sum=sum+1;
}
return sum;
}
void mx_clear(matx* a)
{
register int x,y;
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
a->rptr[x][y]=0.0;
}
}
/* ---- Simple Arithmatic ----------- */
/* a=a+b */
void mx_add_matrix(matx* a, matx* b)
{
register int x,y;
if (a->rows != b->rows || a->cols != b->cols)
{
printf("ERROR in add_matrix: Matrix dimensions must agree\n");
return;
}
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
a->rptr[x][y]=a->rptr[x][y]+b->rptr[x][y];
}
}
/* a=a+s */
void mx_add_scalar(matx* a, double s)
{
register int x,y;
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
a->rptr[x][y]=a->rptr[x][y]+s;
}
}
/* a=a-b; */
void mx_sub_matrix(matx* a, matx* b)
{
register int x,y;
if (a->rows != b->rows || a->cols != b->cols)
{
printf("ERROR in sub_Matx: Matx sizes must agree\n");
return;
}
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
a->rptr[x][y]=a->rptr[x][y]-b->rptr[x][y];
}
}
/* a=a-s */
void mx_sub_scalar(matx* a, double s)
{
register int x,y;
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
a->rptr[x][y]=a->rptr[x][y]-s;
}
}
/* a=a*s */
void mx_mult_scalar( matx* a, double s)
{
register int x,y;
for (x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
a->rptr[x][y]=s*a->rptr[x][y];
}
}
/* a = corr(a,b) */
double mx_corr(matx* a, matx* b)
{
register int x,y;
double meanA, meanB, sumAB=0.0, sumA2=0.0, sumB2=0.0;
if (a->rows != b->rows || a->cols != b->cols) return 0.0;
meanA = mx_mean(a);
meanB= mx_mean(b);
for(x=0; x < a->cols; x++)
for (y=0; y < a->rows; y++)
{
sumAB=sumAB+((a->rptr[x][y]-meanA)*(b->rptr[x][y]-meanB));
sumA2=sumA2+((a->rptr[x][y]-meanA)*(a->rptr[x][y]-meanA));
sumB2=sumB2+((b->rptr[x][y]-meanB)*(b->rptr[x][y]-meanB));
}
return sumAB/(sqrt(sumA2*sumB2));
}