// file: csnr_file_0.cc // // this function reads in the data // // system include file // #include // local include file // #include "global_constants.h" #include "calculate_snr_constants.h" #include "calculate_snr.h" // function: read_signal_cc // // arguments: // float*& signal_window_a: (output) buffer contain the signal // int num_chans_a: (input) number of channels // int start_time_a: (input) starting time of window // char* datafile_a: (input) name of data file // int debug_mode_a: (input) debug level // // return: an int to indicate status // // this function parses the command line arguments // // int read_signal_cc (float*& signal_window_a, int num_chans_a, int start_sample_a, int end_sample_a, int end_file_sample_a, char* datafile_a, int debug_mode_a) { // open the file // FILE* fp = fopen(datafile_a, "r"); if (fp == (FILE*)NULL) { fprintf(stdout, "Unable to open data file\n"); return FALSE; } // make sure that start and end sample are within bounds // int new_start = start_sample_a; int new_end = end_sample_a; int num_samples = 0; if (start_sample_a < 0) { // pad it with 0s if window starts before file starts // for (int i = start_sample_a; i < 0; i++) { signal_window_a[num_samples++] = 0; } new_start = 0; } if (end_sample_a > end_file_sample_a) { new_end = end_file_sample_a; } if (new_start > new_end) { // fprintf(stdout, "start sample is greater than end sample\n"); return FALSE; } // seek to the point in the file we want to read the data // fseek(fp, (int)new_start * CSNR_BYTES_PER_SAMPLE * num_chans_a, SEEK_SET); int samples_read = new_end - new_start; short int* temp_values = new short int[samples_read]; int items_read = fread(temp_values, sizeof(short int), samples_read, fp); if (items_read != samples_read) { return FALSE; } for (int i = 0; i < samples_read; i++) { // convert short int to floating point values // signal_window_a[num_samples++] = (float)temp_values[i]; } // pad it with 0s if window ends after file ends // if (end_sample_a > end_file_sample_a) { for (int i = end_file_sample_a; i < end_sample_a; i++) { signal_window_a[num_samples++] = 0; } } if (debug_mode_a > DEBUG_FULL) { for (int j = 0; j < num_chans_a; j++) { for (int i = j; i < num_samples; i += num_chans_a) { fprintf(stdout, "chan %d :: signal data [%d] = %d\n", j, i/num_chans_a, (short int)signal_window_a[i]); } } fprintf(stdout, "\n"); } // close the file // fclose(fp); if (temp_values != (short int*)NULL) { delete [] temp_values; temp_values = (short int*)NULL; } // exit gracefully // return TRUE; }