fifo.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Includes
10 *****************************************************************************/
11 
12 #include <iostream>
13 #include <gtest/gtest.h>
14 #include "../../include/ecl/containers/array.hpp"
15 #include "../../include/ecl/containers/fifo.hpp"
16 
17 /*****************************************************************************
18 ** Using
19 *****************************************************************************/
20 
21 using ecl::Array;
24 using ecl::FiFo;
25 /*****************************************************************************
26 ** Tests
27 *****************************************************************************/
28 TEST(FiFoTest,constructors)
29 {
30  FiFo<double> fifo(4, 2.0);
31  EXPECT_EQ(2.0, fifo[0]);
32  EXPECT_EQ(2.0, fifo[1]);
33  EXPECT_EQ(2.0, fifo[2]);
34  EXPECT_EQ(2.0, fifo[3]);
35 }
36 
37 TEST(FiFoTest, poping )
38 {
39  FiFo<double> fifo(4);
40  for( int i=0; i<4; i++ )
41  {
42  fifo.push_back( (double)i );
43  }
44 
45  EXPECT_EQ(0.0, fifo[0]);
46  EXPECT_EQ(1.0, fifo[1]);
47  EXPECT_EQ(2.0, fifo[2]);
48  EXPECT_EQ(3.0, fifo[3]);
49 }
50 
54 class movingAvg
55 {
56 public:
57  movingAvg( const unsigned int widowsSize ) :
58  sum(0.0),
59  average(0.0),
60  windows_size(widowsSize),
61  first(true)
62  {
63  fifo.resize( windows_size );
64  }
65  void reset()
66  {
67  fifo.fill(0);
68  sum = 0;
69  average = 0.0;
70  }
71 
72  void update( const double & incomingData )
73  {
74  if( first )
75  {
76  fifo.fill( incomingData );
77  first = false;
78  }
79 
80  sum -= fifo[0];
81  sum += incomingData;
82  fifo.push_back( incomingData );
83  average = sum/(double)(windows_size);
84  }
85  double sum;
86  double average;
87  unsigned int windows_size;
88  FiFo<double> fifo;
89  bool first;
90 };
91 
97 TEST(FiFoTest, application )
98 {
99  movingAvg mavg( 10 );
100 
101  for( int i=0; i<50; i++ )
102  {
103  mavg.update( (double)i );
104 // std::cout << mavg.average << std::endl;
105  }
106 
107  EXPECT_EQ(44.5, mavg.average );
108 }
109 
114 /*
115  * @class
116  * treats differential of Gaussian. Usual objective is to detect the significant change of data incoming.
117  * you need just declaration of this class. and then please call update(..) function with your instance incoming data.
118  * it will return result of Differential of Gaussian.
119  *
120  * @note
121  * now, it build the kernel in contructor with hard coding. however we can formulize later.
122  */
123 template<typename N, int KernelSize>
124 class DifferentialOfGaussian1D
125 {
126 public:
127  DifferentialOfGaussian1D()
128  {
129  buildKernel();
130  invalidate();
131  }
132 
133  bool updateData( const N & incomingDatum, N & resultOfDOG )
134  {
135  resultOfDOG = static_cast<N>(0);
136  data.push_back( incomingDatum );
137  for( int i=0; i<KernelSize; i++ )
138  {
139  resultOfDOG += (data[i]*kernel[i]);
140  }
141 
142  if( number_of_incoming < 5 )
143  {
144  number_of_incoming++;
145  return false;
146  }
147 
148  return true;
149  }
150 
151  void invalidate()
152  {
153  number_of_incoming = 0;
154  }
155 
156 private:
157  FiFo <float> data;
158  Array<N> kernel;
159  int number_of_incoming;
160 
161  void buildKernel()
162  {
163  //build kernel
164  switch( KernelSize )
165  {
166  case 5:
167  data.resize(KernelSize);
168  kernel.resize(KernelSize);
169  kernel[0] = 0.1179f;
170  kernel[1] = 0.2642f;
171  kernel[2] = 0.0f;
172  kernel[3] = -0.2642f;
173  kernel[4] = -0.1179f;
174  break;
175 
176  default:
177  std::cout << "class DifferentialOfGaussian1D<N," << KernelSize << "have no kernel information " << std::endl;
178  break;
179  }
180  }
181 };
182 
188 /*****************************************************************************
189 ** Main program
190 *****************************************************************************/
191 
192 int main(int argc, char **argv) {
193 
194  testing::InitGoogleTest(&argc,argv);
195  return RUN_ALL_TESTS();
196 }
void resize(unsigned int length) ecl_assert_throw_decl(StandardException)
Resize the fifo storage.
Definition: fifo.hpp:167
Fixed size container with a few bells and whistles.
void push_back(const T &datum)
Push back onto the fifo.
Definition: fifo.hpp:143
Really simple fifo implementation.
Definition: fifo.hpp:82
TEST(FiFoTest, constructors)
Definition: fifo.cpp:28
int main(int argc, char **argv)
Definition: fifo.cpp:192


ecl_containers
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:30