-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdiff_mex.c
72 lines (64 loc) · 2.1 KB
/
diff_mex.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
/**
To compile, run from matlab: mex diff_mex.c
---------------------------------------------------------------------------------
Synchrosqueezing Toolbox
Authors: Eugene Brevdo (http://www.math.princeton.edu/~ebrevdo/),
Hau-Tieng Wu
---------------------------------------------------------------------------------
*/
#include <math.h>
#include "mex.h"
#include "matrix.h"
/**
dW = diff_mex(W, dt, dorder)
**/
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
size_t ai, bi, k, N, na;
double dt;
int dorder;
double *Wr, *Wi, *dWr, *dWi;
na = mxGetM(prhs[0]);
N = mxGetN(prhs[0]);
Wr = mxGetPr(prhs[0]);
Wi = mxGetPi(prhs[0]);
dt = mxGetScalar(prhs[1]);
dorder = (int)mxGetScalar(prhs[2]);
if (Wi != NULL)
plhs[0] = mxCreateDoubleMatrix(na, N, mxCOMPLEX);
else
plhs[0] = mxCreateDoubleMatrix(na, N, mxREAL);
if (plhs[0] == NULL)
mexErrMsgTxt("Error allocating output matrix Tx");
dWr = mxGetPr(plhs[0]);
if (Wi != NULL)
dWi = mxGetPi(plhs[0]);
/* Take derivatives of a certain order, in the column direction */
if (dorder == 1) /* 1st order Forward Difference */
for (bi = 0; bi < N-1; ++bi) {
for (ai = 0; ai < na; ++ai) {
dWr[na*bi+ai] = (Wr[na*(bi+1)+ai] - Wr[na*(bi)+ai])/dt;
if (Wi != NULL)
dWi[na*bi+ai] = (Wi[na*(bi+1)+ai] - Wi[na*(bi)+ai])/dt;
}
}
else if (dorder == 2) /* 2nd order Forward Difference */
for (bi = 0; bi < N-2; ++bi)
for (ai = 0; ai < na; ++ai) {
dWr[na*bi+ai] = (4*Wr[na*(bi+1)+ai] - Wr[na*(bi+2)+ai] - 3*Wr[na*bi+ai])/(2*dt);
if (Wi != NULL)
dWi[na*bi+ai] = (4*Wi[na*(bi+1)+ai] - Wi[na*(bi+2)+ai] - 3*Wi[na*bi+ai])/(2*dt);
}
else if (dorder == 4) /* 4th order Central Difference */
for (bi = 2; bi < N-2; ++bi)
for (ai = 0; ai < na; ++ai) {
dWr[na*bi+ai] = (-Wr[na*(bi+2)+ai] + 8*Wr[na*(bi+1)+ai]
-8*Wr[na*(bi-1)+ai] + Wr[na*(bi-2)+ai])/(12*dt);
if (Wi != NULL)
dWi[na*bi+ai] = (-Wi[na*(bi+2)+ai] + 8*Wi[na*(bi+1)+ai]
-8*Wi[na*(bi-1)+ai] + Wi[na*(bi-2)+ai])/(12*dt);
}
else
mexErrMsgTxt("Unknown dorder");
}