#include<fstream>
#include<iostream>
#include<Windows.h>
#include<string>
#include<cmath>
using namespace std;
const double THRESHOLD = 1.5;
//计算短/长时间窗口能量数据的采样值。
double power_w(double arr[], int length, int n);
int main()
{
string filename;
ifstream fin;
int num =0,short_windows=0,long_windows=0;
double time_incr =0,*sensor=NULL,short_power=0,long_power=0;
double ratio;
cout << "Enter name of input file" << endl;
cin >> filename;
fin.open(filename.c_str());
if (fin.fail())
{
cerr << "error opening input file" << endl;
exit(-1);
}
else
{
fin >> num >> time_incr;
cout << "num:" << num << " time_incr:" << time_incr << endl;
if (num >= 0)
{
sensor = new double[num];
for (int i = 0; i < num; i++)
{
fin >> sensor[i];
}
cout << "Enter number of points for short-windows" << endl;
cin >> short_windows;
cout << "Enter number of points for long-windows" << endl;
cin >> long_windows;
//分析能量数据找出地震时间
for (int i = long_windows - 1; i < num; i++)
{
short_power = power_w(sensor, i, short_windows);
long_power = power_w(sensor, i, long_windows);
ratio = short_power / long_power;
if (ratio> THRESHOLD)
{
cout << "Possible event at"<<time_incr*i << "seconds"<<endl;
}
}
delete[] sensor;
}
fin.close();
}
system("pause");
return 0;
}
//统计短/长事件窗口对应的能量值
double power_w(double arr[], int length, int n)
{
double xsquare = 0;
for (int i = 0; i < n; i++)
{
xsquare += pow(arr[length - i], 2);
}
return xsquare / n;
}