audiosignalstime-frequencyc

fscanf (filepointer, "%d\n%lg\n", sig_length, sample_rate); how to interpret it?


I have an audio format conversion from an scientific audio filetype that existed prior to the internet (called TFD v1) an uncompressed uncomplicated simple raw array of sample values... In the get_signal() function on this page, i don't know how the sample values are formatted and why i need the sample rate to read 256 points from the array?

  fscanf (filepointer         , "%d\n%lg\n" , sig_length,  fsam         );
  fscanf (raw_audio_array_file,     ??      ,     256   ,  SRate_Float  );


    /*******************************************************
    * function get_signal reads signal data into sig_re from
    * the file pointed to by filepointer. If the file is a
    * type 2 TFD file then the imaginary part is set too.
    * If the signal is type 1, its hilbert transform is
    * returned in the imaginary part (sig_im).
    ********************************************************/


    void    get_signal (filepointer, sig_re, sig_im, fsam, sig_length)
        FILE   *filepointer;
        double  sig_re[],
                sig_im[],
               *fsam;
        int    *sig_length;

    {
        register int i; /* counter variable */
        int     sigtype; /* data file type */
        double  dummy1,
                dummy2; /* dummy temporary variables */


        fscanf (filepointer, "%d\n", &sigtype);
        if (sigtype == 1) { /* Type one TFD file */
     fscanf (filepointer, "%d\n%lg\n", sig_length, fsam);
     for (i = 0; i < *sig_length; i++) {
         fscanf (filepointer, "%lg\n", &sig_re[i]);
     }
     analytic (sig_re, sig_im, *sig_length);
        }
        else {
     if (sigtype == 2) { /* Type 2 TFD file */
         fprintf(stderr,"Complex signal.\n");    
         fscanf (filepointer, "%d\n%lg\n", sig_length, fsam);
         for (i = 0; i < *sig_length; i++) {
     fscanf (filepointer, "(%lg,%lg)\n", &sig_re[i], &sig_im[i]);
     printf("%lg\n",sig_re[i]);     
         }
     }
     else {
         fprintf (stderr, "ccg : incorrect input format.\n");
         exit (7);
     }
     fclose (filepointer);
        }
    } /* END OF FUNCTION get_signal */

Solution

  • fscanf (filepointer, "%d\n%lg\n", sig_length, fsam); calls the C function fscanf, which reads formatted data from a file according to a format string.

    The format string in this case is "%d\n%lg\n" which breaks down as %d (an integer), a newline, %lg (a double), and another newline. So, basically, it's reading two lines off the file, the first of which should contain an integer with the sample length, and the second which contains the sample rate.

    As an example, with a modern sample rate:

    1
    256
    44100.0
    1.0
    0.98
    0.96
    ...
    

    where the first line is the sigtype, the second and third lines are the signal length (in samples) and the sample rate, and the remaining lines are the signal values.