comparison Code/Descriptors/yin/private/src/mininrange.c @ 0:ea0c737c6323

first commit
author Dawn Black <dawn.black@eecs.qmul.ac.uk>
date Thu, 26 Jul 2012 14:46:25 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ea0c737c6323
1 /*
2 * minInRange.c
3 * for each sample, return index of the smallest value within an interval
4 * of that sample
5 *
6 * Alain de Cheveigné, CNRS/Ircam Dec 2001
7 * (c) 2001 CNRS
8 */
9
10 #include <math.h>
11 #include "mex.h"
12
13 /* #define MWRAP */
14 #include "mwrap_check.h"
15
16 /* Input Arguments */
17
18 #define X_IN prhs[0]
19 #define INTERVAL_IN prhs[1]
20
21 /* Output Arguments */
22
23 #define IDX_OUT plhs[0]
24
25 static void mininrange(
26 double *xp,
27 double *intp,
28 double *idxp,
29 unsigned int n
30 )
31 {
32 int h,k, idx, left, right, interval;
33 double min;
34 double max;
35
36
37 for (k=0; k<n; k++) {
38 interval=(int) GET(intp[k]);
39
40 left = k - interval/2;
41 right = left+interval;
42
43 if (left<0) left=0; /* clip */
44 if (right>n) right=n;
45
46 min = GET(xp[k]);
47 idx = k;
48 for (h=left;h<right; h++) {
49 if (GET(xp[h]) < min) { /* update min */
50 min = GET(xp[h]);
51 idx = h;
52 }
53 }
54 SET(idxp[k])=idx+1;
55 }
56 return;
57 }
58
59 void mexFunction(
60 int nlhs, mxArray *plhs[],
61 int nrhs, const mxArray *prhs[]
62 )
63 {
64 double *xp, *idxp, *intp;
65 unsigned int m,n;
66
67 /* Check for proper number of arguments */
68
69 if (nrhs != 2) {
70 mexErrMsgTxt("MININRANGE requires two input arguments");
71 } else if (nlhs != 1) {
72 mexErrMsgTxt("MININRANGE requires one output argument");
73 }
74
75 /* Check type of input */
76
77 if (!mxIsNumeric(X_IN) || mxIsComplex(X_IN) ||
78 mxIsSparse(X_IN) || !mxIsDouble(X_IN)) {
79 mexErrMsgTxt("MININRANGE: X should be matrix of doubles");
80 }
81 if (!mxIsNumeric(INTERVAL_IN) || mxIsComplex(INTERVAL_IN) ||
82 mxIsSparse(INTERVAL_IN) || !mxIsDouble(INTERVAL_IN)) {
83 mexErrMsgTxt("MININRANGE X should be matrix of doubles");
84 }
85
86 m = mxGetM(X_IN); /* rows */
87 n = mxGetN(X_IN); /* columns */
88 if (m>1 || n <=1) {
89 mexErrMsgTxt("MININRANGE X should be row vector");
90 }
91 if (m != mxGetM(INTERVAL_IN) || n != mxGetN(INTERVAL_IN)) {
92 mexErrMsgTxt("MININRANGE: INTERVAL should be of same size as X");
93 }
94
95 /* Create matrix to return */
96
97 IDX_OUT = mxCreateDoubleMatrix(1, n, mxREAL);
98
99 /* Assign pointers to the various parameters */
100
101 xp = mxGetPr(X_IN);
102 intp = mxGetPr(INTERVAL_IN);
103 idxp = mxGetPr(IDX_OUT);
104
105 checkin_matrix((mxArray *) X_IN);
106 checkin_matrix((mxArray *) INTERVAL_IN);
107 checkin_matrix(IDX_OUT);
108
109 /* Do the actual computations in a subroutine */
110
111 mininrange(xp,intp,idxp,n);
112
113 checkout_matrix((mxArray *) X_IN);
114 checkout_matrix((mxArray *) INTERVAL_IN);
115 checkout_matrix(IDX_OUT);
116 return;
117 }
118
119