2022-06-13 19:30:02 +00:00
|
|
|
#pragma once
|
2022-07-20 12:58:48 +00:00
|
|
|
#include "lasp_daqconfig.h"
|
|
|
|
#include "lasp_types.h"
|
2022-10-10 17:17:38 +00:00
|
|
|
#include <armadillo>
|
|
|
|
#include <cassert>
|
2022-06-13 19:30:02 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <gsl/gsl-lite.hpp>
|
|
|
|
#include <memory>
|
2022-10-10 17:17:38 +00:00
|
|
|
#include <type_traits>
|
2022-06-13 19:30:02 +00:00
|
|
|
|
2022-09-03 18:59:14 +00:00
|
|
|
/** \addtogroup device
|
|
|
|
* @{
|
|
|
|
*/
|
2022-10-04 07:27:27 +00:00
|
|
|
class Daq;
|
|
|
|
|
|
|
|
using byte_t = char;
|
2022-09-03 18:59:14 +00:00
|
|
|
|
2022-06-13 19:30:02 +00:00
|
|
|
/**
|
|
|
|
* @brief Data coming from / going to DAQ. **Non-interleaved format**, which
|
2022-07-20 12:58:48 +00:00
|
|
|
* means data in buffer is ordered by channel: _ptr[frame+channel*nframes]
|
2022-06-13 19:30:02 +00:00
|
|
|
*/
|
|
|
|
class DaqData {
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* @brief Storage for the actual data.
|
|
|
|
*/
|
2022-10-10 17:17:38 +00:00
|
|
|
byte_t *_data;
|
2022-06-13 19:30:02 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
2022-10-10 17:17:38 +00:00
|
|
|
* @brief The number of frames in this block of data.
|
2022-06-13 19:30:02 +00:00
|
|
|
*/
|
2022-10-10 17:17:38 +00:00
|
|
|
us nframes;
|
2022-06-13 19:30:02 +00:00
|
|
|
|
|
|
|
/**
|
2022-10-10 17:17:38 +00:00
|
|
|
* @brief The number of channels
|
2022-06-13 19:30:02 +00:00
|
|
|
*/
|
2022-10-10 17:17:38 +00:00
|
|
|
us nchannels;
|
2022-06-13 19:30:02 +00:00
|
|
|
|
2022-07-20 12:58:48 +00:00
|
|
|
/**
|
|
|
|
* @brief The data type corresponding to a sample
|
|
|
|
*/
|
2022-07-29 07:32:26 +00:00
|
|
|
DataTypeDescriptor::DataType dtype;
|
2022-07-20 12:58:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The data type description corresponding to a sample
|
|
|
|
*/
|
2022-07-29 07:32:26 +00:00
|
|
|
DataTypeDescriptor dtype_descr;
|
2022-06-13 19:30:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The number of bytes per sample (sample width, sw)
|
|
|
|
*/
|
2022-07-29 07:32:26 +00:00
|
|
|
us sw;
|
2022-06-13 19:30:02 +00:00
|
|
|
|
2022-07-20 12:58:48 +00:00
|
|
|
/**
|
|
|
|
* @brief Initialize an empty frame of data
|
|
|
|
*
|
|
|
|
* @param nframes The number of frames
|
2022-10-10 17:17:38 +00:00
|
|
|
* @param nchannels The number of channels
|
2022-07-20 12:58:48 +00:00
|
|
|
* @param dtype The data type
|
|
|
|
*/
|
2022-10-10 17:17:38 +00:00
|
|
|
DaqData(const us nframes, const us nchannels,
|
2022-06-13 19:30:02 +00:00
|
|
|
const DataTypeDescriptor::DataType dtype);
|
2022-07-29 07:32:26 +00:00
|
|
|
/**
|
|
|
|
* @brief Initialize using no allocation
|
|
|
|
*/
|
2022-10-10 17:17:38 +00:00
|
|
|
DaqData(const DaqData &);
|
2022-10-20 15:12:34 +00:00
|
|
|
DaqData(DaqData &&);
|
2022-10-10 17:17:38 +00:00
|
|
|
DaqData &operator=(const DaqData &) = delete;
|
2023-01-04 14:15:03 +00:00
|
|
|
~DaqData();
|
2022-06-13 19:30:02 +00:00
|
|
|
|
|
|
|
/**
|
2022-10-10 17:17:38 +00:00
|
|
|
* @brief Return pointer to the raw data corresponding to a certain sample
|
|
|
|
* (frame, channel combo).
|
2022-07-20 12:58:48 +00:00
|
|
|
*
|
|
|
|
* @param frame The frame number
|
|
|
|
* @param channel The channel number
|
2022-06-13 19:30:02 +00:00
|
|
|
*
|
2022-07-20 12:58:48 +00:00
|
|
|
* @return Pointer to sample, not casted to final type
|
2022-06-13 19:30:02 +00:00
|
|
|
*/
|
2022-10-04 07:27:27 +00:00
|
|
|
byte_t *raw_ptr(const us frame = 0, const us channel = 0) {
|
2022-10-10 17:17:38 +00:00
|
|
|
assert(frame < nframes);
|
|
|
|
assert(channel < nchannels);
|
|
|
|
return &(_data[sw * (frame + channel * nframes)]);
|
2022-10-04 07:27:27 +00:00
|
|
|
}
|
|
|
|
const byte_t *raw_ptr(const us frame = 0, const us channel = 0) const {
|
2022-10-10 17:17:38 +00:00
|
|
|
assert(frame < nframes);
|
|
|
|
assert(channel < nchannels);
|
|
|
|
return &(_data[sw * (frame + channel * nframes)]);
|
2022-09-22 19:02:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 19:30:02 +00:00
|
|
|
/**
|
|
|
|
* @brief Return the total number of bytes
|
|
|
|
*
|
|
|
|
* @return Number of bytes of data.
|
|
|
|
*/
|
2022-10-10 17:17:38 +00:00
|
|
|
us size_bytes() const { return sw * nchannels * nframes; }
|
2022-06-13 19:30:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Copy data from a set of raw pointers of *uninterleaved* data.
|
|
|
|
* Overwrites any existing available data.
|
|
|
|
*
|
|
|
|
* @param ptrs Pointers to data from channels
|
|
|
|
*/
|
2022-10-10 17:17:38 +00:00
|
|
|
void copyInFromRaw(const std::vector<byte_t *> &ptrs);
|
2022-06-13 19:30:02 +00:00
|
|
|
|
2022-10-20 15:12:34 +00:00
|
|
|
/**
|
|
|
|
* @brief Copy data from a set of raw pointers of *uninterleaved* data.
|
|
|
|
* Overwrites any existing available data.
|
|
|
|
*
|
|
|
|
* @param channel The channel to copy to
|
2023-01-20 14:59:08 +00:00
|
|
|
* @param ptr Pointers to data from channels
|
2022-10-20 15:12:34 +00:00
|
|
|
*/
|
|
|
|
void copyInFromRaw(const us channel, const byte_t *ptr);
|
|
|
|
|
2022-06-13 19:30:02 +00:00
|
|
|
/**
|
|
|
|
* @brief Copy contents of DaqData for a certain channel to a raw pointer.
|
|
|
|
*
|
|
|
|
* @param channel The channel to copy.
|
|
|
|
* @param ptr The pointer where data is copied to.
|
|
|
|
*/
|
2022-10-10 17:17:38 +00:00
|
|
|
void copyToRaw(const us channel, byte_t *ptr);
|
2022-10-04 07:27:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convert samples to floating point values and return a nframes x
|
|
|
|
* nchannels array of floats. For data that is not already floating-point,
|
|
|
|
* the data is scaled back from MAX_INT to +1.0.
|
|
|
|
*
|
|
|
|
* @return Array of floats
|
|
|
|
*/
|
|
|
|
arma::Mat<d> toFloat() const;
|
|
|
|
|
|
|
|
/**
|
2022-10-10 17:17:38 +00:00
|
|
|
* @brief Convert samples to floating point value; and return a nframes
|
2022-10-04 07:27:27 +00:00
|
|
|
* column vector of floats. For data that is not already floating-point,
|
|
|
|
* the data is scaled back from MAX_INT to +1.0.
|
|
|
|
*
|
2023-01-20 14:59:08 +00:00
|
|
|
* @param channel_no The channel number to convert
|
2022-10-04 07:27:27 +00:00
|
|
|
*
|
|
|
|
* @return Array of floats
|
|
|
|
*/
|
|
|
|
arma::Col<d> toFloat(const us channel_no) const;
|
2022-06-13 19:30:02 +00:00
|
|
|
|
2022-07-20 12:58:48 +00:00
|
|
|
/**
|
2022-10-10 17:17:38 +00:00
|
|
|
* @brief Convert single sample to floating point value; and return a nframes
|
|
|
|
* column vector of floats. For data that is not already floating-point,
|
|
|
|
* the data is scaled back from MAX_INT to +1.0.
|
2022-07-20 12:58:48 +00:00
|
|
|
*
|
2022-10-10 17:17:38 +00:00
|
|
|
* @param frame_no The frame number to convert
|
2023-01-20 14:59:08 +00:00
|
|
|
* @param channel_no The channel number to convert
|
2022-07-20 12:58:48 +00:00
|
|
|
*
|
2022-10-10 17:17:38 +00:00
|
|
|
* @return Float value
|
2022-07-20 12:58:48 +00:00
|
|
|
*/
|
2022-10-10 17:17:38 +00:00
|
|
|
d toFloat(const us frame_no, const us channel_no) const;
|
|
|
|
|
2022-10-20 15:12:34 +00:00
|
|
|
/**
|
|
|
|
* @brief Convert to channel data of native type from floating point values.
|
|
|
|
* Useful for 'changing' raw data in any way.
|
|
|
|
*
|
2023-01-20 14:59:08 +00:00
|
|
|
* @param frame_no The frame
|
|
|
|
* @param channel_no The channel
|
|
|
|
* @param data The value
|
2022-10-20 15:12:34 +00:00
|
|
|
*/
|
|
|
|
void fromFloat(const us frame_no, const us channel_no,
|
|
|
|
const d data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convert to channel data of native type from floating point values.
|
|
|
|
* Useful for 'changing' raw data in any way.
|
|
|
|
*
|
|
|
|
* @param channel The channel to convert
|
2023-01-20 14:59:08 +00:00
|
|
|
* @param data Data to convert from float values
|
2022-10-20 15:12:34 +00:00
|
|
|
*/
|
|
|
|
void fromFloat(const us channel, const arma::Col<d> &data);
|
|
|
|
|
2022-10-10 17:17:38 +00:00
|
|
|
// Return value based on type
|
|
|
|
template <typename T> T &value(const us frame, const us channel) {
|
|
|
|
#if LASP_DEBUG == 1
|
|
|
|
check_type<T>();
|
|
|
|
#endif
|
|
|
|
return *reinterpret_cast<T *>(raw_ptr(frame, channel));
|
|
|
|
}
|
|
|
|
template <typename T> const T &value(const us frame, const us channel) const {
|
|
|
|
#if LASP_DEBUG == 1
|
|
|
|
check_type<T>();
|
|
|
|
#endif
|
|
|
|
return *reinterpret_cast<const T *>(raw_ptr(frame, channel));
|
2022-06-13 19:30:02 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 12:58:48 +00:00
|
|
|
/**
|
2022-10-10 17:17:38 +00:00
|
|
|
* @brief For debugging purposes: prints some stats
|
2022-07-20 12:58:48 +00:00
|
|
|
*/
|
2022-10-10 17:17:38 +00:00
|
|
|
void print() const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
template <typename T> void check_type() const {
|
|
|
|
using DataType = DataTypeDescriptor::DataType;
|
|
|
|
|
|
|
|
bool correct = false;
|
|
|
|
static_assert(std::is_arithmetic<T>::value);
|
|
|
|
|
|
|
|
if constexpr (std::is_floating_point<T>::value) {
|
|
|
|
correct |= sizeof(T) == 4 && dtype == DataType::dtype_fl32;
|
|
|
|
correct |= sizeof(T) == 8 && dtype == DataType::dtype_fl64;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
correct |= sizeof(T) == 1 && dtype == DataType::dtype_int8;
|
|
|
|
correct |= sizeof(T) == 2 && dtype == DataType::dtype_int16;
|
|
|
|
correct |= sizeof(T) == 4 && dtype == DataType::dtype_int32;
|
|
|
|
}
|
|
|
|
if (!correct) {
|
|
|
|
throw std::runtime_error("Wrong datatype for template argument");
|
|
|
|
}
|
2022-06-13 19:30:02 +00:00
|
|
|
}
|
2022-10-04 07:27:27 +00:00
|
|
|
|
2022-10-10 17:17:38 +00:00
|
|
|
template <typename T> arma::Mat<d> toFloat() const;
|
|
|
|
template <typename T> arma::Col<d> toFloat(const us channel_no) const;
|
|
|
|
template <typename T> d toFloat(const us frame_no, const us channel_no) const;
|
|
|
|
|
2022-10-20 15:12:34 +00:00
|
|
|
/* template <typename T> void fromFloat(const dmat&); */
|
|
|
|
template <typename T>
|
|
|
|
void fromFloat(const us channel_no, const arma::Col<d> &vals);
|
|
|
|
template <typename T>
|
|
|
|
void fromFloat(const us frame_no, const us channel_no, const d val);
|
2022-10-10 17:17:38 +00:00
|
|
|
/**
|
|
|
|
* @brief Return a value as floating point. Does a conversion from integer to
|
|
|
|
* float, if the stored type is integer. Also scales by its maximum value.
|
|
|
|
*
|
|
|
|
* @tparam T The original type
|
|
|
|
* @param frame Frame number
|
|
|
|
* @param channel Channel number
|
|
|
|
*
|
|
|
|
* @return Value converted to floating point
|
|
|
|
*/
|
2022-06-13 19:30:02 +00:00
|
|
|
};
|
2022-10-10 17:17:38 +00:00
|
|
|
|
2022-09-03 18:59:14 +00:00
|
|
|
/** @} */
|