Skip to content

Instantly share code, notes, and snippets.

@Micrified
Last active November 14, 2019 09:52
Show Gist options
  • Save Micrified/c91fc73e71646e0bf18802256454a32e to your computer and use it in GitHub Desktop.
Save Micrified/c91fc73e71646e0bf18802256454a32e to your computer and use it in GitHub Desktop.
Reads both a WFDB record and annotation file at once, pairing sample amplitude to a annotation.
#include <stdio.h>
// #include <malloc.h>
#include <wfdb/wfdb.h>
#define USE_FMT "%s <record> :: Extracts annotations + " \
"amplitude at annotation for a record\n"
int main (int argc, char *argv[], char **envp) {
int sig_count, exit_code = 0;
WFDB_Anninfo a;
WFDB_Annotation annot, last_annot = {0};
WFDB_Sample *v = NULL;
WFDB_Siginfo *s = NULL;
/******************** Initialization ********************/
// Print environment variables
for (char **env = envp; *env != NULL; ++env) {
printf("%s\n", *env);
}
// Check argument count
if (argc != 2) {
fprintf(stderr, USE_FMT, argv[0]);
exit_code = -1; goto cleanup;
}
// Check for signal file (get record count)
if ((sig_count = isigopen(argv[1], NULL, 0)) < 1) {
fprintf(stderr, "Bad return code for isigopen(%s)\n", argv[1]);
exit_code = -1; goto cleanup;
}
// Allocate record size
s = (WFDB_Siginfo *)malloc(sig_count * sizeof(WFDB_Siginfo));
if (isigopen(argv[1], s, sig_count) != sig_count) {
fprintf(stderr, "Couldn't extract %d records from %s sig file!\n",
sig_count, argv[1]);
exit_code = -1; goto cleanup;
}
// Allocate samples to store vectors
v = (WFDB_Sample *)malloc(sig_count * sizeof(WFDB_Sample));
// Configure the attribute file extension
a.name = "atr"; a.stat = WFDB_READ;
// Attempt to open the attribute file
if (annopen(argv[1], &a, 1) < 0) {
fprintf(stderr, "Couldn't open annotation file for %s\n", argv[1]);
exit_code = -1; goto cleanup;
}
/******************** Parsing ********************/
// Print column headers
printf("Time\tSample(#)\tType\tAmplitude\tElapsed\n");
// Move through records
long sample_offset = 0;
// For each annotation
while (getann(0, &annot) == 0) {
// Extract the timestamp (sample number)
long timestamp = annot.time;
// Move up to the sample at this timestamp
while (sample_offset < timestamp) {
if (getvec(v) < 0) {
fprintf(stderr, "Ran out of samples before timestamp!\n");
exit_code = -1; goto cleanup;
}
sample_offset++;
}
// Print annotation and peak
printf("%s\t%ld\t%s", mstimstr(-(annot.time)), annot.time, annstr(annot.anntyp));
// Extract the channel
int channel = annot.chan;
// Print signal in that channel
printf("\t%8d", v[channel]);
// Print elapsed time since last peak (hopefully the last peak was annotated)
printf("\t%s\n", mstimstr(-(annot.time - last_annot.time)));
last_annot = annot;
}
cleanup:
/* Free only defined if initialized to NULL */
free(v); free(s);
return exit_code;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment