/* ERIK S. WHEELER auto_correlate.cc Auto Correlation using linked list read from binary data file. */ #include #include #define FALSE 0 #define MAXLENGTH_DEFAULT 256 #define MAXLD 1000000000 typedef double datatype; typedef struct listelement_type { datatype dataitem; struct listelement_type *next; } listelement; listelement_type * AddItemOldList (listelement_type * finalptr, datatype data) { if ((finalptr -> next = (struct listelement_type *) malloc (sizeof (listelement_type))) == NULL) { printf("\nMemory Allocation Error -- Execution Aborted\n"); exit(1); } finalptr = finalptr -> next; finalptr -> next = NULL; finalptr -> dataitem = data; return finalptr; } listelement_type * AddItemNewList (listelement_type * listptr, datatype data) { if ((listptr = (struct listelement_type *) malloc (sizeof (listelement_type))) == NULL) { printf("\nMemory Allocation Error -- Execution Aborted\n"); exit(1); } listptr -> next = NULL; listptr -> dataitem = data; return listptr; } datatype MultItem (listelement_type *listptr, listelement_type *currptr) { datatype returndata = 0; while (currptr != NULL) { returndata = returndata + ((listptr -> dataitem) * (currptr -> dataitem)); listptr = listptr -> next; currptr = currptr -> next; } return returndata; } void NormalizeToFirst (listelement_type * listptr) { datatype normalfactor = (listptr -> dataitem) / MAXLD; while (listptr != NULL) { listptr -> dataitem = (listptr -> dataitem) / normalfactor ; listptr = listptr -> next; } } void PrintQueue (listelement_type * listptr) { if (listptr == NULL) printf ("Queue is empty!\n"); else while (listptr != NULL) { printf ("%f\t", listptr -> dataitem); listptr = listptr -> next; } printf ("\n"); } listelement_type * RemoveFirstItem (listelement_type * listptr) { listelement_type * tempptr; /* printf ("Element removed is %f\n", listptr -> dataitem); */ tempptr = listptr -> next; free (listptr); return tempptr; } listelement_type * RemoveLastItem (listelement_type * listptr, listelement_type * finalptr) { if (listptr == finalptr) { /* printf ("Element removed is %f\n", listptr -> dataitem); */ free (listptr); listptr = NULL; finalptr = NULL; return listptr; } else { while ((listptr -> next) != finalptr) listptr = listptr -> next; /* printf ("Element removed is %f\n", finalptr -> dataitem); */ finalptr = listptr; free (finalptr -> next); finalptr -> next = NULL; return finalptr; } } /* void ClearQueueLtoF (listelement_type * listptr, listelement_type * finalptr) { while (finalptr != NULL) finalptr = RemoveLastItem (listptr, finalptr); } */ void ClearQueue (listelement_type * listptr) { while (listptr != NULL) listptr = RemoveFirstItem (listptr); } int main (int argc, char *argv[]) { listelement_type *listptrx = NULL, *currptrx = NULL, *finalptrx = NULL, *listptrrxx = NULL, *currptrrxx = NULL, *finalptrrxx = NULL; int i, MAXLENGTH=MAXLENGTH_DEFAULT; long int ld_data; FILE *fpin, *fpout; /* CHECK ARGUMENTS */ if ((argc > 4) || (argc < 3)) { printf("\nUsage: %s from-file to-file [Window-Size]\n", argv[0]); printf("from-file = binary file to be correlated\n"); printf("to-file = normalized autocorrelation of from-file\n"); printf("[Window-Size] = maximum window size (integer)\n"); printf("Brackets [] denote optional\n"); printf("Input and output are long int (32 bit) (max value = %d)\n",MAXLD); exit(1); } if (argc == 4) { MAXLENGTH = atoi(argv[3]); } else { MAXLENGTH = MAXLENGTH_DEFAULT; } listptrx = NULL; currptrx = NULL; finalptrx = NULL; /* OPEN BINARY FILE FOR READING */ if ((fpin=fopen(argv[1],"rb"))==NULL){ printf("Cannot open file %s",argv[1]); exit(1); } /* READ DATA INTO LINKED LIST OF MAXLENGTH <= FILELENGTH */ if (fread(&ld_data, sizeof(long int), 1, fpin) != 1) { printf("read error\n"); exit(1); } listptrx = AddItemNewList (listptrx, (datatype)ld_data); finalptrx = listptrx; i = 0; while (i++ < MAXLENGTH) { if (fread(&ld_data, sizeof(long int), 1, fpin) != 1) { if (!feof(fpin)) { printf("Read Error\n"); exit(1); } else i = MAXLENGTH; } else finalptrx = AddItemOldList (finalptrx, (datatype)ld_data); } fclose(fpin); /* AUTO CORRELATION FUNCTION */ listptrrxx = NULL; currptrrxx = NULL; finalptrrxx = NULL; currptrx = listptrx; listptrrxx = AddItemNewList (listptrrxx, 0); finalptrrxx = listptrrxx; currptrrxx = listptrrxx; while((currptrrxx -> dataitem = MultItem (listptrx, currptrx)) != 0) { finalptrrxx = AddItemOldList (finalptrrxx, 0); currptrx = currptrx -> next; currptrrxx = currptrrxx -> next; } /* PrintQueue (listptrx); PrintQueue (listptrrxx); */ NormalizeToFirst (listptrrxx); finalptrrxx = RemoveLastItem(listptrrxx, finalptrrxx); /* PrintQueue (listptrrxx); */ /* Store normalized Autocorrelation in file fpout as LONG INT */ if ((fpout = fopen(argv[2], "wb")) == NULL) { printf("Cannot open file %s \n",argv[2]); exit(1); } currptrrxx = listptrrxx; while (currptrrxx != NULL) { ld_data = (long int)(currptrrxx -> dataitem); if (fwrite(&ld_data, sizeof(long int), 1, fpout) != 1) { printf ("Read Error\n"); exit(1); } currptrrxx = currptrrxx -> next; } fclose(fpout); ClearQueue (listptrx); ClearQueue (listptrrxx); printf("\nAUTOCORRELATION of %s to %s for N = %d successful\n",argv[1],argv[2],MAXLENGTH); }