Scope
DaqChunk.cpp
1 #include "StdAfx.h"
2 #include "DaqChunk.h"
3 #include "helpers/helpers.h"
4 
5 namespace scope {
6 
7 DaqChunk::DaqChunk(const uint32_t& _perchannel, const uint32_t& _nchannels, const uint32_t& _area)
8  : area(_area)
9  , nchannels(_nchannels)
10  , perchannel(_perchannel)
11  , data(_perchannel*_nchannels, 0)
12  , lastmapped(_nchannels, std::begin(data)) {
13  assert(area < SCOPE_NAREAS);
14  size_t c = 0;
15  for (auto& l : lastmapped )
16  l = std::begin(data) + c++*perchannel;
17 }
18 
19 void DaqChunk::Downsample(const uint32_t& _factor) {
20  assert( (perchannel % _factor) == 0 ); // make sure we have _factor*perchannel=pixelsperchannel
21 
22  if ( _factor == 1 )
23  return;
24  uint32_t acc = 0; // accumulate as 32bit to avoid overflow
25  uint32_t halffactor = _factor>>2;
26  auto pixelend = std::begin(data);
27  // We do not have to care about the channel borders here.
28  // Outer loop increases iterator for downsampled pixels (overwrites old pixels)
29  for ( auto it = std::begin(data), downit = std::begin(data); it != std::end(data) ; ++downit ) {
30  acc = 0;
31  pixelend = it + _factor;
32  // inner loop increases iterator for pixels and accumulates
33  for ( ; it != pixelend ; ++it)
34  acc = acc + static_cast<uint32_t>(*it);
35  *downit = static_cast<uint16_t>(acc / _factor) + ((acc%_factor)>halffactor?1u:0u); // quick rounding trick for integer multiplication
36  }
37  perchannel /= _factor;
38  // since we have overwritten part of the vector with the downsampled pixels, we resize it now
39  data.resize(perchannel*nchannels);
40 
41  size_t c = 0;
42  for (auto& l : lastmapped )
43  l = std::begin(data) + c++*perchannel;
44 }
45 
46 void DaqChunk::Scale(const double& _factor) {
47  assert(_factor > 0 );
48  std::transform(std::begin(data), std::end(data), std::begin(data), [&_factor](uint16_t& elem) {
49  return round2ui16(_factor * static_cast<double>(elem)); } );
50 }
51 
52 }
std::vector< iterator > lastmapped
Iterators to positions that was last mapped.
Definition: DaqChunk.h:29
uint32_t perchannel
Number of samples per channel.
Definition: DaqChunk.h:19
STL namespace.
const uint32_t area
Area of the DaqChunk.
Definition: DaqChunk.h:13
std::vector< uint16_t > data
The data vector.
Definition: DaqChunk.h:26
This is the include file for standard system include files, or project specific include files that ar...
DaqChunk(const uint32_t &_perchannel, const uint32_t &_nchannels, const uint32_t &_area)
Definition: DaqChunk.cpp:7
virtual void Downsample(const uint32_t &_factor)
Downsamples by averaging _factor samples.
Definition: DaqChunk.cpp:19
virtual void Scale(const double &_factor)
Multiplies every sample by _factor.
Definition: DaqChunk.cpp:46
Various helper functions and classes for Scope.
const uint32_t nchannels
Number of channels in that area/DaqChunk.
Definition: DaqChunk.h:16