Debian Codegraph @

biosig4c++/examples/generate_gdf_file.c

raw symbols SCIP
1 /*
2 Example how to generate GDF files as C/C++ code.
3 This is only a rough outline, for details, please check biosig.h.
4
5
6 Copyright (C) 2016,2018 Alois Schloegl <[email protected]>
7 This file is part of the "BioSig for C/C++" repository
8 (biosig4c++) at http://biosig.sf.net/
9
10
11 BioSig is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 3
14 of the License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24 */
25
26
27 #include <biosig.h>
28 #include <assert.h>
29 #include <stdlib.h>
30
31 extern int VERBOSE_LEVEL;
32
33 int main(int argc, char* argv[]) {
34 VERBOSE_LEVEL = 8;
35
36 int number_of_channels=4;
37 int number_of_events=0;
38 double sampling_rate = 5000; // Hz
39 size_t total_number_of_samples=100000;
40 size_t number_of_epochs=5;
41
42 HDRTYPE *hdr=constructHDR(number_of_channels, number_of_events);
43
44 /* alternatively, number of channels, and number of events can be set this way.
45 HDRTYPE *hdr=constructHDR(0, 0);
46 biosig_set_number_of_channels(hdr, number_of_channels);
47 biosig_set_number_of_events(hdr, number_of_events);
48 */
49
50 // set sampling rate
51 biosig_set_samplerate(hdr, sampling_rate);
52
53 /*
54 the data layout can be controlled in this way.
55 Here it is assumed, that all channels have the same sampling rate
56
57 It's also possible, the define different sampling rates for different channels
58 through
59 hdr->CHANNEL[ch].SPR
60 and
61 biosig_channel_set_samples_per_record(hdr->CHANNEL[ch], spr);
62
63 */
64 size_t nrec,spr;
65 // multiplexed data
66 spr=1; nrec=total_number_of_samples;
67
68 // channel-based data
69 spr=total_number_of_samples; nrec=1;
70
71 // sweep-based data
72 spr=total_number_of_samples/number_of_epochs; nrec=number_of_epochs;
73
74 // any other combination is fine as long as
75 assert( spr*nrec == total_number_of_samples);
76 biosig_set_number_of_samples(hdr, nrec, spr);
77
78 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
79
80 /*************************************************************
81 define channel properties
82 *************************************************************/
83 double PhysMax=10;
84 double PhysMin=0;
85 double DigMax= 32767;
86 double DigMin=-32768;
87 double LowPass=sampling_rate/4;
88 double HighPass=0.0;
89 double Notch=50; // 0: notch off, NaN: unkown
90 int chan;
91
92 chan=0;
93 CHANNEL_TYPE *CH = biosig_get_channel(hdr, chan);
94 biosig_channel_set_label(CH, "Channel 1");
95 biosig_channel_set_unit(CH,"mV");
96 biosig_channel_set_scaling(CH, PhysMax, PhysMin, DigMax, DigMin);
97 biosig_channel_set_datatype_to_int16(CH);
98 biosig_channel_set_filter(CH, LowPass, HighPass, Notch);
99 biosig_channel_set_transducer(CH, "EEG");
100 biosig_channel_set_timing_offset(CH, 0);
101
102 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
103
104 // do the same for all other channels
105 for (chan++; chan < number_of_channels; chan++) {
106 CHANNEL_TYPE *CH = biosig_get_channel(hdr, chan);
107 biosig_channel_set_label(CH, "Channel 2");
108 biosig_channel_set_unit(CH,"mV");
109 biosig_channel_set_scaling(CH, PhysMax, PhysMin, DigMax, DigMin);
110 biosig_channel_set_datatype_to_int16(CH);
111 biosig_channel_set_filter(CH, LowPass, HighPass, Notch);
112 biosig_channel_set_timing_offset(CH, chan*1e-6); // e.g. 1us delay in ADC multiplexer, 0.0 when simultaneous sampling
113 }
114 CHANNEL_TYPE *ch1 = biosig_get_channel(hdr, 0);
115 CHANNEL_TYPE *ch2 = biosig_get_channel(hdr, 1);
116 CHANNEL_TYPE *ch3 = biosig_get_channel(hdr, 2);
117 CHANNEL_TYPE *ch4 = biosig_get_channel(hdr, 3);
118 biosig_channel_set_samples_per_record(ch2, spr/2);
119 biosig_channel_set_samples_per_record(ch3, spr/4);
120 biosig_channel_set_samples_per_record(ch4, spr/5);
121
122 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
123
124
125 /*************************************************************
126 demographic data
127 *************************************************************/
128 time_t timer;
129 timer=time(NULL);
130 struct tm *StartTime = localtime(&timer);
131 biosig_set_startdatetime(hdr, *StartTime);
132 struct tm birthday;
133 birthday.tm_year=2005-1900;
134 birthday.tm_mon=1;
135 birthday.tm_mday=1;
136 biosig_set_birthdate(hdr, birthday);
137
138 biosig_set_patient_name(hdr, "Anonymous", "A.", NULL);
139 biosig_set_patient_id(hdr, "pseudonym_0123456789");
140 biosig_set_recording_id(hdr, "recording 0000");
141 // biosig_set_technician(hdr, "Ms.Expert"); // by login name is used
142 biosig_set_manufacturer_name(hdr, "Vendor XYZ");
143 biosig_set_manufacturer_model(hdr, "SuperDuper");
144 biosig_set_manufacturer_version(hdr, "alpha");
145 biosig_set_manufacturer_serial_number(hdr, "000000");
146 biosig_set_application_specific_information(hdr, "anything");
147
148 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
149
150
151 /*
152 set markers in event table, this can be called at any time
153 file is closed with SCLOSE.
154 */
155 #if 0
156 // add 3 markers indicating "start-of-new-segment"
157 spr = 5000;
158 biosig_set_nth_event(hdr, 0, 0x7ffe, spr, 0, 0, NULL, NULL);
159 biosig_set_nth_event(hdr, 0, 0x7ffe, 2*spr, 0, 0, NULL, NULL);
160 biosig_set_nth_event(hdr, 0, 0x7ffe, 3*spr, 0, 0, NULL, NULL);
161
162 // add free text annoations, typ must be between 1 and 255 (0xff)
163 biosig_set_nth_event(hdr, 0, 0x01, 7000, 0, 0, NULL, "Marker1");
164 biosig_set_nth_event(hdr, 0, 0, 0, 0, 5000, NULL, "Seg1chan1");
165 biosig_set_nth_event(hdr, 0, 0, 5000, 3, 5000, NULL, "Seg2chan4");
166 biosig_set_nth_event(hdr, 0, 0, 10000, 2, 5000, NULL, "Seg3chan3");
167 #endif
168
169 /*************************************************************
170 open file and write header
171 *************************************************************/
172
173 const char *f="filename.gdf";
174 if (argc>1) f=argv[1];
175
176 hdr->TYPE=GDF;
177 hdr->VERSION=3.0;
178 hdr = sopen(f,"w",hdr);
179
180 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
181
182
183 /*************************************************************
184 write data block:
185 there are high level and low level approaches.
186 high level approaches assume data samples are available
187 in a huge matrix, and swrite will deal with the details,
188 like endian conversion, downsampling of the data, etc.
189
190 the low level approach, assumes that the user, knows how
191 to generate the correct binary layout of the data samples,
192 and will use fwrite(..,..,..,hdr->FILE.FID). This might
193 cause ABI incompatibilities on Windows. Most likely,
194 another API need to be provided by biosig.
195 *************************************************************/
196 size_t i,j,k;
197
198 /* highlevel approach: needs to define data matrix, and its properties
199 BIOSIG_FLAG_UCAL : do not apply scaling factors to convert from physical to digital values
200 if this flag is not set, the scaling factors are applied in SWRITE
201 BIOSIG_FLAG_ROW_BASED_CHANNELS: consecutive samples are from neighboring channel
202 if not defined, all samples of first channel, then all samples form next
203 channel must be available in data.
204 */
205 biosig_set_flag(hdr, BIOSIG_FLAG_UCAL | BIOSIG_FLAG_ROW_BASED_CHANNELS);
206
207 /* lets generate all data
208 1,2,3,4 Hz sinusoid, for each channel, resp.
209 */
210 biosig_data_type* data=(biosig_data_type*)malloc(number_of_channels * total_number_of_samples * sizeof(biosig_data_type));
211 for (k=0; k < number_of_channels * total_number_of_samples; k++)
212 data[k] = 33000 * sin( 1.0*(k%number_of_channels +1 )*(k%total_number_of_samples)*2*M_PI/sampling_rate );
213
214 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
215
216 #if defined(HL1)
217 // option 1: define all data
218 size_t count = swrite(data, nrec, hdr);
219 if (nrec != count) fprintf(stderr,"%s (line %i) error\n",__FILE__,__LINE__);
220
221 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
222
223 #elif defined(HL2)
224 // option 2: define data of a single block
225 data = realloc(data, number_of_channels * spr * sizeof(biosig_data_type));
226 for (k=0; k<nrec; k++) {
227 for (k=0; k < number_of_channels * spr; k++) data[k] = (k % 2^15);
228
229 size_t count = swrite(data, 1, hdr);
230 if (count != 1) fprintf(stderr,"%s (line %i) error\n",__FILE__,__LINE__);
231 }
232
233 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
234
235 #elif 0
236 // this
237 spr=total_number_of_samples/number_of_epochs;
238 for (k=0; k < number_of_epochs; k++) {
239 for (chan=0; chan < number_of_channels; chan++) {
240 for (chan=0; chan < number_of_channels; chan++) {
241 CHANNEL_TYPE *ch = biosig_get_channel(hdr, chan);
242 for (j=0; j < spr; j++) {
243 data[j + chan*spr + k*spr*number_of_channels] = 3;
244
245 // option 1: write sample j channel chan of sweep k
246 // fwrite(&sample[k][chan][j],sizeof(int16_t),1,hdr->FILE.FID);
247 }
248 // option 2: avoid inner loop
249 // fwrite(&sample_block[k][chan],sizeof(int16_t),spr,hdr->FILE.FID);
250 }
251 }
252
253
254 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
255
256
257 /* low level approaches using fwrite - chances are it will
258 not work in Windows because of ABI incompatibilities */
259
260 #elif defined(LL1)
261 /* option 1: all the data is prepared and written at once. */
262 uint16_t *sample_block=(uint16_t*)malloc(hdr->AS.bpb*nrec);
263 for (k=0; k < hdr->AS.bpb * nrec / sizeof(uint16_t); k++)
264 sample_block[k] = 33000 * sin( 1.0*(k%number_of_channels +1 )*(k%total_number_of_samples)*2*M_PI/sampling_rate );
265
266 fwrite(sample_block,hdr->AS.bpb,nrec,hdr->FILE.FID);
267 free(sample_block);
268
269 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
270
271 #elif defined(LL2)
272 uint16_t *sample_block=(uint16_t*)malloc(hdr->AS.bpb);
273 for (k=0; k < hdr->AS.bpb / sizeof(uint16_t); k++)
274 sample_block[k] = 33000 * sin( 1.0*(k%number_of_channels +1 )*(k%total_number_of_samples)*2*M_PI/sampling_rate );
275
276 for (k=0; k < nrec; k++) {
277 /* option 3: the size of a single block is hdr->AS.bpb,
278 a single data block is define, and written at once.
279 change content of sample_block within loop
280 */
281 fwrite(sample_block,hdr->AS.bpb,1,hdr->FILE.FID);
282 }
283 free(sample_block);
284
285 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
286
287 #endif
288 /*************************************************************
289 write event table and close file
290 *************************************************************/
291
292 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
293
294 sclose(hdr);
295 /* cleanup, free any allocated memory to avoid memory leaks */
296 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
297
298 destructHDR(hdr);
299
300 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i)\n",__FILE__,__LINE__);
301
302 if (data!=NULL) free(data);
303
304 }
305