BeoSubSensor.C

Go to the documentation of this file.
00001 /*!@file BeoSub/BeoSubSensor.C Encapsulation of a sensor for the BeoSub */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/BeoSub/BeoSubSensor.C $
00035 // $Id: BeoSubSensor.C 5709 2005-10-13 07:53:05Z ilab16 $
00036 //
00037 
00038 #include "BeoSub/BeoSubSensor.H"
00039 #include "Util/log.H"
00040 #include <algorithm>
00041 #include <cmath>
00042 
00043 // ######################################################################
00044 template <class T>
00045 BeoSubSensor<T>::BeoSubSensor(const uint qlen, const double decay) :
00046   itsQ(), itsQlen(qlen), itsCacheValid(false), itsDecay(decay)
00047 {
00048   pthread_mutex_init(&itsMutex, NULL);
00049 }
00050 
00051 // ######################################################################
00052 template <class T>
00053 BeoSubSensor<T>::~BeoSubSensor()
00054 {
00055   pthread_mutex_destroy(&itsMutex);
00056 }
00057 
00058 // ######################################################################
00059 template <class T>
00060 void BeoSubSensor<T>::newMeasurement(const T& val)
00061 {
00062   pthread_mutex_lock(&itsMutex);
00063   itsCacheValid = false;
00064   itsQ.push_front(val);
00065   while (itsQ.size() > itsQlen) itsQ.pop_back();
00066   pthread_mutex_unlock(&itsMutex);
00067 }
00068 
00069 // ######################################################################
00070 template <class T>
00071 T BeoSubSensor<T>::getValue() const
00072 {
00073   // compute the average of the data:
00074   T val;
00075   pthread_mutex_lock(const_cast<pthread_mutex_t *>(&itsMutex));
00076   if (itsCacheValid)
00077     val = itsCachedValue;
00078   else
00079     {
00080       val = averageBeoSubSensorValue(itsQ, itsDecay);
00081       (const_cast<BeoSubSensor *>(this))->itsCachedValue = val;
00082       (const_cast<BeoSubSensor *>(this))->itsCacheValid = true;
00083     }
00084   pthread_mutex_unlock(const_cast<pthread_mutex_t *>(&itsMutex));
00085   return val;
00086 }
00087 
00088 // ######################################################################
00089 template <class T>
00090 void BeoSubSensor<T>::reset()
00091 {
00092   pthread_mutex_lock(&itsMutex);
00093   itsQ.clear();
00094   itsCacheValid = false;
00095   pthread_mutex_unlock(&itsMutex);
00096 }
00097 
00098 // ######################################################################
00099 template <class T>
00100 bool BeoSubSensor<T>::check() const
00101 {
00102 
00103   // FIXME: will need to think about that now that the class is templatized
00104 
00105   /*
00106   // get min, max and average:
00107   float mi = 1.0e30F, ma = -1.0e30F, avg = 0.0F; int n = 0;
00108   pthread_mutex_lock(&itsMutex);
00109   for (std::deque<float>::const_iterator i = itsQ.begin(); i < itsQ.end(); i++)
00110     {
00111       float val = *i;
00112       if (val < mi) mi = val;
00113       if (val > ma) ma = val;
00114       avg += val;
00115       ++ n;
00116     }
00117   pthread_mutex_unlock(&itsMutex);
00118 
00119   // if we did not accumulate any data, we are not ok!
00120   if (n == 0) { LINFO("No data -- RETURNING FALSE"); return false; }
00121 
00122   // compare (max-min) to avg:
00123   float diff = fabs(ma - mi); avg = fabs(avg) / float(n);
00124   if (diff > 0.25 * avg)
00125     {
00126       LINFO("max-min=%f, avg=%f -- RETURNING FALSE", diff, avg);
00127       return false;
00128     }
00129   */
00130   // everything seems okay:
00131   return true;
00132 }
00133 
00134 // ######################################################################
00135 template <class T>
00136 T averageBeoSubSensorValue(const std::deque<T> data, const double factor)
00137 {
00138   T val(0); double fac = 1.0;
00139   for (typename std::deque<T>::const_iterator
00140          i = data.begin(); i < data.end(); ++i)
00141     {
00142       val += (*i) * fac;
00143       fac *= factor;
00144     }
00145   if (data.size()) val /= data.size();
00146   return val;
00147 }
00148 
00149 // ######################################################################
00150 Angle averageBeoSubSensorValue(std::deque<Angle> data, const double factor)
00151 { return averageVectorAngle(data.begin(), data.end(), factor); }
00152 
00153 
00154 // Instantiations:
00155 template class BeoSubSensor<float>;
00156 template class BeoSubSensor<double>;
00157 template class BeoSubSensor<Angle>;
00158 
00159 // ######################################################################
00160 /* So things look consistent in everyone's emacs... */
00161 /* Local Variables: */
00162 /* indent-tabs-mode: nil */
00163 /* End: */
Generated on Sun May 8 08:40:19 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3