0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 /*
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 Mex file that implements polynomial detrending of a data vector.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 M Hewitson 5-02-08
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 $Id: ltpda_polyreg.c,v 1.4 2009/12/17 08:02:08 mauro Exp $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 #include <math.h>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 #include <stdio.h>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 #include <stdlib.h>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 #include <string.h>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 #include <mex.h>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 #include "ltpda_polyreg.h"
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 #include "version.h"
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18 #include "../c_sources/polyreg.c"
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20 #define DEBUG 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 /*
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 Matlab mex file to make polynomial detrending of a data vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 * Inputs
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 * - data
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 * - N
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 *
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 function [y,a] = ltpda_polyreg(x, N);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 void mexFunction( int nlhs, mxArray *plhs[],
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 int nrhs, const mxArray *prhs[])
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 /* Parse inputs */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 if( (nrhs == 0) && (nlhs == 0) )
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 print_usage(VERSION);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 else if ( (nrhs == 2) && (nlhs >= 1) ) /* let's go */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 double *xdata;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 long int nData;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 int order;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 double *yptr, *aptr;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 /* Extract inputs */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 xdata = mxGetPr(prhs[0]); /* Pointer to data */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 nData = mxGetNumberOfElements(prhs[0]); /* Number of data points */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 order = (int)mxGetScalar(prhs[1]); /* Order of detrending */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 if (order > 10 || order < -1)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 mexErrMsgTxt("Detrending order must be between -1 and 10");
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57 /* Set output matrices */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 plhs[0] = mxCreateDoubleMatrix(nData, 1, mxREAL);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 /* Get pointers to output matrices */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 yptr = mxGetPr(plhs[0]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 if (nlhs == 2)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 plhs[1] = mxCreateDoubleMatrix(order+1, 1, mxREAL);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 aptr = mxGetPr(plhs[1]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 aptr = (double*)mxCalloc(order+1, sizeof(double)); /* detrending output */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
72 /* Detrend segment */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
73 switch (order)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
74 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
75 case -1:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
76 /* no detrending */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
77 memcpy(yptr, xdata, nData*sizeof(double));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
78 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
79 case 0:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
80 /* mean removal */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
81 polyreg0 (xdata, nData, yptr, aptr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
82 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
83 case 1:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
84 /* linear detrending */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
85 polyreg1 (xdata, nData, yptr, aptr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
86 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
87 case 2:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
88 /* 2nd order detrending */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
89 polyreg2 (xdata, nData, yptr, aptr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
90 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
91 case 3:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
92 /* 3rd order detrending */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
93 polyreg3 (xdata, nData, yptr, aptr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
94 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
95 case 4:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
96 /* 4th order detrending */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
97 polyreg4 (xdata, nData, yptr, aptr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
98 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
99 case 5:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
100 /* 5th order detrending */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
101 polyreg5 (xdata, nData, yptr, aptr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
102 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
103 case 6:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
104 /* 6th order detrending */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
105 polyreg6 (xdata, nData, yptr, aptr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
106 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
107 case 7:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
108 /* 7th order detrending */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
109 polyreg7 (xdata, nData, yptr, aptr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
110 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
111 case 8:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
112 /* 8th order detrending */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
113 polyreg8 (xdata, nData, yptr, aptr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
114 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
115 case 9:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
116 /* 9th order detrending */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
117 polyreg9 (xdata, nData, yptr, aptr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
118 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
119 case 10:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
120 /* 10th order detrending */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
121 polyreg10(xdata, nData, yptr, aptr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
122 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
123 }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
124
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
125 /*// if (nlhs == 2)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
126 // {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
127 // // Free coefficient array
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
128 // mxFree(aptr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
129 // }*/
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
130 }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
131 else /* we have an error */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
132 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
133 print_usage(VERSION);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
134 mexErrMsgTxt("### incorrect usage");
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
135 }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
136 }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
137
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
138 /*
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
139 * Output usage to MATLAB terminal
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
140 *
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
141 */
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
142 void print_usage(char *version)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
143 {
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
144 mexPrintf("ltpda_polyreg version %s\n", version);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
145 mexPrintf(" usage: function [y, a] = ltpda_polyreg(x, order); \n");
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
146 }
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
147
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
148
|