00001 #pragma ident "$Id: AllanDeviation.hpp 1209 2008-04-22 03:45:34Z ocibu $"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #ifndef GPSTK_ALLANDEVIATION_HPP
00029 #define GPSTK_ALLANDEVIATION_HPP
00030
00031 #include <vector>
00032 #include <cmath>
00033 #include <ostream>
00034
00035 #include "Exception.hpp"
00036
00037 namespace gpstk
00038 {
00041
00042
00044 class AllanDeviation
00045 {
00046 public:
00047 AllanDeviation(std::vector<double>& phase, double tau0) throw(Exception)
00048 : N(phase.size()-1), numGaps(0)
00049 {
00050 if(N < 1 )
00051 {
00052 Exception e("Need more than 2 point to compute a meaningful allan variance.");
00053 GPSTK_THROW(e);
00054 }
00055
00056
00057
00058
00059
00060
00061 double sum, sigma;
00062 for(int m = 1; m <= (N-1)/2; m++)
00063 {
00064 double tau = m*tau0;
00065 sigma = 0;
00066
00067 for(int i = 0; i < (N-2*m); i++)
00068 {
00069 sum = 0;
00070 if((phase[i+2*m]==0 || phase[i+m]==0 || phase[i]==0)
00071 && i!=0 && i!=(N-2*m-1))
00072 numGaps++;
00073 else
00074 sum = phase[i+2*m] - 2*phase[i+m] + phase[i];
00075 sigma += sum * sum;
00076 }
00077
00078 sigma = sigma / (2.0*((double)N-(double)numGaps-0-2.0*(double)m)*tau*tau);
00079 sigma = sqrt(sigma);
00080 deviation.push_back(sigma);
00081 time.push_back(tau);
00082 }
00083 }
00084
00085 void dump(std::ostream& s = std::cout) const throw()
00086 {
00087 std::vector<double>::const_iterator i=deviation.begin(),j=time.begin();
00088 for (; i != deviation.end() && j != time.end(); i++,j++)
00089 s << *j << " " << *i << std::endl;
00090 };
00091
00092 const int N;
00093 std::vector<double> deviation, time;
00094 int numGaps;
00095 };
00096
00097 std::ostream& operator<<(std::ostream& s, const AllanDeviation& a)
00098 {
00099 a.dump(s);
00100 return s;
00101 }
00102
00103 }
00104
00105 #endif