Puka - Software for Detection of Breaths in Strain Gauge Recordings 1.0.0

File: <base>/convertEKG/convertECG.c (5,066 bytes)
#include <stdio.h>
#include <stdlib.h>
#include <wfdb/wfdb.h>

int main()
{
	WFDB_Signal sigIn = 0;
	FILE *stream;            // need a pointer to FILE for the stream 
    float dblIn;    // need an int to hold a single character
	char answer[32], record[8], directory[100], inputFile[100];
    int i; int nsig = 1;  //I only want one signal
    long nsamp, t;
    double freq = 0;  
    char **filename, **description, **units;
    WFDB_Sample *v;
    WFDB_Siginfo *s;
	
	(void)printf("the program is running\n");  //prints on the command line
    
	// ********************************************************** start example 8
    do {
        printf("Choose a record name [up to 6 characters]: ");
        fgets(record, 8, stdin); record[strlen(record)-1] = '\0';
    } while (newheader(record) < 0);
    s = (WFDB_Siginfo *)malloc(nsig * sizeof(WFDB_Siginfo));
    v = (WFDB_Sample *)malloc(nsig * sizeof(WFDB_Sample));
    filename = (char **)malloc(nsig * sizeof(char *));
    description = (char **)malloc(nsig * sizeof(char *));
    units = (char **)malloc(nsig * sizeof(char *));
    if (s == NULL || v == NULL || filename == NULL ||
	description == NULL || units == NULL) {
	fprintf(stderr, "insufficient memory\n");
	exit(1);
    }
    for (i = 0; i < nsig; i++) {
	if ((filename[i] = (char *)malloc(32)) == NULL ||
	    (description[i] = (char *)malloc(32)) == NULL ||
	    (units[i] = (char *)malloc(32)) == NULL) {
	    fprintf(stderr, "insufficient memory\n");
	    exit(1);
	}
    }
	
	do {   //set sampling frequency
      printf("Sampling frequency [Hz per signal, > 0]: ");
      fgets(answer, 32, stdin); sscanf(answer, "%lf", &freq);
    } while (setsampfreq(freq) < 0);

    do {
        printf("Length of record (H:M:S): ");
        fgets(answer, 32, stdin);
    } while ((nsamp = strtim(answer)) < 1L);
    printf("Directory for signal files [up to 30 characters]: ");
    fgets(directory, 98, stdin);
    directory[strlen(directory)-1] = '\0';

    printf("Directory for input files [up to 100 characters]: ");
    fgets(inputFile, 98, stdin);
    inputFile[strlen(inputFile)-1] = '\0';

	s[0].fmt = 16;  //format 16 is fairly standard; just use that one
    sprintf(filename[0], "%s/d.%s", directory, record);  //this puts the directory & filname strings together
    s[0].fname = filename[0];  //then assign the names into s - the WFDB_Siginfo variable
    s[0].group = 0;   //then assign this into  s - the WFDB_Siginfo variable

    for (i = 0; i < nsig; i++) {
        s[i].fmt = s[0].fmt; s[i].bsize = 0;
        printf("Signal %d description [up to 30 characters]: ", i);
        fgets(description[i], 32, stdin);
        description[i][strlen(description[i])-1] = '\0';
        s[i].desc = description[i];
        printf("Signal %d units [up to 20 characters]: ", i);
        fgets(units[i], 22, stdin);
        units[i][strlen(units[i])-1] = '\0';
        s[i].units = (*units[i]) ? units[i] : "mV";
        do {
            printf(" Signal %d gain [adu/%s]: ", i, s[i].units);
            fgets(answer, 32, stdin);
            sscanf(answer, "%lf", &s[i].gain);
        } while (s[i].gain < 0.);
        do {
            printf(" Signal %d ADC resolution in bits [8-16]: ", i);
            fgets(answer, 32, stdin);
            sscanf(answer, "%d", &s[i].adcres);
        } while (s[i].adcres < 8 || s[i].adcres > 16);
        printf(" Signal %d ADC zero level [adu]: ", i);
        fgets(answer, 32, stdin);
        sscanf(answer, "%d", &s[i].adczero);
    }
    if (osigfopen(s, nsig) < nsig) exit(1);

	(void)printf("set up s all the way!\n");  //prints on the command line

// ************************************************** end example8

	//stream = fopen("c:\\kailua\\puka\\ecg.txt", "r"); //opens ecg.txt for reading in same dir as convertECG.exe
	stream = fopen(inputFile, "r");

	if (stream == NULL) { printf("fopen returned NULL"); exit(2); }

	if (stream == (FILE *)0) { (void)printf("the file errored\n"); exit (1); } //checks if file opened right
	else { (void)printf("the file was opened\n"); }

while ((dblIn=getc(stream))!=EOF) {
	//dblIn = getc(stream);
	fscanf(stream, "%f", &dblIn);  //read in dblIn
	//(void)printf("dblIn before: %f \n", dblIn); //dblIn is substituted into the string at place %f
	*v = physadu(sigIn, dblIn); //call fn: WFDB_Sample physadu(WFDB_Signal s, double v)
	//(void)printf("dblIn after: %f \n", dblIn); //dblIn is substituted into the string at place %f
	//(void)printf("*v: %f \n", &v); 
	if (putvec(v) < 0) exit(1);
}

// close the file after you are done with it, if file doesn't close then report and exit */
    if ((fclose(stream)) == EOF) {
        fprintf(stderr,"Error closing stream.  (printed to standard error)\n");
        exit(1);
        }  	

    (void)newheader(record); //int newheader(char *record) Return: 0=Success -1=Failure: unable to create header file 

	wfdbquit();  //call fn to exit out of the wfdb library
	return EXIT_SUCCESS;
}