lasp/src/lasp/device/lasp_daqdata.cpp

107 lines
3.2 KiB
C++
Raw Normal View History

/* #define DEBUGTRACE_ENABLED */
#include "lasp_daqdata.h"
2022-06-13 19:30:02 +00:00
#include "debugtrace.hpp"
#include "lasp_mathtypes.h"
2022-06-13 19:30:02 +00:00
#include <cassert>
DEBUGTRACE_VARIABLES;
DaqData::DaqData(const us nchannels, const us nframes,
const DataTypeDescriptor::DataType dtype)
: nchannels(nchannels), nframes(nframes), dtype(dtype),
dtype_descr(dtype_map.at(dtype)), sw(dtype_descr.sw) {
2022-06-13 19:30:02 +00:00
static_assert(sizeof(char) == 1, "Invalid char size");
const DataTypeDescriptor &desc = dtype_map.at(dtype);
_data.resize(nframes * nchannels * desc.sw);
}
void DaqData::copyInFromRaw(const std::vector<uint8_t *> &ptrs) {
us ch = 0;
assert(ptrs.size() == nchannels);
for (auto ptr : ptrs) {
std::copy(ptr, ptr + sw * nframes, &_data[sw * ch * nframes]);
ch++;
}
}
void DaqData::copyToRaw(const us channel, uint8_t *ptr) {
std::copy(raw_ptr(0, channel),
raw_ptr(nframes, channel), ptr);
2022-06-13 19:30:02 +00:00
}
template <typename int_type> d convertToFloat(const int_type val) {
if constexpr (std::is_integral<int_type>::value) {
return static_cast<d>(val) / std::numeric_limits<int_type>::max();
} else {
return static_cast<d>(val);
}
}
template <typename int_type>
inline vd channelToFloat(const byte_t *vals, const us nframes) {
vd res(nframes);
for (us i = 0; i < nframes; i++) {
res(i) = convertToFloat(reinterpret_cast<const int_type *>(vals)[i]);
}
return res;
}
template <typename int_type>
inline dmat allToFloat(const byte_t *vals, const us nframes, const us nchannels) {
dmat res(nframes, nchannels);
for (us j = 0; j < nchannels; j++) {
for (us i = 0; i < nframes; i++) {
res(i, j) = convertToFloat(
reinterpret_cast<const int_type *>(vals)[i + j * nframes]);
}
}
return res;
}
vd DaqData::toFloat(const us channel_no) const {
using DataType = DataTypeDescriptor::DataType;
switch (dtype) {
case (DataType::dtype_int8): {
return channelToFloat<int8_t>(raw_ptr(0, channel_no), nframes);
} break;
case (DataType::dtype_int16): {
return channelToFloat<int16_t>(raw_ptr(0, channel_no), nframes);
} break;
case (DataType::dtype_int32): {
return channelToFloat<int32_t>(raw_ptr(0, channel_no), nframes);
} break;
case (DataType::dtype_fl32): {
return channelToFloat<float>(raw_ptr(0, channel_no), nframes);
} break;
case (DataType::dtype_fl64): {
return channelToFloat<double>(raw_ptr(0, channel_no), nframes);
} break;
default:
throw std::runtime_error("BUG");
} // End of switch
}
dmat DaqData::toFloat() const {
dmat result(nframes, nchannels);
using DataType = DataTypeDescriptor::DataType;
switch (dtype) {
case (DataType::dtype_int8): {
return allToFloat<int8_t>(raw_ptr(0), nframes, nchannels);
} break;
case (DataType::dtype_int16): {
return allToFloat<int16_t>(raw_ptr(0), nframes, nchannels);
} break;
case (DataType::dtype_int32): {
return allToFloat<int32_t>(raw_ptr(0), nframes, nchannels);
} break;
case (DataType::dtype_fl32): {
return allToFloat<float>(raw_ptr(0), nframes, nchannels);
} break;
case (DataType::dtype_fl64): {
return allToFloat<double>(raw_ptr(0), nframes, nchannels);
} break;
default:
throw std::runtime_error("BUG");
} // End of switch
}