Scarab  v1.5.2
Project 8 C++ Utility Library
digital.hh
Go to the documentation of this file.
1 /*
2  * digital.h
3  *
4  * written by jared kofron <jared.kofron@gmail.com>
5  *
6  * functions for converting from D2A and A2D. prototypes are declared in
7  * digital.h. versions are included for both floats and doubles. there are
8  * two styles of the functions inspired by the GSL way of doing things - a
9  * normal version which returns the simple type of interest, and an 'error'
10  * version which can return information about bad arguments.
11  */
12 
13 #ifndef SCARAB_DIGITAL_HH_
14 #define SCARAB_DIGITAL_HH_
15 
16 #include "scarab_api.hh"
17 
18 namespace scarab
19 {
20 
22  {
23  unsigned bit_depth;
24  unsigned levels;
25  unsigned data_type_size;
26  double v_range;
27  double v_offset;
28  double inv_levels;
29  double inv_v_range;
30  double dac_gain;
32  };
33 
34 
35 
36  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 );
37  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 );
38 
39 
40  /*
41  * convert a digital <=64 bit value to a double or float.
42  */
43  template< typename dig_type, typename an_type >
44  an_type d2a( dig_type dig, const struct dig_calib_params* params )
45  {
46  return params->v_offset + params->dac_gain * ( an_type )dig;
47  }
48 
49  /*
50  * convert an analog value to a digital value.
51  */
52  template< typename an_type, typename dig_type >
53  dig_type a2d( an_type analog, const struct dig_calib_params* params )
54  {
55  analog = ( analog - params->v_offset ) * params->inv_v_range * (an_type)(params->levels);
56  if( analog > (an_type)(params->levels - 1) ) analog = params->levels - 1;
57  else if( analog < 0. ) analog = 0.;
58  return (dig_type)analog;
59  }
60 
61 }
62 
63 #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)
Definition: digital.cc:32
#define SCARAB_API
Definition: scarab_api.hh:24
an_type d2a(dig_type dig, const struct dig_calib_params *params)
Definition: digital.hh:44
dig_type a2d(an_type analog, const struct dig_calib_params *params)
Definition: digital.hh:53
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)
Definition: digital.cc:18