implemented conversion for 16bit floats

This commit is contained in:
Mario Fink 2020-02-10 16:37:20 +00:00
parent f6240a1789
commit 899582b0c1
9 changed files with 3282 additions and 2428 deletions

599
csv/VehicleSpeed_HS.csv Normal file
View File

@ -0,0 +1,599 @@
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
7.67
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
5.11
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
1.04
1.03
1.02
1.01
0.99
0.98
0.96
0.95
0.94
0.9
0.87
0.84
0.8
0.79
0.78
0.75
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
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56 4.82
57 4.77
58 4.74
59 4.7
60 4.66
61 4.62
62 4.58
63 4.54
64 4.48
65 4.43
66 4.37
67 4.31
68 4.24
69 4.18
70 4.13
71 4.06
72 3.98
73 3.91
74 3.83
75 3.77
76 3.72
77 3.64
78 3.57
79 3.51
80 3.44
81 3.39
82 3.33
83 3.28
84 3.24
85 3.17
86 3.11
87 3.06
88 3.01
89 2.96
90 2.92
91 2.88
92 2.83
93 2.79
94 2.75
95 2.71
96 2.68
97 2.64
98 2.61
99 2.59
100 2.56
101 5.11
102 2.52
103 2.51
104 2.49
105 2.48
106 2.46
107 2.46
108 2.45
109 2.44
110 2.43
111 2.42
112 2.41
113 2.4
114 2.41
115 2.39
116 2.38
117 2.35
118 2.31
119 2.26
120 2.21
121 2.16
122 2.1
123 2.05
124 2.01
125 1.94
126 1.91
127 1.84
128 1.77
129 1.73
130 1.65
131 1.59
132 1.58
133 1.55
134 1.52
135 1.53
136 1.52
137 1.5
138 1.49
139 1.47
140 1.47
141 1.47
142 1.47
143 1.48
144 1.48
145 1.48
146 1.48
147 1.48
148 1.46
149 1.44
150 1.44
151 1.43
152 1.41
153 1.41
154 1.4
155 1.38
156 1.37
157 1.36
158 1.35
159 1.33
160 1.32
161 1.31
162 1.28
163 1.27
164 1.26
165 1.24
166 1.22
167 1.21
168 1.21
169 1.19
170 1.17
171 1.16
172 1.16
173 1.13
174 1.12
175 1.12
176 1.11
177 1.09
178 1.08
179 1.07
180 1.07
181 1.05
182 1.04
183 1.03
184 1.02
185 1.01
186 0.99
187 0.98
188 0.96
189 0.95
190 0.94
191 0.9
192 0.87
193 0.84
194 0.8
195 0.79
196 0.78
197 0.75
198 0
199 0
200 0
201 0
202 0
203 0
204 0
205 0
206 0
207 0
208 0
209 0
210 0
211 0
212 0
213 0
214 0
215 0
216 0
217 0
218 0
219 0
220 0
221 0
222 0
223 0
224 0
225 0
226 0
227 0
228 0
229 0
230 0
231 0
232 0
233 0
234 0
235 0
236 0
237 0
238 0
239 0
240 0
241 0
242 0
243 0
244 0
245 0
246 0
247 0
248 0
249 0
250 0
251 0
252 0
253 0
254 0
255 0
256 0
257 0
258 0
259 0
260 0
261 0
262 0
263 0
264 0
265 0
266 0
267 0
268 0
269 0
270 0
271 0
272 0
273 0
274 0
275 0
276 0
277 0
278 0
279 0
280 0
281 0
282 0
283 0
284 0
285 0
286 0
287 0
288 0
289 0
290 0
291 0
292 0
293 0
294 0
295 0
296 0
297 0
298 0
299 0
300 0
301 0
302 0
303 0
304 0
305 0
306 0
307 0
308 0
309 0
310 0
311 0
312 0
313 0
314 0
315 0
316 0
317 0
318 0
319 0
320 0
321 0
322 0
323 0
324 0
325 0
326 0
327 0
328 0
329 0
330 0
331 0
332 0
333 0
334 0
335 0
336 0
337 0
338 0
339 0
340 0
341 0
342 0
343 0
344 0
345 0
346 0
347 0
348 0
349 0
350 0
351 0
352 0
353 0
354 0
355 0
356 0
357 0
358 0
359 0
360 0
361 0
362 0
363 0
364 0
365 0
366 0
367 0
368 0
369 0
370 0
371 0
372 0
373 0
374 0
375 0
376 0
377 0
378 0
379 0
380 0
381 0
382 0
383 0
384 0
385 0
386 0
387 0
388 0
389 0
390 0
391 0
392 0
393 0
394 0
395 0
396 0
397 0
398 0
399 0
400 0
401 0
402 0
403 0
404 0
405 0
406 0
407 0
408 0
409 0
410 0
411 0
412 0
413 0
414 0
415 0
416 0
417 0
418 0
419 0
420 0
421 0
422 0
423 0
424 0
425 0
426 0
427 0
428 0
429 0
430 0
431 0
432 0
433 0
434 0
435 0
436 0
437 0
438 0
439 0
440 0
441 0
442 0
443 0
444 0
445 0
446 0
447 0
448 0
449 0
450 0
451 0
452 0
453 0
454 0
455 0
456 0
457 0
458 0
459 0
460 0
461 0
462 0
463 0
464 0
465 0
466 0
467 0
468 0
469 0
470 0
471 0
472 0
473 0
474 0
475 0
476 0
477 0
478 0
479 0
480 0
481 0
482 0
483 0
484 0
485 0
486 0
487 0
488 0
489 0
490 0
491 0
492 0
493 0
494 0
495 0
496 0
497 0
498 0
499 0
500 0
501 0
502 0
503 0
504 0
505 0
506 0
507 0
508 0
509 0
510 0
511 0
512 0
513 0
514 0
515 0
516 0
517 0
518 0
519 0
520 0
521 0
522 0
523 0
524 0
525 0
526 0
527 0
528 0
529 0
530 0
531 0
532 0
533 0
534 0
535 0
536 0
537 0
538 0
539 0
540 0
541 0
542 0
543 0
544 0
545 0
546 0
547 0
548 0
549 0
550 0
551 0
552 0
553 0
554 0
555 0
556 0
557 0
558 0
559 0
560 0
561 0
562 0
563 0
564 0
565 0
566 0
567 0
568 0
569 0
570 0
571 0
572 0
573 0
574 0
575 0
576 0
577 0
578 0
579 0
580 0
581 0
582 0
583 0
584 0
585 0
586 0
587 0
588 0
589 0
590 0
591 0
592 0
593 0
594 0
595 0
596 0
597 0
598 0
599 0

File diff suppressed because it is too large Load Diff

2402
csv/pressure_Vacuum.csv Normal file

File diff suppressed because it is too large Load Diff

BIN
eatit

Binary file not shown.

View File

@ -7,7 +7,7 @@ EXE = eatit
CCC = g++
OPT = -O3 -Wall
$(EXE) : $(SRC)main.cpp $(SRC)raweat.hpp
$(EXE) : $(SRC)main.cpp $(SRC)raweat.hpp $(SRC)half_precision_floating_point.hpp
$(CCC) $(OPT) $< -o $@
clean :

60
src/endian.hpp Normal file
View File

@ -0,0 +1,60 @@
//---------------------------------------------------------------------------//
#ifndef MY_LITTLE_ENDIAN
#define MY_LITTLE_ENDIAN
#include <iostream>
#define IS_BIG_ENDIAN (*(uint16_t*)"\0\xff" < 0x100)
//---------------------------------------------------------------------------//
class endian
{
private:
// endianness
bool little_endian_;
public:
// constructor
endian()
{
// check endianness on machine
little_endian_ = get_endian();
}
// provide information about little versus big endian architecture
bool get_endian()
{
// declare short (16 bit) integer representing "1"
short int wd = 0x001;
// get byte pattern of integer
uint8_t *pwd = (uint8_t*)(&wd);
//for ( int i = 0; i < 2; i ++ ) std::cout<<std::hex<<(int)pwd[i]<<" ";
//std::cout<<"\n";
// check if byte pattern is 0x 0001 or 0x 0100
return ((int)pwd[0] == 1 ? true : false);
}
// retrieve information
bool little_endian()
{
return little_endian_?true:false;
}
bool big_endian()
{
return little_endian_?false:true;
}
};
#endif
//---------------------------------------------------------------------------//

View File

@ -0,0 +1,100 @@
//---------------------------------------------------------------------------//
#ifndef HALF_PRECISION_FLOATING_POINT
#define HALF_PRECISION_FLOATING_POINT
#include <vector>
#include <bitset>
#include "endian.hpp"
//---------------------------------------------------------------------------//
class half_precision_floating_point
{
private:
// size of half precision floating point number in bits
int size_ = 16;
// construction of the number (binary16) according to IEEE 754-2008 standard
std::bitset<1> sign_;
std::bitset<5> expo_;
std::bitset<10> frac_;
// endianness of machine/architecture
bool little_endian_;
// number as single-precision floating point number
float num_;
// array of bytes representing the number
std::vector<uint8_t> numbytes_;
public:
// constructors
half_precision_floating_point(std::vector<uint8_t> numbytes) :
numbytes_(numbytes)
{
assert( numbytes_.size() == 2 );
endian endi;
little_endian_ = endi.little_endian();
assert( little_endian_ && "just don't work on big endian machines!" );
}
half_precision_floating_point(float num) :
num_(num)
{
}
// destructor
~half_precision_floating_point()
{
}
// assignment operator
half_precision_floating_point& operator=(const half_precision_floating_point& other)
{
if ( this != &other )
{
size_ = other.size_;
sign_ = other.sign_;
expo_ = other.expo_;
frac_ = other.frac_;
little_endian_ = other.little_endian_;
num_ = other.num_;
numbytes_ = other.numbytes_;
}
return *this;
}
// convert byte pattern to number
void convert_to_float()
{
// declare a single precision floating point number
// float myfloat = 0.0;
if ( little_endian_ )
{
}
}
// convert floating point number to bytes
void convert_to_byte()
{
}
};
#endif
//---------------------------------------------------------------------------//

View File

@ -9,13 +9,13 @@
int main(int argc, char* argv[])
{
// path of filename provided ?
assert( argc > 1 && "please provide a filename and path" );
assert( argc > 2 && "please provide a filename and path for input and output" );
std::cout<<"number of CLI-arguments: "<<argc<<"\n";
for ( int i = 0; i < argc; i++ ) std::cout<<std::setw(5)<<i<<": "<<argv[i]<<"\n";
// check number of CLI arguments
assert( argc == 2 );
assert( argc == 3 );
// get name/path of file from CLI argument
std::string rawfile(argv[1]);
@ -23,9 +23,12 @@ int main(int argc, char* argv[])
// declare instance of "raw_eater"
raw_eater eatraw(rawfile);
eatraw.show_markers();
//eatraw.show_markers();
// determine position of markers in buffer and read data
eatraw.find_markers();
std::cout<<"\n";
std::map<std::string,std::vector<unsigned char>> marks = eatraw.get_markers();
for ( auto mrk: marks )
{
@ -39,13 +42,32 @@ int main(int argc, char* argv[])
std::cout<<"\n";
}
eatraw.convert_data();
// convert unsigned char data in buffer to desired data type
//eatraw.convert_data();
eatraw.convert_data_16_bit_float();
std::vector<double> mydata = eatraw.get_data();
std::cout<<"\n"<<mydata.size()<<"\n\n";
// get array of encoded data
std::vector<double> maindata = eatraw.get_data();
std::cout<<"\nsize of data array: "<<maindata.size()<<"\n\n";
//for ( unsigned long int i = 0; i < 10; i++ ) std::cout<<mydata[i]<<"\n";
eatraw.write_data(std::string("csv/myfile.csv"));
// write data in csv-file
eatraw.write_data(std::string(argv[2]));
float ab = -0.75;
std::cout<<ab<<"\n";
uint8_t* pab = reinterpret_cast<uint8_t*>(&ab);
for ( int i = 0; i < (int)sizeof(float); i++ ) std::cout<<std::hex<<(int)pab[i]<<std::dec<<" ";
std::cout<<"\n";
uint8_t sc = 0x4f;
std::bitset<8> bssc(sc);
for ( int i = 0; i < 8; i++ ) std::cout<<bssc[i]<<" ";
std::cout<<"\n";
std::cout<<std::hex<<bssc.to_ulong()<<"\n\n";
endian endi;
std::cout<<(endi.little_endian()?"little endian":"big endian")<<"\n";
return 0;
}

View File

@ -1,5 +1,8 @@
//---------------------------------------------------------------------------//
#ifndef RAW_EATER
#define RAW_EATER
#include <assert.h>
#include <iostream>
#include <fstream>
@ -8,6 +11,9 @@
#include <iterator>
#include <map>
#include "half_precision_floating_point.hpp"
#include "endian.hpp"
//---------------------------------------------------------------------------//
class raw_eater
@ -40,6 +46,9 @@ private:
// data sections corresponding to markers
std::map<std::string,std::vector<unsigned char>> datasec_;
// length of data array
unsigned long int datsize_;
// TODO preliminary: for now, we assume 32/64 bit ? floats in all data
std::vector<double> datmes_;
@ -61,17 +70,6 @@ public:
(std::istreambuf_iterator<char>()));
rawdata_ = rawdata;
// show size of buffer
std::cout<<"size of buffer "<<rawdata_.size()<<"\n";
// show excerpt from buffer
int ista = 0, iend = 128;
for ( int i= ista; i < iend; i++ )
{
std::cout<<std::hex<<(int)rawdata_[i]<<" ";
if ( (i+1)%16 == 0 ) std::cout<<"\n";
}
std::cout<<"\n";
}
// destructor
@ -80,6 +78,22 @@ public:
}
// display buffer/data properties
void show_buffer(int numel = 128)
{
// show size of buffer
std::cout<<"size of buffer "<<rawdata_.size()<<"\n\n";
// show excerpt from buffer
int ista = 0, iend = numel;
for ( int i= ista; i < iend; i++ )
{
std::cout<<std::hex<<(int)rawdata_[i]<<" ";
if ( (i+1)%16 == 0 ) std::cout<<"\n";
}
std::cout<<"\n";
}
// show predefined markers
void show_markers()
{
@ -135,6 +149,9 @@ public:
{
markseq.push_back(rawdata_[didx]);
}
// obtain length of data segment
datsize_ = markseq.size();
}
datasec_.insert(std::pair<std::string,std::vector<unsigned char>>(mrk.first,markseq));
}
@ -198,12 +215,9 @@ public:
// convert actual measurement data
void convert_data()
{
// length of data array
unsigned long int datsize = datasec_["datas marker"].size();
assert ( (datsize_-28)%4 == 0 && "length of buffer is not a multiple of 4" );
assert ( (datsize-28)%4 == 0 && "length of buffer is not a multiple of 4" );
unsigned long int totnumfl = (datsize-28)/4;
unsigned long int totnumfl = (datsize_-28)/(int)sizeof(float);
for ( unsigned long int numfl = 0; numfl < totnumfl; numfl++ )
{
// assuming 4 byte float
@ -211,6 +225,8 @@ public:
uint8_t* pnum = reinterpret_cast<uint8_t*>(&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)];
}
@ -219,6 +235,61 @@ public:
}
}
// convert half-precision (16bit) floating point numbers
void convert_data_16_bit_float()
{
// single (32bit) floating point number
float fl = 0.0;
unsigned long int totnumby = (datsize_-28)/2;
for ( unsigned long int by = 0; by < totnumby; by++ )
{
// retrieve two bytes of floating point number
std::vector<uint8_t> pnum;
for ( int i = 0; i < 2; i++ ) pnum.push_back(datasec_["datas marker"][(unsigned long int)(28+by*2+i)]);
// obtain bitset
std::bitset<8> byA(pnum[0]), byB(pnum[1]);
// TODO all following code only works for little endian!!
// sign
float sign = byB[0];
// exponent of 16bit float
long int expo = 0;
for ( int i = 0; i < 5; i++ ) if ( byB[1+i] ) expo += pow(2.0,4-i);
expo -= 15;
// convert to exponent of 32bit float
// mantissa
// declare bitset of float
std::bitset<8> flA(0x00), flB(0x00), flC(0x00), flD(0x00);
}
}
// convert 16bit "decimal-encoding" floating point numbers
void convert_data_16_bit_decimal()
{
//assert ( (datsize_-29)%2 == 0 && "length of buffer is not a multiple of 2" );
unsigned long int totnumby = (datsize_-30)/2;
for ( unsigned long int by = 0; by < totnumby; by++ )
{
std::vector<uint8_t> pnum;
for ( int i = 0; i < 2; i++ ) pnum.push_back(datasec_["datas marker"][(unsigned long int)(29+by*2+i)]);
datmes_.push_back((double)( (((int)pnum[0]-128)*256 + (int)pnum[1])/100.0 ));
}
}
// get data array encoded as floats/doubles
std::vector<double>& get_data()
{
@ -226,14 +297,14 @@ public:
}
// write data to csv-like file
void write_data(std::string filename)
void write_data(std::string filename, int precision = 9)
{
// open file
std::ofstream fout(filename.c_str());
for ( auto el : datmes_ )
{
fout<<std::dec<<el<<"\n";
fout<<std::dec<<std::setprecision(precision)<<el<<"\n";
}
// close file
@ -242,4 +313,6 @@ public:
};
#endif
//---------------------------------------------------------------------------//