Scarab  v3.4.0
Project 8 C++ Utility Library
digital.hh
Go to the documentation of this file.
1 /*
2  * digital.hh
3  *
4  * Created on: ~2016
5  * Author: N.S. Oblath
6  *
7  *
8  */
9 
10 #ifndef SCARAB_DIGITAL_HH_
11 #define SCARAB_DIGITAL_HH_
12 
13 #include "scarab_api.hh"
14 
15 #include <cmath>
16 #include <type_traits>
17 
18 namespace scarab
19 {
20 
44  {
45  unsigned bit_depth;
46  unsigned levels;
47  unsigned data_type_size;
48  double v_range;
49  double v_offset;
50  double inv_levels;
51  double inv_v_range;
52  double dac_gain;
54  };
55 
56 
58  SCARAB_API void get_calib_params( unsigned n_bits, unsigned data_type_size, double v_offset, double v_range, bool bits_r_aligned, dig_calib_params *params );
60  SCARAB_API void get_calib_params2( unsigned n_bits, unsigned data_type_size, double v_offset, double v_range, double dac_gain, bool bits_r_aligned, dig_calib_params *params );
61 
62 
63  namespace detail {
64  enum class is_signed {};
65  enum class is_unsigned {};
66  }
67 
68  template< typename condition >
70 
71  template< typename condition >
73 
75  template< typename dig_type, typename an_type >
76  an_type d2a( dig_type dig, const struct dig_calib_params* params )
77  {
78  return params->v_offset + params->dac_gain * ( an_type )dig;
79  }
80 
82  template< typename an_type, typename dig_type,
84  dig_type a2d( an_type analog, const struct dig_calib_params* params )
85  {
86  analog = ( analog - params->v_offset ) * params->inv_v_range * (an_type)(params->levels);
87  if( analog > (an_type)(params->levels - 1) ) analog = params->levels - 1;
88  else if( analog < 0. ) analog = 0.;
89  return (dig_type)std::round( analog );
90  }
91 
93  template< typename an_type, typename dig_type,
95  dig_type a2d( an_type analog, const struct dig_calib_params* params )
96  {
97  double half_levels = params->levels * 0.5;
98  analog = ( analog - params->v_offset ) * params->inv_v_range * (an_type)(params->levels);
99  if( analog > (an_type)(half_levels - 1) ) analog = half_levels - 1;
100  else if( analog < -half_levels ) analog = -half_levels;
101  return (dig_type)std::round( analog );
102  }
103 
104 }
105 
106 #endif // SCARAB_DIGITAL_HH_
void get_calib_params2(unsigned n_bits, unsigned data_type_size, double v_offset, double v_range, double dac_gain, bool bits_r_aligned, dig_calib_params *params)
Calculate the digitizer calibration parameters when given the DAC gain (e.g. from a digitizer&#39;s own c...
Definition: digital.cc:32
#define SCARAB_API
Definition: scarab_api.hh:24
std::string type(const x_type &a_param)
Definition: typename.hh:24
an_type d2a(dig_type dig, const struct dig_calib_params *params)
Convert a signed or unsigned digital value to an analog value.
Definition: digital.hh:76
Collection of parameters used for converting between analog and digital data.
Definition: digital.hh:43
typename std::enable_if< condition::value, detail::is_unsigned >::type enable_if_unsigned
Definition: digital.hh:72
typename std::enable_if< condition::value, detail::is_signed >::type enable_if_signed
Definition: digital.hh:69
dig_type a2d(an_type analog, const struct dig_calib_params *params)
Convert an analog value to an unsigned digital value.
Definition: digital.hh:84
void get_calib_params(unsigned n_bits, unsigned data_type_size, double v_offset, double v_range, bool bits_r_aligned, dig_calib_params *params)
Calculate the digitizer calibration parameters with basic parameters: number of bits, Voffset, and Vrange.
Definition: digital.cc:18
To round(const std::chrono::duration< Rep, Period > &d)
Definition: date.h:1162