diff --git a/csv/VehicleSpeed_HS.csv b/csv/VehicleSpeed_HS.csv index e69de29..fc86944 100644 --- a/csv/VehicleSpeed_HS.csv +++ b/csv/VehicleSpeed_HS.csv @@ -0,0 +1,600 @@ +5.94 +5.93 +5.92 +5.9 +5.9 +5.89 +5.89 +5.87 +5.84 +5.83 +5.81 +5.79 +5.78 +5.76 +5.75 +5.74 +5.74 +5.73 +5.73 +5.72 +5.71 +5.71 +5.69 +5.69 +5.68 +5.66 +5.63 +5.62 +5.6 +5.58 +5.57 +5.55 +5.53 +5.51 +5.5 +5.47 +5.45 +5.42 +5.4 +5.38 +5.35 +5.31 +5.28 +5.27 +5.24 +5.19 +5.16 +5.14 +5.11 +5.06 +5.02 +5 +4.97 +4.92 +4.88 +4.86 +4.82 +4.77 +4.74 +4.7 +4.66 +4.62 +4.58 +4.54 +4.48 +4.43 +4.37 +4.31 +4.24 +4.18 +4.13 +4.06 +3.98 +3.91 +3.83 +3.77 +3.72 +3.64 +3.57 +3.51 +3.44 +3.39 +3.33 +3.28 +3.24 +3.17 +3.11 +3.06 +3.01 +2.96 +2.92 +2.88 +2.83 +2.79 +2.75 +2.71 +2.68 +2.64 +2.61 +2.59 +2.56 +2.55 +2.52 +2.51 +2.49 +2.48 +2.46 +2.46 +2.45 +2.44 +2.43 +2.42 +2.41 +2.4 +2.41 +2.39 +2.38 +2.35 +2.31 +2.26 +2.21 +2.16 +2.1 +2.05 +2.01 +1.94 +1.91 +1.84 +1.77 +1.73 +1.65 +1.59 +1.58 +1.55 +1.52 +1.53 +1.52 +1.5 +1.49 +1.47 +1.47 +1.47 +1.47 +1.48 +1.48 +1.48 +1.48 +1.48 +1.46 +1.44 +1.44 +1.43 +1.41 +1.41 +1.4 +1.38 +1.37 +1.36 +1.35 +1.33 +1.32 +1.31 +1.28 +1.27 +1.26 +1.24 +1.22 +1.21 +1.21 +1.19 +1.17 +1.16 +1.16 +1.13 +1.12 +1.12 +1.11 +1.09 +1.08 +1.07 +1.07 +1.05 +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+0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 diff --git a/eatit b/eatit index 636f108..8686a60 100755 Binary files a/eatit and b/eatit differ diff --git a/src/main.cpp b/src/main.cpp index ac07bfc..6e1a744 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,6 +28,9 @@ int main(int argc, char* argv[]) // determine position of markers in buffer and read data eatraw.find_markers(); + // split data segments into elements + eatraw.split_segments(); + std::cout<<"\n"; std::map> marks = eatraw.get_markers(); for ( auto mrk: marks ) @@ -40,12 +43,14 @@ int main(int argc, char* argv[]) std::cout< segvec = eatraw.get_segment(mrk.first); + std::cout<<"number of elements in segment: "< maindata = eatraw.get_data(); diff --git a/src/raweat.hpp b/src/raweat.hpp index 58f9f87..74c0660 100644 --- a/src/raweat.hpp +++ b/src/raweat.hpp @@ -53,12 +53,13 @@ private: // data sections corresponding to markers std::map> datasec_; + // split segments into arrays of simple number/string element + std::map> segments_; + // length of data array unsigned long int datsize_; // TODO preliminary: for now, we assume 32/64 bit ? floats in all data - // index in buffer of datasec_["datas marker"] where actual data starts - unsigned long int datstartidx_; std::vector datmes_; public: @@ -116,12 +117,14 @@ public: std::cout<> mrk : markers_ ) { - assert( mrk.second.size() > 0 && "please don't defined any empty marker" ); + assert( mrk.second.size() > 0 && "please don't define any empty marker" ); // find marker's byte sequence in buffer for ( unsigned long int idx = 0; idx < rawdata_.size(); idx++ ) @@ -150,10 +153,7 @@ public: } else { - // make sure the data marker is actually the last and extends until end of file - //assert( TODO && "data marker doesn't appear to be the very last"); - - // that's the data itself + // data marker is actually assumed to be the last and should extend until end of file for ( unsigned long int didx = idx; didx < rawdata_.size()-1; didx++ ) { markseq.push_back(rawdata_[didx]); @@ -161,21 +161,6 @@ public: // obtain length of data segment datsize_ = markseq.size(); - - // find starting index (supposed to be after fourth comma = 0x2c) - int countcomma = 0; - for ( unsigned long int buidx = 0; buidx < datsize_; buidx++ ) - { - // count number of comma chars in head of data segment - if ( markseq[buidx] == 0x2c ) countcomma++; - - // save position following fourth comma - if ( countcomma == 4 ) - { - datstartidx_ = buidx + 1; - break; - } - } } // save segment corresponding to marker @@ -198,6 +183,143 @@ public: return datasec_[marker]; } + // split data segments into arrays + void split_segments() + { + // split segments of all markers + for (std::pair> mrk : markers_ ) + { + // declare empty array for this segment and auxiliary string + std::vector segvec; + std::string elstr(""); + + // only start collecting after first comma in segment + bool parse = false; + + // count number of commata + long int commcount = 0; + + // parse data segment + for ( unsigned char el: datasec_[mrk.first] ) + { + // note that data segment of "datas marker" may contain any number of 0x2c's + if ( ( el != 0x2c && parse ) || ( mrk.first == "datas marker" && commcount > 2 ) ) + { + elstr.push_back(el); + } + else if ( el == 0x2c && parse ) + { + // comma marks end of element of segment: save string and reset it + segvec.push_back(elstr); + elstr = std::string(""); + commcount++; + } + else + { + // enable parsing after first comma + if ( el == 0x2c ) parse = true; + } + } + // include last element + segvec.push_back(elstr); + + // save array of elements + segments_.insert(std::pair>(mrk.first,segvec));; + } + } + +//---------------------------------------------------------------------------// + + // convert actual measurement data + void convert_data() + { + // by convention, the actual data is the 4th element + std::string datstr = segments_["datas marker"][3]; + std::vector datbuf(datstr.begin(),datstr.end()); + + // retrieve datatype from segment + int typesize = std::stoi(segments_["datyp marker"][5]); + + if ( typesize == 32 ) convert_data_32_bit_float(datbuf); + if ( false ) convert_data_16_bit_float(); + if ( typesize == 16 ) convert_data_16_bit_decimal(datbuf); + + } + + // convert single precision 32bit floating point numbers + void convert_data_32_bit_float(std::vector &datbuf) + { + // check size of buffer assuming size of single precision float is 4 byte + assert ( datbuf.size()%4 == 0 && "length of buffer is not a multiple of 4" ); + + // get number of single precision floats in buffer + unsigned long int totnumfl = datbuf.size()/(int)sizeof(float); + for ( unsigned long int numfl = 0; numfl < totnumfl; numfl++ ) + { + // assuming 4 byte (32bit) float + float num = 0.0; + uint8_t* pnum = reinterpret_cast(&num); + + // parse all 4 bytes of the number + for ( int byi = 0; byi < (int)sizeof(float); byi++ ) + { + // TODO what's the byte order (little/big endian) in the file?? + // for now, we just don't care... + pnum[byi] = (int)datbuf[(unsigned long int)(numfl*sizeof(float)+byi)]; + } + + // add number of array + datmes_.push_back((double)num); + } + } + + // convert half-precision (16bit) floating point numbers + void convert_data_16_bit_float() + { + assert ( (datsize_-28)%2 == 0 && "length of buffer is not a multiple of 2" ); + + unsigned long int totnumby = (datsize_-28)/2; + for ( unsigned long int by = 0; by < totnumby; by++ ) + { + // declare single (16bit) floating point number + half_float::half hfl; + + // reinterpret bytes in buffer as memory of floating point number + uint8_t* pnum = reinterpret_cast(&hfl); + for ( int i = 0; i < (int)sizeof(half_float::half); i++ ) + { + pnum[i] = (int)datasec_["datas marker"][(unsigned long int)(28+by*sizeof(half_float::half)+i)]; + } + + // add number to array + datmes_.push_back((double)hfl); + } + } + + // convert 16bit "decimal-encoding" floating point numbers + void convert_data_16_bit_decimal(std::vector &datbuf) + { + assert ( datbuf.size()%2 == 0 && "length of data is not a multiple of 2" ); + + // encoding parameters + double shift = -128.; + double scale = 1.0/100.; + double offse = 0.0; + + for ( unsigned long int idx = 0; idx < datbuf.size()-1; idx += 2 ) + { + // convert to float + datmes_.push_back( + + (double)( (int)(datbuf[idx])*1. + ( (int)(datbuf[idx+1])*1. + shift )*256. )*scale + offse + + ); + } + + } + +//---------------------------------------------------------------------------// + // show hex dump void show_hex(std::vector &datavec, int width = 32, unsigned long int maxchars = 512) { @@ -238,80 +360,18 @@ public: std::cout<(&num); - for ( int byi = 0; byi < (int)sizeof(float); byi++ ) - { - // TODO what's the byte order in the file?? - // for now, we just don't care... - pnum[byi] = (int)datasec_["datas marker"][(unsigned long int)(28+numfl*sizeof(float)+byi)]; - } - - // add number of array - datmes_.push_back((double)num); - } - } - - // convert half-precision (16bit) floating point numbers - void convert_data_16_bit_float() - { - assert ( (datsize_-28)%2 == 0 && "length of buffer is not a multiple of 2" ); - - unsigned long int totnumby = (datsize_-28)/2; - for ( unsigned long int by = 0; by < totnumby; by++ ) - { - // declare single (16bit) floating point number - half_float::half hfl; - - // reinterpret bytes in buffer as memory of floating point number - uint8_t* pnum = reinterpret_cast(&hfl); - for ( int i = 0; i < (int)sizeof(half_float::half); i++ ) - { - pnum[i] = (int)datasec_["datas marker"][(unsigned long int)(28+by*sizeof(half_float::half)+i)]; - } - - // add number to array - datmes_.push_back((double)hfl); - } - } - - // convert 16bit "decimal-encoding" floating point numbers - void convert_data_16_bit_decimal() - { - assert ( (datsize_-datstartidx_)%2 == 0 && "length of data is not a multiple of 2" ); - - double flstp = 0.04395; - - // parse bytes in data buffer - unsigned long int totnumby = (datsize_-datstartidx_)/2; - for ( unsigned long int by = 0; by < totnumby; by++ ) - { - // retrieve set of two subsequent bytes - std::vector pnum; - for ( int i = 0; i < 2; i++ ) pnum.push_back(datasec_["datas marker"][(unsigned long int)(datstartidx_+by*2+i)]); - - // convert to double - //datmes_.push_back((double)( (((int)pnum[0]-128)*256 + (int)pnum[1])/100.0 )); - datmes_.push_back((double)( (((int)pnum[1]-128)*256. + (int)pnum[0])/100.0 )); - //datmes_.push_back( (double) ( ( (int)pnum[0] + (int)pnum[1]*256 )*flstp ) ); - } - } - // get data array encoded as floats/doubles std::vector& get_data() { return datmes_; } + // get segment's array of elements + std::vector get_segment(std::string marker) + { + return segments_[marker]; + } + // write data to csv-like file void write_data(std::string filename, int precision = 9) {