| 1 | /* EMArawData.cpp |
| 2 | * |
| 3 | * Copyright (C) 2023 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "EMA.h"cxx . . $ `<file>/sensors/EMA.h`/ |
| 20 | #include "EMArawData.h"cxx . . $ `<file>/sensors/EMArawData.h`/ |
| 21 | #include "melder.h"cxx . . $ `<file>/melder/melder.h`/ |
| 22 | |
| 23 | #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/ |
| 24 | #include "EMArawData_def.h"cxx . . $ `<file>/sensors/EMArawData_def.h`/ |
| 25 | #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/ |
| 26 | #include "EMArawData_def.h"cxx . . $ `<file>/sensors/EMArawData_def.h`/ |
| 27 | #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/ |
| 28 | #include "EMArawData_def.h"cxx . . $ `<file>/sensors/EMArawData_def.h`/ |
| 29 | #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/ |
| 30 | #include "EMArawData_def.h"cxx . . $ `<file>/sensors/EMArawData_def.h`/ |
| 31 | #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/ |
| 32 | #include "EMArawData_def.h"cxx . . $ `<file>/sensors/EMArawData_def.h`/ |
| 33 | #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/ |
| 34 | #include "EMArawData_def.h"cxx . . $ `<file>/sensors/EMArawData_def.h`/ |
| 35 | #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/ |
| 36 | #include "EMArawData_def.h"cxx . . $ `<file>/sensors/EMArawData_def.h`/ |
| 37 | #include "oo_READ_BINARY.h"cxx . . $ `<file>/sys/oo_READ_BINARY.h`/ |
| 38 | #include "EMArawData_def.h"cxx . . $ `<file>/sensors/EMArawData_def.h`/ |
| 39 | #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/ |
| 40 | #include "EMArawData_def.h"cxx . . $ `<file>/sensors/EMArawData_def.h`/ |
| 41 | |
| 42 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (EMArawData, Sampled, 0); |
| 43 | |
| 44 | void structEMArawData :: v1_info () { |
| 45 | structDaatacxx . . $ structDaata# :: v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). (); |
| 46 | } |
| 47 | |
| 48 | autoEMArawData EMArawData_readFromCarstensEMA50xAmpFilecxx . . $ EMArawData_readFromCarstensEMA50xAmpFile(9a96048fc64a8bf). (MelderFilecxx . . $ MelderFile# file) { |
| 49 | try { |
| 50 | integercxx . . $ integer# version, headerSize, numberOfSensors, samplingFrequency, numberOfBytesInLines1234; |
| 51 | CarstensEMA_processHeadercxx . . $ CarstensEMA_processHeader(f1559e9bae5bd2fd). (file, & version, & headerSize, & numberOfSensors, & samplingFrequency, & numberOfBytesInLines1234); |
| 52 | (void) MelderFile_opencxx . . $ MelderFile_open(c1cfd957ea3b2501). (file); |
| 53 | /* |
| 54 | We have to read the sensor calibration data which are located in the header part. |
| 55 | 1. Read the complete header and find the start of the calibration part |
| 56 | */ |
| 57 | autostring8cxx . . $ autostring8# headercxx . . $ _autostring#_autostring(f153f132b2bc41c2). (headerSize, false); |
| 58 | size_t nread = fread (& header [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0], 1, headerSize, file -> filePointercxx . . $ structMelderFile#filePointer.); |
| 59 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (nread == headerSize, |
| 60 | U"Header size does not match its specification."); |
| 61 | header [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).headerSize - 1] = 0; // just in case |
| 62 | char *pCalf = & header [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0] + numberOfBytesInLines1234; |
| 63 | pCalf = strstr (pCalf, "Calf_Channel_"); |
| 64 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (pCalf, |
| 65 | U"The calibration part is missing."); |
| 66 | /* |
| 67 | 2. Create the EMArawData object |
| 68 | */ |
| 69 | const integercxx . . $ integer# numberOfBytes = MelderFile_lengthcxx . . $ MelderFile_length(4c086c6b3713bb2e). (file); |
| 70 | const integercxx . . $ integer# numberOfAmpBytes = numberOfBytes - headerSize; |
| 71 | constexpr integercxx . . $ integer# numberOfBytesPerValue = 4; |
| 72 | constexpr integercxx . . $ integer# numberOfTransmitters = 9; |
| 73 | const integercxx . . $ integer# frameSize = numberOfSensors * numberOfTransmitters * numberOfBytesPerValue; |
| 74 | const integercxx . . $ integer# numberOfFrames = numberOfAmpBytes / frameSize; |
| 75 | const double dt = 1.0 / samplingFrequency; |
| 76 | const double duration = numberOfFrames * dt; |
| 77 | |
| 78 | autoEMArawData me = EMArawData_createcxx . . $ EMArawData_create(f3433d1acfa87d94). (0.0, duration, numberOfSensors, numberOfTransmitters, numberOfFrames, dt, 0.5 * dt); |
| 79 | /* |
| 80 | 3. Get the calibration matrix (numberOfSensors x numberOfTransmitters) |
| 81 | |
| 82 | */ |
| 83 | constexpr integercxx . . $ integer# ncharCalf = 13; |
| 84 | char *headerEnd = & header [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0] + headerSize - 1; |
| 85 | for (integercxx . . $ integer# isensor = 1; isensor <= numberOfSensors; isensor ++) { |
| 86 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (strnequcxx . . $ `melder/melder_string32.h:70:9`! (pCalf, "Calf_Channel_", ncharCalf), |
| 87 | U"We expected 'Calf_Channel_'."); |
| 88 | pCalf += ncharCalf; // start of channel number |
| 89 | char *qCalf = pCalf; |
| 90 | while (*qCalf ++ != '=' && qCalf < headerEnd); |
| 91 | *(--qCalf) = '\0'; |
| 92 | const integercxx . . $ integer# isensorf = atoicxx . . $ atoi(f5ecf3191ed0cd93). (pCalf); |
| 93 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (isensorf == isensor - 1, |
| 94 | U"Wrong sensor number ", isensorf, U")." ); |
| 95 | pCalf = qCalf + 3; // skip "=[ " |
| 96 | for (integercxx . . $ integer# itrans = 1; itrans <= numberOfTransmitters; itrans ++) { |
| 97 | qCalf = pCalf; |
| 98 | while (*qCalf ++ != ' ' && qCalf < headerEnd); |
| 99 | *(--qCalf) = '\0'; |
| 100 | mycxx . . $ `melder/melder.h:44:9`! sensorCalibrations [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).isensor] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).itrans] = atofcxx . . $ atof(9b0361fcbc284e4f). (pCalf); |
| 101 | pCalf = qCalf + 1; |
| 102 | } |
| 103 | pCalf += 2; // "]\n" |
| 104 | } |
| 105 | /* |
| 106 | 4. Get the amplitude data. |
| 107 | */ |
| 108 | for (integercxx . . $ integer# iframe = 1; iframe <= numberOfFrames; iframe ++) { |
| 109 | EMArawData_Frame emaRawDataFrame = & mycxx . . $ `melder/melder.h:44:9`! emaRawDataFrames [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 110 | emaRawDataFrame -> numberOfSensors = numberOfSensors; |
| 111 | emaRawDataFrame -> transmitterFrames =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). newvectorzerocxx . . $ newvectorzero(d6d5973469733409).<structEMATransmitter_Frame> (numberOfSensors); |
| 112 | for (integercxx . . $ integer# isensor = 1; isensor <= numberOfSensors; isensor ++) { |
| 113 | EMATransmitter_Frame transmitterFrame = & (emaRawDataFrame -> transmitterFrames [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).isensor]); |
| 114 | transmitterFrame -> numberOfTransmitters = numberOfTransmitters; |
| 115 | transmitterFrame -> amplitudes =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (numberOfTransmitters); |
| 116 | for (integercxx . . $ integer# itrans = 1; itrans <= numberOfTransmitters; itrans ++) |
| 117 | transmitterFrame -> amplitudes [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).itrans] = bingetr32LEcxx . . $ bingetr32LE(aa8fd5995df79f3e). (file -> filePointercxx . . $ structMelderFile#filePointer.); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | MelderFile_closecxx . . $ MelderFile_close(1145552d769d7b5d). (file); |
| 122 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 123 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 124 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Carstens EMArawData50x amp file not read."); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | void EMArawData_initcxx . . $ EMArawData_init(df6deac6e4ee712e). (EMArawData me, double tmin, double tmax, integercxx . . $ integer# numberOfSensors, integercxx . . $ integer# numberOfTransmitters, integercxx . . $ integer# numberOfFrames, double dt, double t1) { |
| 132 | Sampled_initcxx . . $ Sampled_init(5b6f70c633f31208). (me, tmin, tmax, numberOfFrames, dt, t1); |
| 133 | mycxx . . $ `melder/melder.h:44:9`! numberOfSensors = numberOfSensors; |
| 134 | mycxx . . $ `melder/melder.h:44:9`! emaRawDataFrames =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <structEMArawData_Frame> (numberOfFrames); |
| 135 | mycxx . . $ `melder/melder.h:44:9`! sensorCalibrations =cxx . . $ automatrix#`operator=`(a0f46581f260c488). zero_MATcxx . . $ zero_MAT(3d09731056d2b97d). (numberOfSensors, numberOfTransmitters); |
| 136 | } |
| 137 | |
| 138 | autoEMArawData EMArawData_createcxx . . $ EMArawData_create(f3433d1acfa87d94). (double tmin, double tmax, integercxx . . $ integer# numberOfSensors, integercxx . . $ integer# numberOfTransmitters, integercxx . . $ integer# numberOfFrames, double dt, double t1) { |
| 139 | autoEMArawData me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (EMArawData); |
| 140 | mycxx . . $ `melder/melder.h:44:9`! numberOfSensors = numberOfSensors; |
| 141 | mycxx . . $ `melder/melder.h:44:9`! numberOfTransmitters = numberOfTransmitters; |
| 142 | EMArawData_initcxx . . $ EMArawData_init(df6deac6e4ee712e). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmin, tmax, numberOfSensors, numberOfTransmitters, numberOfFrames, dt, t1); |
| 143 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 144 | } |
| 145 | |
| 146 | /* End of file EMArawData.cpp */ |
| 147 |