You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wheeldetect/3part/Cyclops/include/GaussProbModel.h

87 lines
1.7 KiB
C++

/*! \file GaussProbModel.h
\brief A brief file description.
A more elaborated file description.
Created: 2015/06/22, author: Jin Bingwen.
*/
#ifndef __GaussProbModel_h_
#define __GaussProbModel_h_
#include <math.h>
#include <QFile>
#include <QTextStream>
class GaussProbModel
{
public:
GaussProbModel()
: mMean(0), mStddev(1) {}
GaussProbModel(double mean, double stddev)
: mMean(mean), mStddev(stddev) {}
double prob(double x) const
{
if (mStddev < 0.00001) {
printf("stddev < 0.00001\n");
return 0;
}
double normX = (x - mMean) / mStddev;
return exp(-0.5 * normX * normX) / mStddev * 0.39894228;
}
double normProb(double x) const{
return prob(x) * mStddev;
}
static void save2file(const vector<GaussProbModel>& vec, std::string fileName)
{
QFile file(fileName.data());
if (!file.open(QIODevice::WriteOnly))
{
return;
}
QTextStream out(&file);
for (int i = 0; i < vec.size(); ++i)
{
const GaussProbModel& gaussModel = vec[i];
out << gaussModel.getMean() << " " << gaussModel.getStddev() << "\n";
}
}
static void loadFromFile(vector<GaussProbModel>& vec, std::string fileName)
{
vec.clear();
QFile file(fileName.data());
if (!file.open(QIODevice::ReadOnly))
{
return;
}
QTextStream in(&file);
while (!in.atEnd())
{
double mean, stddev;
in >> mean;
in >> stddev;
vec.push_back(GaussProbModel(mean, stddev));
}
}
double getMean() const { return mMean; }
void setMean(double iVal) { mMean = iVal; }
double getStddev() const { return mStddev; }
void setStddev(double iVal) { mStddev = iVal; }
protected:
double mMean;
double mStddev;
private:
};
#endif // __GaussProbModel_h_