27 int main(
int argc,
char *argv[])
31 cerr <<
"USAGE: " << argv[0] <<
" spectrum" << endl;
33 cerr <<
" an example file, spectrum-example.txt, is distributed with libatomprobe" << endl;
38 std::string filename = argv[1];
39 cerr <<
"Processing file :" << filename << endl;
41 vector<vector<float> > data;
42 vector<string> headers;
45 cerr <<
"failed to load data" << endl;
51 cerr <<
"0 rows found. Aborting" << endl;
57 if(data[0].size() !=2)
59 cerr <<
"Expected nx2 data size, got nx" << data[0].size() << endl;
63 cerr <<
"Loaded data : " << data.size() <<
" rows" << endl;
71 for(
auto &v : data[0])
74 vector<double> xData,yData;
75 xData.resize(data[0].size());
76 yData.resize(data[0].size());
77 for(
auto ui=0u;ui<data[0].size();ui++)
79 xData[ui] = data[0][ui];
80 yData[ui] = data[1][ui];
83 double sigma,gamma,mu,amp;
84 if(!
fitVoigt(xData,yData, sigma,gamma,mu,amp))
86 cerr <<
"Curve fit failed. " << endl;
90 cerr <<
"VOIGT " << endl;
91 cerr <<
"=========" << endl;
92 cerr <<
"sigma:\t" << sigma <<endl;
93 cerr <<
"gamma:\t" << gamma <<endl;
94 cerr <<
"mu:\t" << mu <<
" ( Sqrt : " << mu*mu<<
")" << endl;
95 cerr <<
"amplitude:\t" << amp <<endl;
96 cerr <<
"=========" << endl;
100 vector<double> fittedVoigtProfile;
101 voigtProfile(xData,sigma,gamma,mu,amp,fittedVoigtProfile);
106 cerr <<
"Attempting semi-random fits of exponential-normal distribution." << endl;
107 cerr <<
"This can be unstable if too far from initial conditions" << endl;
108 cerr <<
" This is effectively a budget simulated anneal, and can fail. Try running a few times." << endl;
110 vector<double> bestV = {K,sigma,mu,amp};
112 const unsigned int N_ATTEMPTS=200;
114 float bestLSQ=std::numeric_limits<float>::max();
118 auto handler=gsl_set_error_handler_off();
123 fitExpNorm(xData,yData,K,mu,sigma,amp,firstIt);
128 vector<double> fittedExpNormProfile;
129 expNorm(xData,K,mu,sigma,amp,fittedExpNormProfile);
130 lsqThis=
lsq(yData,fittedExpNormProfile);
131 if(lsqThis < bestLSQ)
138 cerr <<
"Best: (K,Sigma,Mu,Amp) : (" << K <<
"," << sigma <<
"," << mu <<
"," << amp <<
")" << endl;
141 const float SCALE=0.35;
142 K = bestV[0] + gsl_ran_gaussian(r,bestV[0]*SCALE);
143 sigma = bestV[1] + gsl_ran_gaussian(r,bestV[1]*SCALE);
144 mu= bestV[2] + gsl_ran_gaussian(r,bestV[2]*SCALE);
145 amp = bestV[3] + gsl_ran_gaussian(r,bestV[3]*SCALE);
153 }
while (iter < N_ATTEMPTS);
154 gsl_set_error_handler(handler);
163 cerr <<
"ExpNorm" << endl;
164 cerr <<
"=========" << endl;
165 cerr <<
"K:\t" << K <<endl;
166 cerr <<
"mu:\t" << mu <<
" ( Sqrt : " << mu*mu<<
")" << endl;
167 cerr <<
"sigma:\t" << sigma <<endl;
168 cerr <<
"amplitude:\t" << amp <<endl;
169 cerr <<
"---------" << endl;
170 cerr <<
"LSQ:" << bestLSQ << endl;
171 cerr <<
"=========" << endl;
173 vector<double> fittedExpNormProfile;
174 expNorm(xData,K,mu,sigma,amp,fittedExpNormProfile);
177 ofstream fOut(
"fittedProfile.txt");
180 cerr <<
"Failed to open fittedProfile.txt for output" << endl;
184 fOut <<
"x\torig\tvoigt\tllg" << endl;
185 for(
auto ui=0u; ui<xData.size();ui++)
187 fOut << (xData[ui]) <<
"\t" << yData[ui] <<
"\t" << fittedVoigtProfile[ui] <<
"\t" << fittedExpNormProfile[ui]<<endl;
191 cerr <<
"Wrote curves to : fittedProfile.txt" << endl;
void transposeVector(std::vector< std::vector< T > > &v)
Perform an out-of-place tranposition of a given vector.
void voigtProfile(const std::vector< double > &x, double sigma, double gamma, double mu, double amp, std::vector< double > &y)
Generate a shifted voigt profile.
unsigned int loadTextData(const char *cpFilename, std::vector< std::vector< float > > &dataVec, std::vector< std::string > &headerVec, const char *delim="\", bool allowNan=true, bool allowConvFails=false, unsigned int headerCount=-1)
Load a CSV, TSV or similar text file. Assumes "C" Locale for input (ie "." as decimal separator)...
int main(int argc, char *argv[])
void expNorm(const std::vector< double > &x, double &K, double &mu, double &sigma, double &, std::vector< double > &y)
Exponentially decaying normal distribution.
bool fitExpNorm(const std::vector< double > &x, const std::vector< double > &y, double &K, double &mu, double &sigma, double &, bool autoInit=true)
Fit a smoothed log-gaussian curve (arxiv:0711.4449)
bool fitVoigt(const std::vector< double > &x, const std::vector< double > &y, double &sigma, double &gamma, double &mu, double &, bool autoInit=true)
Fit a Voigt function to the given X/Y values. Internally, a function minimiser is used...
gsl_rng * getRng() const
Obtain a GSL random number generator.
double lsq(const std::vector< double > &y, const std::vector< double > &yFit)