// // Postprocess samples.dat // // // Author: P.J. Drongowski // 8 June 2013 // // Copyright (c) 2013 Paul. J. Drongowski // #include <errno.h> #include <stdlib.h> #include <stdio.h> #define SAMPLES_FILE_NAME "samples.dat" FILE* samples_file = NULL ; #define RESULTS_FILE_NAME "histogram.txt" FILE* results_file = NULL ; unsigned int number_of_samples = 1000 ; unsigned int* samples ; #define NUMBER_OF_BINS 50 #define MIN_VALUE 0 #define MAX_VALUE 200 #define BIN_SIZE ((MAX_VALUE-MIN_VALUE)/NUMBER_OF_BINS) unsigned long int bins[NUMBER_OF_BINS] ; unsigned long int overflows = 0 ; int main(int argc, char* argv[]) { int status = 0 ; int index = 0 ; unsigned long int sample = 0 ; int i ; double sum = 0.0 ; double mean = 0.0 ; if ((samples_file = fopen(SAMPLES_FILE_NAME, "r")) == NULL) { fprintf(stderr, "*error* Couldn't open samples file\n") ; exit( EXIT_FAILURE ) ; } if ((results_file = fopen(RESULTS_FILE_NAME, "w")) == NULL) { fprintf(stderr, "*error* Couldn't open results file\n") ; exit( EXIT_FAILURE ) ; } status = fscanf(samples_file, "%lu", &number_of_samples) ; if (errno) { fprintf(stderr, "*error* File I/O failed\n") ; exit( EXIT_FAILURE ) ; } samples = (unsigned int*)malloc(number_of_samples * sizeof(unsigned int)) ; index = 0 ; do { status = fscanf(samples_file, "%lu", &sample) ; samples[index] = sample ; index++ ; } while (status != EOF) ; if (errno) { fprintf(stderr, "*error* File I/O failed\n") ; exit( EXIT_FAILURE ) ; } fprintf(results_file, "Read %d samples\n", index-1) ; for (i = 0 ; i < NUMBER_OF_BINS ; i++) { bins[i] = 0 ; } sum = 0.0 ; overflows = 0 ; for (i = 0 ; i < number_of_samples ; i++) { index = samples[i] / BIN_SIZE ; if (index >= NUMBER_OF_BINS) { // fprintf(results_file, "Sample: %lu\n", samples[i]) ; overflows++ ; } else { bins[index]++ ; } sum += samples[i] ; } fprintf(results_file, "Mean: %7.2f\n", sum / (double)number_of_samples) ; fprintf(results_file, "%ld overflow samples\n", overflows) ; fprintf(results_file, "Bin size is %d cycles\n", BIN_SIZE) ; for (i = 0 ; i < NUMBER_OF_BINS ; i++) { fprintf(results_file, "Bin[%3d]: %ld\n", i, bins[i]) ; } fclose(results_file) ; fclose(samples_file) ; return( EXIT_SUCCESS ) ; }