| 1 |
/* |
| 2 |
//------------------------------------------------------------------- |
| 3 |
// C-MEX implementation of ACCOVF - this function is part of the NaN-toolbox. |
| 4 |
// |
| 5 |
// |
| 6 |
// This program is free software; you can redistribute it and/or modify |
| 7 |
// it under the terms of the GNU General Public License as published by |
| 8 |
// the Free Software Foundation; either version 3 of the License, or |
| 9 |
// (at your option) any later version. |
| 10 |
// |
| 11 |
// This program is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
// |
| 16 |
// You should have received a copy of the GNU General Public License |
| 17 |
// along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
// |
| 19 |
// |
| 20 |
// accovf: auto- and crosscovariance function |
| 21 |
// usage: |
| 22 |
// [Sxx,Nxx,Sxy,Nxy,lag] = accovf(X,Y,maxlag); |
| 23 |
// [... ] = accovf(X,Y,maxlag,tix); |
| 24 |
// [Sxx,Nxx,lag] = accovf(X,maxlag,tix); |
| 25 |
// [Sxx,Nxx,lag] = accovf(X,maxlag); |
| 26 |
// Sxx./Nxx : auto-covariance function |
| 27 |
// Sxy./Nxy : cross-covariance function |
| 28 |
|
| 29 |
The purpose is speeding up these computations: |
| 30 |
|
| 31 |
for k = -maxlag : maxlag, |
| 32 |
Rxy(k+1+maxlag) = mean(X(ixtrain-k) .* Y(ixtrain)); |
| 33 |
Rxx(k+1+maxlag) = mean(X(ixtrain-k) .* X(ixtrain)); |
| 34 |
end; |
| 35 |
|
| 36 |
|
| 37 |
// Copyright (C) 2022 Alois Schloegl <[email protected]> |
| 38 |
// This function is part of the NaN-toolbox |
| 39 |
// http://pub.ist.ac.at/~schloegl/matlab/NaN/ |
| 40 |
// |
| 41 |
//------------------------------------------------------------------- |
| 42 |
*/ |
| 43 |
|
| 44 |
#ifdef __GNUC__ |
| 45 |
#include <stdint.h> |
| 46 |
#endif |
| 47 |
#include <math.h> |
| 48 |
#include <stdio.h> |
| 49 |
#include <string.h> |
| 50 |
|
| 51 |
#include "mex.h" |
| 52 |
// #include "matrix.h" |
| 53 |
|
| 54 |
/*#define NO_FLAG*/ |
| 55 |
|
| 56 |
/* |
| 57 |
math.h has isnan() defined for all sizes of floating point numbers, |
| 58 |
but c++ assumes isnan(double), causing possible conversions for float and long double |
| 59 |
*/ |
| 60 |
#define ISNAN(a) (a!=a) |
| 61 |
|
| 62 |
#ifndef typeof |
| 63 |
#define typeof __typeof__ |
| 64 |
#endif |
| 65 |
|
| 66 |
void mexFunction(int POutputCount, mxArray* POutput[], int PInputCount, const mxArray *PInputs[]) { |
| 67 |
|
| 68 |
double *X0=NULL, *Y0=NULL, *W=NULL; |
| 69 |
double *CC; |
| 70 |
double *NN = NULL; |
| 71 |
|
| 72 |
size_t rX,cX,rY,cY; |
| 73 |
size_t i; |
| 74 |
char flag_isNaN = 0; |
| 75 |
int ACC_LEVEL; |
| 76 |
|
| 77 |
/*********** check input arguments *****************/ |
| 78 |
|
| 79 |
// check for proper number of input and output arguments |
| 80 |
if ((PInputCount <= 0) || (PInputCount > 4)) { |
| 81 |
mexPrintf("usage:\n\t[Sxx,Nxx,Sxy,Nxy,lag] = accovf_mex(X, Y, lag)\n"); |
| 82 |
mexPrintf( "\t[Sxx,Nxx,Sxy,Nxy,lag] = accovf_mex(X, Y, lag, tix)\n"); |
| 83 |
// mexPrintf("\t[Sxx,Nxx] = accovf_mex(X,lag)\n"); |
| 84 |
// mexPrintf("\t[Sxx,Nxx] = accovf_mex(X,lag,tix)\n"); |
| 85 |
return; |
| 86 |
} |
| 87 |
if (POutputCount > 5) |
| 88 |
mexErrMsgTxt("accovf_mex.mex has no more then 5 output arguments."); |
| 89 |
|
| 90 |
// get 1st argument |
| 91 |
if(mxIsDouble(PInputs[0]) && !mxIsComplex(PInputs[0]) && !mxIsSparse(PInputs[0]) ) |
| 92 |
X0 = mxGetPr(PInputs[0]); |
| 93 |
else |
| 94 |
mexErrMsgTxt("First argument must be non-sparse REAL/DOUBLE."); |
| 95 |
|
| 96 |
// get 2nd argument |
| 97 |
if (mxIsDouble(PInputs[1]) && !mxIsComplex(PInputs[1]) && !mxIsSparse(PInputs[1])) |
| 98 |
Y0 = mxGetPr(PInputs[1]); |
| 99 |
else |
| 100 |
mexErrMsgTxt("Second argument must be non-sparse REAL/DOUBLE."); |
| 101 |
|
| 102 |
mwSize MAXLAG=1000; // DEFAULT |
| 103 |
rX = mxGetM(PInputs[0]); |
| 104 |
cX = mxGetN(PInputs[0]); |
| 105 |
rY = mxGetM(PInputs[1]); |
| 106 |
cY = mxGetN(PInputs[1]); |
| 107 |
int argix_lag=2; //default |
| 108 |
double *tix = NULL; |
| 109 |
if (rY==1 && cY==1) { |
| 110 |
Y0 = NULL; |
| 111 |
rY = 0; |
| 112 |
cY = 0; |
| 113 |
argix_lag=1; |
| 114 |
} |
| 115 |
if (PInputCount > argix_lag) { |
| 116 |
MAXLAG = (long)(*(double*)mxGetPr(PInputs[argix_lag])); |
| 117 |
} |
| 118 |
|
| 119 |
if (cY>0 && rX != rY) |
| 120 |
mexErrMsgTxt("number of rows in X and Y do not match"); |
| 121 |
if (cX != 1 || cY > 1) |
| 122 |
mexErrMsgTxt("number of columns must be 1"); |
| 123 |
|
| 124 |
if (PInputCount > (argix_lag+1)) { |
| 125 |
tix = (double*)mxGetPr(PInputs[argix_lag+1]); |
| 126 |
rY = mxGetM(PInputs[argix_lag+1]); |
| 127 |
rY *= mxGetN(PInputs[argix_lag+1]); |
| 128 |
} |
| 129 |
|
| 130 |
/*********** create output arguments *****************/ |
| 131 |
const mwSize DIMS[2] = {2*MAXLAG+1, 1}; |
| 132 |
|
| 133 |
POutput[0] = mxCreateDoubleMatrix(DIMS[0], DIMS[1], mxREAL); |
| 134 |
POutput[1] = mxCreateDoubleMatrix(DIMS[0], DIMS[1], mxREAL); |
| 135 |
double *Sxx = mxGetPr(POutput[0]); |
| 136 |
double *Nxx = mxGetPr(POutput[1]); |
| 137 |
|
| 138 |
POutput[2] = mxCreateDoubleMatrix(DIMS[0], DIMS[1], mxREAL); |
| 139 |
POutput[3] = mxCreateDoubleMatrix(DIMS[0], DIMS[1], mxREAL); |
| 140 |
POutput[4] = mxCreateDoubleMatrix(DIMS[0], DIMS[1], mxREAL); |
| 141 |
|
| 142 |
double *Sxy = mxGetPr(POutput[2]); |
| 143 |
double *Nxy = mxGetPr(POutput[3]); |
| 144 |
double *LAG = mxGetPr(POutput[4]); |
| 145 |
|
| 146 |
for (long k=-MAXLAG; k<=MAXLAG; k++) { |
| 147 |
size_t ix = k+MAXLAG; |
| 148 |
Sxx[ix] = 0.0; |
| 149 |
Nxx[ix] = 0; |
| 150 |
Sxy[ix] = 0.0; |
| 151 |
Nxy[ix] = 0; |
| 152 |
LAG[ix] = k; |
| 153 |
} |
| 154 |
|
| 155 |
uint32_t *nxx = (uint32_t*)mxMalloc((2*MAXLAG+1)*sizeof(uint32_t)); |
| 156 |
uint32_t *nxy = (uint32_t*)mxMalloc((2*MAXLAG+1)*sizeof(uint32_t)); |
| 157 |
memset(nxx,0,(2*MAXLAG+1)*sizeof(uint32_t)); |
| 158 |
memset(nxy,0,(2*MAXLAG+1)*sizeof(uint32_t)); |
| 159 |
|
| 160 |
if (tix==NULL) { |
| 161 |
// [Sxx,Nxx,Sxy,Nxy,lag] = accovf(X,Y,maxlag); |
| 162 |
for (size_t t=0; t < rX; t++) { |
| 163 |
if ( (t % (1<<30) == 0) ) { |
| 164 |
for (long k=0; k<=2*MAXLAG; k++) { |
| 165 |
Nxx[k] += nxx[k]; |
| 166 |
Nxy[k] += nxy[k]; |
| 167 |
} |
| 168 |
memset(nxx,0,(2*MAXLAG+1)*sizeof(uint32_t)); |
| 169 |
memset(nxy,0,(2*MAXLAG+1)*sizeof(uint32_t)); |
| 170 |
} |
| 171 |
|
| 172 |
for (long k=0; k<=2*MAXLAG; k++) { |
| 173 |
long k0 = k-MAXLAG; |
| 174 |
if ( (t-k0) < 0) continue; |
| 175 |
if ( (t-k0) >= rX ) continue; |
| 176 |
double a = X0[t-k0] * X0[t]; |
| 177 |
double b = X0[t-k0] * Y0[t]; |
| 178 |
if (a==a) { |
| 179 |
Sxx[k] += a; |
| 180 |
nxx[k] += 1; |
| 181 |
} |
| 182 |
if (b==b) { |
| 183 |
Sxy[k] += b; |
| 184 |
nxy[k] += 1; |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
else { |
| 190 |
// [Sxx,Nxx,Sxy,Nxy,lag] = accovf(X,Y,maxlag,tix); |
| 191 |
for (size_t l=0; l < rY; l++) { |
| 192 |
if ( (l % (1<<30) == 0) ) { |
| 193 |
for (long k=0; k<=2*MAXLAG; k++) { |
| 194 |
Nxx[k] += nxx[k]; |
| 195 |
Nxy[k] += nxy[k]; |
| 196 |
} |
| 197 |
memset(nxx,0,(2*MAXLAG+1)*sizeof(uint32_t)); |
| 198 |
memset(nxy,0,(2*MAXLAG+1)*sizeof(uint32_t)); |
| 199 |
} |
| 200 |
|
| 201 |
size_t t = (size_t)tix[l]; |
| 202 |
for (long k=0; k<=2*MAXLAG; k++) { |
| 203 |
long k0 = k-MAXLAG; |
| 204 |
if ( (t-k0) < 0) continue; |
| 205 |
if ( (t-k0) >= rX ) continue; |
| 206 |
double a = X0[t-k0] * X0[t]; |
| 207 |
double b = X0[t-k0] * Y0[t]; |
| 208 |
if (a==a) { |
| 209 |
Sxx[k] += a; |
| 210 |
nxx[k] += 1; |
| 211 |
} |
| 212 |
if (b==b) { |
| 213 |
Sxy[k] += b; |
| 214 |
nxy[k] += 1; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
for (long k=0; k<=2*MAXLAG; k++) { |
| 221 |
Nxx[k] += nxx[k]; |
| 222 |
Nxy[k] += nxy[k]; |
| 223 |
} |
| 224 |
if (nxx) mxFree(nxx); |
| 225 |
if (nxy) mxFree(nxy); |
| 226 |
|
| 227 |
} |
| 228 |
|