comparison Code/Descriptors/yin/private/src/dftoperiod2.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 * dftoperiods.c
3 * estimate period from df
4 *
5 * Alain de Cheveigné, CNRS/Ircam Dec 2001
6 * (c) 2001 CNRS
7 */
8
9 #include <math.h>
10 #include "mex.h"
11
12 /* #define MWRAP */
13 #include "mwrap_check.h"
14
15 /* Input Arguments */
16 #define D_IN prhs[0]
17 #define B_IN prhs[1]
18 #define T_IN prhs[2]
19
20 /* output arguments */
21 #define PRD_OUT plhs[0]
22
23 static void ddftoperiods(
24 double *dp, /* input df */
25 double *bp, /* bounds */
26 double *prdp, /* periods */
27 double thresh, /* threshold */
28 int m, /* nrows of df */
29 int n /* ncols of df */
30 )
31 {
32 int j,k, p, lo, hi, maxj, goodflag;
33 double z, min, globalmin;
34
35 /* bounds */
36 lo= GET(bp[0]);
37 hi= GET(bp[1]);
38 if (lo<0 || hi>m ) {
39 mexPrintf("%d %d\n", lo, hi);
40 mexErrMsgTxt("DFTOPERIOD: bounds are out of bounds");
41 }
42
43 #define MARGIN 1.5
44
45 for (k=0;k<n;k++) { /* for each col */
46
47 goodflag=0;
48
49 /* estimate global min */
50 globalmin=GET(dp[k*m+lo]);
51 for (j=lo;j<hi; j++) { /* for each row */
52 z = GET(dp[k*m+j]);
53 if (z<globalmin) { /* update min & period estimate */
54 globalmin = z;
55 }
56 }
57
58 thresh += globalmin;
59
60 /* estimate period */
61 p=lo;
62 min=GET(dp[k*m+lo]);
63 maxj=hi;
64 for (j=lo;j<maxj; j++) { /* for each row */
65
66 z = GET(dp[k*m+j]);
67
68 if (z<thresh && z <= min) { /* good candidate, restrict search range */
69 maxj=j*MARGIN;
70 if (maxj> hi) { maxj = hi; }
71 }
72
73 if (z<min) { /* update min & period estimate */
74 min = z;
75 p=j;
76 }
77
78 }
79
80 /* mexPrintf("%f %d %f\n", thresh, p, min); */
81
82 SET(prdp[k])=p;
83 }
84
85 return;
86 }
87
88 void mexFunction(
89 int nlhs, mxArray *plhs[],
90 int nrhs, const mxArray *prhs[]
91 )
92 {
93 double *dp, *bp, *tp, *prdp, thresh;
94 int m, n;
95
96 /* Check for proper number of arguments */
97 if (nrhs != 3){
98 mexErrMsgTxt("DFTOPERIOD requires 3 input arguments");
99 }
100
101 /* Check type of input */
102 if (!mxIsNumeric(D_IN) || mxIsComplex(D_IN) ||
103 mxIsSparse(D_IN) || !mxIsDouble(D_IN) ) {
104 mexErrMsgTxt("DFTOPERIOD: D should be doubles");
105 }
106 if (!mxIsNumeric(B_IN) || mxIsComplex(B_IN) ||
107 mxIsSparse(B_IN) || !mxIsDouble(B_IN) ) {
108 mexErrMsgTxt("DFTOPERIOD: B should be doubles");
109 }
110 if (!mxIsNumeric(T_IN) || mxIsComplex(T_IN) ||
111 mxIsSparse(T_IN) || !mxIsDouble(T_IN) ) {
112 mexErrMsgTxt("DFTOPERIOD: T should be double");
113 }
114 m=mxGetM(D_IN); /* rows */
115 n=mxGetN(D_IN); /* columns */
116
117 if (mxGetM(B_IN) * mxGetN(B_IN) != 2) {
118 mexErrMsgTxt("DFTOPERIOD: B should be a size 2 vector");
119 }
120 if (mxGetM(T_IN)*mxGetN(T_IN) != 1) {
121 mexErrMsgTxt("DFTOPERIOD: T should be scalar");
122 }
123
124 /* Create matrix to return */
125 PRD_OUT = mxCreateDoubleMatrix(1,n, mxREAL);
126
127 dp = mxGetPr(D_IN);
128 bp = mxGetPr(B_IN);
129 tp = mxGetPr(T_IN); thresh=tp[0];
130 prdp = mxGetPr(PRD_OUT);
131
132 checkin_matrix((mxArray *) D_IN);
133 checkin_matrix((mxArray *) B_IN);
134 checkin_matrix((mxArray *) PRD_OUT);
135
136 /* Do the actual computations in a subroutine */
137 ddftoperiods(dp,bp,prdp,thresh,m,n);
138
139 checkout_matrix((mxArray *) D_IN);
140 checkout_matrix((mxArray *) B_IN);
141 checkout_matrix((mxArray *) PRD_OUT);
142 return;
143 }
144