Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleMeanAndDev.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /** @example ExampleMeanAndDev.cpp
26  @ingroup g_examples
27  @brief simple example which calculates mean and standard
28  deviation of values in a file read from disk
29  @author MIP */
30 
31 
32 #include <bias_config.h>
33 #include <math.h>
34 #include <Base/Debug/Debug.hh>
35 #include <Base/Debug/Error.hh>
36 #include <iostream>
37 #include <Base/Common/BIASpragma.hh>
38 
39 using namespace BIAS;
40 using namespace std;
41 
42 
43 int main(int argc, char *argv[])
44 {
45  if (argc<3) {
46  cerr << "Not enough parameters given!" << endl;
47  cout << "Usage: ExampleMeanAndDev [file name] [columns]" << endl;
48  exit(0);
49  }
50  FILE* myfile = fopen(argv[1], "r");
51  const int column = atoi(argv[2]);
52  char thestring[1024];
53  int nummeasurements = 0;
54  float *value = new float[column];
55  double valuesum=0,mean=0;
56 
57  while (fgets(thestring, 1020, myfile)) {
58  if (sscanf(thestring, "%f %f %f",&(value[0]),&(value[1]),&(value[2])) <
59  column) continue;
60  nummeasurements++;
61  valuesum += value[column-1];
62  }
63  fclose(myfile);
64  mean = valuesum / double(nummeasurements);
65 
66  myfile = fopen(argv[1], "r");
67  nummeasurements = 0;
68  valuesum = 0;
69 
70  while (fgets(thestring, 1020, myfile)) {
71  if (sscanf(thestring, "%f %f %f",&(value[0]),&(value[1]),&(value[2])) <
72  column) continue;
73  nummeasurements++;
74  valuesum += (mean-value[column-1]) * (mean-value[column-1]);
75  }
76  valuesum /= double(nummeasurements-1);
77  cout<<"Mean is "<< mean << " and dev is "<<sqrt(valuesum)<<" on "
78  <<nummeasurements<<" measurements"<<endl;
79  fclose(myfile);
80 
81  return 0;
82 }