地震监测系统(完结版)

项目实现:

1.问题描述:
使用数据文件中的一组地震检波器测量值确定可能的地震事件的位置。

2. 输入输出描述:
(1)程序的输入是名为seismic.dat的数据文件和用于计算短时间能量和长时间能量的取样值的数目。输出是给出关于潜在的地震事件次数的报告。
(2)seismic.dat 的结构是这样的,第一行包含两个值: 地震检波器能量值的数目和时间间隔,从第二行开始就是能量值的数据,以空格分开。
(3)短时间窗口和长时间窗口的值可以由键盘读入。
(4)判定地震事件给定的阀值是 1.5。

seismic.dat 中的数据如下:
11 0.01
1 2 1 1 1 5 4 2 1 1 1

算法设计:

  1. 读取文件头并分配内存。
  2. 从数据文件读取地震数据,从键盘读取计算能量的短时间和长时间窗口测量值的数目。
  3. 计算各个时间点上的短时间窗口和长时间窗口的能量值,打印出可能的地震事件时间,在这里,因为会涉及到频繁调用短时间窗口和长时间窗口的能量值,我们可以将计算能量值设计为单独的一个函数。

需添加txt文件命名为:seismic.txt
内容为:11 0.01
1 2 1 1 1 5 4 2 1 1 1

代码实现如下:

#include <stdio.h>
#include <string>
#include <fstream>
#include <cmath>
#include <iostream>
#include <Windows.h>

using namespace std;

const double THRESHOLD = 1.5;

//计算短/长时间窗口能量数据的采样值
double power_w(double arr[], int length, int n);

int main(void) {
	string filename;
	ifstream fin;
	int num = NULL, short_window = NULL, long_window = NULL;
	double time_incr = NULL, *sensor = NULL, short_power = NULL, long_power = NULL;
	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-window: " << endl;
			cin >> short_window;

			cout << "Enter number of points 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 << "Possible event at " << time_incr * i << "seconds \n";
				}
			}

			delete[] sensor;
		}

		fin.close();
	}

	system("pause");
	return NULL;
}

//统计短/长事件窗口对应能量的采样值
double power_w(double arr[], int length, int n) {
	double xsquare = NULL;

	for (int i = 0; i < n; i++) {
		xsquare += pow(arr[length - i], 2);
	}
	return xsquare / n;

}

程序输出结果为:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学无止境12138

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值