| 1 |
/* |
| 2 |
|
| 3 |
Copyright (C) 2007-2019,2022 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 <math.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
#include <strings.h> |
| 19 |
#ifdef HAVE_CHOLMOD |
| 20 |
# if !defined(__APPLE__) |
| 21 |
# include <suitesparse/cholmod.h> |
| 22 |
# else |
| 23 |
# include <cholmod.h> |
| 24 |
# endif |
| 25 |
#endif |
| 26 |
#include <biosig-dev.h> |
| 27 |
#include <biosig.h> |
| 28 |
|
| 29 |
#ifdef NDEBUG |
| 30 |
#define VERBOSE_LEVEL 0 // turn off debugging information, but its only used without NDEBUG |
| 31 |
#else |
| 32 |
extern int VERBOSE_LEVEL; // used for debugging |
| 33 |
#endif |
| 34 |
|
| 35 |
|
| 36 |
#ifdef tmwtypes_h |
| 37 |
#if (MX_API_VER<=0x07020000) |
| 38 |
typedef int mwSize; |
| 39 |
#endif |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifndef TRUE |
| 43 |
#define TRUE (1) |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifdef CHOLMOD_H |
| 47 |
//#include "cholmod/matlab/cholmod_matlab.h" |
| 48 |
/* |
| 49 |
The function sputil_get_sparse and its license was downloaded on Oct 16, 2009 from |
| 50 |
http://www.cise.ufl.edu/research/sparse/cholmod/CHOLMOD/MATLAB/cholmod_matlab.c |
| 51 |
http://www.cise.ufl.edu/research/sparse/cholmod/CHOLMOD/MATLAB/License.txt |
| 52 |
*/ |
| 53 |
/* |
| 54 |
CHOLMOD/MATLAB Module. |
| 55 |
Copyright (C) 2005-2006, Timothy A. Davis |
| 56 |
CHOLMOD is also available under other licenses; contact authors for details. |
| 57 |
MATLAB(tm) is a Registered Trademark of The MathWorks, Inc. |
| 58 |
http://www.cise.ufl.edu/research/sparse |
| 59 |
|
| 60 |
Note that this license is for the CHOLMOD/MATLAB module only. |
| 61 |
All CHOLMOD modules are licensed separately. |
| 62 |
|
| 63 |
|
| 64 |
-------------------------------------------------------------------------------- |
| 65 |
|
| 66 |
|
| 67 |
This Module is free software; you can redistribute it and/or |
| 68 |
modify it under the terms of the GNU General Public License |
| 69 |
as published by the Free Software Foundation; either version 2 |
| 70 |
of the License, or (at your option) any later version. |
| 71 |
|
| 72 |
This Module is distributed in the hope that it will be useful, |
| 73 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 74 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 75 |
GNU General Public License for more details. |
| 76 |
|
| 77 |
You should have received a copy of the GNU General Public License |
| 78 |
along with this Module; if not, write to the Free Software |
| 79 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 80 |
*/ |
| 81 |
|
| 82 |
/* ========================================================================== */ |
| 83 |
/* === sputil_get_sparse ==================================================== */ |
| 84 |
/* ========================================================================== */ |
| 85 |
|
| 86 |
/* Create a shallow CHOLMOD copy of a MATLAB sparse matrix. No memory is |
| 87 |
* allocated. The resulting matrix A must not be modified. |
| 88 |
*/ |
| 89 |
|
| 90 |
cholmod_sparse *sputil_get_sparse |
| 91 |
( |
| 92 |
const mxArray *Amatlab, /* MATLAB version of the matrix */ |
| 93 |
cholmod_sparse *A, /* CHOLMOD version of the matrix */ |
| 94 |
double *dummy, /* a pointer to a valid scalar double */ |
| 95 |
mwSize stype /* -1: lower, 0: unsymmetric, 1: upper */ |
| 96 |
) |
| 97 |
{ |
| 98 |
mwSize *Ap ; |
| 99 |
A->nrow = mxGetM (Amatlab) ; |
| 100 |
A->ncol = mxGetN (Amatlab) ; |
| 101 |
A->p = (mwSize *) mxGetJc (Amatlab) ; |
| 102 |
A->i = (mwSize *) mxGetIr (Amatlab) ; |
| 103 |
Ap = (mwSize*)A->p ; |
| 104 |
A->nzmax = Ap [A->ncol] ; |
| 105 |
A->packed = TRUE ; |
| 106 |
A->sorted = TRUE ; |
| 107 |
A->nz = NULL ; |
| 108 |
A->itype = CHOLMOD_LONG ; /* was CHOLMOD_INT in v1.6 and earlier */ |
| 109 |
A->dtype = CHOLMOD_DOUBLE ; |
| 110 |
A->stype = stype ; |
| 111 |
|
| 112 |
#ifndef MATLAB6p1_OR_EARLIER |
| 113 |
|
| 114 |
if (mxIsLogical (Amatlab)) |
| 115 |
{ |
| 116 |
A->x = NULL ; |
| 117 |
A->z = NULL ; |
| 118 |
A->xtype = CHOLMOD_PATTERN ; |
| 119 |
} |
| 120 |
else if (mxIsEmpty (Amatlab)) |
| 121 |
{ |
| 122 |
/* this is not dereferenced, but the existence (non-NULL) of these |
| 123 |
* pointers is checked in CHOLMOD */ |
| 124 |
A->x = dummy ; |
| 125 |
A->z = dummy ; |
| 126 |
A->xtype = mxIsComplex (Amatlab) ? CHOLMOD_ZOMPLEX : CHOLMOD_REAL ; |
| 127 |
} |
| 128 |
else if (mxIsDouble (Amatlab)) |
| 129 |
{ |
| 130 |
A->x = mxGetPr (Amatlab) ; |
| 131 |
A->z = mxGetPi (Amatlab) ; |
| 132 |
A->xtype = mxIsComplex (Amatlab) ? CHOLMOD_ZOMPLEX : CHOLMOD_REAL ; |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
/* only logical and complex/real double matrices supported */ |
| 137 |
//sputil_error (ERROR_INVALID_TYPE, 0) ; // modified by AS, Oct 2009 |
| 138 |
} |
| 139 |
|
| 140 |
#else |
| 141 |
|
| 142 |
if (mxIsEmpty (Amatlab)) |
| 143 |
{ |
| 144 |
/* this is not dereferenced, but the existence (non-NULL) of these |
| 145 |
* pointers is checked in CHOLMOD */ |
| 146 |
A->x = dummy ; |
| 147 |
A->z = dummy ; |
| 148 |
A->xtype = mxIsComplex (Amatlab) ? CHOLMOD_ZOMPLEX : CHOLMOD_REAL ; |
| 149 |
} |
| 150 |
else |
| 151 |
{ |
| 152 |
/* in MATLAB 6.1, the matrix is sparse, so it must be double */ |
| 153 |
A->x = mxGetPr (Amatlab) ; |
| 154 |
A->z = mxGetPi (Amatlab) ; |
| 155 |
A->xtype = mxIsComplex (Amatlab) ? CHOLMOD_ZOMPLEX : CHOLMOD_REAL ; |
| 156 |
} |
| 157 |
|
| 158 |
#endif |
| 159 |
|
| 160 |
return (A) ; |
| 161 |
} |
| 162 |
/* ========================================================================== */ |
| 163 |
/* === end of sputil_get_sparse ============================================= */ |
| 164 |
/* ========================================================================== */ |
| 165 |
#endif |
| 166 |
|
| 167 |
#ifdef WITH_PDP |
| 168 |
void sopen_pdp_read(HDRTYPE *hdr); |
| 169 |
#endif |
| 170 |
|
| 171 |
//#define VERBOSE_LEVEL 9 |
| 172 |
//extern int VERBOSE_LEVEL; |
| 173 |
//#define DEBUG |
| 174 |
|
| 175 |
void mexFunction( |
| 176 |
int nlhs, /* number of expected outputs */ |
| 177 |
mxArray *plhs[], /* array of pointers to output arguments */ |
| 178 |
int nrhs, /* number of inputs */ |
| 179 |
const mxArray *prhs[] /* array of pointers to input arguments */ |
| 180 |
) |
| 181 |
|
| 182 |
{ |
| 183 |
size_t k,k1; |
| 184 |
const mxArray *arg; |
| 185 |
mxArray *HDR; |
| 186 |
HDRTYPE *hdr; |
| 187 |
CHANNEL_TYPE* cp; |
| 188 |
size_t count; |
| 189 |
time_t T0; |
| 190 |
char *FileName=NULL; |
| 191 |
int status; |
| 192 |
int CHAN = 0; |
| 193 |
int TARGETSEGMENT = 1; |
| 194 |
double *ChanList=NULL; |
| 195 |
int NS = -1; |
| 196 |
char FlagOverflowDetection = 1, FlagUCAL = 0; |
| 197 |
int argSweepSel = -1; |
| 198 |
|
| 199 |
#if (BIOSIG_VERSION >= 10905) |
| 200 |
biosig_options_type biosig_options; |
| 201 |
biosig_options.free_text_event_limiter="\0"; // default value |
| 202 |
#endif |
| 203 |
|
| 204 |
#ifdef CHOLMOD_H |
| 205 |
cholmod_sparse RR,*rr=NULL; |
| 206 |
double dummy; |
| 207 |
#endif |
| 208 |
|
| 209 |
// ToDO: output single data |
| 210 |
// mxClassId FlagMXclass=mxDOUBLE_CLASS; |
| 211 |
|
| 212 |
|
| 213 |
if (nrhs<1) { |
| 214 |
#ifdef mexSOPEN |
| 215 |
mexPrintf(" Usage of mexSOPEN:\n"); |
| 216 |
mexPrintf("\tHDR = mexSOPEN(f)\n"); |
| 217 |
mexPrintf(" Input:\n\tf\tfilename\n"); |
| 218 |
mexPrintf("\t... = mexSLOAD(f,'--free-text-event-limiter',';')\n"); |
| 219 |
#if (BIOSIG_VERSION >= 10905) |
| 220 |
mexPrintf("\t'--free-text-event-limiter',';' : free text limited by first \";\", remainder is ignored." |
| 221 |
"\n\t\tThis can help to reduce the number of distinct free text events.\n\n"); |
| 222 |
#endif |
| 223 |
mexPrintf(" Output:\n\tHDR\theader structure\n\n"); |
| 224 |
#else |
| 225 |
mexPrintf(" Usage of mexSLOAD:\n"); |
| 226 |
mexPrintf("\t[s,HDR]=mexSLOAD(f)\n"); |
| 227 |
mexPrintf("\t[s,HDR]=mexSLOAD(f,chan)\n\t\tchan must be sorted in ascending order\n"); |
| 228 |
#ifdef CHOLMOD_H |
| 229 |
mexPrintf("\t[s,HDR]=mexSLOAD(f,ReRef)\n\t\treref is a (sparse) matrix for rerefencing\n"); |
| 230 |
#endif |
| 231 |
mexPrintf("\t[s,HDR]=mexSLOAD(f,chan,'...')\n"); |
| 232 |
mexPrintf("\t[s,HDR]=mexSLOAD(f,chan,'OVERFLOWDETECTION:ON')\n"); |
| 233 |
mexPrintf("\t[s,HDR]=mexSLOAD(f,chan,'OVERFLOWDETECTION:OFF')\n"); |
| 234 |
mexPrintf("\t[s,HDR]=mexSLOAD(f,chan,'UCAL:ON')\n"); |
| 235 |
mexPrintf("\t[s,HDR]=mexSLOAD(f,chan,'UCAL:OFF')\n"); |
| 236 |
mexPrintf("\t[s,HDR]=mexSLOAD(f,chan,'OUTPUT:SINGLE')\n"); |
| 237 |
mexPrintf("\t[s,HDR]=mexSLOAD(f,chan,'TARGETSEGMENT:<N>')\n"); |
| 238 |
mexPrintf("\t[s,HDR]=mexSLOAD(f,chan,'SWEEP',[NE, NG, NS])\n"); |
| 239 |
mexPrintf("\t[s,HDR]=mexSLOAD(f,chan,'--free-text-event-limiter',';')\n"); |
| 240 |
mexPrintf(" Input:\n\tf\tfilename\n"); |
| 241 |
mexPrintf("\tchan\tlist of selected channels; 0=all channels [default]\n"); |
| 242 |
mexPrintf("\tUCAL\tON: do not calibrate data; default=OFF\n"); |
| 243 |
// mexPrintf("\tOUTPUT\tSINGLE: single precision; default='double'\n"); |
| 244 |
mexPrintf("\tOVERFLOWDETECTION\tdefault = ON\n\t\tON: values outside dynamic range are not-a-number (NaN)\n"); |
| 245 |
mexPrintf("\tTARGETSEGMENT:<N>\n\t\tselect segment <N> in multisegment files (like Nihon-Khoden), default=1\n\t\tIt has no effect for other data formats.\n"); |
| 246 |
mexPrintf("\t[NE, NG, NS] are the number of the experiment, the series and the sweep, resp. for sweep selection in HEKA/PatchMaster files. (0 indicates all)\n"); |
| 247 |
mexPrintf("\t\t examples: [1,2,3] the 3rd sweep from the 2nd series of experiment 1; [1,3,0] selects all sweeps from experiment=1, series=3. \n\n"); |
| 248 |
#if (BIOSIG_VERSION >= 10905) |
| 249 |
mexPrintf("\t'--free-text-event-limiter',';' : free text limited by first \";\", remainder is ignored." |
| 250 |
"\n\t\tThis can help to reduce the number of distinct free text events.\n\n"); |
| 251 |
#endif |
| 252 |
mexPrintf(" Output:\n\ts\tsignal data, each column is one channel\n"); |
| 253 |
mexPrintf("\tHDR\theader structure\n\n"); |
| 254 |
#endif |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
/* |
| 259 |
improve checks for input arguments |
| 260 |
*/ |
| 261 |
/* process input arguments */ |
| 262 |
for (k = 0; k < nrhs; k++) { |
| 263 |
arg = prhs[k]; |
| 264 |
if (mxIsEmpty(arg) && (k>0)) { |
| 265 |
#ifdef DEBUG |
| 266 |
mexPrintf("arg[%i] Empty\n",k); |
| 267 |
#endif |
| 268 |
} |
| 269 |
else if ((k==0) && mxIsCell(arg) && mxGetNumberOfElements(arg)==1 && mxGetCell(arg,0) && mxIsChar(mxGetCell(arg,0))) { |
| 270 |
FileName = mxArrayToString(mxGetCell(arg,0)); |
| 271 |
#ifdef DEBUG |
| 272 |
mexPrintf("arg[%i] IsCell\n",k); |
| 273 |
#endif |
| 274 |
} |
| 275 |
else if ((k==0) && mxIsStruct(arg)) { |
| 276 |
FileName = mxArrayToString(mxGetField(prhs[k],0,"FileName")); |
| 277 |
#ifdef DEBUG |
| 278 |
mexPrintf("arg[%i] IsStruct\n",k); |
| 279 |
#endif |
| 280 |
} |
| 281 |
else if ((k==1) && mxIsSparse(arg)) { |
| 282 |
#ifdef CHOLMOD_H |
| 283 |
rr = sputil_get_sparse(arg,&RR,&dummy,0); |
| 284 |
#else |
| 285 |
mexErrMsgTxt("This version of mexSLOAD does not support re-referencing matrix - recompile with -DWITH_CHOLMOD -lcholmod \n"); |
| 286 |
#endif |
| 287 |
} |
| 288 |
else if ((k==1) && mxIsNumeric(arg)) { |
| 289 |
#ifdef DEBUG |
| 290 |
mexPrintf("arg[%i] IsNumeric\n",k); |
| 291 |
#endif |
| 292 |
ChanList = (double*)mxGetData(prhs[k]); |
| 293 |
NS = mxGetNumberOfElements(prhs[k]); |
| 294 |
} |
| 295 |
else if (mxIsChar(arg)) { |
| 296 |
#ifdef DEBUG |
| 297 |
mexPrintf("arg[%i]=%s \n",k,mxArrayToString(prhs[k])); |
| 298 |
#endif |
| 299 |
if (k==0) |
| 300 |
FileName = mxArrayToString(prhs[k]); |
| 301 |
else if (!strcmp(mxArrayToString(prhs[k]), "CNT32")) |
| 302 |
; // obsolete - supported for backwards compatibility |
| 303 |
else if (!strcmp(mxArrayToString(prhs[k]), "OVERFLOWDETECTION:ON")) |
| 304 |
FlagOverflowDetection = 1; |
| 305 |
else if (!strcmp(mxArrayToString(prhs[k]), "OVERFLOWDETECTION:OFF")) |
| 306 |
FlagOverflowDetection = 0; |
| 307 |
else if (!strcmp(mxArrayToString(prhs[k]), "UCAL:ON")) |
| 308 |
FlagUCAL = 1; |
| 309 |
else if (!strcmp(mxArrayToString(prhs[k]), "UCAL:OFF")) |
| 310 |
FlagUCAL = 0; |
| 311 |
// else if (!strcmp(mxArrayToString(prhs[k]),"OUTPUT:SINGLE")) |
| 312 |
// FlagMXclass = mxSINGLE_CLASS; |
| 313 |
else if (!strncmp(mxArrayToString(prhs[k]),"TARGETSEGMENT:",14)) |
| 314 |
TARGETSEGMENT = atoi(mxArrayToString(prhs[k])+14); |
| 315 |
else if (!strcasecmp(mxArrayToString(prhs[k]), "SWEEP") && (prhs[k+1] != NULL) && mxIsNumeric(prhs[k+1])) |
| 316 |
argSweepSel = ++k; |
| 317 |
#if (BIOSIG_VERSION >= 10905) |
| 318 |
else if (!strcasecmp(mxArrayToString(prhs[k]), "--free-text-event-limiter") && (prhs[k+1] != NULL) && mxIsChar(prhs[k+1])) |
| 319 |
biosig_options.free_text_event_limiter = mxArrayToString(prhs[++k]); |
| 320 |
#endif |
| 321 |
} |
| 322 |
else { |
| 323 |
#ifndef mexSOPEN |
| 324 |
mexPrintf("mexSLOAD: argument #%i is invalid.",k+1); |
| 325 |
mexErrMsgTxt("mexSLOAD fails because of unknown parameter\n"); |
| 326 |
#else |
| 327 |
mexPrintf("mexSOPEN: argument #%i is invalid.",k+1); |
| 328 |
mexErrMsgTxt("mexSOPEN fails because of unknown parameter\n"); |
| 329 |
#endif |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
if (VERBOSE_LEVEL>7) |
| 334 |
mexPrintf("110: input arguments checked\n"); |
| 335 |
|
| 336 |
hdr = constructHDR(0,0); |
| 337 |
|
| 338 |
#ifdef __LIBBIOSIG2_H__ |
| 339 |
|
| 340 |
unsigned flags = (!!FlagOverflowDetection)*BIOSIG_FLAG_OVERFLOWDETECTION + (!!FlagUCAL)*BIOSIG_FLAG_UCAL; |
| 341 |
#ifdef CHOLMOD_H |
| 342 |
flags += (rr!=NULL)*BIOSIG_FLAG_ROW_BASED_CHANNELS; |
| 343 |
#endif |
| 344 |
biosig_reset_flag(hdr, BIOSIG_FLAG_ROW_BASED_CHANNELS | BIOSIG_FLAG_OVERFLOWDETECTION | BIOSIG_FLAG_UCAL); |
| 345 |
biosig_set_flag(hdr, flags); |
| 346 |
|
| 347 |
biosig_set_targetsegment(hdr, TARGETSEGMENT); |
| 348 |
|
| 349 |
// sweep selection for Heka format |
| 350 |
if (argSweepSel>0) { |
| 351 |
double *SZ = (double*) mxGetData(prhs[argSweepSel]); |
| 352 |
k = 0; |
| 353 |
while (k < mxGetNumberOfElements(prhs[argSweepSel]) && k < 5) { |
| 354 |
biosig_set_segment_selection(hdr, k+1, (uint32_t)SZ[k]); |
| 355 |
k++; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
#else //__LIBBIOSIG2_H__ |
| 360 |
|
| 361 |
|
| 362 |
hdr->FLAG.OVERFLOWDETECTION = FlagOverflowDetection; |
| 363 |
hdr->FLAG.UCAL = FlagUCAL; |
| 364 |
#ifdef CHOLMOD_H |
| 365 |
hdr->FLAG.ROW_BASED_CHANNELS = (rr!=NULL); |
| 366 |
#else |
| 367 |
hdr->FLAG.ROW_BASED_CHANNELS = 0; |
| 368 |
#endif |
| 369 |
hdr->FLAG.TARGETSEGMENT = TARGETSEGMENT; |
| 370 |
|
| 371 |
// sweep selection for Heka format |
| 372 |
if (argSweepSel>0) { |
| 373 |
double *SZ = (double*) mxGetData(prhs[argSweepSel]); |
| 374 |
k = 0; |
| 375 |
while (k < mxGetNumberOfElements(prhs[argSweepSel]) && k < 5) { |
| 376 |
hdr->AS.SegSel[k] = (uint32_t)SZ[k]; |
| 377 |
k++; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
#endif // __LIBBIOSIG2_H__ : TODO: below, nothing is converted to level-2 interface, yet |
| 383 |
|
| 384 |
|
| 385 |
if (VERBOSE_LEVEL>7) |
| 386 |
mexPrintf("120: going to sopen\n"); |
| 387 |
|
| 388 |
#if (BIOSIG_VERSION >= 10905) |
| 389 |
hdr = sopen_extended(FileName, "r", hdr, &biosig_options); |
| 390 |
#else |
| 391 |
hdr = sopen(FileName, "r", hdr); |
| 392 |
#endif |
| 393 |
|
| 394 |
/* |
| 395 |
#ifdef WITH_PDP |
| 396 |
if (hdr->AS.B4C_ERRNUM) { |
| 397 |
hdr->AS.B4C_ERRNUM = 0; |
| 398 |
sopen_pdp_read(hdr); |
| 399 |
} |
| 400 |
#endif |
| 401 |
*/ |
| 402 |
if (VERBOSE_LEVEL>7) |
| 403 |
mexPrintf("121: sopen done\n"); |
| 404 |
|
| 405 |
if ((status=serror2(hdr))) { |
| 406 |
|
| 407 |
const char* fields[]={"TYPE","VERSION","FileName","FLAG","ErrNum","ErrMsg"}; |
| 408 |
HDR = mxCreateStructMatrix(1, 1, 6, fields); |
| 409 |
#ifdef __LIBBIOSIG2_H__ |
| 410 |
mxSetField(HDR,0,"FileName",mxCreateString(biosig_get_filename(hdr))); |
| 411 |
const char *FileTypeString = GetFileTypeString(biosig_get_filetype(hdr)); |
| 412 |
mxSetField(HDR,0,"VERSION",mxCreateDoubleScalar(biosig_get_version(hdr))); |
| 413 |
#else |
| 414 |
mxSetField(HDR,0,"FileName",mxCreateString(hdr->FileName)); |
| 415 |
const char *FileTypeString = GetFileTypeString(hdr->TYPE); |
| 416 |
mxSetField(HDR,0,"VERSION",mxCreateDoubleScalar(hdr->VERSION)); |
| 417 |
#endif |
| 418 |
mxArray *errnum = mxCreateNumericMatrix(1,1,mxUINT8_CLASS,mxREAL); |
| 419 |
*(uint8_t*)mxGetData(errnum) = (uint8_t)status; |
| 420 |
mxSetField(HDR,0,"ErrNum",errnum); |
| 421 |
|
| 422 |
#ifdef HAVE_OCTAVE |
| 423 |
// handle bug in octave: mxCreateString(NULL) causes segmentation fault |
| 424 |
// Octave 3.2.3 causes a seg-fault in mxCreateString(NULL) |
| 425 |
if (FileTypeString) FileTypeString="\0"; |
| 426 |
#endif |
| 427 |
mxSetField(HDR,0,"TYPE",mxCreateString(FileTypeString)); |
| 428 |
|
| 429 |
|
| 430 |
char *msg = (char*)malloc(72+23+strlen(FileName)); // 72: max length of constant text, 23: max length of GetFileTypeString() |
| 431 |
if (msg == NULL) |
| 432 |
mxSetField(HDR,0,"ErrMsg",mxCreateString("Error mexSLOAD: Cannot open file\n")); |
| 433 |
else { |
| 434 |
if (status==B4C_CANNOT_OPEN_FILE) |
| 435 |
sprintf(msg,"Error mexSLOAD: file %s not found.\n",FileName); /* Flawfinder: ignore *** sufficient memory is allocated above */ |
| 436 |
else if (status==B4C_FORMAT_UNKNOWN) |
| 437 |
sprintf(msg,"Error mexSLOAD: Cannot open file %s - format %s not known.\n",FileName,FileTypeString); /* Flawfinder: ignore *** sufficient memory is allocated above */ |
| 438 |
else if (status==B4C_FORMAT_UNSUPPORTED) |
| 439 |
sprintf(msg,"Error mexSLOAD: Cannot open file %s - format %s not supported [%s].\n", FileName, FileTypeString, hdr->AS.B4C_ERRMSG); /* Flawfinder: ignore *** sufficient memory is allocated above */ |
| 440 |
else |
| 441 |
sprintf(msg,"Error %i mexSLOAD: Cannot open file %s - format %s not supported [%s].\n", status, FileName, FileTypeString, hdr->AS.B4C_ERRMSG); /* Flawfinder: ignore *** sufficient memory is allocated above */ |
| 442 |
|
| 443 |
mxSetField(HDR,0,"ErrMsg",mxCreateString(msg)); |
| 444 |
free(msg); |
| 445 |
} |
| 446 |
|
| 447 |
if (VERBOSE_LEVEL>7) |
| 448 |
mexPrintf("737: abort mexSLOAD - sopen failed\n"); |
| 449 |
|
| 450 |
destructHDR(hdr); |
| 451 |
|
| 452 |
if (VERBOSE_LEVEL>7) |
| 453 |
mexPrintf("757: abort mexSLOAD - sopen failed\n"); |
| 454 |
|
| 455 |
#ifdef mexSOPEN |
| 456 |
plhs[0] = HDR; |
| 457 |
#else |
| 458 |
plhs[0] = mxCreateDoubleMatrix(0,0, mxREAL); |
| 459 |
plhs[1] = HDR; |
| 460 |
#endif |
| 461 |
if (VERBOSE_LEVEL>7) |
| 462 |
mexPrintf("777: abort mexSLOAD - sopen failed\n"); |
| 463 |
|
| 464 |
return; |
| 465 |
} |
| 466 |
|
| 467 |
#ifdef CHOLMOD_H |
| 468 |
RerefCHANNEL(hdr,rr,2); |
| 469 |
#endif |
| 470 |
|
| 471 |
if (!hdr->FLAG.OVERFLOWDETECTION && FlagOverflowDetection) |
| 472 |
mexPrintf("Warning %s: Overflowdetection not supported in file %s\n", __FILE__, hdr->FileName); |
| 473 |
if (hdr->FLAG.UCAL != FlagUCAL) |
| 474 |
mexPrintf("Warning %s: Flag UCAL is %i instead of %i (%s)\n", __FILE__, hdr->FLAG.UCAL, FlagUCAL, hdr->FileName); |
| 475 |
|
| 476 |
|
| 477 |
if (VERBOSE_LEVEL>7) |
| 478 |
fprintf(stderr,"[112] SOPEN-R finished NS=%i %i\n",hdr->NS,NS); |
| 479 |
|
| 480 |
// convert2to4_eventtable(hdr); |
| 481 |
|
| 482 |
#ifdef CHOLMOD_H |
| 483 |
if (hdr->Calib != NULL) { |
| 484 |
NS = hdr->Calib->ncol; |
| 485 |
} |
| 486 |
else |
| 487 |
#endif |
| 488 |
if ((NS<0) || ((NS==1) && (ChanList[0] == 0.0))) { // all channels |
| 489 |
for (k=0, NS=0; k<hdr->NS; ++k) { |
| 490 |
if (hdr->CHANNEL[k].OnOff) NS++; |
| 491 |
} |
| 492 |
} |
| 493 |
else { |
| 494 |
for (k=0; k<hdr->NS; ++k) |
| 495 |
hdr->CHANNEL[k].OnOff = 0; // reset |
| 496 |
for (k=0; k<NS; ++k) { |
| 497 |
int ch = (int)ChanList[k]; |
| 498 |
if ((ch < 1) || (ch > hdr->NS)) |
| 499 |
mexPrintf("Invalid channel number CHAN(%i) = %i!\n",k+1,ch); |
| 500 |
else |
| 501 |
hdr->CHANNEL[ch-1].OnOff = 1; // set |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
if (VERBOSE_LEVEL>7) |
| 506 |
fprintf(stderr,"[113] NS=%i %i\n",hdr->NS,NS); |
| 507 |
|
| 508 |
#ifndef mexSOPEN |
| 509 |
if (hdr->FLAG.ROW_BASED_CHANNELS) |
| 510 |
plhs[0] = mxCreateDoubleMatrix(NS, hdr->NRec*hdr->SPR, mxREAL); |
| 511 |
else |
| 512 |
plhs[0] = mxCreateDoubleMatrix(hdr->NRec*hdr->SPR, NS, mxREAL); |
| 513 |
|
| 514 |
count = sread(mxGetPr(plhs[0]), 0, hdr->NRec, hdr); |
| 515 |
hdr->NRec = count; |
| 516 |
#endif |
| 517 |
sclose(hdr); |
| 518 |
#ifdef CHOLMOD_H |
| 519 |
if (hdr->Calib && hdr->rerefCHANNEL) { |
| 520 |
hdr->NS = hdr->Calib->ncol; |
| 521 |
free(hdr->CHANNEL); |
| 522 |
hdr->CHANNEL = hdr->rerefCHANNEL; |
| 523 |
hdr->rerefCHANNEL = NULL; |
| 524 |
hdr->Calib = NULL; |
| 525 |
} |
| 526 |
#endif |
| 527 |
if ((status=serror2(hdr))) return; |
| 528 |
|
| 529 |
if (VERBOSE_LEVEL>7) |
| 530 |
fprintf(stderr,"\n[129] SREAD/SCLOSE on %s successful [%i,%i] [%i,%i] %i.\n",hdr->FileName,(int)hdr->data.size[0],(int)hdr->data.size[1],(int)hdr->NRec,(int)count,(int)NS); |
| 531 |
|
| 532 |
|
| 533 |
// hdr2ascii(hdr,stderr,4); |
| 534 |
|
| 535 |
#ifndef mexSOPEN |
| 536 |
|
| 537 |
if (nlhs>1) { |
| 538 |
#endif |
| 539 |
|
| 540 |
char* mexFileName = (char*)mxMalloc(strlen(hdr->FileName)+1); |
| 541 |
|
| 542 |
mxArray *tmp, *tmp2, *Patient, *Manufacturer, *ID, *EVENT, *Filter, *Flag, *FileType; |
| 543 |
uint16_t numfields; |
| 544 |
const char *fnames[] = {"TYPE","VERSION","FileName","T0","tzmin","Patient",\ |
| 545 |
"HeadLen","NS","SPR","NRec","SampleRate", "FLAG", \ |
| 546 |
"EVENT","Label","LeadIdCode","PhysDimCode","PhysDim","Filter",\ |
| 547 |
"PhysMax","PhysMin","DigMax","DigMin","Transducer","Cal","Off","GDFTYP","TOffset",\ |
| 548 |
"ELEC","Impedance","fZ","AS","Dur","REC","Manufacturer",NULL}; |
| 549 |
|
| 550 |
for (numfields=0; fnames[numfields++] != NULL; ); |
| 551 |
HDR = mxCreateStructMatrix(1, 1, --numfields, fnames); |
| 552 |
|
| 553 |
mxSetField(HDR,0,"TYPE",mxCreateString(GetFileTypeString(hdr->TYPE))); |
| 554 |
mxSetField(HDR,0,"HeadLen",mxCreateDoubleScalar(hdr->HeadLen)); |
| 555 |
mxSetField(HDR,0,"VERSION",mxCreateDoubleScalar(hdr->VERSION)); |
| 556 |
mxSetField(HDR,0,"NS",mxCreateDoubleScalar(NS)); |
| 557 |
mxSetField(HDR,0,"SPR",mxCreateDoubleScalar(hdr->SPR)); |
| 558 |
mxSetField(HDR,0,"NRec",mxCreateDoubleScalar(hdr->NRec)); |
| 559 |
mxSetField(HDR,0,"SampleRate",mxCreateDoubleScalar(hdr->SampleRate)); |
| 560 |
mxSetField(HDR,0,"Dur",mxCreateDoubleScalar(hdr->SPR/hdr->SampleRate)); |
| 561 |
mxSetField(HDR,0,"FileName",mxCreateString(hdr->FileName)); |
| 562 |
|
| 563 |
mxSetField(HDR,0,"T0",mxCreateDoubleScalar(ldexp(hdr->T0,-32))); |
| 564 |
mxSetField(HDR,0,"tzmin",mxCreateDoubleScalar(hdr->tzmin)); |
| 565 |
|
| 566 |
/* Channel information */ |
| 567 |
#ifdef CHOLMOD_H |
| 568 |
/* |
| 569 |
if (hdr->Calib == NULL) { // is refering to &RR, do not destroy |
| 570 |
mxArray *Calib = mxCreateDoubleMatrix(hdr->Calib->nrow, hdr->Calib->ncol, mxREAL); |
| 571 |
|
| 572 |
} |
| 573 |
*/ |
| 574 |
#endif |
| 575 |
mxArray *LeadIdCode = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 576 |
mxArray *PhysDimCode = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 577 |
mxArray *GDFTYP = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 578 |
mxArray *PhysMax = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 579 |
mxArray *PhysMin = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 580 |
mxArray *DigMax = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 581 |
mxArray *DigMin = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 582 |
mxArray *Cal = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 583 |
mxArray *Off = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 584 |
mxArray *Toffset = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 585 |
mxArray *ELEC_POS = mxCreateDoubleMatrix(NS,3, mxREAL); |
| 586 |
/* |
| 587 |
mxArray *ELEC_Orient = mxCreateDoubleMatrix(NS,3, mxREAL); |
| 588 |
mxArray *ELEC_Area = mxCreateDoubleMatrix(NS,1, mxREAL); |
| 589 |
*/ |
| 590 |
mxArray *LowPass = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 591 |
mxArray *HighPass = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 592 |
mxArray *Notch = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 593 |
mxArray *Impedance = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 594 |
mxArray *fZ = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 595 |
mxArray *SPR = mxCreateDoubleMatrix(1,NS, mxREAL); |
| 596 |
mxArray *Label = mxCreateCellMatrix(NS,1); |
| 597 |
mxArray *Transducer = mxCreateCellMatrix(NS,1); |
| 598 |
mxArray *PhysDim1 = mxCreateCellMatrix(NS,1); |
| 599 |
|
| 600 |
for (k=0,k1=0; k1<NS; ++k) |
| 601 |
if (hdr->CHANNEL[k].OnOff) { |
| 602 |
*(mxGetPr(LeadIdCode)+k1) = (double)hdr->CHANNEL[k].LeadIdCode; |
| 603 |
*(mxGetPr(PhysDimCode)+k1) = (double)hdr->CHANNEL[k].PhysDimCode; |
| 604 |
*(mxGetPr(GDFTYP)+k1) = (double)hdr->CHANNEL[k].GDFTYP; |
| 605 |
*(mxGetPr(PhysMax)+k1) = (double)hdr->CHANNEL[k].PhysMax; |
| 606 |
*(mxGetPr(PhysMin)+k1) = (double)hdr->CHANNEL[k].PhysMin; |
| 607 |
*(mxGetPr(DigMax)+k1) = (double)hdr->CHANNEL[k].DigMax; |
| 608 |
*(mxGetPr(DigMin)+k1) = (double)hdr->CHANNEL[k].DigMin; |
| 609 |
*(mxGetPr(Toffset)+k1) = (double)hdr->CHANNEL[k].TOffset; |
| 610 |
*(mxGetPr(Cal)+k1) = (double)hdr->CHANNEL[k].Cal; |
| 611 |
*(mxGetPr(Off)+k1) = (double)hdr->CHANNEL[k].Off; |
| 612 |
*(mxGetPr(SPR)+k1) = (double)hdr->CHANNEL[k].SPR; |
| 613 |
*(mxGetPr(LowPass)+k1) = (double)hdr->CHANNEL[k].LowPass; |
| 614 |
*(mxGetPr(HighPass)+k1) = (double)hdr->CHANNEL[k].HighPass; |
| 615 |
*(mxGetPr(Notch)+k1) = (double)hdr->CHANNEL[k].Notch; |
| 616 |
*(mxGetPr(Impedance)+k1) = (double)hdr->CHANNEL[k].Impedance; |
| 617 |
*(mxGetPr(fZ)+k1) = (double)hdr->CHANNEL[k].fZ; |
| 618 |
*(mxGetPr(ELEC_POS)+k1) = (double)hdr->CHANNEL[k].XYZ[0]; |
| 619 |
*(mxGetPr(ELEC_POS)+k1+NS) = (double)hdr->CHANNEL[k].XYZ[1]; |
| 620 |
*(mxGetPr(ELEC_POS)+k1+NS*2) = (double)hdr->CHANNEL[k].XYZ[2]; |
| 621 |
/* |
| 622 |
*(mxGetPr(ELEC_Orient)+k1) = (double)hdr->CHANNEL[k].Orientation[0]; |
| 623 |
*(mxGetPr(ELEC_Orient)+k1+NS) = (double)hdr->CHANNEL[k].Orientation[1]; |
| 624 |
*(mxGetPr(ELEC_Orient)+k1+NS*2) = (double)hdr->CHANNEL[k].Orientation[2]; |
| 625 |
*(mxGetPr(ELEC_Area)+k1) = (double)hdr->CHANNEL[k].Area; |
| 626 |
*/ |
| 627 |
mxSetCell(Label,k1,mxCreateString(hdr->CHANNEL[k].Label ? hdr->CHANNEL[k].Label : "")); |
| 628 |
mxSetCell(Transducer,k1,mxCreateString(hdr->CHANNEL[k].Transducer ? hdr->CHANNEL[k].Transducer : "")); |
| 629 |
|
| 630 |
mxSetCell(PhysDim1,k1,mxCreateString(PhysDim3(hdr->CHANNEL[k].PhysDimCode))); |
| 631 |
k1++; |
| 632 |
} |
| 633 |
|
| 634 |
mxSetField(HDR,0,"LeadIdCode",LeadIdCode); |
| 635 |
mxSetField(HDR,0,"PhysDimCode",PhysDimCode); |
| 636 |
mxSetField(HDR,0,"GDFTYP",GDFTYP); |
| 637 |
mxSetField(HDR,0,"PhysMax",PhysMax); |
| 638 |
mxSetField(HDR,0,"PhysMin",PhysMin); |
| 639 |
mxSetField(HDR,0,"DigMax",DigMax); |
| 640 |
mxSetField(HDR,0,"DigMin",DigMin); |
| 641 |
mxSetField(HDR,0,"TOffset",Toffset); |
| 642 |
mxSetField(HDR,0,"Cal",Cal); |
| 643 |
mxSetField(HDR,0,"Off",Off); |
| 644 |
mxSetField(HDR,0,"Impedance",Impedance); |
| 645 |
mxSetField(HDR,0,"fZ",fZ); |
| 646 |
mxSetField(HDR,0,"Off",Off); |
| 647 |
mxSetField(HDR,0,"PhysDim",PhysDim1); |
| 648 |
mxSetField(HDR,0,"Transducer",Transducer); |
| 649 |
mxSetField(HDR,0,"Label",Label); |
| 650 |
|
| 651 |
const char* field[] = {"XYZ",NULL}; |
| 652 |
for (numfields=0; field[numfields++] != 0; ); |
| 653 |
tmp = mxCreateStructMatrix(1, 1, --numfields, field); |
| 654 |
mxSetField(tmp,0,"XYZ",ELEC_POS); |
| 655 |
/* |
| 656 |
mxSetField(tmp,0,"Orientation",ELEC_Orient); |
| 657 |
mxSetField(tmp,0,"Area",ELEC_Area); |
| 658 |
*/ |
| 659 |
mxSetField(HDR,0,"ELEC",tmp); |
| 660 |
|
| 661 |
const char* field2[] = {"SPR",NULL}; |
| 662 |
for (numfields=0; field2[numfields++] != 0; ); |
| 663 |
tmp2 = mxCreateStructMatrix(1, 1, --numfields, field2); |
| 664 |
mxSetField(tmp2,0,"SPR",SPR); |
| 665 |
if (hdr->AS.bci2000!=NULL) { |
| 666 |
mxAddField(tmp2, "BCI2000"); |
| 667 |
mxSetField(tmp2,0,"BCI2000",mxCreateString(hdr->AS.bci2000)); |
| 668 |
} |
| 669 |
if (hdr->TYPE==Sigma) { |
| 670 |
mxAddField(tmp2, "H1"); |
| 671 |
mxSetField(tmp2,0,"H1",mxCreateString((char*)hdr->AS.Header)); |
| 672 |
} |
| 673 |
mxSetField(HDR,0,"AS",tmp2); |
| 674 |
|
| 675 |
/* FLAG */ |
| 676 |
const char* field3[] = {"UCAL","OVERFLOWDETECTION","ROW_BASED_CHANNELS",NULL}; |
| 677 |
for (numfields=0; field3[numfields++] != 0; ); |
| 678 |
Flag = mxCreateStructMatrix(1, 1, --numfields, field3); |
| 679 |
#ifdef MX_API_VER |
| 680 |
//#if 1 |
| 681 |
// Matlab, Octave 3.6.1 |
| 682 |
mxSetField(Flag,0,"UCAL",mxCreateLogicalScalar(hdr->FLAG.UCAL)); |
| 683 |
mxSetField(Flag,0,"OVERFLOWDETECTION",mxCreateLogicalScalar(hdr->FLAG.OVERFLOWDETECTION)); |
| 684 |
mxSetField(Flag,0,"ROW_BASED_CHANNELS",mxCreateLogicalScalar(hdr->FLAG.ROW_BASED_CHANNELS)); |
| 685 |
#else |
| 686 |
// mxCreateLogicalScalar are not included in Octave 3.0 |
| 687 |
mxSetField(Flag,0,"UCAL",mxCreateDoubleScalar(hdr->FLAG.UCAL)); |
| 688 |
mxSetField(Flag,0,"OVERFLOWDETECTION",mxCreateDoubleScalar(hdr->FLAG.OVERFLOWDETECTION)); |
| 689 |
mxSetField(Flag,0,"ROW_BASED_CHANNELS",mxCreateDoubleScalar(hdr->FLAG.ROW_BASED_CHANNELS)); |
| 690 |
#endif |
| 691 |
mxSetField(HDR,0,"FLAG",Flag); |
| 692 |
|
| 693 |
/* Filter */ |
| 694 |
const char *filter_fields[] = {"HighPass","LowPass","Notch",NULL}; |
| 695 |
for (numfields=0; filter_fields[numfields++] != 0; ); |
| 696 |
Filter = mxCreateStructMatrix(1, 1, --numfields, filter_fields); |
| 697 |
mxSetField(Filter,0,"LowPass",LowPass); |
| 698 |
mxSetField(Filter,0,"HighPass",HighPass); |
| 699 |
mxSetField(Filter,0,"Notch",Notch); |
| 700 |
mxSetField(HDR,0,"Filter",Filter); |
| 701 |
|
| 702 |
/* annotation, marker, event table */ |
| 703 |
const char *event_fields[] = {"SampleRate","TYP","POS","DUR","CHN","Desc",NULL}; |
| 704 |
|
| 705 |
if (hdr->EVENT.DUR == NULL) |
| 706 |
EVENT = mxCreateStructMatrix(1, 1, 3, event_fields); |
| 707 |
else { |
| 708 |
EVENT = mxCreateStructMatrix(1, 1, 5, event_fields); |
| 709 |
mxArray *DUR = mxCreateDoubleMatrix(hdr->EVENT.N,1, mxREAL); |
| 710 |
mxArray *CHN = mxCreateDoubleMatrix(hdr->EVENT.N,1, mxREAL); |
| 711 |
for (k=0; k<hdr->EVENT.N; ++k) { |
| 712 |
*(mxGetPr(DUR)+k) = (double)hdr->EVENT.DUR[k]; |
| 713 |
*(mxGetPr(CHN)+k) = (double)hdr->EVENT.CHN[k]; // channels use a 1-based index, 0 indicates all channels |
| 714 |
} |
| 715 |
mxSetField(EVENT,0,"DUR",DUR); |
| 716 |
mxSetField(EVENT,0,"CHN",CHN); |
| 717 |
} |
| 718 |
|
| 719 |
if (hdr->EVENT.CodeDesc != NULL) { |
| 720 |
mxAddField(EVENT, "CodeDesc"); |
| 721 |
mxArray *CodeDesc = mxCreateCellMatrix(hdr->EVENT.LenCodeDesc-1,1); |
| 722 |
for (k=1; k < hdr->EVENT.LenCodeDesc; ++k) { |
| 723 |
mxSetCell(CodeDesc,k-1,mxCreateString(hdr->EVENT.CodeDesc[k])); |
| 724 |
} |
| 725 |
mxSetField(EVENT,0,"CodeDesc",CodeDesc); |
| 726 |
} |
| 727 |
|
| 728 |
mxArray *TYP = mxCreateDoubleMatrix(hdr->EVENT.N,1, mxREAL); |
| 729 |
mxArray *POS = mxCreateDoubleMatrix(hdr->EVENT.N,1, mxREAL); |
| 730 |
|
| 731 |
for (k=0; k<hdr->EVENT.N; ++k) { |
| 732 |
*(mxGetPr(TYP)+k) = (double)hdr->EVENT.TYP[k]; |
| 733 |
*(mxGetPr(POS)+k) = (double)hdr->EVENT.POS[k]+1; // conversion from 0-based to 1-based indexing |
| 734 |
} |
| 735 |
mxSetField(EVENT,0,"TYP",TYP); |
| 736 |
mxSetField(EVENT,0,"POS",POS); |
| 737 |
|
| 738 |
#if (BIOSIG_VERSION >= 10500) |
| 739 |
if (hdr->EVENT.TimeStamp) { |
| 740 |
mxArray *TimeStamp = mxCreateDoubleMatrix(hdr->EVENT.N,1, mxREAL); |
| 741 |
for (k=0; k<hdr->EVENT.N; ++k) { |
| 742 |
*(mxGetPr(TimeStamp)+k) = ldexp(hdr->EVENT.TimeStamp[k],-32); |
| 743 |
} |
| 744 |
mxAddField(EVENT, "TimeStamp"); |
| 745 |
mxSetField(EVENT,0,"TimeStamp",TimeStamp); |
| 746 |
} |
| 747 |
#endif |
| 748 |
mxSetField(EVENT,0,"SampleRate",mxCreateDoubleScalar(hdr->EVENT.SampleRate)); |
| 749 |
mxSetField(HDR,0,"EVENT",EVENT); |
| 750 |
|
| 751 |
/* Record identification */ |
| 752 |
const char *ID_fields[] = {"Recording","Technician","Hospital","Equipment","IPaddr",NULL}; |
| 753 |
for (numfields=0; ID_fields[numfields++] != 0; ); |
| 754 |
ID = mxCreateStructMatrix(1, 1, --numfields, ID_fields); |
| 755 |
mxSetField(ID,0,"Recording",mxCreateString(hdr->ID.Recording)); |
| 756 |
mxSetField(ID,0,"Technician",mxCreateString(hdr->ID.Technician)); |
| 757 |
mxSetField(ID,0,"Hospital",mxCreateString(hdr->ID.Hospital)); |
| 758 |
mxSetField(ID,0,"Equipment",mxCreateString((char*)&hdr->ID.Equipment)); |
| 759 |
int len = 4; |
| 760 |
uint8_t IPv6=0; |
| 761 |
for (k=4; k<16; k++) IPv6 |= hdr->IPaddr[k]; |
| 762 |
if (IPv6) len=16; |
| 763 |
mxArray *IPaddr = mxCreateNumericMatrix(1,len,mxUINT8_CLASS,mxREAL); |
| 764 |
memcpy(mxGetData(IPaddr),hdr->IPaddr,len); |
| 765 |
mxSetField(ID,0,"IPaddr",IPaddr); |
| 766 |
mxSetField(HDR,0,"REC",ID); |
| 767 |
|
| 768 |
/* Patient Information */ |
| 769 |
const char *patient_fields[] = {"Sex","Handedness","Id","Name","Weight","Height","Birthday",NULL}; |
| 770 |
for (numfields=0; patient_fields[numfields++] != 0; ); |
| 771 |
Patient = mxCreateStructMatrix(1, 1, --numfields, patient_fields); |
| 772 |
const char *strarray; |
| 773 |
#ifdef __LIBBIOSIG2_H__ |
| 774 |
strarray = biosig_get_patient_name(hdr); |
| 775 |
mxSetField(Patient,0,"Name",mxCreateCharMatrixFromStrings(strarray!=NULL, &strarray)); |
| 776 |
|
| 777 |
strarray = biosig_get_patient_id(hdr); |
| 778 |
mxSetField(Patient,0,"Id",mxCreateCharMatrixFromStrings(strarray!=NULL, &strarray)); |
| 779 |
#else |
| 780 |
strarray = hdr->Patient.Name; |
| 781 |
mxSetField(Patient,0,"Name",mxCreateCharMatrixFromStrings(strarray!=NULL, &strarray)); |
| 782 |
|
| 783 |
strarray = hdr->Patient.Id; |
| 784 |
mxSetField(Patient,0,"Id",mxCreateCharMatrixFromStrings(strarray!=NULL, &strarray)); |
| 785 |
#endif |
| 786 |
mxSetField(Patient,0,"Handedness",mxCreateDoubleScalar(hdr->Patient.Handedness)); |
| 787 |
|
| 788 |
mxSetField(Patient,0,"Sex",mxCreateDoubleScalar(hdr->Patient.Sex)); |
| 789 |
mxSetField(Patient,0,"Weight",mxCreateDoubleScalar((double)hdr->Patient.Weight)); |
| 790 |
mxSetField(Patient,0,"Height",mxCreateDoubleScalar((double)hdr->Patient.Height)); |
| 791 |
mxSetField(Patient,0,"Birthday",mxCreateDoubleScalar(ldexp(hdr->Patient.Birthday,-32))); |
| 792 |
|
| 793 |
double d; |
| 794 |
if (hdr->Patient.Weight==0) d = NAN; // not-a-number |
| 795 |
else if (hdr->Patient.Weight==255) d = INFINITY; // Overflow |
| 796 |
else d = (double)hdr->Patient.Weight; |
| 797 |
mxSetField(Patient,0,"Weight",mxCreateDoubleScalar(d)); |
| 798 |
|
| 799 |
if (hdr->Patient.Height==0) d = NAN; // not-a-number |
| 800 |
else if (hdr->Patient.Height==255) d = INFINITY; // Overflow |
| 801 |
else d = (double)hdr->Patient.Height; |
| 802 |
mxSetField(Patient,0,"Height",mxCreateDoubleScalar(d)); |
| 803 |
|
| 804 |
/* Manufacturer Information */ |
| 805 |
const char *manufacturer_fields[] = {"Name","Model","Version","SerialNumber",NULL}; |
| 806 |
for (numfields=0; manufacturer_fields[numfields++] != 0; ); |
| 807 |
Manufacturer = mxCreateStructMatrix(1, 1, --numfields, manufacturer_fields); |
| 808 |
|
| 809 |
#ifdef __LIBBIOSIG2_H__ |
| 810 |
strarray = biosig_get_manufacturer_name(hdr); |
| 811 |
mxSetField(Manufacturer,0,"Name",mxCreateCharMatrixFromStrings(strarray!=NULL, &strarray)); |
| 812 |
|
| 813 |
strarray = biosig_get_manufacturer_model(hdr); |
| 814 |
mxSetField(Manufacturer,0,"Model",mxCreateCharMatrixFromStrings(strarray!=NULL, &strarray)); |
| 815 |
|
| 816 |
strarray = biosig_get_manufacturer_version(hdr); |
| 817 |
mxSetField(Manufacturer,0,"Version",mxCreateCharMatrixFromStrings(strarray!=NULL, &strarray)); |
| 818 |
|
| 819 |
strarray = biosig_get_manufacturer_serial_number(hdr); |
| 820 |
mxSetField(Manufacturer,0,"SerialNumber",mxCreateCharMatrixFromStrings(strarray!=NULL, &strarray)); |
| 821 |
|
| 822 |
#else |
| 823 |
strarray = hdr->ID.Manufacturer.Name; |
| 824 |
mxSetField(Manufacturer,0,"Name",mxCreateCharMatrixFromStrings(strarray!=NULL, &strarray)); |
| 825 |
|
| 826 |
strarray = hdr->ID.Manufacturer.Model; |
| 827 |
mxSetField(Manufacturer,0,"Model",mxCreateCharMatrixFromStrings(strarray!=NULL, &strarray)); |
| 828 |
|
| 829 |
strarray = hdr->ID.Manufacturer.Version; |
| 830 |
mxSetField(Manufacturer,0,"Version",mxCreateCharMatrixFromStrings(strarray!=NULL, &strarray)); |
| 831 |
|
| 832 |
strarray = hdr->ID.Manufacturer.SerialNumber; |
| 833 |
mxSetField(Manufacturer,0,"SerialNumber",mxCreateCharMatrixFromStrings(strarray!=NULL, &strarray)); |
| 834 |
#endif |
| 835 |
mxSetField(HDR,0,"Manufacturer",Manufacturer); |
| 836 |
|
| 837 |
|
| 838 |
if (VERBOSE_LEVEL>7) |
| 839 |
fprintf(stdout,"[148] going for SCLOSE\n"); |
| 840 |
|
| 841 |
mxSetField(HDR,0,"Patient",Patient); |
| 842 |
|
| 843 |
#ifndef mexSOPEN |
| 844 |
plhs[1] = HDR; |
| 845 |
} |
| 846 |
#else |
| 847 |
plhs[0] = HDR; |
| 848 |
#endif |
| 849 |
|
| 850 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"[151] going for SCLOSE\n"); |
| 851 |
#ifdef CHOLMOD_H |
| 852 |
hdr->Calib = NULL; // is refering to &RR, do not destroy |
| 853 |
#endif |
| 854 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"[156] SCLOSE finished\n"); |
| 855 |
destructHDR(hdr); |
| 856 |
hdr = NULL; |
| 857 |
if (VERBOSE_LEVEL>7) fprintf(stdout,"[157] SCLOSE finished\n"); |
| 858 |
}; |
| 859 |
|