TwoBit1or2.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2008 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // dspsr/Kernel/Classes/dsp/TwoBit1or2.h
10 
11 #ifndef __TwoBit1or2_h
12 #define __TwoBit1or2_h
13 
14 // #define _DEBUG
15 
16 #include "dsp/TwoBitLookup.h"
17 #include <vector>
18 
19 namespace dsp
20 {
21  class TwoBit1or2 : public TwoBitLookup
22  {
23  public:
24 
25  TwoBit1or2 ();
26  ~TwoBit1or2 ();
27 
29  void lookup_build (TwoBitTable*, JenetAnderson98* = 0);
30 
32  void nlow_build (TwoBitTable* table);
33 
35  void get_lookup_block (float* lookup, TwoBitTable* table);
36 
38  unsigned get_lookup_block_size ();
39 
40  protected:
41 
42  char nlow_lookup [4];
43 
44  std::vector<unsigned char> temp_values;
45  void create ();
46  };
47 
48 
50  template<unsigned N>
51  class TwoBitToChar
52  {
53  };
54 
56  template<>
57  class TwoBitToChar<1>
58  {
59  public:
60  template<class Iterator, class Mask>
61  inline void operator() (Iterator& from, Mask& mask,
62  unsigned char* to, unsigned n)
63  {
64  for (unsigned i=0; i<n; i++)
65  {
66  to[i] = mask (*from);
67  ++ from;
68  }
69  }
70  };
71 
73  template<>
74  class TwoBitToChar<2>
75  {
76  public:
77  template<class Iterator, class Mask>
78  inline void operator() (Iterator& from, Mask& mask,
79  unsigned char* to, unsigned n)
80  {
81  const unsigned n2 = n >> 1; // quick division by 2
82  for (unsigned i=0; i<n2; i++)
83  {
84  to[i*2] = mask (*from, 0);
85  to[i*2+1] = mask (*from, 1);
86  ++ from;
87  }
88  }
89  };
90 
92 
96  template<unsigned N, class Mask>
97  class TwoBit : public TwoBit1or2
98  {
99 
100  TwoBitToChar<N> unpacker;
101 
102  public:
103 
104  Mask mask;
105 
106  static const unsigned samples_per_byte;
107  static const unsigned lookup_block_size;
108 
109  bool bad;
110 
111  template<class Iterator>
112  inline void prepare (Iterator& input, unsigned ndat)
113  {
114  temp_values.resize (ndat);
115 
116 #ifdef _DEBUG
117  std::cerr << "TwoBit<" << N << ">::prepare ndat=" << ndat << std::endl;
118 #endif
119 
120  unpacker (input, mask, &(temp_values[0]), ndat);
121 
122  nlow = 0;
123 
124  unsigned total = 0;
125 
126  for (unsigned idat=0; idat < ndat; idat++)
127  {
128  nlow += nlow_lookup[ temp_values[idat] ];
129  total += temp_values[idat];
130  }
131 
132  bad = (total == 0);
133  }
134 
135  template<class Iterator>
136  inline void unpack (const Iterator& input, unsigned ndat,
137  float* output, unsigned output_incr, unsigned& _nlow)
138  {
139  _nlow = nlow;
140 
141  nlow /= ndim;
142 
143 #ifdef _DEBUG
144  std::cerr << "TwoBit<" << N << ">::unpack ndat=" << ndat
145  << " nlow=" << nlow << " nlow_min=" << nlow_min << std::endl;
146 #endif
147 
148  if (nlow < nlow_min || nlow > nlow_max)
149  return;
150 
151  float* fourval = lookup_base + (nlow-nlow_min) * lookup_block_size;
152 
153  for (unsigned idat=0; idat < ndat; idat++)
154  output[idat*output_incr] = fourval[ temp_values[idat] ];
155  }
156  };
157 }
158 
159 template<unsigned N, class Mask>
160 const unsigned dsp::TwoBit<N,Mask>::samples_per_byte = N;
161 
162 template<unsigned N, class Mask>
163 const unsigned dsp::TwoBit<N,Mask>::lookup_block_size = 4;
164 
165 #endif
166 
Contains all Baseband Data Reduction Library classes.
Definition: ASCIIObservation.h:17
Masks samples in the input TimeSeries by setting them to zero.
Definition: Mask.h:25
Unpack one or two 2-bit samples per byte from an array of bytes.
Definition: TwoBit1or2.h:102
Defines interface to 2-bit extractors.
Definition: TwoBit1or2.h:56

Generated using doxygen 1.8.17