Modify your seismic program so that it can work with arbitrarily large data files without the use of a
header line! A data file will be posted on edmodo for your to process.
/* This program reads a seismic data file and then */
/* determines the times of possible seismic events. */
#include <stdio.h>
#define FILENAME "seismic1.txt"
#define MAX_SIZE 1000
double THRESHOLD;
int main(void)
{
int val=0;
while(val==0){
printf("Enter the Threshold Number:\n");
scanf("%lf", &THRESHOLD);
if(THRESHOLD<1){
printf("Please Enter a Positive Value above 1\n");
}
else{
val++;
}}
/* Declare variables and function prototypes. */
int k, npts, short_window, long_window, count=0;
double sensor[MAX_SIZE], time[2], time_incr, short_power, long_power, ratio;
FILE *file_ptr;
double power_w(double *ptr,int n);
/* Read sensor data file. */
file_ptr = fopen(FILENAME,"r");
if (file_ptr == NULL)
printf("Error opening input file. \n");
else
{
fscanf(file_ptr,"%d %lf",&npts,&time_incr);
if (npts > MAX_SIZE)
printf("Data file too large for array. \n");
else
{
/* Read data into an array. */
for (k=0; k<=npts-1; k++)
fscanf(file_ptr,"%lf",&sensor[k]);
/* Read window sizes from the keyboard. */
printf("Enter number of points for short window: \n");
scanf("%d",&short_window);
printf("Enter number of points for long window: \n");
scanf("%d",&long_window);
/* Compute power ratios and search for events. */
for (k=long_window-1; k<=npts-1; k++)
{
short_power = power_w(&sensor[k],short_window);
long_power = power_w(&sensor[k],long_window);
ratio = short_power/long_power;
if (ratio > THRESHOLD){
printf("Possible event at %f seconds \n",time_incr*k);
count++;
time[k]=time_incr*k;}
}
for(int j=long_window-1; j<=npts-1; j++){
if((time[j]-time[j+1])==.01){
count++;
}}
printf("%d Events Occured", count);
/* Close file. */
fclose(file_ptr);
}
}
return 0;
}
/*????????????????????????????????????????????????????????????-*/
/* This function computes the average power in a specified */
/* window of a double array. */
double power_w(double *ptr, int n)
{
/* Declare and initialize variables. */
int k;
double xsquare=0;
/* Compute sum of values squared in the array x. */
for (k=0; k<=n-1; k++)
xsquare += *(ptr-k)*(*(ptr-k));
/* Return the average squared value. */
return xsquare/n;
}
No comments:
Post a Comment