lasp/src/lasp/device/lasp_daqdata.cpp

211 lines
5.3 KiB
C++

/* #define DEBUGTRACE_ENABLED */
#include "debugtrace.hpp"
#include <armadillo>
#include "lasp_daqdata.h"
#include "lasp_mathtypes.h"
#include <cassert>
#include <memory>
using std::cerr;
using std::cout;
using std::endl;
using rte = std::runtime_error;
static_assert(sizeof(byte_t) == 1, "Invalid char size");
DEBUGTRACE_VARIABLES;
/// Constructors and destructors
DaqData::DaqData(const us nframes, const us nchannels,
const DataTypeDescriptor::DataType dtype)
: nframes(nframes), nchannels(nchannels), dtype(dtype),
dtype_descr(dtype_map.at(dtype)), sw(dtype_descr.sw) {
DEBUGTRACE_ENTER;
DEBUGTRACE_PRINT(sw);
_data = new (std::align_val_t{8}) byte_t[sw * nchannels * nframes];
if (!_data) {
throw rte("Could not allocate memory for DaqData!");
}
}
DaqData::DaqData(const DaqData &o) : DaqData(o.nframes, o.nchannels, o.dtype) {
DEBUGTRACE_ENTER;
/* std::copy(o._data, &o._data[sw * nchannels * nframes], _data); */
memcpy(_data, o._data, sw * nchannels * nframes);
}
/* DaqData::DaqData(DaqData &&o) */
/* : nframes(o.nframes), nchannels(o.nchannels), dtype(o.dtype), */
/* dtype_descr(std::move(o.dtype_descr)), sw(o.sw) { */
/* DEBUGTRACE_ENTER; */
/* _data = o._data; */
/* /// Nullptrs do not get deleted */
/* o._data = nullptr; */
/* } */
DaqData::~DaqData() {
DEBUGTRACE_ENTER;
if (_data)
delete[] _data;
}
void DaqData::copyInFromRaw(const std::vector<byte_t *> &ptrs) {
DEBUGTRACE_ENTER;
us ch = 0;
assert(ptrs.size() == nchannels);
for (auto& ptr : ptrs) {
assert(ch < nchannels);
memcpy(&_data[sw * ch * nframes], ptr, sw * nframes);
/* std::copy(ptr, ptr + sw * nframes, &_data[sw * ch * nframes]); */
ch++;
}
dmat data2(nframes, nchannels);
}
void DaqData::copyToRaw(const us channel, byte_t *ptr) {
/* std::copy(raw_ptr(0, channel), raw_ptr(nframes, channel), ptr); */
memcpy(ptr, raw_ptr(0, channel), sw*nframes);
}
template <typename T>
d DaqData::toFloat(const us frame, const us channel) const {
DEBUGTRACE_ENTER;
if constexpr (std::is_integral<T>::value) {
return static_cast<d>(value<T>(frame, channel)) /
std::numeric_limits<T>::max();
} else {
return static_cast<d>(value<T>(frame, channel));
}
}
template <typename T> vd DaqData::toFloat(const us channel) const {
DEBUGTRACE_ENTER;
#if LASP_DEBUG == 1
check_type<T>();
#endif
vd res(nframes);
for (us i = 0; i < nframes; i++) {
res(i) = toFloat<T>(i, channel);
}
return res;
}
template <typename T> dmat DaqData::toFloat() const {
DEBUGTRACE_ENTER;
#if LASP_DEBUG == 1
check_type<T>();
#endif
dmat res(nframes, nchannels);
for (us i = 0; i < nframes; i++) {
for (us j = 0; j < nchannels; j++) {
res(i, j) = toFloat<T>(i, j);
}
}
return res;
}
d DaqData::toFloat(const us frame, const us channel) const {
DEBUGTRACE_ENTER;
using DataType = DataTypeDescriptor::DataType;
switch (dtype) {
case (DataType::dtype_int8):
return toFloat<int8_t>(frame, channel);
break;
case (DataType::dtype_int16):
return toFloat<int16_t>(frame, channel);
break;
case (DataType::dtype_int32):
return toFloat<int32_t>(frame, channel);
break;
case (DataType::dtype_fl32):
return toFloat<float>(frame, channel);
break;
case (DataType::dtype_fl64):
return toFloat<double>(frame, channel);
break;
default:
throw std::runtime_error("BUG");
} // End of switch
// Never arrives her
return 0;
}
vd DaqData::toFloat(const us channel_no) const {
DEBUGTRACE_ENTER;
using DataType = DataTypeDescriptor::DataType;
cerr << (int) dtype << endl;
switch (dtype) {
case (DataType::dtype_int8): {
return toFloat<int8_t>(channel_no);
} break;
case (DataType::dtype_int16): {
return toFloat<int16_t>(channel_no);
} break;
case (DataType::dtype_int32): {
return toFloat<int32_t>(channel_no);
} break;
case (DataType::dtype_fl32): {
return toFloat<float>(channel_no);
} break;
case (DataType::dtype_fl64): {
return toFloat<double>(channel_no);
} break;
default:
throw std::runtime_error("BUG");
} // End of switch
// Never arrives here
return vd();
}
dmat DaqData::toFloat() const {
DEBUGTRACE_ENTER;
/* DEBUGTRACE_PRINT(nframes); */
/* DEBUGTRACE_PRINT(nchannels); */
using DataType = DataTypeDescriptor::DataType;
/* cerr << "DataType: " << (int) dtype << endl; */
switch (dtype) {
case (DataType::dtype_int8):
return toFloat<int8_t>();
break;
case (DataType::dtype_int16):
return toFloat<int16_t>();
break;
case (DataType::dtype_int32):
return toFloat<int32_t>();
break;
case (DataType::dtype_fl32):
return toFloat<float>();
break;
case (DataType::dtype_fl64):
return toFloat<double>();
break;
default:
throw std::runtime_error("BUG");
} // End of switch
// Never reached
return dmat();
}
void DaqData::print() const {
cout << "Number of frames: " << nframes << endl;
cout << "Number of channels: " << nchannels << endl;
cout << "DataType: " << dtype_map.at(dtype).name << endl;
cout << "First sample of first channel (as float)" << toFloat(0,0) << endl;
cout << "Last sample of first channel (as float)" << toFloat(nframes-1,0) << endl;
cout << "Last sample of last channel (as float)" << toFloat(nframes-1,nchannels-1) << endl;
}