home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / lib / mathlib / libfft / TRY / r_check1d.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.7 KB  |  227 lines

  1. #include <stdio.h>
  2. #include <sys/time.h>
  3.  
  4. #include "fft.h"
  5. #include "constant.h"
  6.  
  7. /*                        */
  8. /*    Precision dependant declarations    */
  9. /*                        */
  10. #ifdef    DOUBLE
  11.  
  12. #define TOLERANCE   DTOLERANCE
  13. typedef        double    this_type;
  14.  
  15. #define        THIS_SUM    dsum_
  16. #define        THIS_GEN    dgen_
  17. #define        THIS_FT    dft1d_
  18.  
  19. #define        THIS_FFTI    dfft1di
  20. #define        THIS_FFT    dfft1d
  21. #define        THIS_SCAL    dscal1d
  22.  
  23. #endif
  24. #ifdef    SINGLE
  25.  
  26. typedef        float        this_type;
  27.  
  28. #define TOLERANCE   STOLERANCE
  29.  
  30. #define        THIS_SUM    ssum_
  31. #define        THIS_GEN    sgen_
  32. #define        THIS_FT    sft1d_
  33.  
  34. #define        THIS_FFTI    sfft1di
  35. #define        THIS_FFT    sfft1d
  36. #define        THIS_SCAL    sscal1d
  37.  
  38. #endif
  39.  
  40. /*                        */
  41. this_type THIS_SUM();
  42.  
  43. void inimat_();
  44. void GetArguments();
  45. void get_values();
  46.  
  47. int size, inc, n_trials;
  48. this_type *pa, *pb, *pRef, *pWork;
  49.  
  50. main(argc,argv)
  51. int argc;
  52. char *argv[];
  53. {
  54.     int i, ia, j, k, n_total, is_wrong, nerr;
  55.     double x, y, dx, dy, emax, sum, err, maxerr;
  56.     this_type    t;
  57.  
  58. /* ******************************************************* */
  59.     GetArguments( argc, argv);
  60. /* ******************************************************* */
  61.  
  62.     srandom( (123*getpid()) | 0x01);
  63.  
  64.     for( k = 0; k < n_trials ; k++) { 
  65.     get_values();
  66.  
  67.     if( !(k%1) && (k)) printf("\n");
  68.     printf( "<%3d,%3d>", size, inc);
  69.     fflush(stdout);
  70.  
  71.     n_total = ((size+1)*inc);
  72.     pa = (this_type *)malloc( (3 * (n_total+2) + size + FACTOR_SPACE) * sizeof(this_type));
  73.     if( !(pa) ) {
  74.         fprintf( stdout, "Could not allocate ... Exiting\n");
  75.         exit (-1);
  76.     }
  77.     pb = pa + n_total + 2;
  78.     pRef = pb + n_total + 2;
  79.     pWork = pRef + n_total + 2;
  80.  
  81. /* *******************************************************
  82.     Initialize arrays
  83. ******************************************************* */
  84.     i = n_total+2;
  85.     THIS_GEN(pRef, &n_total);
  86.     pRef[n_total] = HUGE;
  87.     pRef[n_total+1] = -HUGE;
  88.     bcopy( pRef, pa, i*sizeof(this_type));
  89.     bcopy( pRef, pb, i*sizeof(this_type));
  90.  
  91. /*---PACKED VERSION---*/
  92. /* *******************************************************
  93.     direct Fourier Transform
  94. ******************************************************* */
  95.         j = -1;
  96.     THIS_FT( &j, &size, pb, &inc);
  97.     pWork = THIS_FFTI( size, pWork);
  98.     is_wrong = THIS_FFT( -1, size, pa, inc, pWork);
  99.  
  100.     emax = TOLERANCE*sqrt(size);
  101.     for(i = 0, nerr=0 ; i < (size*inc) ; i ++) {
  102.         x = pa[i] - pb[i];
  103.         if( (t= x*x) > (emax)) {
  104.         fprintf( stderr, " !!! - %d ", i);
  105.         nerr++;
  106.         }
  107.     }
  108.     if( !(nerr)){
  109.         fprintf( stdout, ".", size, i);
  110.     }
  111.     x =  THIS_SUM( &size, pRef, &inc);
  112.     dx = x - pa[0];
  113.     if( (t= dx *dx ) > TOLERANCE){
  114.         fprintf( stdout, 
  115.         "\nError direct FFT(%d) at index #0 : (%f)<->(%f)error(%f)",
  116.         size, pa[0], x, dx);
  117.  
  118.     }
  119.     dx = x - pb[0];
  120.     if( (t= dx *dx) > TOLERANCE){
  121.         fprintf( stdout, 
  122.         "\nError direct DFT(%d) at index #0 : (%f)<->(%f)error(%f)",
  123.         size, pa[0], x, dx);
  124.     }
  125. /* *******************************************************
  126.     inverse Fourier Transform
  127. ******************************************************* */
  128.     is_wrong = THIS_FFT( 1, size, pa, inc, pWork);
  129.  
  130.     i = 2 * inc;
  131.     j = (size -1) / 2;
  132.     x =  pb[0] + 2. * THIS_SUM( &j, pb+inc, &i);
  133.     if ( !(size & 1))
  134.         x += *(pb + (size -1) * inc);
  135.     dx = x - pa[0];
  136.     if( (t= dx *dx ) > TOLERANCE){
  137.         fprintf( stderr, "#");
  138.     } else {
  139.         fprintf( stderr, ".");
  140.     }
  141.  
  142.     t = 1. / (double)size;
  143.     THIS_SCAL( size, t, pa, inc);
  144.  
  145.     emax = TOLERANCE;
  146.     emax = emax * emax;
  147.     sum = 0.;
  148.     err= 0.;
  149.     nerr= 0;
  150.     maxerr= 0.;
  151.  
  152.     for(i = 0 ; i < (size*inc) ; i += inc) {
  153.         sum += (pRef[i] * pRef[i]) + (pRef[i] * pRef[i]);
  154.         x = pa[i] - pRef[i];
  155.         if( (t= x*x) > (emax))
  156.         nerr++;
  157.         err += t;
  158.         if( t > maxerr) maxerr = t;
  159.     }
  160.     err = sqrt(err / (double)size);
  161.     sum = sqrt(sum / (double)size);
  162.     maxerr = sqrt(maxerr);
  163.     printf(": avg:(%8.3g /%8.3g)= %8.3g <> maxerr: (%8.3g /%8.3g)= %8.3g", 
  164.     err, sum, err/sum, maxerr, sum, maxerr/sum);
  165.     if(nerr){
  166.         printf("\n%d errors detected \n");
  167.         exit(-2);
  168.     }
  169.     free(pa);
  170.     }
  171.     printf("\n");
  172.     return(0);
  173. }
  174.  
  175. int is_random;
  176.  
  177. void GetArguments( argc, argv)
  178. int argc;
  179. char *argv[];
  180. {
  181.     int i, j, k;
  182.     int nerror = 0;
  183.  
  184. #define ON    1
  185.  
  186.     n_trials = DEF_TRIALS;
  187.     is_random = 1;
  188.  
  189. /* ******************************************************* */
  190.     for ( i = 1 ; (i < argc) && (nerror != ON) ; i ++ ) {
  191.     if( argv[i][0] == '-') {
  192.         switch ( argv[i][1]) {
  193.         case 'i' :
  194.         case 'I' :  
  195.                 is_random = 0;
  196.                 break;
  197.         default  :  nerror = ON;
  198.         }
  199.     }
  200.     else {
  201.         if( i+1 > argc)
  202.         nerror = ON;
  203.         else { 
  204.         n_trials = atoi( argv[i]);
  205.         }
  206.     }
  207.     }
  208.     if( nerror == ON) {
  209.     fprintf( stderr, 
  210.         "Usage : %s [-p(arallel)] [n_trials]\n", argv[0]);
  211.     exit(-1);
  212.     }
  213. }
  214.  
  215. void get_values()
  216. {
  217.   if( is_random){ 
  218.     size = random() % MAX_SIZE + 1;
  219.     inc = random() % MAX_STRIDE + 1;
  220.   } else{
  221.     printf( "Enter SIZE : ");
  222.     scanf("%d", &size);
  223.     printf( "Enter STRIDE : ");
  224.     scanf("%d", &inc);
  225.   }
  226. }
  227.