| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include <assert.h> |
| 25 |
#include <ctype.h> |
| 26 |
#include <stddef.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
#ifdef WITH_MATIO |
| 31 |
#include <matio.h> |
| 32 |
#endif |
| 33 |
#include "../biosig.h" |
| 34 |
|
| 35 |
|
| 36 |
int sopen_matlab(HDRTYPE* hdr) { |
| 37 |
#ifdef WITH_MATIO |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
ifclose(hdr); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
fprintf(stdout, "Trying to read Matlab data using MATIO v%i.%i.%i\n", MATIO_MAJOR_VERSION, MATIO_MINOR_VERSION, MATIO_RELEASE_LEVEL); |
| 51 |
|
| 52 |
mat_t *matfile = Mat_Open(hdr->FileName, MAT_ACC_RDONLY); |
| 53 |
matvar_t *EEG=NULL, *pnts=NULL, *nbchan=NULL, *trials=NULL, *srate=NULL, *data=NULL, *chanlocs=NULL, *event=NULL; |
| 54 |
if (matfile != NULL) { |
| 55 |
EEG = Mat_VarRead(matfile, "EEG" ); |
| 56 |
if (EEG != NULL) { |
| 57 |
Mat_VarReadDataAll(matfile, EEG ); |
| 58 |
pnts = Mat_VarGetStructField(EEG, "pnts", MAT_BY_NAME, 0); |
| 59 |
nbchan = Mat_VarGetStructField(EEG, "nbchan", MAT_BY_NAME, 0); |
| 60 |
trials = Mat_VarGetStructField(EEG, "trials", MAT_BY_NAME, 0); |
| 61 |
srate = Mat_VarGetStructField(EEG, "srate", MAT_BY_NAME, 0); |
| 62 |
data = Mat_VarGetStructField(EEG, "data", MAT_BY_NAME, 0); |
| 63 |
chanlocs = Mat_VarGetStructField(EEG, "chanlocs", MAT_BY_NAME, 0); |
| 64 |
event = Mat_VarGetStructField(EEG, "event", MAT_BY_NAME, 0); |
| 65 |
|
| 66 |
hdr->NS = *(double*)(nbchan->data); |
| 67 |
hdr->SPR = *(double*)(pnts->data); |
| 68 |
hdr->NRec= *(double*)(trials->data); |
| 69 |
hdr->SampleRate = *(double*)(srate->data); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
uint16_t gdftyp = 16; |
| 77 |
hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE)); |
| 78 |
size_t k; |
| 79 |
for (k=0; k<hdr->NS; k++) { |
| 80 |
CHANNEL_TYPE *hc = hdr->CHANNEL+k; |
| 81 |
sprintf(hc->Label,"#%2d",k+1); |
| 82 |
hc->SPR = hdr->SPR; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
size_t sz = hdr->NS*hdr->SPR*hdr->NRec*GDFTYP_BITS[gdftyp]>>3; |
| 103 |
hdr->AS.rawdata = realloc(hdr->AS.rawdata, sz); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
hdr->EVENT.N = 0; |
| 108 |
hdr->EVENT.POS = (uint32_t*) realloc(hdr->EVENT.POS, hdr->EVENT.N*sizeof(*hdr->EVENT.POS)); |
| 109 |
hdr->EVENT.TYP = (uint16_t*) realloc(hdr->EVENT.TYP, hdr->EVENT.N*sizeof(*hdr->EVENT.TYP)); |
| 110 |
hdr->EVENT.DUR = (uint32_t*) realloc(hdr->EVENT.DUR, hdr->EVENT.N*sizeof(*hdr->EVENT.DUR)); |
| 111 |
hdr->EVENT.CHN = (uint16_t*) realloc(hdr->EVENT.CHN, hdr->EVENT.N*sizeof(*hdr->EVENT.CHN)); |
| 112 |
for (k=0; k<hdr->EVENT.N; k++) { |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
hdr->AS.bpb = hdr->NS*2; |
| 123 |
hdr->FLAG.OVERFLOWDETECTION = 0; |
| 124 |
|
| 125 |
|
| 126 |
Mat_VarPrint(pnts, 1); |
| 127 |
Mat_VarPrint(nbchan, 1); |
| 128 |
Mat_VarPrint(trials, 1); |
| 129 |
Mat_VarPrint(srate, 1); |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
Mat_VarFree(EEG); |
| 136 |
} |
| 137 |
|
| 138 |
Mat_Close(matfile); |
| 139 |
} |
| 140 |
|
| 141 |
biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Error reading MATLAB file"); |
| 142 |
return(-1); |
| 143 |
|
| 144 |
#else |
| 145 |
biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "SOPEN(MATIO): - matlab format not supported - libbiosig need to be recompiled with libmatio support."); |
| 146 |
ifclose(hdr); |
| 147 |
return(-1); |
| 148 |
#endif |
| 149 |
} |
| 150 |
|