3#include "debugtrace.hpp"
12using rte = std::runtime_error;
14static_assert(
sizeof(
byte_t) == 1,
"Invalid char size");
22 : nframes(nframes), nchannels(nchannels), dtype(dtype),
23 dtype_descr(
dtype_map.at(dtype)), sw(dtype_descr.sw) {
28 assert(
sw > 0 &&
sw <= 8);
33 throw rte(
"Could not allocate memory for DaqData!");
43 : nframes(o.nframes), nchannels(o.nchannels), dtype(o.dtype),
44 dtype_descr(std::move(o.dtype_descr)), sw(o.sw) {
57 delete[](
reinterpret_cast<double *
>(
_data));
64 for (
auto &ptr : ptrs) {
85 if constexpr (std::is_integral<T>::value) {
86 return static_cast<d
>(value<T>(frame, channel)) /
87 std::numeric_limits<T>::max();
89 return static_cast<d
>(value<T>(frame, channel));
100 res(i) = toFloat<T>(i, channel);
114 res(i, j) = toFloat<T>(i, j);
124 case (DataType::dtype_int8):
125 return toFloat<int8_t>(frame, channel);
127 case (DataType::dtype_int16):
128 return toFloat<int16_t>(frame, channel);
130 case (DataType::dtype_int32):
131 return toFloat<int32_t>(frame, channel);
133 case (DataType::dtype_fl32):
134 return toFloat<float>(frame, channel);
136 case (DataType::dtype_fl64):
137 return toFloat<double>(frame, channel);
140 throw std::runtime_error(
"BUG");
152 case (DataType::dtype_int8): {
153 return toFloat<int8_t>(channel_no);
155 case (DataType::dtype_int16): {
156 return toFloat<int16_t>(channel_no);
158 case (DataType::dtype_int32): {
159 return toFloat<int32_t>(channel_no);
161 case (DataType::dtype_fl32): {
162 return toFloat<float>(channel_no);
164 case (DataType::dtype_fl64): {
165 return toFloat<double>(channel_no);
168 throw std::runtime_error(
"BUG");
186 case (DataType::dtype_int8):
187 return toFloat<int8_t>();
189 case (DataType::dtype_int16):
190 DEBUGTRACE_PRINT(
"Dtype = int16");
191 return toFloat<int16_t>();
193 case (DataType::dtype_int32):
194 return toFloat<int32_t>();
196 case (DataType::dtype_fl32):
197 DEBUGTRACE_PRINT(
"Dtype = float32");
198 return toFloat<float>();
200 case (DataType::dtype_fl64):
201 DEBUGTRACE_PRINT(
"Dtype = float64");
202 return toFloat<double>();
205 throw std::runtime_error(
"BUG");
218 if constexpr (std::is_integral<T>::value) {
219 value<T>(frame, channel) =
220 static_cast<T
>(val * std::numeric_limits<T>::max());
222 value<T>(frame, channel) =
static_cast<T
>(val);
231 case (DataType::dtype_int8):
232 return fromFloat<int8_t>(frame, channel, val);
234 case (DataType::dtype_int16):
235 return fromFloat<int16_t>(frame, channel, val);
237 case (DataType::dtype_int32):
238 return fromFloat<int32_t>(frame, channel, val);
240 case (DataType::dtype_fl32):
241 return fromFloat<float>(frame, channel, val);
243 case (DataType::dtype_fl64):
244 return fromFloat<float>(frame, channel, val);
247 throw std::runtime_error(
"BUG");
252 throw rte(
"Invalid number of frames in channel data");
256 case (DataType::dtype_int8):
257 for (
us frame = 0; frame <
nframes; frame++) {
258 fromFloat<int8_t>(frame, channel, vals(frame));
261 case (DataType::dtype_int16):
262 for (
us frame = 0; frame <
nframes; frame++) {
263 fromFloat<int16_t>(frame, channel, vals(frame));
266 case (DataType::dtype_int32):
267 for (
us frame = 0; frame <
nframes; frame++) {
268 fromFloat<int32_t>(frame, channel, vals(frame));
271 case (DataType::dtype_fl32):
272 for (
us frame = 0; frame <
nframes; frame++) {
273 fromFloat<float>(frame, channel, vals(frame));
276 case (DataType::dtype_fl64):
277 for (
us frame = 0; frame <
nframes; frame++) {
278 fromFloat<double>(frame, channel, vals(frame));
282 throw std::runtime_error(
"BUG");
287 cout <<
"Number of frames: " <<
nframes << endl;
288 cout <<
"Number of channels: " <<
nchannels << endl;
290 cout <<
"First sample of first channel (as float)" <<
toFloat(0, 0) << endl;
291 cout <<
"Last sample of first channel (as float)" <<
toFloat(
nframes - 1, 0)
293 cout <<
"Last sample of last channel (as float)"
296 vrd max = arma::max(data, 0);
297 vrd min = arma::min(data, 0);
298 cout <<
"Maximum value in buf: " << max << endl;
299 cout <<
"Minumum value in buf: " << min << endl;
Data coming from / going to DAQ. Non-interleaved format, which means data in buffer is ordered by cha...
void copyInFromRaw(const std::vector< byte_t * > &ptrs)
Copy data from a set of raw pointers of uninterleaved data. Overwrites any existing available data.
arma::Mat< d > toFloat() const
Convert samples to floating point values and return a nframes x nchannels array of floats....
DataTypeDescriptor::DataType dtype
The data type corresponding to a sample.
byte_t * raw_ptr(const us frame=0, const us channel=0)
Return pointer to the raw data corresponding to a certain sample (frame, channel combo).
void print() const
For debugging purposes: prints some stats.
void copyToRaw(const us channel, byte_t *ptr)
Copy contents of DaqData for a certain channel to a raw pointer.
void fromFloat(const us frame_no, const us channel_no, const d data)
Convert to channel data of native type from floating point values. Useful for 'changing' raw data in ...
us nchannels
The number of channels.
us sw
The number of bytes per sample (sample width, sw)
byte_t * _data
Storage for the actual data.
DaqData(const us nframes, const us nchannels, const DataTypeDescriptor::DataType dtype)
Initialize an empty frame of data.
us nframes
The number of frames in this block of data.
DataType
Basic data types coming from a DAQ that we can deal with. The naming will be self-explainging.
const std::map< DataTypeDescriptor::DataType, const DataTypeDescriptor > dtype_map
size_t us
We often use boolean values.