Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleMeanAndDev.cpp

simple example which calculates mean and standard deviation of values in a file read from disk

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** @example ExampleMeanAndDev.cpp
@ingroup g_examples
@brief simple example which calculates mean and standard
deviation of values in a file read from disk
@author MIP */
#include <bias_config.h>
#include <math.h>
#include <Base/Debug/Debug.hh>
#include <Base/Debug/Error.hh>
#include <iostream>
#include <Base/Common/BIASpragma.hh>
using namespace BIAS;
using namespace std;
int main(int argc, char *argv[])
{
if (argc<3) {
cerr << "Not enough parameters given!" << endl;
cout << "Usage: ExampleMeanAndDev [file name] [columns]" << endl;
exit(0);
}
FILE* myfile = fopen(argv[1], "r");
const int column = atoi(argv[2]);
char thestring[1024];
int nummeasurements = 0;
float *value = new float[column];
double valuesum=0,mean=0;
while (fgets(thestring, 1020, myfile)) {
if (sscanf(thestring, "%f %f %f",&(value[0]),&(value[1]),&(value[2])) <
column) continue;
nummeasurements++;
valuesum += value[column-1];
}
fclose(myfile);
mean = valuesum / double(nummeasurements);
myfile = fopen(argv[1], "r");
nummeasurements = 0;
valuesum = 0;
while (fgets(thestring, 1020, myfile)) {
if (sscanf(thestring, "%f %f %f",&(value[0]),&(value[1]),&(value[2])) <
column) continue;
nummeasurements++;
valuesum += (mean-value[column-1]) * (mean-value[column-1]);
}
valuesum /= double(nummeasurements-1);
cout<<"Mean is "<< mean << " and dev is "<<sqrt(valuesum)<<" on "
<<nummeasurements<<" measurements"<<endl;
fclose(myfile);
return 0;
}