contrib/mul/mfpf/mfpf_norm_vec.h
Go to the documentation of this file.
00001 #ifndef mfpf_norm_vec_h_
00002 #define mfpf_norm_vec_h_
00003 //:
00004 // \file
00005 // \brief Sets vec to have zero mean and unit length
00006 // \author Tim Cootes
00007 
00008 #include <vnl/vnl_vector.h>
00009 #include <vcl_algorithm.h> // std::max()
00010 
00011 //: Sets vec to have zero mean and unit length
00012 inline void mfpf_norm_vec(vnl_vector<double>& vec, double var_min=1.0E-6,
00013                           double* pvar=0)
00014 {
00015   unsigned n=vec.size();
00016   double *v=vec.data_block();
00017   double sum=0.0,sum_sq=0.0;
00018   for (unsigned i=0;i<n;++i)
00019   {
00020     sum+=v[i]; sum_sq+=v[i]*v[i];
00021   }
00022   double mean = sum/n;
00023   double ss = vcl_max(var_min,sum_sq-n*mean*mean);
00024   double s = vcl_sqrt(ss);
00025 
00026   for (unsigned i=0;i<n;++i) v[i]=(v[i]-mean)/s;
00027   if(pvar) //optionally return variance
00028       *pvar=ss;
00029 }
00030 
00031 #endif // mfpf_norm_vec_h_