整个项目思路即在一组观测数据中,地震专家(即用户)输入观测窗口的最小数量和最大数量,进行预测峰值点
数据文件如图所示:
#define _CRT_SECURE_NO_WARNINGS
#include<fstream>
#include<string>
#include<iostream>
#include<cmath>using namespace std;const double THRESHOLD = 1.5;
//长时间窗口的平方和除个数,与短时间窗口的平方和除个数//计算短/长时间窗口能量数据的采样值
//统计窗口对应能量值的采样
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;}int main() {string filename;ifstream fin;//从文件读取数据的输入文件流int num=0,short_window=0,long_window=0;//定义读取能量数据的长度是11double 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 piont for short_window" << endl;cin >> short_window;cout << "Enter number of piont for long_window" << endl;cin >> long_window;//分析能量数据,找出地震时间for (int i = long_window - 1; i < num; i++){short_power = power_w(sensor, i, short_window);long_power = power_w(sensor, i, long_window);ratio = short_power / long_power;if (ratio > THRESHOLD) {cout << "Passible event at "<<time_incr*i<<"seconds\n";}}}delete[] sensor;fin.close();}return 0;}
运行结果:
在0.05秒和0.06秒到达地震,