| 1 |
/* |
| 2 |
|
| 3 |
Copyright (C) 2011,2013,2015,2022,2024,2025 Alois Schloegl <[email protected]> |
| 4 |
This file is part of the "BioSig for C/C++" repository |
| 5 |
(biosig4c++) at http://biosig.sf.net/ |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or |
| 8 |
modify it under the terms of the GNU General Public License |
| 9 |
as published by the Free Software Foundation; either version 3 |
| 10 |
of the License, or (at your option) any later version. |
| 11 |
|
| 12 |
*/ |
| 13 |
|
| 14 |
#include "mex.h" |
| 15 |
#include <ctype.h> |
| 16 |
#include <math.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
#include <time.h> |
| 20 |
|
| 21 |
#include <biosig-dev.h> |
| 22 |
#include <biosig.h> |
| 23 |
|
| 24 |
|
| 25 |
#ifdef NDEBUG |
| 26 |
#define VERBOSE_LEVEL 0 // turn off debugging information, but its only used without NDEBUG |
| 27 |
#else |
| 28 |
extern int VERBOSE_LEVEL; // used for debugging |
| 29 |
#endif |
| 30 |
|
| 31 |
#ifdef tmwtypes_h |
| 32 |
#if (MX_API_VER<=0x07020000) |
| 33 |
typedef int mwSize; |
| 34 |
#endif |
| 35 |
#endif |
| 36 |
|
| 37 |
static const double EPS = 2.220446049250313e-16; // 0x1p-52; /* about 2.220e-16 */ |
| 38 |
|
| 39 |
double getDouble(const mxArray *pm, size_t idx) { |
| 40 |
size_t n = mxGetNumberOfElements(pm); |
| 41 |
if (n == 0) return(NAN); |
| 42 |
if (n <= idx) idx = n-1; |
| 43 |
|
| 44 |
switch (mxGetClassID(pm)) { |
| 45 |
case mxCHAR_CLASS: |
| 46 |
case mxLOGICAL_CLASS: |
| 47 |
case mxINT8_CLASS: |
| 48 |
return(*((int8_t*)mxGetData(pm) + idx)); |
| 49 |
case mxUINT8_CLASS: |
| 50 |
return(*((uint8_t*)mxGetData(pm) + idx)); |
| 51 |
case mxDOUBLE_CLASS: |
| 52 |
return(*((double*)mxGetData(pm) + idx)); |
| 53 |
case mxSINGLE_CLASS: |
| 54 |
return(*((float*)mxGetData(pm) + idx)); |
| 55 |
case mxINT16_CLASS: |
| 56 |
return(*((int16_t*)mxGetData(pm) + idx)); |
| 57 |
case mxUINT16_CLASS: |
| 58 |
return(*((uint16_t*)mxGetData(pm) + idx)); |
| 59 |
case mxINT32_CLASS: |
| 60 |
return(*((int32_t*)mxGetData(pm) + idx)); |
| 61 |
case mxUINT32_CLASS: |
| 62 |
return(*((uint32_t*)mxGetData(pm) + idx)); |
| 63 |
case mxINT64_CLASS: |
| 64 |
return(*((int64_t*)mxGetData(pm) + idx)); |
| 65 |
case mxUINT64_CLASS: |
| 66 |
return(*((uint64_t*)mxGetData(pm) + idx)); |
| 67 |
/* |
| 68 |
case mxFUNCTION_CLASS: |
| 69 |
case mxUNKNOWN_CLASS: |
| 70 |
case mxCELL_CLASS: |
| 71 |
case mxSTRUCT_CLASS: |
| 72 |
*/ |
| 73 |
default: |
| 74 |
return(NAN); |
| 75 |
} |
| 76 |
return(NAN); |
| 77 |
} |
| 78 |
|
| 79 |
void mexFunction( |
| 80 |
int nlhs, /* number of expected outputs */ |
| 81 |
mxArray *plhs[], /* array of pointers to output arguments */ |
| 82 |
int nrhs, /* number of inputs */ |
| 83 |
const mxArray *prhs[] /* array of pointers to input arguments */ |
| 84 |
) |
| 85 |
|
| 86 |
{ |
| 87 |
int k; |
| 88 |
const mxArray *arg; |
| 89 |
HDRTYPE *hdr; |
| 90 |
size_t count; |
| 91 |
time_t T0; |
| 92 |
char *FileName; |
| 93 |
char tmpstr[128]; |
| 94 |
int CHAN = 0; |
| 95 |
double *ChanList=NULL; |
| 96 |
int NS = -1; |
| 97 |
char FlagOverflowDetection = -1, FlagUCAL = -1; |
| 98 |
void *data = NULL; |
| 99 |
mxArray *p = NULL, *p1 = NULL, *p2 = NULL; |
| 100 |
uint16_t gdftyp = 0; |
| 101 |
int istatus; |
| 102 |
double *status; |
| 103 |
|
| 104 |
#ifdef CHOLMOD_H |
| 105 |
cholmod_sparse RR,*rr=NULL; |
| 106 |
double dummy; |
| 107 |
#endif |
| 108 |
|
| 109 |
// ToDO: output single data |
| 110 |
// mxClassId FlagMXclass=mxDOUBLE_CLASS; |
| 111 |
|
| 112 |
if (nrhs<1) { |
| 113 |
mexPrintf(" Usage of mexSSAVE:\n"); |
| 114 |
mexPrintf("\tstatus=mexSSAVE(HDR,data)\n"); |
| 115 |
mexPrintf(" Input:\n\tHDR\tHeader structure \n"); |
| 116 |
mexPrintf("\tdata\tdata matrix, one channel per column\n"); |
| 117 |
mexPrintf("\tHDR\theader structure\n\n"); |
| 118 |
mexPrintf("\tstatus 0: file saves successfully\n\n"); |
| 119 |
mexPrintf("\tstatus <>0: file could not saved\n\n"); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
#ifndef NDEBUG |
| 124 |
VERBOSE_LEVEL=0; // set debug level to minimum |
| 125 |
#endif |
| 126 |
|
| 127 |
if (nlhs > 0) { |
| 128 |
plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL); |
| 129 |
status = mxGetPr(plhs[0]); |
| 130 |
*status = 0.0; |
| 131 |
} |
| 132 |
/* |
| 133 |
improve checks for input arguments |
| 134 |
*/ |
| 135 |
/* process input arguments */ |
| 136 |
if (mxIsNumeric(prhs[1]) && |
| 137 |
mxIsStruct( prhs[0])) { |
| 138 |
data = (void*) mxGetData(prhs[1]); |
| 139 |
// get number of channels |
| 140 |
size_t NS = mxGetN (prhs[1]); // TODO: CHECK SIZE |
| 141 |
|
| 142 |
// get number of events |
| 143 |
size_t NEvt=0; |
| 144 |
if ( (p = mxGetField(prhs[0], 0, "EVENT") ) != NULL ) { |
| 145 |
if ( (p1 = mxGetField(p, 0, "POS") ) != NULL ) { |
| 146 |
NEvt = mxGetNumberOfElements(p1); |
| 147 |
} |
| 148 |
if ( (p1 = mxGetField(p, 0, "TYP") ) != NULL ) { |
| 149 |
size_t n = mxGetNumberOfElements(p1); |
| 150 |
if (n>NEvt) NEvt = n; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// allocate memory for header structure |
| 155 |
hdr = constructHDR (NS, NEvt); |
| 156 |
hdr->NRec = mxGetM (prhs[1]); // TODO: CHECK SIZE |
| 157 |
hdr->SPR = 1; |
| 158 |
data = (biosig_data_type*) mxGetData (prhs[1]); |
| 159 |
|
| 160 |
switch (mxGetClassID(prhs[1])) { |
| 161 |
case mxDOUBLE_CLASS: gdftyp=17; break; |
| 162 |
case mxSINGLE_CLASS: gdftyp=16; break; |
| 163 |
case mxCHAR_CLASS: gdftyp=1; break; |
| 164 |
case mxINT8_CLASS: gdftyp=1; break; |
| 165 |
case mxUINT8_CLASS: gdftyp=2; break; |
| 166 |
case mxINT16_CLASS: gdftyp=3; break; |
| 167 |
case mxUINT16_CLASS: gdftyp=4; break; |
| 168 |
case mxINT32_CLASS: gdftyp=5; break; |
| 169 |
case mxUINT32_CLASS: gdftyp=6; break; |
| 170 |
case mxINT64_CLASS: gdftyp=7; break; |
| 171 |
case mxUINT64_CLASS: gdftyp=8; break; |
| 172 |
|
| 173 |
case mxLOGICAL_CLASS: |
| 174 |
case mxVOID_CLASS: |
| 175 |
case mxFUNCTION_CLASS: |
| 176 |
case mxUNKNOWN_CLASS: |
| 177 |
case mxCELL_CLASS: |
| 178 |
case mxSTRUCT_CLASS: |
| 179 |
default: |
| 180 |
*status = -1.0; |
| 181 |
mexErrMsgTxt("mexSSAVE(HDR,data) failed datatype of data is not supported\n"); |
| 182 |
return; |
| 183 |
} |
| 184 |
} |
| 185 |
else { |
| 186 |
*status = -2.0; |
| 187 |
mexErrMsgTxt("mexSSAVE(HDR,data) failed because HDR and data, are not a struct and numeric, resp.\n"); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
for (k = 2; k < nrhs; k++) { |
| 193 |
arg = prhs[k]; |
| 194 |
|
| 195 |
if (mxIsChar(arg)) { |
| 196 |
#ifdef DEBUG |
| 197 |
mexPrintf("arg[%i]=%s \n",k,mxArrayToString(prhs[k])); |
| 198 |
#endif |
| 199 |
if (!strcmp(mxArrayToString(prhs[k]), "OVERFLOWDETECTION:ON")) |
| 200 |
FlagOverflowDetection = 1; |
| 201 |
else if (!strcmp(mxArrayToString(prhs[k]), "OVERFLOWDETECTION:OFF")) |
| 202 |
FlagOverflowDetection = 0; |
| 203 |
else if (!strcmp(mxArrayToString(prhs[k]), "UCAL:ON")) |
| 204 |
FlagUCAL = 1; |
| 205 |
else if (!strcmp(mxArrayToString(prhs[k]), "UCAL:OFF")) |
| 206 |
FlagUCAL = 0; |
| 207 |
} |
| 208 |
else { |
| 209 |
*status = -3.0; |
| 210 |
mexPrintf("%s: argument #%i is invalid.",__FILE__, k+1); |
| 211 |
mexErrMsgTxt("mexSOPEN fails because of unknown parameter\n"); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/***** SET INPUT ARGUMENTS *****/ |
| 216 |
#ifdef CHOLMOD_H |
| 217 |
hdr->FLAG.ROW_BASED_CHANNELS = (rr!=NULL); |
| 218 |
#else |
| 219 |
hdr->FLAG.ROW_BASED_CHANNELS = 0; |
| 220 |
#endif |
| 221 |
|
| 222 |
/***** CHECK INPUT HDR STUCTURE CONVERT TO libbiosig hdr *****/ |
| 223 |
if (VERBOSE_LEVEL>7) mexPrintf("110: input arguments checked\n"); |
| 224 |
|
| 225 |
if ( (p = mxGetField(prhs[0], 0, "FLAG") ) != NULL ) { |
| 226 |
if (FlagUCAL==-1) |
| 227 |
if ( (p1 = mxGetField(p, 0, "UCAL") ) != NULL ) { |
| 228 |
FlagUCAL = (fabs(getDouble(p1, 0)) < EPS); |
| 229 |
} |
| 230 |
if (FlagOverflowDetection==-1) |
| 231 |
if ( (p1 = mxGetField(p, 0, "OVERFLOWDETECTION") ) != NULL ) { |
| 232 |
FlagOverflowDetection = (fabs(getDouble(p1, 0)) < EPS); |
| 233 |
} |
| 234 |
} |
| 235 |
if (FlagUCAL==-1) FlagUCAL=0; |
| 236 |
hdr->FLAG.UCAL = FlagUCAL; |
| 237 |
if (FlagOverflowDetection==-1) FlagOverflowDetection=1; |
| 238 |
hdr->FLAG.OVERFLOWDETECTION = FlagOverflowDetection; |
| 239 |
|
| 240 |
if ( (p = mxGetField(prhs[0], 0, "TYPE") ) != NULL ) { |
| 241 |
mxGetString(p,tmpstr,sizeof(tmpstr)); |
| 242 |
hdr->TYPE = GetFileTypeFromString(tmpstr); |
| 243 |
} |
| 244 |
if ( (p = mxGetField(prhs[0], 0, "VERSION") ) != NULL ) { |
| 245 |
switch (mxGetClassID(p)) { |
| 246 |
case mxCHAR_CLASS: |
| 247 |
mxGetString(p, tmpstr, sizeof(tmpstr)); |
| 248 |
hdr->VERSION = atof(tmpstr); |
| 249 |
break; |
| 250 |
case mxDOUBLE_CLASS: |
| 251 |
hdr->VERSION = getDouble(p, 0); |
| 252 |
break; |
| 253 |
} |
| 254 |
} |
| 255 |
if ( (p = mxGetField(prhs[0], 0, "T0") ) != NULL ) hdr->T0 = (gdf_time)ldexp(getDouble(p, 0),32); |
| 256 |
if ( (p = mxGetField(prhs[0], 0, "tzmin") ) != NULL ) |
| 257 |
hdr->tzmin = (int16_t)getDouble(p, 0); |
| 258 |
else { |
| 259 |
#if __FreeBSD__ || __APPLE__ || __NetBSD__ |
| 260 |
time_t t = gdf_time2t_time(hdr->T0); |
| 261 |
struct tm *tt = localtime(&t); |
| 262 |
hdr->tzmin = tt->tm_gmtoff/60; |
| 263 |
#else |
| 264 |
hdr->tzmin = -timezone/60; |
| 265 |
#endif |
| 266 |
} |
| 267 |
if ( (p = mxGetField(prhs[0], 0, "FileName") ) != NULL ) FileName = mxArrayToString(p); |
| 268 |
if ( (p = mxGetField(prhs[0], 0, "SampleRate") ) != NULL ) hdr->SampleRate = getDouble(p, 0); |
| 269 |
|
| 270 |
#ifdef DEBUG |
| 271 |
mexPrintf("%s (line %d): TYPE=<%s><%s> VERSION=%f\n",__FILE__,__LINE__,tmpstr,GetFileTypeString(hdr->TYPE),hdr->VERSION); |
| 272 |
#endif |
| 273 |
|
| 274 |
if ( (p = mxGetField(prhs[0], 0, "NS") ) != NULL ) { |
| 275 |
hdr->NS = getDouble(p, 0); |
| 276 |
mexPrintf("HDR.NS=%d/%g is defined - but will not be used, size(data,2) is used instead;\n", hdr->NS, getDouble(p, 0)); |
| 277 |
} |
| 278 |
p1=NULL; p2=NULL; |
| 279 |
if ( (p1 = mxGetField(prhs[0], 0, "SPR") ) != NULL ) { |
| 280 |
mexPrintf("HDR.SPR=%d/%g is defined - but will not be used, size(data,1) is used instead;\n", hdr->SPR, getDouble(p1, 0)); |
| 281 |
} |
| 282 |
if ( (p2 = mxGetField(prhs[0], 0, "NRec") ) != NULL ) { |
| 283 |
mexPrintf("HDR.NRec=%d/%g is defined - but will not be used, size(data,1) is used instead;\n", hdr->NRec, getDouble(p2, 0)); |
| 284 |
} |
| 285 |
|
| 286 |
if ( p1 && p2) { |
| 287 |
hdr->SPR = (size_t)getDouble(p1, 0); |
| 288 |
hdr->NRec = (size_t)getDouble(p2, 0); |
| 289 |
} |
| 290 |
else if (!p1 && p2) { |
| 291 |
hdr->SPR = hdr->NRec; |
| 292 |
hdr->NRec = (size_t)getDouble(p2, 0); |
| 293 |
hdr->SPR /= hdr->NRec; |
| 294 |
} |
| 295 |
else if ( p1 && !p2) { |
| 296 |
hdr->SPR = (size_t)getDouble(p1, 0); |
| 297 |
hdr->NRec /= hdr->SPR; |
| 298 |
} |
| 299 |
else if (!p1 && !p2) { |
| 300 |
; /* use default values SPR=1, NREC = size(data,1) */ |
| 301 |
} |
| 302 |
|
| 303 |
if (hdr->TYPE == SCP_ECG) { |
| 304 |
/* TODO: convert HDR.data to hdr->AS.rawdata including |
| 305 |
- rescaling (HDR.FLAG.UCAL) |
| 306 |
- row/column orientation |
| 307 |
- check whether dimensions fit [SPR*NRec,NS] |
| 308 |
- Check OnOff channels |
| 309 |
- conversion to int16 |
| 310 |
*/ |
| 311 |
gdftyp = 3; |
| 312 |
// hdr->FLAG.ROW_BASED_CHANNELS = 1; |
| 313 |
|
| 314 |
mexPrintf("%s: WARNING: writing SCPECG is work in progress, has known bugs, and is not ready for production use !!!!\n",__FILE__); |
| 315 |
|
| 316 |
#ifndef NDEBUG |
| 317 |
VERBOSE_LEVEL=8; // writing SCP is not well tested - increase verbosity |
| 318 |
#endif |
| 319 |
for (k = 0; k < hdr->NS; k++) { |
| 320 |
hdr->CHANNEL[k].GDFTYP = gdftyp; // double |
| 321 |
} |
| 322 |
} |
| 323 |
else { |
| 324 |
if (hdr->NRec * hdr->SPR != mxGetM (prhs[1]) ) |
| 325 |
mexPrintf("mexSSAVE: warning HDR.NRec * HDR.SPR (%i*%i = %i) does not match number of rows (%i) in data.", hdr->NRec, hdr->SPR, hdr->NRec*hdr->SPR, mxGetM(prhs[1]) ); |
| 326 |
} |
| 327 |
|
| 328 |
if ( (p = mxGetField(prhs[0], 0, "LeadIdCode") ) != NULL ) { |
| 329 |
for (k = 0; k < hdr->NS; k++) |
| 330 |
hdr->CHANNEL[k].LeadIdCode = (uint8_t)getDouble(p,k); |
| 331 |
} |
| 332 |
else if ( (p = mxGetField(prhs[0], 0, "Label") ) != NULL ) { |
| 333 |
if ( mxIsCell(p) ) { |
| 334 |
for (k = 0; k < hdr->NS; k++) |
| 335 |
mxGetString(mxGetCell(p,k), hdr->CHANNEL[k].Label, MAX_LENGTH_LABEL+1); |
| 336 |
} |
| 337 |
} |
| 338 |
if ( (p = mxGetField(prhs[0], 0, "Transducer") ) != NULL ) { |
| 339 |
if ( mxIsCell(p) ) { |
| 340 |
for (k = 0; k < hdr->NS; k++) |
| 341 |
mxGetString(mxGetCell(p,k), hdr->CHANNEL[k].Transducer, MAX_LENGTH_LABEL+1); |
| 342 |
} |
| 343 |
} |
| 344 |
if ( (p = mxGetField(prhs[0], 0, "LowPass") ) != NULL ) { |
| 345 |
for (k = 0; k < hdr->NS; k++) |
| 346 |
hdr->CHANNEL[k].LowPass = (float)getDouble(p,k); |
| 347 |
} |
| 348 |
if ( (p = mxGetField(prhs[0], 0, "HighPass") ) != NULL ) { |
| 349 |
for (k = 0; k < hdr->NS; k++) |
| 350 |
hdr->CHANNEL[k].HighPass = (float)getDouble(p,k); |
| 351 |
} |
| 352 |
if ( (p = mxGetField(prhs[0], 0, "Notch") ) != NULL ) { |
| 353 |
for (k = 0; k < hdr->NS; k++) |
| 354 |
hdr->CHANNEL[k].Notch = (float)getDouble(p,k); |
| 355 |
} |
| 356 |
if ( (p = mxGetField(prhs[0], 0, "PhysMax") ) != NULL ) { |
| 357 |
for (k = 0; k < hdr->NS; k++) |
| 358 |
hdr->CHANNEL[k].PhysMax = (double)getDouble(p,k); |
| 359 |
} |
| 360 |
if ( (p = mxGetField(prhs[0], 0, "PhysMin") ) != NULL ) { |
| 361 |
for (k = 0; k < hdr->NS; k++) |
| 362 |
hdr->CHANNEL[k].PhysMin = (double)getDouble(p,k); |
| 363 |
} |
| 364 |
if ( (p = mxGetField(prhs[0], 0, "DigMax") ) != NULL ) { |
| 365 |
for (k = 0; k < hdr->NS; k++) |
| 366 |
hdr->CHANNEL[k].DigMax = (double)getDouble(p,k); |
| 367 |
} |
| 368 |
if ( (p = mxGetField(prhs[0], 0, "DigMin") ) != NULL ) { |
| 369 |
for (k = 0; k < hdr->NS; k++) |
| 370 |
hdr->CHANNEL[k].DigMin = (double)getDouble(p,k); |
| 371 |
} |
| 372 |
|
| 373 |
if ( (p = mxGetField(prhs[0], 0, "PhysDimCode") ) != NULL ) { |
| 374 |
for (k = 0; k < hdr->NS; k++) |
| 375 |
hdr->CHANNEL[k].PhysDimCode = (uint16_t)getDouble(p,k); |
| 376 |
} |
| 377 |
else if ( (p = mxGetField(prhs[0], 0, "PhysDim") ) != NULL ) { |
| 378 |
if ( mxIsCell(p) ) { |
| 379 |
for (k = 0; k < hdr->NS; k++) |
| 380 |
mxGetString(mxGetCell(p,k), tmpstr, sizeof(tmpstr)); |
| 381 |
hdr->CHANNEL[k].PhysDimCode = PhysDimCode(tmpstr); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
if ( (p = mxGetField(prhs[0], 0, "OnOff") ) != NULL ) { |
| 386 |
for (k = 0; k < hdr->NS; k++) |
| 387 |
hdr->CHANNEL[k].OnOff = (uint8_t)getDouble(p,k); |
| 388 |
} |
| 389 |
//* TODO: GDFTYP is derived from 'data', ignore this |
| 390 |
if ( (p = mxGetField(prhs[0], 0, "GDFTYP") ) != NULL ) { |
| 391 |
for (k = 0; k < hdr->NS; k++) |
| 392 |
hdr->CHANNEL[k].GDFTYP = (uint16_t)getDouble(p,k); |
| 393 |
} |
| 394 |
if ( (p = mxGetField(prhs[0], 0, "TOffset") ) != NULL ) { |
| 395 |
for (k = 0; k < hdr->NS; k++) |
| 396 |
hdr->CHANNEL[k].TOffset = (float)getDouble(p,k); |
| 397 |
} |
| 398 |
if ( (p = mxGetField(prhs[0], 0, "Impedance") ) != NULL ) { |
| 399 |
for (k = 0; k < hdr->NS; k++) |
| 400 |
hdr->CHANNEL[k].Impedance = (float)getDouble(p,k); |
| 401 |
} |
| 402 |
if ( (p = mxGetField(prhs[0], 0, "fZ") ) != NULL ) { |
| 403 |
for (k = 0; k < hdr->NS; k++) |
| 404 |
hdr->CHANNEL[k].fZ = (float)getDouble(p,k); |
| 405 |
} |
| 406 |
if ( (p = mxGetField(prhs[0], 0, "AS") ) != NULL ) { |
| 407 |
if ( (p1 = mxGetField(p, 0, "SPR") ) != NULL ) { |
| 408 |
// define channel-based samplingRate, HDR.SampleRate*HDR.AS.SPR(channel)/HDR.SPR; |
| 409 |
for (k = 0; k < hdr->NS; k++) |
| 410 |
hdr->CHANNEL[k].SPR = (uint32_t)getDouble(p1,k); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
if ( (p = mxGetField(prhs[0], 0, "Patient") ) != NULL ) { |
| 415 |
if ( (p1 = mxGetField(p, 0, "Id") ) != NULL ) |
| 416 |
if (mxIsChar(p1)) mxGetString(p1, hdr->Patient.Id, MAX_LENGTH_PID+1); |
| 417 |
if ( (p1 = mxGetField(p, 0, "Name") ) != NULL ) |
| 418 |
if (mxIsChar(p1)) mxGetString(p1, hdr->Patient.Name, MAX_LENGTH_PID+1); |
| 419 |
if ( (p1 = mxGetField(p, 0, "Sex") ) != NULL ) { |
| 420 |
if (mxIsChar(p1)) { |
| 421 |
char sex = toupper(*mxGetChars(p1)); |
| 422 |
hdr->Patient.Sex = (sex=='M') + 2*(sex=='F'); |
| 423 |
} |
| 424 |
else |
| 425 |
hdr->Patient.Sex = (int8_t)getDouble(p1,0); |
| 426 |
} |
| 427 |
|
| 428 |
if ( (p1 = mxGetField(p, 0, "Handedness") ) != NULL ) |
| 429 |
hdr->Patient.Handedness = (int8_t)getDouble(p1,0); |
| 430 |
if ( (p1 = mxGetField(p, 0, "Smoking") ) != NULL ) |
| 431 |
hdr->Patient.Smoking = (int8_t)getDouble(p1,0); |
| 432 |
if ( (p1 = mxGetField(p, 0, "AlcoholAbuse") ) != NULL ) |
| 433 |
hdr->Patient.AlcoholAbuse = (int8_t)getDouble(p1,0); |
| 434 |
if ( (p1 = mxGetField(p, 0, "DrugAbuse") ) != NULL ) |
| 435 |
hdr->Patient.DrugAbuse = (int8_t)getDouble(p1,0); |
| 436 |
if ( (p1 = mxGetField(p, 0, "Medication") ) != NULL ) |
| 437 |
hdr->Patient.Medication = (int8_t)getDouble(p1,0); |
| 438 |
if ( (p1 = mxGetField(p, 0, "Impairment") ) != NULL ) { |
| 439 |
if ( (p2 = mxGetField(p1, 0, "Visual") ) != NULL ) |
| 440 |
hdr->Patient.Impairment.Visual = (int8_t)getDouble(p2,0); |
| 441 |
if ( (p2 = mxGetField(p1, 0, "Heart") ) != NULL ) |
| 442 |
hdr->Patient.Impairment.Heart = (int8_t)getDouble(p2,0); |
| 443 |
} |
| 444 |
|
| 445 |
if ( (p1 = mxGetField(p, 0, "Weight") ) != NULL ) |
| 446 |
hdr->Patient.Weight = (uint8_t)getDouble(p1,0); |
| 447 |
if ( (p1 = mxGetField(p, 0, "Height") ) != NULL ) |
| 448 |
hdr->Patient.Height = (uint8_t)getDouble(p1,0); |
| 449 |
if ( (p1 = mxGetField(p, 0, "Birthday") ) != NULL ) |
| 450 |
hdr->Patient.Birthday = (gdf_time)getDouble(p1,0); |
| 451 |
} |
| 452 |
|
| 453 |
if ( (p = mxGetField(prhs[0], 0, "ID") ) != NULL ) { |
| 454 |
if ( (p1 = mxGetField(p, 0, "Recording") ) != NULL ) |
| 455 |
if (mxIsChar(p1)) mxGetString(p1, hdr->ID.Recording, MAX_LENGTH_RID+1); |
| 456 |
if ( (p1 = mxGetField(p, 0, "Technician") ) != NULL ) |
| 457 |
if (mxIsChar(p1)) { |
| 458 |
char* str = mxArrayToString(p1); |
| 459 |
hdr->ID.Technician = strdup(str); |
| 460 |
} |
| 461 |
if ( (p1 = mxGetField(p, 0, "Hospital") ) != NULL && mxIsChar(p1) ) { |
| 462 |
size_t len = mxGetN(p1)*mxGetN(p1); |
| 463 |
hdr->ID.Hospital = (char*)realloc(hdr->ID.Hospital, len+1); |
| 464 |
mxGetString(p1, hdr->ID.Hospital, len); |
| 465 |
} |
| 466 |
if ( (p1 = mxGetField(p, 0, "Equipment") ) != NULL ) |
| 467 |
memcpy(&hdr->ID.Equipment,mxGetData(p1), 8); |
| 468 |
if ( (p1 = mxGetField(p, 0, "Manufacturer") ) != NULL ) { |
| 469 |
uint8_t pos = 0; |
| 470 |
if ( ( (p2 = mxGetField(p1, 0, "Name") ) != NULL ) && mxIsChar(p2)) { |
| 471 |
//hdr->ID.Manufacturer.Name=mxGetChars(p2); |
| 472 |
mxGetString(p1, hdr->ID.Manufacturer._field,MAX_LENGTH_MANUF); |
| 473 |
pos = strlen(hdr->ID.Manufacturer._field)+1; |
| 474 |
} |
| 475 |
else { |
| 476 |
hdr->ID.Manufacturer._field[pos++] = 0; |
| 477 |
} |
| 478 |
|
| 479 |
if ( ( (p2 = mxGetField(p1, 0, "Model") ) != NULL ) && mxIsChar(p2)) { |
| 480 |
//hdr->ID.Manufacturer.Model=mxGetChars(p2); |
| 481 |
mxGetString(p1, hdr->ID.Manufacturer._field + pos, MAX_LENGTH_MANUF); |
| 482 |
pos += strlen(hdr->ID.Manufacturer._field + pos)+1; |
| 483 |
} |
| 484 |
else { |
| 485 |
hdr->ID.Manufacturer._field[pos++] = 0; |
| 486 |
} |
| 487 |
|
| 488 |
if ( ( (p2 = mxGetField(p1, 0, "Version") ) != NULL ) && mxIsChar(p2)) { |
| 489 |
//hdr->ID.Manufacturer.Version=mxGetChars(p2); |
| 490 |
mxGetString(p1, hdr->ID.Manufacturer._field + pos, MAX_LENGTH_MANUF); |
| 491 |
pos += strlen(hdr->ID.Manufacturer._field+pos)+1; |
| 492 |
} |
| 493 |
else { |
| 494 |
hdr->ID.Manufacturer._field[pos++] = 0; |
| 495 |
} |
| 496 |
|
| 497 |
if ( ( (p2 = mxGetField(p1, 0, "SerialNumber") ) != NULL ) && mxIsChar(p2)) { |
| 498 |
//hdr->ID.Manufacturer.SerialNumber=mxGetChars(p2); |
| 499 |
mxGetString(p1, hdr->ID.Manufacturer._field + pos, MAX_LENGTH_MANUF); |
| 500 |
pos += strlen(hdr->ID.Manufacturer._field+pos)+1; |
| 501 |
} |
| 502 |
else { |
| 503 |
hdr->ID.Manufacturer._field[pos++] = 0; |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
if ( (p = mxGetField(prhs[0], 0, "FLAG") ) != NULL ) { |
| 509 |
if ( (p1 = mxGetField(p, 0, "OVERFLOWDETECTION") ) != NULL ) |
| 510 |
hdr->FLAG.OVERFLOWDETECTION = (char)getDouble(p1,0); |
| 511 |
if ( (p1 = mxGetField(p, 0, "UCAL") ) != NULL ) |
| 512 |
hdr->FLAG.UCAL = (char)getDouble(p1,0); |
| 513 |
if ( (p1 = mxGetField(p, 0, "ANONYMOUS") ) != NULL ) |
| 514 |
hdr->FLAG.ANONYMOUS = (char)getDouble(p1,0); |
| 515 |
if ( (p1 = mxGetField(p, 0, "ROW_BASED_CHANNELS") ) != NULL ) |
| 516 |
hdr->FLAG.ROW_BASED_CHANNELS = (char)getDouble(p1,0); |
| 517 |
} |
| 518 |
|
| 519 |
if ( (p = mxGetField(prhs[0], 0, "EVENT") ) != NULL ) { |
| 520 |
if ( (p1 = mxGetField(p, 0, "SampleRate") ) != NULL ) { |
| 521 |
hdr->EVENT.SampleRate = (double)getDouble(p1,0); |
| 522 |
} |
| 523 |
if ( (p1 = mxGetField(p, 0, "POS") ) != NULL ) { |
| 524 |
size_t n = mxGetNumberOfElements(p1); |
| 525 |
for (k = 0; k < n; k++) |
| 526 |
hdr->EVENT.POS[k] = (uint32_t)getDouble(p1,k) - 1; // convert from 1-based indexing to 0-based indexing |
| 527 |
} |
| 528 |
if ( (p1 = mxGetField(p, 0, "TYP") ) != NULL ) { |
| 529 |
size_t n = mxGetNumberOfElements(p1); |
| 530 |
for (k = 0; k < n; k++) |
| 531 |
hdr->EVENT.TYP[k] = (uint16_t)getDouble(p1,k); |
| 532 |
} |
| 533 |
if ( (p1 = mxGetField(p, 0, "DUR") ) != NULL ) { |
| 534 |
size_t n = mxGetNumberOfElements(p1); |
| 535 |
for (k = 0; k < n; k++) |
| 536 |
hdr->EVENT.DUR[k] = (uint32_t)getDouble(p1,k); |
| 537 |
} |
| 538 |
if ( (p1 = mxGetField(p, 0, "CHN") ) != NULL ) { |
| 539 |
size_t n = mxGetNumberOfElements(p1); |
| 540 |
for (k = 0; k < n; k++) |
| 541 |
hdr->EVENT.CHN[k] = (uint16_t)getDouble(p1,k); |
| 542 |
} |
| 543 |
|
| 544 |
if ( (p1 = mxGetField(p, 0, "CodeDesc") ) != NULL ) { |
| 545 |
size_t n = mxGetNumberOfElements(p1); |
| 546 |
hdr->EVENT.LenCodeDesc = n+1; |
| 547 |
// get total size for storing all CodeDesc strings |
| 548 |
size_t memsiz = 1; |
| 549 |
for (k = 0; k < n; k++) { |
| 550 |
mxArray *p2 = mxGetCell(p1,k); |
| 551 |
memsiz += mxGetN(p2)+1; |
| 552 |
} |
| 553 |
/* allocate memory for |
| 554 |
hdr->.AS.auxBUF contains the \0-terminated CodeDesc strings, |
| 555 |
hdr->EVENT.CodeDesc contains the pointer to the strings |
| 556 |
*/ |
| 557 |
hdr->EVENT.CodeDesc = (const char**) realloc(hdr->EVENT.CodeDesc, hdr->EVENT.LenCodeDesc*sizeof(char*)); |
| 558 |
hdr->AS.auxBUF = (uint8_t*)realloc(hdr->AS.auxBUF, memsiz*sizeof(char)); |
| 559 |
|
| 560 |
// first element is always the empty string. |
| 561 |
hdr->AS.auxBUF[0] = 0; |
| 562 |
hdr->EVENT.CodeDesc[0] = (char*)hdr->AS.auxBUF; |
| 563 |
size_t pos = 1; |
| 564 |
for (k = 0; k < n; k++) { |
| 565 |
mxArray *p2 = mxGetCell(p1,k); |
| 566 |
size_t buflen = mxGetN(p2)+1; //*mxGetM(p2) |
| 567 |
mxGetString(p2, (char*)hdr->AS.auxBUF+pos, buflen); |
| 568 |
hdr->EVENT.CodeDesc[k+1] = (char*)hdr->AS.auxBUF+pos; |
| 569 |
pos += buflen; |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
hdr = sopen(FileName, "w", hdr); |
| 575 |
istatus = serror2(hdr); |
| 576 |
if (istatus) { |
| 577 |
mexErrMsgTxt("mexSSAVE: sopen failed \n"); |
| 578 |
*status = istatus; |
| 579 |
} |
| 580 |
|
| 581 |
swrite((biosig_data_type*)data, hdr->NRec, hdr); |
| 582 |
istatus = serror2(hdr); |
| 583 |
if (istatus) { |
| 584 |
mexErrMsgTxt("mexSSAVE: swrite failed \n"); |
| 585 |
*status = istatus; |
| 586 |
} |
| 587 |
|
| 588 |
destructHDR(hdr); |
| 589 |
istatus = serror2(hdr); |
| 590 |
if (istatus) { |
| 591 |
mexErrMsgTxt("mexSSAVE: sclose failed \n"); |
| 592 |
*status = istatus; |
| 593 |
} |
| 594 |
}; |
| 595 |
|