-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindlocalmax.c
291 lines (253 loc) · 7.24 KB
/
findlocalmax.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
#include"mex.h"
#include<stdlib.h>
#include"mex.h"
#include<math.h>
///////////////////////////////////////////////////////////
#undef M_PI
#define M_PI 3.14159265358979
int uIsRealScalar(const mxArray* A)
{
return
mxIsDouble(A) &&
!mxIsComplex(A) &&
mxGetNumberOfDimensions(A) == 2 &&
mxGetM(A) == 1 &&
mxGetN(A) == 1 ;
}
int uIsRealMatrix(const mxArray* A, int M, int N)
{
return
mxIsDouble(A) &&
!mxIsComplex(A) &&
mxGetNumberOfDimensions(A) == 2 &&
((M>=0)?(mxGetM(A) == M):1) &&
((N>=0)?(mxGetN(A) == N):1) ;
}
int uIsRealVector(const mxArray* V, int D)
{
int M = mxGetM(V) ;
int N = mxGetN(V) ;
int is_vector = (N == 1) || (M == 1) ;
return
mxIsDouble(V) &&
!mxIsComplex(V) &&
mxGetNumberOfDimensions(V) == 2 &&
is_vector &&
( D < 0 || N == D || M == D) ;
}
int uIsString(const mxArray* S, int L)
{
int M = mxGetM(S) ;
int N = mxGetN(S) ;
return
mxIsChar(S) &&
M == 1 &&
(L < 0 || N == L) ;
}
////////////////////////////////////////////////////////////
/** Matlab driver.
**/
#define greater(a,b) ((a) > (b)+threshold)
void
mexFunction(int nout, mxArray *out[],
int nin, const mxArray *in[])
{
int M, N ;
const double* F_pt ;
int ndims ;
int pdims = -1 ;
int* offsets ;
int* midx ;
int* neighbors ;
int nneighbors ;
int* dims ;
enum {F=0,THRESHOLD,P} ;
enum {MAXIMA=0} ;
double threshold = - mxGetInf() ;
/* ------------------------------------------------------------------
* Check the arguments
* --------------------------------------------------------------- */
if (nin < 1) {
mexErrMsgTxt("At least one input argument is required.");
} else if (nin > 3) {
mexErrMsgTxt("At most three arguments are allowed.") ;
} else if (nout > 1) {
mexErrMsgTxt("Too many output arguments");
}
/* The input must be a real matrix. */
if (!mxIsDouble(in[F]) || mxIsComplex(in[F])) {
mexErrMsgTxt("Input must be real matrix.");
}
if(nin > 1) {
if(!uIsRealScalar(in[THRESHOLD])) {
mexErrMsgTxt("THRESHOLD must be a real scalar.") ;
}
threshold = *mxGetPr(in[THRESHOLD]) ;
}
if(nin > 2) {
if(!uIsRealScalar(in[P]))
mexErrMsgTxt("P must be a non-negative integer") ;
pdims = (int) *mxGetPr(in[P]) ;
if(pdims < 0)
mexErrMsgTxt("P must be a non-negative integer") ;
}
ndims = mxGetNumberOfDimensions(in[F]) ;
{
/* We need to make a copy because in one special case (see below)
we need to adjust dims[].
*/
int d ;
const int* const_dims = (int*) mxGetDimensions(in[F]) ;
dims = mxMalloc(sizeof(int)*ndims) ;
for(d=0 ; d < ndims ; ++d) dims[d] = const_dims[d] ;
}
M = dims[0] ;
N = dims[1] ;
F_pt = mxGetPr(in[F]) ;
/*
If there are only two dimensions and if one is singleton, then
assume that a vector has been provided as input (and treat this
as a COLUMN matrix with p=1). We do this because Matlab does not
distinguish between vectors and 1xN or Mx1 matrices and because
the cases 1xN and Mx1 are trivial (the result is alway empty).
*/
if((ndims == 2) && (pdims < 0) && (M == 1 || N == 1)) {
pdims = 1 ;
M = (M>N)?M:N ;
N = 1 ;
dims[0]=M ;
dims[1]=N ;
}
/* search the local maxima along the first p dimensions only */
if(pdims < 0)
pdims = ndims ;
if(pdims > ndims) {
mxFree(dims) ;
mexErrMsgTxt("P must not be greater than the number of dimensions") ;
}
/* ------------------------------------------------------------------
* Do the job
* --------------------------------------------------------------- */
{
int maxima_size = M*N ;
int* maxima_start = (int*) mxMalloc(sizeof(int) * maxima_size) ;
int* maxima_iterator = maxima_start ;
int* maxima_end = maxima_start + maxima_size ;
int i,h,o ;
const double* pt = F_pt ;
/* Compute the offsets between dimensions. */
offsets = (int*) mxMalloc(sizeof(int) * ndims) ;
offsets[0] = 1 ;
for(h = 1 ; h < ndims ; ++h)
offsets[h] = offsets[h-1]*dims[h-1] ;
/* Multi-index. */
midx = (int*) mxMalloc(sizeof(int) * ndims) ;
for(h = 0 ; h < ndims ; ++h)
midx[h] = 1 ;
/* Neighbors. */
nneighbors = 1 ;
o=0 ;
for(h = 0 ; h < pdims ; ++h) {
nneighbors *= 3 ;
midx[h] = -1 ;
o -= offsets[h] ;
}
nneighbors -= 1 ;
neighbors = (int*) mxMalloc(sizeof(int) * nneighbors) ;
/* Precompute offsets from offset(-1,...,-1,0,...0) to
* offset(+1,...,+1,0,...,0). */
i = 0 ;
while(true) {
if(o != 0)
neighbors[i++] = o ;
h = 0 ;
while( o += offsets[h], (++midx[h]) > 1 ) {
o -= 3*offsets[h] ;
midx[h] = -1 ;
if(++h >= pdims)
goto stop ;
}
}
stop: ;
/* Starts at the corner (1,1,...,1,0,0,...0) */
for(h = 0 ; h < pdims ; ++h) {
midx[h] = 1 ;
pt += offsets[h] ;
}
for(h = pdims ; h < ndims ; ++h) {
midx[h] = 0 ;
}
/* ---------------------------------------------------------------
* Loop
* ------------------------------------------------------------ */
/*
If any dimension in the first P is less than 3 elements wide
then just return the empty matrix (if we proceed without doing
anything we break the carry reporting algorithm below).
*/
for(h=0 ; h < pdims ; ++h)
if(dims[h] < 3) goto end ;
while(true) {
/* Propagate carry along multi index midx */
h = 0 ;
while((midx[h]) >= dims[h] - 1) {
pt += 2*offsets[h] ; /* skip first and last el. */
midx[h] = 1 ;
if(++h >= pdims)
goto next_layer ;
++midx[h] ;
}
/* Scan neighbors */
{
double v = *pt ;
bool is_greater = (v >= threshold) ;
i = 0 ;
while(is_greater && i < nneighbors)
is_greater &= v > *(pt + neighbors[i++]) ;
/* Add the local maximum */
if(is_greater) {
/* Need more space? */
if(maxima_iterator == maxima_end) {
maxima_size += M*N ;
maxima_start = (int*) mxRealloc(maxima_start,
maxima_size*sizeof(int)) ;
maxima_end = maxima_start + maxima_size ;
maxima_iterator = maxima_end - M*N ;
}
*maxima_iterator++ = pt - F_pt + 1 ;
}
/* Go to next element */
pt += 1 ;
++midx[0] ;
continue ;
next_layer: ;
if( h >= ndims )
goto end ;
while((++midx[h]) >= dims[h]) {
midx[h] = 0 ;
if(++h >= ndims)
goto end ;
}
}
}
end:;
/* Return. */
{
double* M_pt ;
out[MAXIMA] = mxCreateDoubleMatrix
(1, maxima_iterator-maxima_start, mxREAL) ;
maxima_end = maxima_iterator ;
maxima_iterator = maxima_start ;
M_pt = mxGetPr(out[MAXIMA]) ;
while(maxima_iterator != maxima_end) {
*M_pt++ = *maxima_iterator++ ;
}
}
/* Release space. */
mxFree(offsets) ;
mxFree(neighbors) ;
mxFree(midx) ;
mxFree(maxima_start) ;
}
mxFree(dims) ;
}