Created
January 22, 2016 14:46
-
-
Save glS512/a6a67c8f9198b13e3c27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <string.h> | |
// #include <time.h> | |
#define TIMESTAMP_BYTE_COUNT 4 | |
#define INITIALIZATION_OFFSET 40 | |
typedef struct | |
{ | |
uint32_t timestamp; | |
uint8_t channel; | |
} Timetag; | |
// off_t fsize(const char*); | |
// int write_results_to_file(const char*, uint8_t(*)[4], uint16_t); | |
// void get_timetag(char*, Timetag*); | |
// fsize returns the size of file whose name is given in input | |
size_t fsize(FILE* file) | |
{ | |
if( !file ) { return 0; } | |
const size_t curr_position = ftell(file); | |
fseek(file, 0, SEEK_END); | |
size_t file_size = ftell(file); | |
fseek(file, curr_position, SEEK_SET); | |
return file_size; | |
} | |
// write the quadruples found to file | |
int write_results_to_file( const char* file_name,\ | |
uint8_t (*results)[TIMESTAMP_BYTE_COUNT], \ | |
uint16_t number_of_elements | |
) | |
{ | |
FILE *file; | |
file = fopen(file_name, "a"); | |
for(int i = 0; i < number_of_elements; i++) | |
{ | |
// printf("fourfold coincidence: "); | |
// for(int j = 0; j < 3; j++) | |
// printf("%d,", results[i][j]); | |
// printf("%d\n", results[i][3]); | |
fprintf(file, "%d,%d,%d,%d\n",\ | |
results[i][0],\ | |
results[i][1],\ | |
results[i][2],\ | |
results[i][3] | |
); | |
} | |
fclose(file); | |
return 0; | |
} | |
void get_timetag(char* line, Timetag* timetag) | |
{ | |
// Timetag *timetag = malloc(sizeof(Timetag)); | |
char tmp_str[128]; | |
char *end; | |
// for(int i = 0; i < line -> len; i++) | |
int i = 0; | |
while( line[i] ) | |
{ | |
if( line[i] == ',' ) | |
{ | |
memcpy(tmp_str, line, i); | |
tmp_str[i] = 0; | |
timetag -> timestamp = strtoul(tmp_str, &end, 10); | |
timetag -> channel = strtoul((char *) &line[i+1], &end, 10); | |
// printf("string: %s\n", tmp_str); | |
// printf("channel: %s\n", (char *) &line[i+1]); | |
} | |
i++; | |
} | |
return; | |
} | |
int main() { | |
// clock_t begin_time; | |
// begin_time = clock(); | |
const char FILE_NAME[] = "timestamps.txt"; | |
const char OUTPUT_FILE_NAME[] = "processed_timestamps.txt"; | |
const uint8_t WINDOW_SIZE = 100; | |
// we use arrays with 4 elements, being interested only in fourfold coincidences. | |
uint32_t timestamps[TIMESTAMP_BYTE_COUNT]; | |
uint8_t channels[TIMESTAMP_BYTE_COUNT]; | |
// assuming that there are no more than 1000 coincidences | |
uint8_t quadruples[1000][4]; | |
uint16_t quadruples_pos = 0; | |
// curr_byte and curr_timestamp will be used in the for loop | |
uint32_t curr_timestamp; | |
// open file in read mode | |
FILE *file; | |
file = fopen(FILE_NAME, "r"); | |
if( file == NULL ) { | |
perror(FILE_NAME); | |
return(-1); | |
} | |
char line[128]; | |
Timetag* timetag_p = malloc(sizeof(Timetag)); | |
fgets(line, sizeof(line), file); | |
get_timetag(line, timetag_p); | |
timestamps[0] = timetag_p -> timestamp; | |
channels[0] = timetag_p -> channel; | |
uint8_t timestamps_pos = 1; | |
unsigned int counter = 0; | |
while(fgets(line, sizeof(line), file) != NULL) | |
{ | |
counter++; | |
get_timetag(line, timetag_p); | |
// printf("timestamp:%8u, channel:%d\n",\ | |
// timetag_p -> timestamp, | |
// (timetag_p -> channel) | |
// ); | |
if( timetag_p->timestamp - timestamps[0] <= WINDOW_SIZE \ | |
&& timestamps_pos < 4 ) | |
{ | |
timestamps[timestamps_pos] = timetag_p->timestamp; | |
channels[timestamps_pos] = timetag_p->channel; | |
timestamps_pos++; | |
} | |
else if( timetag_p->timestamp - timestamps[0] <= WINDOW_SIZE ) | |
{ | |
// then a new window is opened | |
timestamps_pos = 1; | |
timestamps[0] = timetag_p -> timestamp; | |
channels[0] = timetag_p -> channel; | |
} | |
else // we reinitialize the arrays and start a new window | |
{ | |
if(timestamps_pos == 4) | |
{ | |
memcpy(&quadruples[quadruples_pos], channels, sizeof(channels)); | |
quadruples_pos++; | |
} | |
// then a new window is opened | |
timestamps_pos = 1; | |
timestamps[0] = timetag_p -> timestamp; | |
channels[0] = timetag_p -> channel; | |
} | |
} | |
fclose(file); | |
free(timetag_p); | |
// printf("Time required: %f\n", (double) (clock()-begin_time) / CLOCKS_PER_SEC); | |
write_results_to_file(OUTPUT_FILE_NAME, quadruples, quadruples_pos); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment