locked
Parser of the frequency analiser for Nradix>32768 RRS feed

  • Question

  • My dummy code for  Embarcadero freecommandline tools  BC 5.5.1  and Dev C++

    //fsautil.cpp

    #include <stdio.h> //cstdio
    #include <stdlib.h> //cstdlib
    #include <math.h> //cmath 
    #include <conio.h>

    typedef   int indexnumb;  // change to long int  , change %d -> %ld (%lu) ? for N>32767

    FILE *fp1,*fp2;


    char tokenpreamble;
    char tokenpreableend;
    char tokenbeg='#';
    char tokenend=':';
    char tokendelim=';';
    char tokenpreamblebeg='[';
    char tokenpreambleend=']';
    char tokenNumb='N';
    char tokendelim2='=';
    char tokendelim3=' ';
    char tokenF='f';
    void put_string(indexnumb n, double *data1 , int nargs, char *fname)
    {
    //printf("\n   *  Put string to file  %s",fname );

    int narg;
     double data;      
    if ((fp1=fopen(fname,"a"))==NULL) { printf ("Error. ") ; exit(1);}
    //: num ; data1 ; data2 #
    //printf("\n   *  Put start token ");
    fprintf(fp1,"\n%c %d  ",tokenbeg,n );
    //printf("\n   *  Put data ");
    for (narg=0; narg<nargs; narg++)
     {
      data=  data1[narg];
      fprintf(fp1," %c ", tokendelim  ); 
      fprintf(fp1," %le ", data  );
      
      }
    //printf("\n   *  Put end token \n");
    fprintf(fp1,"%c",tokenend);

    fclose (fp1);
    }


    void put_string1(indexnumb n, double  data, char *fname)
    {

     
           
    if ((fp1=fopen(fname,"a"))==NULL) { printf ("Error.") ; exit(1);}
    //: num ; data1   #
    fprintf(fp1,"\n%c %d ",tokenbeg,n);
    fprintf(fp1," %c ", tokendelim  );
    fprintf(fp1," %le ",data  ); 
    fprintf(fp1,"%c",tokenend);

    fclose (fp1);
    }


    double get_string_data(indexnumb n,  int nargs, int argnum, char *fname)
    {
    // printf("\n Get string data from %s n=%d page=%d",fname,n, argnum);
     
    int narg;
    indexnumb num=0;
    double data,result=0;

      if ((fp1=fopen(fname,"r"))==NULL) { printf ("Error.") ; exit(1);}
    label1: 
      char token_beg='\0';
      char token_end='\0';
      char token_delim='\0';


     
     
    while (token_beg!=tokenbeg)
    {
     fscanf(fp1,"%s", &token_beg );
     //printf("\n %s", &token_beg);
     
     }
     
    fscanf(fp1,"%d",&num );
    //printf(" n= %d ",num);


    for (narg=0; narg<nargs; narg++)
     {
     result=0; 
     //printf("\n narg=  %d",narg);
    fscanf(fp1,"%s",&token_delim ); //printf(" %s", &token_delim);
    if(token_delim!=tokendelim) { exit(1); } 
     fscanf(fp1,"%le",&data ); //printf("data=%le",data);

     if( (narg==argnum)&&(num==n) ){
       result=data;
      // printf("\n result=%le",result);
        return result; }
     }
     fscanf(fp1,"%s", &token_end );
     //printf(" %s", &token_end);
    if(token_end!=tokenend) { exit(1); }
    if(num<n) { num++; goto label1;}

     //printf("\n   result=   %le",result);

    return 0;


    }

    /*******************************************************/


    void cdft(indexnumb N, char *fname, char *fnameout )
    {
    double angle;   
    double  Xk[2] ;
    indexnumb k,n  ;
    //printf("\n Parse cdft data");

     for (k=0;k<N;k++)
     {
     Xk[0] =0;
     Xk[1] =0;
     for(n=0;n<N;n++)
       {
       angle=-2*M_PI*k*n/N;
       Xk[0] +=  get_string_data(n,2,0,fname)*cos(angle)- get_string_data(n,2,1,fname)*sin(angle);
       Xk[1] +=  get_string_data(n,2,0,fname)*sin(angle)+ get_string_data(n,2,1,fname)*cos(angle);
       }
     //  printf("\n Poke result ");
      put_string(k, Xk, 2,fnameout);

     }

     return;
    }


    void  DoScaleDFT( indexnumb  num , char *fname, char *fnameout )
    {
    // printf("\n Scale cdft data");
    //"dft.txt"
    //char *fnameout="koeffs.txt";
    indexnumb i;
    double Xk[2];
    for (i=0; i<num ; i++)
      {
        Xk[0]=get_string_data(i,2,0,fname)/num;
        Xk[1]=get_string_data(i,2,1,fname)/num;
        put_string(i, Xk, 2,fnameout);
      }

    return ;
    }


    void  cidft( indexnumb N , char *fname, char *fnameout1 )
    {
    //printf("\n Get CIDFT");
    indexnumb k,n ;
    double angle;
    double idft_xn[2];
     
     for (n=0;n<N;n++)
     {
     idft_xn[0]=0;
     idft_xn[1]=0;
     // printf("\n Parse string %d",n);
     for(k=0;k<N;k++)
      {
        angle= 2*M_PI*k*n/N;
      idft_xn[0]+=get_string_data(k,2,0,fname)*cos(angle)-get_string_data(k,2,1,fname)*sin(angle);
      idft_xn[1]+=get_string_data(k,2,0,fname)*sin(angle)+get_string_data(k,2,1,fname)*cos(angle);
      }
      idft_xn[0]=idft_xn[0]/N;
      idft_xn[1]=idft_xn[1]/N;
     
     // printf("\n Put string %d %le  ; %le ",n,idft_xn[0],idft_xn[1] );
      put_string(n, idft_xn, 2,fnameout1);
     }

    return ;
    }


    void hilbert( char *fname_xr,char *fnameXNout , indexnumb n)
    {

    printf("\n Get Hilbert transform");
    char *fname_dft__xn="Hdft_xn.tmp";
    char *fname_dft_Xk="Hdft_Xk.tmp";
    char *fname_ht="Hhtflt.tmp";
    char *fname_idft__XK="HidftXK.tmp";

     indexnumb  k,i,no2;
     double dft__xn[2],dft__Xk[2],idft__XK[2],idft__XN[2];

     
     if (n==0)
     {
     printf("\n Error. n=0    ");
     idft__XN[0]=0;
     idft__XN[1]=0;
     put_string(0, idft__XN, 2,fnameXNout);
     return;
     }

     //xnreal=real(xr);
      no2=floor (n/2);

    //printf("\n    Poke dft data array");

     for (i=0;i<n;i++)
     {
      dft__xn[0]=get_string_data(i,1,0,fname_xr );;
      dft__xn[1]=0;
      
       put_string(i, dft__xn, 2,fname_dft__xn);
     }
    //getch();

    //printf("\n    Parse dft");
    //fft
    cdft(n,fname_dft__xn,fname_dft_Xk );
    // creating h[t] coefs of of filter
     if((n>0)&&((2*floor(n/2))==(n))) // n is even
     {
      put_string1(0,1,fname_ht ); /* h[0]=1; */ 
      for(k=1;k<n/2;k++) {   put_string1(k,2,fname_ht ); /* h[k]=2; */    }
      put_string1(n/2,1,fname_ht ); /* h[n/2]=1; */
      for (i=0.5*n+1 ;i<=n;i++) {    put_string1(i,0,fname_ht ); /* h[i]=0; */   }
     }
     else //n is odd
     {
       if(n>0)
       {
        put_string1(0,1,fname_ht ); /* h[0]=1; */
        for(k=1;k<(n+1)/2;k++)    {   put_string1(k,2,fname_ht ); /* h[k]=2; */    }
        for (i=(n+1)/2;i<=n;i++) {    put_string1(i,0,fname_ht ); /* h[i]=0; */    }
       }

     }
    //getch();
    // convolution with h[t] of filter
     for(i=0;i<n;i++)
     {
     idft__XK[0]=get_string_data(i,2,0,fname_dft_Xk)*get_string_data(i,1,0,fname_ht );
     idft__XK[1]=get_string_data(i,2,1,fname_dft_Xk)*get_string_data(i,1,0,fname_ht );
     put_string(i, idft__XK, 2,fname_idft__XK);
     }
    //getch();
    cidft(n , fname_idft__XK,fnameXNout );

    //fname_idft__XN is output file
    return;
    }


    /************************************************/

    void getanalyticsignal(char  *fname_xn,char *fname_out, indexnumb num)
    {
     
    char *fname_hilb="ASHilbt.tmp";
    double x_n[2],Hilb[2];
    indexnumb  i;
    //printf("\n Get analitic signal");

      //x=x+jy,y=0
      //hilb(x)=a+jb
      //z=x+jHilb(x)=x+j(a+jb)=x-b+j*a
      //I=x(t), Q=j*Hilb(xre(t))
    hilbert(fname_xn, fname_hilb,num );
    for (i=0; i<num; i++)
    {
     x_n[0]=get_string_data(i,1,0,fname_xn );
     x_n[1]=get_string_data(i,2,1,fname_hilb );
     
     put_string(i, x_n, 2,fname_out);
    }
    return  ;
    }

    double Arg(double xre, double xim )
    {
    if (xre>0) { return  atan(xim/xre); }     // 1,4 quadrant  0...+/-89.9999 deg (271-359.9999)
    if ((xre==0)&&(xim>0)) { return M_PI/2; } //   90 deg
    if ((xre==0)&&(xim<0)) { return -M_PI/2;} // -90 deg,    (+270 deg)
    if ((xre<0)&&(xim>=0)) { return atan(xim/xre)+M_PI ; }  //2 quadrant 90.0001...179.9999 deg
    if ((xre<0)&&(xim<0))  { return atan(xim/xre)-M_PI ; }  //3 quadrant 180.00001...269.9999 deg
    if ((xre==0)&&(xim=0)) {                  //|X|=0
                             printf("\n Arg parsing error ,(0+j*0) \n");
                             return 0  ;
                           }
    }

    void get_r_fi(char *fname_in ,char *fnameout, indexnumb n, int mode   )
    {


     indexnumb i;
     double res[2];
     
     for (i=0;i<n;i++)
     {
     res[0]=pow( (pow( get_string_data(i,2,0,fname_in),2)+pow(get_string_data(i,2,1,fname_in),2)),0.5);
     res[1]=Arg(get_string_data(i,2,0,fname_in),get_string_data(i,2,1,fname_in));
     if (mode==1)  { res[1]=res[1]*180/M_PI;  } 
     put_string(i, res, 2,fnameout );          
     }

     return ; 
    }

    void  GetSpectrum(char  *fnamexn,char *fnameout, indexnumb  num )
    {

    char *fname_ansig="ASsampl.tmp";
    char *fname_dft="ASdftf.tmp";
    char *fname_dfts="ASdftsc.tmp";
     
     
    getanalyticsignal(fnamexn,fname_ansig,num);
    cdft(num,fname_ansig, fname_dft ) ;
    DoScaleDFT(num , fname_dft, fname_dfts );
    get_r_fi(fname_dfts , fnameout, num, 1  );
    return;
    }

    /*******************************************/
    //void main

    int main()
    {
    char *file="input.txt"; 
    char *fileoutput="spectrum.txt";
    indexnumb num,num1;
    double f;
    char token_preamblebeg,token_delim3,token_Numb,token_delim2, token_delim,token_F,token_preambleend;
    if ((fp1=fopen(file,"r"))==NULL) { printf ("Error.") ; exit(1);}

     fscanf(fp1,"%c",&token_preamblebeg);  //[
     //printf("%c",token_preamblebeg);
     fscanf(fp1,"%c",&token_delim3); //space
     //printf("%c",token_delim3);
    fscanf(fp1,"%c",&token_Numb);  //N
    //printf("%c",token_Numb);
    fscanf(fp1,"%c",&token_delim2); //=
    //printf("%c",token_delim2);
    fscanf(fp1,"%d",&num1);  //value  , %d    ---> ?
    fscanf(fp1,"%c",&token_delim3); //space
     //printf("%c",token_delim3);
    fscanf(fp1,"%c",&token_delim);  //;
    fscanf(fp1,"%c",&token_delim3); //space
     //printf("%c",token_delim3);
    fscanf(fp1,"%c",&token_F);  //f.
    fscanf(fp1,"%c",&token_delim2);//=
    fscanf(fp1,"%le",&f); // value of sampling freq
    fscanf(fp1,"%c",&token_delim3); //space
     //printf("%c",token_delim3);
    fscanf(fp1,"%c",&token_delim); //;
    fscanf(fp1,"%c",&token_preambleend); //]

    fclose(fp1 );
    num=num1;
     printf ("\n%d  %le", num, f);
     GetSpectrum(file,fileoutput, num );
     
     
     
     return 0;  //return
     
    }

    //input.txt

    [ N=8 ; f=44100 ; ]
    # 0 ; 1 :
    # 1 ; 1 :
    # 2 ; 1 :
    # 3 ; 1 :
    # 4 ; 0 :
    # 5 ; 0 :
    # 6 ; 0 :
    # 7 ; 0 :

    (for example)

    How to  use this program without errors after 5  line in ASHilbt.tmp   ?

    How to use Visual Studio for compiling this file without errors ?

    //Hdft_xn.tmp

    # 0   ;  1.000000e+000  ;  0.000000e+000 :
    # 1   ;  1.000000e+000  ;  0.000000e+000 :
    # 2   ;  1.000000e+000  ;  0.000000e+000 :
    # 3   ;  1.000000e+000  ;  0.000000e+000 :
    # 4   ;  0.000000e+000  ;  0.000000e+000 :
    # 5   ;  0.000000e+000  ;  0.000000e+000 :
    # 6   ;  0.000000e+000  ;  0.000000e+000 :
    # 7   ;  0.000000e+000  ;  0.000000e+000 :

    //Hdft_Xk.tmp


    # 0   ;  4.000000e+000  ;  0.000000e+000 :
    # 1   ;  1.000000e+000  ;  -2.414214e+000 :
    # 2   ;  -1.836910e-016  ;  -2.220446e-016 :
    # 3   ;  1.000000e+000  ;  -4.142136e-001 :
    # 4   ;  0.000000e+000  ;  -2.449213e-016 :
    # 5   ;  1.000000e+000  ;  4.142136e-001 :
    # 6   ;  3.290283e-016  ;  -3.330669e-016 :
    # 7   ;  1.000000e+000  ;  2.414214e+000 :

    //Hhtflt.tmp

    # 0  ;  1.000000e+000 :
    # 1  ;  2.000000e+000 :
    # 2  ;  2.000000e+000 :
    # 3  ;  2.000000e+000 :
    # 4  ;  1.000000e+000 :
    # 5  ;  0.000000e+000 :
    # 6  ;  0.000000e+000 :
    # 7  ;  0.000000e+000 :
    # 8  ;  0.000000e+000 :

    //HidftXK.tmp

    # 0   ;  4.000000e+000  ;  0.000000e+000 :
    # 1   ;  2.000000e+000  ;  -4.828428e+000 :
    # 2   ;  -3.673820e-016  ;  -4.440892e-016 :
    # 3   ;  2.000000e+000  ;  -8.284272e-001 :
    # 4   ;  0.000000e+000  ;  -2.449213e-016 :
    # 5   ;  0.000000e+000  ;  0.000000e+000 :
    # 6   ;  0.000000e+000  ;  -0.000000e+000 :
    # 7   ;  0.000000e+000  ;  0.000000e+000 :

    //ASHilbt.tmp

    # 0   ;  1.000000e+000  ;  -7.071069e-001 :
    # 1   ;  1.000000e+000  ;  -7.071068e-008 :
    # 2   ;  1.000000e+000  ;  -1.158741e-017 :
    # 3   ;  1.000000e+000  ;  7.071069e-001 :
    # 4   ;  3.804194e-017  ;  7.071069e-001 :
    # 5   ;  -8.401380e-008  ;  7.071068e-008 :-> parsing error

    //spectrum.txt

    # 0   ;  1.000000e+000  ;  1.452934e-015 :
    # 1   ;  5.554339e-017  ;  -9.195235e+001 :
    # 2   ;  1.143444e-016  ;  -9.000000e+001 :
    # 3   ;  1.536651e-016  ;  -6.457117e+001 : error

    //clrtmp.bat

    del Hdft_xn.tmp
    del Hdft_Xk.tmp
    del Hhtflt.tmp
    del HidftXK.tmp
    del ASHilbt.tmp
    del ASsampl.tmp
    del ASdftf.tmp
    del ASdftsc.tmp

     pause 0

    How to work with       indexnumb num,num1  as long double or N>1E6   samples in file ?

    Friday, May 19, 2017 12:31 AM

All replies

  • my old file for  build it for Windows 98-XP   for BCC32

    make.bat

    rem Файл make_console.bat
    rem компиляция и компоновка консольных приложений
    rem в Borland Builder C++ 5.5
    rem объявление переменных
    path C:\Borland\BCC55\bin;%path%
    set include=C:\Borland\BCC55\include
    set lib=C:\Borland\BCC55\lib
    rem имя исходного файла (подразумевается расширение .cpp)
    set app=fsautil
    rem удаляем прежние результаты компиляции
    if exist %app%.exe del %app%.exe
    if exist %app%.obj del %app%.obj
    rem запуск компилятора
    bcc32.exe -I%include% -L%lib% %app%.cpp > errout.txt
    if exist %app%.obj del %app%.obj
    if exist %app%.tds del %app%.tds
    pause  0
    %app%.exe
    pause 0

    It returns error after  2-nd line.  DevC++ in Windows 7 x64 Home  returns error after 5...7 line in ASHilbt.tmp . (DFT parsing  and IO error). How to fix it if I use Visual Studio , products of Microsoft  , how to change syntax to use program for parsing N> 1024...256000 samples?

    Friday, May 19, 2017 12:37 AM