#include "bnewhead.h"
int main ()
{
int y,i=1,n= 400 ;
double in_data_sd, in_data_sd_toll, in_data_size, in_data_abs_toll;
double out_flag_const_data;
vector<double> in_data; //indata
ofstream out1("sinfn.dat");
out_flag_const_data = 0;
in_data_sd = 0;
in_data_size = 0;
in_data_abs_toll = 0.1;
in_data_sd_toll = 0.001; //analyse data initially
for(y = 0; y < n; y++ )//generating the data to be analysed
{
in_data.push_back(sin(y*.1 ) + 1);
}
in_data[30] = 9;
in_data[40] = -2;
for(y = 0; y < n; y++ ) //sending to data to a file (so i can plot it and look at it)
{
out1 << y << " " << in_data[y]<< "\n";
}
in_data_sd = stddev(in_data);
extreme(in_data, in_data_sd, i,i); //calling the function here
return 0;
}
double extreme( vector<double> in_data, double std_dev, double omega1, double field)
{
int i;
int flag_old_extrema;
double in_data_sd, in_data_sd_toll, in_data_size, in_data_abs_toll;
double out_flag_const_data = 0;
vector<double> out_extrema;
in_data_abs_toll = 0.1; //tollerance of the difference to specify a new extrema
in_data_sd_toll = 0.001; //analyse data initially by getting the standard deviation
//(to see if itsmore or less constant
in_data_sd = std_dev;
in_data_size = in_data.size();
//if its constant data then we can just send out one number.. bish bash bosh
if (in_data_sd < in_data_sd_toll)
{
out_flag_const_data = 1;
out_extrema.push_back(in_data[in_data_size]);
}
//if it isn't constant then we have loop over all the data
else
{
for(i= 0; i < in_data_size - 1; i++)
{
if ((in_data[i] > in_data[i-1]) && (in_data[i]>in_data[i+1]))
{
if (out_extrema.size() == 0)
{
//First extrema
out_extrema.push_back(in_data[i]);
}
// possible new extremum
else
{
flag_old_extrema = 0;
if ( abs(in_data[i] - out_extrema[i-1]) <= in_data_abs_toll )
{
flag_old_extrema = 1;
}
if (flag_old_extrema == 0)
{
out_extrema.push_back(in_data[i]);
}
}
}
if ((in_data[i] < in_data[i-1]) && (in_data[i] < in_data[i+1]))
{
if (out_extrema.size() == 0)
{
//First extrema
out_extrema.push_back(in_data[i]);
}
// possible new extremum
else
{ flag_old_extrema = 0;
if ( abs(in_data[i] - out_extrema[i-1]) <= in_data_abs_toll )
{
flag_old_extrema = 1;
}
if (flag_old_extrema == 0)
{
out_extrema.push_back(in_data[i]);
}
}
}
}
}
if(field == 1) {
for(i = 0; i < out_extrema.size(); i++)
{
cout<< i<< " " << out_extrema[i] << "\n";
}
}
if(field == 2) {
for(i = 0; i < out_extrema.size(); i++)
{
// out2<< omega1<< " " << out_extrema[i] << "\n";
}
}
return 0;
}
lnx0023ae8648e1 (87) g++ extr.cpp 2dev.cpp -Wall
extr.cpp:4: error: 'vector' was not declared in this scope
extr.cpp:4: error: expected primary-expression before 'double'
extr.cpp:4: error: expected primary-expression before 'double'
extr.cpp:4: error: expected primary-expression before 'double'
extr.cpp:4: error: expected primary-expression before 'double'
extr.cpp:4: error: initializer expression list treated as compound expression
extr.cpp:5: error: expected ',' or ';' before '{' token
2dev.cpp:7: error: expected initializer before 'int'
from the error statements it appears that there is something wrong with the header file, but i can't understand what the problem is...
when i put the function which is causing trouble in the main code it works fine... without any problems...
You are not #including anything in extr.cpp so it doesn't know about std::vector.
In the header you have the function bodies of square and stddev but you should move them to a cpp file.
And you shouldn't be usingnamespace std; in a header
What I ended up doin was putting all of my functions in a single .cpp file... but I still ended up putting usingnamespace std; in the header file (it wouldn't work otherwise)
#include "3newhead.h"
usingnamespace std;
//square function (so i don't have to use pow!!)
double square(double x ){return x*x; }
//std_deviation
double stddev(vector<double> x, double avg)
{
int i;
double size, deviation, sum, var;
/* sum = 0;
for(i = 0; i < x.size(); i ++)
{
sum+= x[i];
}
avg = sum/x.size();
*/
for(i = 0; i< x.size(); i++)
{
var += square(avg - x[i]);
}
deviation = sqrt(var/x.size());
return deviation;
}
//average
//
double avg(vector<double> x)
{
int i;
double average, sum;
sum = 0;
for( i = 0; i < x.size(); i++)
{ sum += x[i]; }
average = sum/x.size();
return average;
}
//picks out extrema ... kindof
vector<double> extreme( vector<double> in_data, double std_dev, double omega1,
double field,double in_data_abs_toll,
double in_data_sd_toll)
{
int i;
int flag_old_extrema;
double out_flag_const_data;
vector<double> out_extrema;
//if its constant data then we can just send out one number
//(i reckon the average of the data is safe enough)
if (std_dev < in_data_sd_toll)
{
out_flag_const_data = 1;
cout << "\nconst_data\n";
out_extrema.push_back(avg(in_data));
}
//if it isn't constant then we have loop over all the data
else
{
for(i= 0; i < in_data.size(); i++)
{
if ((in_data[i] > in_data[i-1]) && (in_data[i]>in_data[i+1]))
{
if (out_extrema.size() == 0)
{
//First extrema
out_extrema.push_back(in_data[i]);
}
// possible new extremum!!
else
{
flag_old_extrema = 0;
if ( abs(in_data[i] - out_extrema[i-1]) <= in_data_abs_toll )
{
flag_old_extrema = 1;
}
if (flag_old_extrema == 0)
{
out_extrema.push_back(in_data[i]);
}
}
}
/*
Leaving this out so I can get the code to actually work properly
... once it can find the maxima then it will be easy to get it to find the minima
if ((in_data[i] < in_data[i-1]) && (in_data[i] < in_data[i+1]))
{
if (out_extrema.size() == 0)
{
//First extrema
out_extrema.push_back(in_data[i]);
}
// possible new extremum!!
else
{
flag_old_extrema = 0;
if ( abs(in_data[i] - out_extrema[i-1]) <= in_data_abs_toll )
{
flag_old_extrema = 1;
}
if (flag_old_extrema == 0)
{
out_extrema.push_back(in_data[i]);
}
}
}
*/
}
}
return out_extrema;
}
the code now runs .. which I am very happy about (thanks bazzy!) ...
but it doesn't seem to work particularly well... if I get it working properly I will post it here