| 1 |
/* |
| 2 |
|
| 3 |
Copyright (C) 2011,2013,2015,2019 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 <physicalunits.h> |
| 16 |
|
| 17 |
#ifdef tmwtypes_h |
| 18 |
#if (MX_API_VER<=0x07020000) |
| 19 |
typedef int mwSize; |
| 20 |
#endif |
| 21 |
#endif |
| 22 |
|
| 23 |
// read numeric value and convert to uint16_t |
| 24 |
uint16_t getUINT16(const mxArray *pm, size_t idx) { |
| 25 |
size_t n = mxGetNumberOfElements(pm); |
| 26 |
if (n == 0) return(0); |
| 27 |
if (n <= idx) idx = 0; |
| 28 |
|
| 29 |
switch (mxGetClassID(pm)) { |
| 30 |
/* |
| 31 |
case mxCHAR_CLASS: |
| 32 |
case mxLOGICAL_CLASS: |
| 33 |
case mxINT8_CLASS: |
| 34 |
return(*((int8_t*)mxGetData(pm) + idx)); |
| 35 |
case mxUINT8_CLASS: |
| 36 |
return(*((uint8_t*)mxGetData(pm) + idx)); |
| 37 |
*/ |
| 38 |
case mxDOUBLE_CLASS: |
| 39 |
return(*((double*)mxGetData(pm) + idx)); |
| 40 |
case mxSINGLE_CLASS: |
| 41 |
return(*((float*)mxGetData(pm) + idx)); |
| 42 |
case mxINT16_CLASS: |
| 43 |
return(*((int16_t*)mxGetData(pm) + idx)); |
| 44 |
case mxUINT16_CLASS: |
| 45 |
return(*((uint16_t*)mxGetData(pm) + idx)); |
| 46 |
case mxINT32_CLASS: |
| 47 |
return(*((int32_t*)mxGetData(pm) + idx)); |
| 48 |
case mxUINT32_CLASS: |
| 49 |
return(*((uint32_t*)mxGetData(pm) + idx)); |
| 50 |
case mxINT64_CLASS: |
| 51 |
return(*((int64_t*)mxGetData(pm) + idx)); |
| 52 |
case mxUINT64_CLASS: |
| 53 |
return(*((uint64_t*)mxGetData(pm) + idx)); |
| 54 |
/* |
| 55 |
case mxFUNCTION_CLASS: |
| 56 |
case mxUNKNOWN_CLASS: |
| 57 |
case mxCELL_CLASS: |
| 58 |
case mxSTRUCT_CLASS: |
| 59 |
*/ |
| 60 |
default: |
| 61 |
return(0); |
| 62 |
} |
| 63 |
return(0); |
| 64 |
} |
| 65 |
|
| 66 |
void mexFunction( |
| 67 |
int nlhs, /* number of expected outputs */ |
| 68 |
mxArray *plhs[], /* array of pointers to output arguments */ |
| 69 |
int nrhs, /* number of inputs */ |
| 70 |
const mxArray *prhs[] /* array of pointers to input arguments */ |
| 71 |
) { |
| 72 |
|
| 73 |
const char HELPTEXT[] = |
| 74 |
"PHYSICALUNITS converts PhysDim inte PhysDimCode and vice versa\n" |
| 75 |
" according to Annex A of FEF Vital Signs Format [1] \n" |
| 76 |
"\n" |
| 77 |
" HDR = physicalunits(HDR); \n" |
| 78 |
" adds HDR.PhysDim or HDR.PhysDimCode, if needed \n" |
| 79 |
"\n" |
| 80 |
" PhysDim = physicalunits(PhysDimCode);\n" |
| 81 |
" converts Code of PhysicalUnits into descriptive physical units\n" |
| 82 |
"\n" |
| 83 |
" PhysDimCode = physicalunits(PhysDim);\n" |
| 84 |
" converts descriptive units into Code for physical units.\n" |
| 85 |
"\n" |
| 86 |
" [..., scale] = physicalunits(...);\n" |
| 87 |
" scale contains the scaling factor of the decimal prefix\n" |
| 88 |
"\n" |
| 89 |
" Reference(s):\n" |
| 90 |
" ISO/IEEE 11073-10101:2004\n" |
| 91 |
" Health Informatics - Point-of-care medical device communications - Part 10101: Nomenclature\n" |
| 92 |
" p.62-75. Table A.6.3: Vital signs units of measurements\n"; |
| 93 |
|
| 94 |
if (nrhs<1) { |
| 95 |
mexPrintf( HELPTEXT); |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
const mxArray *mxPhysDim_Input = NULL; |
| 100 |
const mxArray *mxPhysDimCode_Input = NULL; |
| 101 |
mxArray *mxPhysDim_Output = NULL; |
| 102 |
mxArray *mxPhysDimCode_Output = NULL; |
| 103 |
mxArray *mxScale = NULL; |
| 104 |
|
| 105 |
mwSize nDims = 0; |
| 106 |
mwSize Numel = 1; |
| 107 |
const mwSize *SZ = NULL; |
| 108 |
mwSize k; |
| 109 |
|
| 110 |
/************************************************************* |
| 111 |
Input |
| 112 |
*************************************************************/ |
| 113 |
mxClassID Type = mxGetClassID(prhs[0]); |
| 114 |
/* process input arguments */ |
| 115 |
switch (Type) { |
| 116 |
case mxSTRUCT_CLASS: { |
| 117 |
mxPhysDim_Input = mxGetField(prhs[0], 0, "PhysDim"); |
| 118 |
mxPhysDimCode_Input = mxGetField(prhs[0], 0, "PhysDimCode"); |
| 119 |
break; |
| 120 |
} |
| 121 |
case mxINT16_CLASS: |
| 122 |
case mxINT32_CLASS: |
| 123 |
case mxINT64_CLASS: |
| 124 |
case mxUINT16_CLASS: |
| 125 |
case mxUINT32_CLASS: |
| 126 |
case mxUINT64_CLASS: |
| 127 |
case mxSINGLE_CLASS: |
| 128 |
case mxDOUBLE_CLASS: { |
| 129 |
mxPhysDimCode_Input = prhs[0]; |
| 130 |
break; |
| 131 |
} |
| 132 |
//case mxCHAR_CLASS: |
| 133 |
case mxCELL_CLASS: { |
| 134 |
mxPhysDim_Input = prhs[0]; |
| 135 |
break; |
| 136 |
} |
| 137 |
default: { |
| 138 |
mexPrintf("input arguments not supported\n"); |
| 139 |
return; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// Get dimensions of input argument and set dimensions of output |
| 144 |
if (mxPhysDimCode_Input && !mxPhysDim_Input) { |
| 145 |
nDims = mxGetNumberOfDimensions(mxPhysDimCode_Input); |
| 146 |
SZ = mxGetDimensions(mxPhysDimCode_Input); |
| 147 |
Numel = mxGetNumberOfElements(mxPhysDimCode_Input); |
| 148 |
} |
| 149 |
else if (mxPhysDim_Input && !mxPhysDimCode_Input ) { |
| 150 |
nDims = mxGetNumberOfDimensions(mxPhysDim_Input); |
| 151 |
SZ = mxGetDimensions(mxPhysDim_Input); |
| 152 |
Numel = mxGetNumberOfElements(mxPhysDim_Input); |
| 153 |
} |
| 154 |
|
| 155 |
else if (!mxPhysDim_Input && !mxPhysDimCode_Input) { |
| 156 |
mexPrintf("Warning %s(..): Neither PhysDim nor PhysDimCode defined\n",__FILE__); |
| 157 |
plhs[0] = mxDuplicateArray(prhs[0]); |
| 158 |
return; |
| 159 |
} |
| 160 |
else if (!mxIsCell(mxPhysDim_Input) ) { |
| 161 |
mexErrMsgIdAndTxt(__FILE__":PhysDim_not_a_cell_array", "ERROR: PhysDim must be a cell array, but is not.\n"); |
| 162 |
return; |
| 163 |
} |
| 164 |
else { |
| 165 |
Numel = mxGetNumberOfElements(mxPhysDimCode_Input); |
| 166 |
if (Numel != mxGetNumberOfElements(mxPhysDim_Input)) { |
| 167 |
mexErrMsgIdAndTxt(__FILE__":Size_of_PhysDim_PhysDimCode_do_not_match", "ERROR: number of elements in PhysDim and PhysDimCode do not match.\n"); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
int errorFlag=0; |
| 172 |
for (k=0; k < Numel; k++) { |
| 173 |
uint16_t PDC1 = getUINT16(mxPhysDimCode_Input, k); |
| 174 |
|
| 175 |
const int STRLEN=63; |
| 176 |
char tmpstring[STRLEN+1]; |
| 177 |
mxArray *tmpArr = mxGetCell(mxPhysDim_Input, k); |
| 178 |
mxGetString(tmpArr, tmpstring, STRLEN); |
| 179 |
uint16_t PDC2 = PhysDimCode(tmpstring); |
| 180 |
if (PDC1 != PDC2) { |
| 181 |
mexPrintf("ERROR %s(...): element %d doe not match %d~=%d, <%s>~=<%s>\n",__FILE__, k, PDC1,PDC2,PhysDim3(PDC1),tmpstring); |
| 182 |
errorFlag=1; |
| 183 |
} |
| 184 |
} |
| 185 |
if (errorFlag) { |
| 186 |
mexErrMsgIdAndTxt(__FILE__":some_elements_in_PhysDim_PhysDimCode_do_not_match", "ERROR: some elements in PhysDim and PhysDimCode do not match.\n"); |
| 187 |
return; |
| 188 |
} |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
// allocate 2nd output argument |
| 193 |
if ((nlhs > 1) && (nrhs > 0)) { |
| 194 |
mxScale = mxCreateNumericArray(nDims, SZ, mxDOUBLE_CLASS, mxREAL); |
| 195 |
plhs[1] = mxScale; |
| 196 |
} |
| 197 |
|
| 198 |
/************************************************************* |
| 199 |
Conversion |
| 200 |
*************************************************************/ |
| 201 |
if ( mxPhysDimCode_Input && !mxPhysDim_Input ) { |
| 202 |
mxPhysDim_Output = mxCreateCellArray(nDims, SZ); |
| 203 |
// Conversion: PhysDimCode -> PhysDim |
| 204 |
for (k=0; k < Numel; k++) { |
| 205 |
uint16_t PDC = getUINT16(mxPhysDimCode_Input, k); |
| 206 |
mxSetCell(mxPhysDim_Output, k, mxCreateString( PhysDim3( PDC ))); |
| 207 |
if (mxScale != NULL) |
| 208 |
*((double*)mxGetData(mxScale)+k) = PhysDimScale(PDC); |
| 209 |
} |
| 210 |
} |
| 211 |
else if ( mxPhysDim_Input && !mxPhysDimCode_Input && mxIsCell(mxPhysDim_Input) ) { |
| 212 |
mxPhysDimCode_Output = mxCreateNumericArray(nDims, SZ, mxUINT16_CLASS, mxREAL); |
| 213 |
// Conversion: PhysDim -> PhysDimCode |
| 214 |
for (k=0; k < Numel; k++) { |
| 215 |
const int STRLEN=63; |
| 216 |
char tmpstring[STRLEN+1]; |
| 217 |
|
| 218 |
mxArray *tmpArr = mxGetCell(mxPhysDim_Input, k); |
| 219 |
mxGetString(tmpArr, tmpstring, STRLEN); |
| 220 |
uint16_t PDC = PhysDimCode(tmpstring); |
| 221 |
|
| 222 |
*(((uint16_t*)mxGetData(mxPhysDimCode_Output))+k) = PDC; |
| 223 |
if (mxScale != NULL) |
| 224 |
*((double*)mxGetData(mxScale)+k) = PhysDimScale(PDC); |
| 225 |
} |
| 226 |
} |
| 227 |
else { |
| 228 |
mexPrintf("ERROR %s line %d\n",__FILE__,__LINE__); |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
/************************************************************* |
| 233 |
Output |
| 234 |
*************************************************************/ |
| 235 |
if ( mxIsStruct(prhs[0]) ) { |
| 236 |
plhs[0] = mxDuplicateArray(prhs[0]); |
| 237 |
if (mxPhysDimCode_Output) { |
| 238 |
int n = mxAddField( plhs[0], "PhysDimCode"); |
| 239 |
mxSetFieldByNumber( plhs[0], 0, n, mxDuplicateArray(mxPhysDimCode_Output) ); |
| 240 |
} |
| 241 |
if (mxPhysDim_Output) { |
| 242 |
int n = mxAddField( plhs[0], "PhysDim"); |
| 243 |
mxSetFieldByNumber( plhs[0], 0, n, mxDuplicateArray(mxPhysDim_Output) ); |
| 244 |
} |
| 245 |
} |
| 246 |
else if (mxPhysDim_Input && !mxPhysDimCode_Input) { |
| 247 |
plhs[0] = mxPhysDimCode_Output; |
| 248 |
} |
| 249 |
else if (mxPhysDimCode_Input && !mxPhysDim_Input) { |
| 250 |
plhs[0] = mxPhysDim_Output; |
| 251 |
} |
| 252 |
else { |
| 253 |
mexPrintf("ERROR %s line %d\n",__FILE__,__LINE__); |
| 254 |
return; |
| 255 |
} |
| 256 |
} |