fifo.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Includes
00010 *****************************************************************************/
00011 
00012 #include <iostream>
00013 #include <gtest/gtest.h>
00014 #include "../../include/ecl/containers/array.hpp"
00015 #include "../../include/ecl/containers/fifo.hpp"
00016 
00017 /*****************************************************************************
00018 ** Using
00019 *****************************************************************************/
00020 
00021 using ecl::Array;
00022 using ecl::StandardException;
00023 using ecl::ContainerConcept;
00024 using ecl::FiFo;
00025 /*****************************************************************************
00026 ** Tests
00027 *****************************************************************************/
00028 TEST(FiFoTest,constructors)
00029 {
00030         FiFo<double> fifo(4, 2.0);
00031         EXPECT_EQ(2.0, fifo[0]);
00032         EXPECT_EQ(2.0, fifo[1]);
00033         EXPECT_EQ(2.0, fifo[2]);
00034         EXPECT_EQ(2.0, fifo[3]);
00035 }
00036 
00037 TEST(FiFoTest, poping )
00038 {
00039         FiFo<double> fifo(4);
00040         for( int i=0; i<4; i++ )
00041         {
00042                 fifo.push_back( (double)i );
00043         }
00044 
00045         EXPECT_EQ(0.0, fifo[0]);
00046         EXPECT_EQ(1.0, fifo[1]);
00047         EXPECT_EQ(2.0, fifo[2]);
00048         EXPECT_EQ(3.0, fifo[3]);
00049 }
00050 
00054 class movingAvg
00055 {
00056 public:
00057         movingAvg( const unsigned int widowsSize ) :
00058                 sum(0.0),
00059                 average(0.0),
00060                 windows_size(widowsSize),
00061                 first(true)
00062         {
00063                 fifo.resize( windows_size );
00064         }
00065         void reset()
00066         {
00067                 fifo.fill(0);
00068                 sum = 0;
00069                 average = 0.0;
00070         }
00071 
00072         void update( const double & incomingData )
00073         {
00074                 if( first )
00075                 {
00076                         fifo.fill( incomingData );
00077                         first = false;
00078                 }
00079 
00080                 sum -= fifo[0];
00081                 sum += incomingData;
00082                 fifo.push_back( incomingData );
00083                 average = sum/(double)(windows_size);
00084         }
00085         double sum;
00086         double average;
00087         unsigned int windows_size;
00088         FiFo<double> fifo;
00089         bool first;
00090 };
00091 
00097 TEST(FiFoTest, application )
00098 {
00099         movingAvg mavg( 10 );
00100 
00101         for( int i=0; i<50; i++ )
00102         {
00103                 mavg.update( (double)i );
00104 //              std::cout << mavg.average << std::endl;
00105         }
00106 
00107         EXPECT_EQ(44.5, mavg.average );
00108 }
00109 
00114 /*
00115  * @class
00116  * treats differential of Gaussian. Usual objective is to detect the significant change of data incoming.
00117  * you need just declaration of this class. and then please call update(..) function with your instance incoming data.
00118  * it will return result of Differential of Gaussian.
00119  *
00120  * @note
00121  * now, it build the kernel in contructor with hard coding. however we can formulize later.
00122  */
00123 template<typename N, int KernelSize>
00124 class DifferentialOfGaussian1D
00125 {
00126 public:
00127         DifferentialOfGaussian1D()
00128         {
00129                 buildKernel();
00130                 invalidate();
00131         }
00132 
00133         bool updateData( const N & incomingDatum, N & resultOfDOG )
00134         {
00135                 resultOfDOG = static_cast<N>(0);
00136                 data.push_back( incomingDatum );
00137                 for( int i=0; i<KernelSize; i++ )
00138                 {
00139                         resultOfDOG += (data[i]*kernel[i]);
00140                 }
00141 
00142                 if( number_of_incoming < 5 )
00143                 {
00144                         number_of_incoming++;
00145                         return false;
00146                 }
00147 
00148                 return true;
00149         }
00150 
00151         void invalidate()
00152         {
00153                 number_of_incoming = 0;
00154         }
00155 
00156 private:
00157         FiFo <float> data;
00158         Array<N> kernel;
00159         int number_of_incoming;
00160 
00161         void buildKernel()
00162         {
00163                 //build kernel
00164                 switch( KernelSize )
00165                 {
00166                 case 5:
00167                         data.resize(KernelSize);
00168                         kernel.resize(KernelSize);
00169                         kernel[0] = 0.1179f;
00170                         kernel[1] = 0.2642f;
00171                         kernel[2] = 0.0f;
00172                         kernel[3] = -0.2642f;
00173                         kernel[4] =  -0.1179f;
00174                         break;
00175 
00176                 default:
00177                         std::cout << "class DifferentialOfGaussian1D<N," << KernelSize  << "have no kernel information " << std::endl;
00178                         break;
00179                 }
00180         }
00181 };
00182 
00188 /*****************************************************************************
00189 ** Main program
00190 *****************************************************************************/
00191 
00192 int main(int argc, char **argv) {
00193 
00194     testing::InitGoogleTest(&argc,argv);
00195     return RUN_ALL_TESTS();
00196 }


ecl_containers
Author(s): Daniel Stonier
autogenerated on Mon Jul 3 2017 02:21:42