//====================================================== file = corr2.c ===== //= Program to correlate anomalies and glitches between two time series = //= - Uses a "backward" lag between glitch and anomaly = //=========================================================================== //= Notes: = //= 1) Input from input file "in.dat" to stdin (see example below) = //= * Input is two columns of integers with no commas = //= * No comments are allowed in the input file = //= 2) Anomalies occur in X[][0] and glitches occurs in X[][1]. An = //= anomaly is a "1" and a non-anomaly is a "0". A glitch is a "1" = //= and a non-glitch is a "0". = //= 3) The LAG is the distance in the forward direction allowed between = //= an anomaly and a glitch to trigger a "match". The value of LAG = //= must be manually set. = //= 4) Note that anomalies in the last LAG positions are not counted. = //= 5) Output is to stdout = //=-------------------------------------------------------------------------= //= Example "in.dat" file: = //= = //= 0 0 = //= 1 0 = //= 0 0 = //= 0 0 = //= 0 0 = //= 0 1 = //= 1 0 = //= 1 0 = //= 0 1 = //= 0 0 = //= 0 0 = //= 1 0 = //= 0 1 = //= 0 1 = //= 0 0 = //= 0 1 = //=-------------------------------------------------------------------------= //= Example output (for above "in.dat" and LAG = 3) = //= = //= ------------------------------------------------- corr2.c ----- = //= Total of 16 values = //= Lag = 3 = //= Number of glitches = 5 = //= Number of matching anomalies = 2 = //= Match percentage = 40.000000 % = //= --------------------------------------------------------------- = //=-------------------------------------------------------------------------= //= Build: bcc32 corr2.c = //=-------------------------------------------------------------------------= //= Execute: corr2 < in.dat = //=-------------------------------------------------------------------------= //= Author: Kenneth J. Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (07/04/02) - Genesis = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() and feof() #include // Needed for exit() and atof() #include // Needed for strcmp() //----- Defines ------------------------------------------------------------- #define FALSE 0 // Boolean false #define TRUE 1 // Boolean true #define MAX_SIZE 1000000 // Maximum size of data array #define LAG 3 // Lag in which to look for event //----- Globals ------------------------------------------------------------- int X[MAX_SIZE][2]; // Time series pairs read from "in.dat" int N; // Number of value pairs in "in.dat" //----- Function prototypes ------------------------------------------------- void load_X_array(void); // Load X array //=========================================================================== //= Main program = //=========================================================================== void main(void) { int glitch_count; // Count of X[][1] glitches int match_count; // Count of X[][0] matches int flag; // Boolean flag int i, j; // Loop counters // Load the series X printf("------------------------------------------------- corr2.c ----- \n"); load_X_array(); // Do lagged correlation tests in "backwards" direction glitch_count = match_count = 0; for (i=(N-1); i>=LAG; i--) { if (X[i][1] == TRUE) { glitch_count++; flag = FALSE; for (j=i; j>=(i - LAG); j--) if (X[j][0] == TRUE) { flag = TRUE; X[j][0] = FALSE; break; } if (flag == TRUE) match_count++; } } // Output results printf(" Total of %d values \n", N); printf(" Lag = %d \n", LAG); printf(" Number of glitches = %d \n", glitch_count); printf(" Number of matching anomalies = %d \n", match_count); if (glitch_count > 0) printf(" Match percentage = %f %% \n", 100.0 * (double) match_count / glitch_count); printf("--------------------------------------------------------------- \n"); } //=========================================================================== //= Function to load X array from stdin and determine N = //= - Does not allow for "&"-style comments = //=========================================================================== void load_X_array(void) { char instring0[256]; // Temporary string variable #1 char instring1[256]; // Temporary string variable #2 // Read all values into X N = 0; scanf("%s %s", instring0, instring1); while(!feof(stdin)) { // Enter values in array and increment array index X[N][0] = atoi(instring0); X[N][1] = atoi(instring1); N++; // Check if MAX_SIZE data values exceeded if (N >= MAX_SIZE) { printf("*** ERROR - greater than %ld data values \n", MAX_SIZE); exit(1); } // Read next two values as strings scanf("%s %s", instring0, instring1); } return; }