include/wipal/wifi/frame/filter/linear_regression_synchronizer.hxx

00001 /*
00002  * WiPal - A library and a set of tools to manipulate wireless traces.
00003  * Copyright (C) 2007  Universite Pierre et Marie Curie - Paris 6
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  * MA  02110-1301  USA
00019  *
00020  * Author: Thomas Claveirole <thomas.claveirole@lip6.fr>
00021  */
00022 #ifndef WIFI_FRAME_FILTER_SYNCHRONIZER_HXX_
00023 # define WIFI_FRAME_FILTER_SYNCHRONIZER_HXX_
00024 
00025 # include "linear_regression_synchronizer.hh"
00026 
00027 # include <wipal/tool/linear_regression.hh>
00028 # include <wipal/tool/window.hh>
00029 # include <wipal/wifi/frame/filter/intersector.hh>
00030 # include <wipal/wifi/frame/filter/reference_blacklist.hh>
00031 
00032 namespace wifi
00033 {
00034   namespace frame
00035   {
00036     namespace filter
00037     {
00038 
00039       template <class OriginalFrameType, class ImplType>
00040       synchronized_frame<OriginalFrameType, ImplType>::
00041       synchronized_frame(const OriginalFrameType&       orig,
00042                          const coefs_type&              coefs):
00043         OriginalFrameType (orig),
00044         coefs (coefs)
00045       {
00046       }
00047 
00048       namespace internals
00049       {
00050 
00051         template <class I, class B1, class B2>
00052         lr_sync_iterator<I, B1, B2>::
00053         lr_sync_iterator(const iterable_type& iterable, bool end):
00054           super_type (),
00055           iterable_ (&iterable),
00056           next_ (end ? iterable.last_ : iterable.first_),
00057           next_wpos_ (0)
00058         {
00059           if (not end)
00060             increment();
00061         }
00062 
00063         template <class I, class B1, class B2>
00064         bool
00065         lr_sync_iterator<I, B1, B2>::equal(const exact_type& rhs) const
00066         {
00067           if (not rhs.value())
00068             return not this->value();
00069           return next_wpos_ == rhs.next_wpos_ and next_ == rhs.next_;
00070         }
00071 
00072         template <class I, class B1, class B2>
00073         void
00074         lr_sync_iterator<I, B1, B2>::increment()
00075         {
00076           typedef typename value_type::impl_type        impl_type;
00077 
00078           typedef
00079             typename tool::lr::pair_with_microseconds<impl_type>
00080             adapter_type;
00081 
00082           const I&      last = iterable_->last_;
00083           const size_t  window_size = next_->size();
00084 
00085           if (next_wpos_ == window_size and next_ == last)
00086             {
00087               this->value() = boost::none_t ();
00088               return;
00089             }
00090 
00091           {
00092             const std::pair<impl_type, impl_type> p =
00093               tool::linear_regression<impl_type, adapter_type> (next_->begin(),
00094                                                                 next_->end());
00095             this->value() = value_type (next_[next_wpos_], p);
00096 
00097             if (p.first < 0.9 or 1.1 < p.first)
00098               std::cerr << "WARNING: Dubious sync. coef. of " << p.first
00099                         << " for " << next_[next_wpos_].first.frame_id()
00100                         << '-' << next_[next_wpos_].second.frame_id()
00101                         << '.' << std::endl;
00102           }
00103           if (next_wpos_ < window_size / 2)
00104             ++next_wpos_;
00105           else
00106             {
00107               if (next_ != last)
00108                 ++next_;
00109               if (next_ == last)
00110                 ++next_wpos_;
00111             }
00112         }
00113 
00114       } // End of namespace wifi::frame::filter::internals.
00115 
00116       template <class I, class B>
00117       linear_regression_synchronizer<I, B>::
00118       linear_regression_synchronizer(const I& first, const I& last):
00119         first_ (first),
00120         last_ (last)
00121       {
00122       }
00123 
00124       namespace internals
00125       {
00126 
00127         template <class F, class BL>
00128         struct provide_lr_synchronizer
00129         {
00130           enum
00131             {
00132               window_size =
00133 
00134 #ifdef LRSYNC_WINDOW_SIZE
00135               LRSYNC_WINDOW_SIZE
00136 #else // ! LRSYNC_WINDOW_SIZE
00137               3
00138 #endif
00139 
00140             };
00141 
00142           typedef BL    blacklist;
00143 
00144           provide_lr_synchronizer(F& func, const blacklist& blist):
00145             func_ (func),
00146             blist_ (blist)
00147           {
00148           }
00149 
00150           template <class Intersector>
00151           void
00152           operator () (const Intersector& i)
00153           {
00154             typedef typename Intersector::const_iterator        i_iterator;
00155             typedef reference_blacklist<i_iterator>             blist;
00156             typedef typename blist::const_iterator              b_iterator;
00157             typedef tool::window_maker<b_iterator, window_size> window_maker;
00158             typedef typename window_maker::const_iterator       w_iterator;
00159             typedef linear_regression_synchronizer<w_iterator>  synchronizer;
00160 
00161             blist               l (i.begin(), i.end(), blist_);
00162             window_maker        w (l.begin(), l.end());
00163             synchronizer        s (w.begin(), w.end());
00164 
00165             func_(s);
00166           }
00167 
00168         private:
00169           F&                    func_;
00170           const blacklist&      blist_;
00171         };
00172 
00173       } // End of namespace wifi::frame::filter::internals.
00174 
00175       template <class U, class HT, template <class, class, class> class Int,
00176                 class I1, class I2, class F, class BL>
00177       void
00178       provide_lr_synchronizer(const I1& first1, const I1&       last1,
00179                               const I2& first2, const I2&       last2,
00180                               addr_mapping&                     mapping,
00181                               F&                                func,
00182                               bool                              filter_prism,
00183                               tool::endian::endianness          phy_end,
00184                               const BL&                         blist)
00185       {
00186         internals::provide_lr_synchronizer<F, BL> func2 (func, blist);
00187 
00188         provide_intersector<U, HT, Int>(first1, last1,
00189                                         first2, last2,
00190                                         mapping, func2, filter_prism, phy_end);
00191       }
00192 
00193 
00194     } // End of namespace wifi::frame::filter.
00195 
00196   } // End of namespace wifi::frame.
00197 
00198 } // End of namespace wifi.
00199 
00200 #endif // ! WIFI_FRAME_FILTER_SYNCHRONIZER_HXX_

Generated on Tue Jan 15 19:32:31 2008 for wipal by  doxygen 1.5.4