Friday, November 3, 2017

Day 17 - Pointers in Function References + Bob Ross

HOMEWORK:
Seismic Event Code:
/* This program reads a seismic data file and then */
/* determines the times of possible seismic events. */
#include <stdio.h>
#define FILENAME "seismic2.txt"
#define MAX_SIZE 1000
//#define THRESHOLD 1.5
int main(void)
{
/* Declare variables and function prototypes. */
int k, t=0, g, npts, short_window, long_window, event_count=0;
double sensor[MAX_SIZE], event[MAX_SIZE], time_incr, short_power;
double long_power, ratio, threshold, event_time;
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);
printf("Time increment: %f seconds \n", 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);
printf("Enter a positive number for threshold value: \n");
scanf("%lf",&threshold);
if (threshold < 1)
{
printf("Sorry, threshold must be greater than 1.");
return 0;
}

/* 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 180*%d seconds \n",k);
event[t] = time_incr*(double)k;
t++;
g=t;
//printf("%d \n", g);
}
else
printf("No event at 180*%d seconds \n",k);
}
/* Close file. */
fclose(file_ptr);
}
//for (t=0; t<=g-1; t++)
//printf("%f \n", event[t]);
if (g==1)
printf("Number of Events: %d\n", g);
else
{
for (t=0; t<=g-2; t++)
{
event_time = (event[t+1] - event[t]);
printf("Event time= %f \n", event_time);
//printf("Time Increment= %f \n", time_incr);
if ((int)event_time <= (int)time_incr)
{
event_count++;
//printf("Event Count= %d \n", event_count);
}
}
printf("Number of Events: %d\n", event_count);
}
}
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