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_check2d.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.9 KB  |  229 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_half;
  14. typedef        double    this_type;
  15.  
  16. #define        THIS_SUM    dsum_
  17. #define        THIS_GEN    dgen_
  18. #define        THIS_FT    dft2d_
  19.  
  20. #define        THIS_FFTI    dfft2di
  21. #define        THIS_FFT    dfft2d
  22. #define        THIS_SCAL    dscal2d
  23.  
  24. #endif
  25. #ifdef    SINGLE
  26.  
  27. typedef        float        this_half;
  28. typedef        float        this_type;
  29.  
  30. #define TOLERANCE   STOLERANCE
  31.  
  32. #define        THIS_SUM    ssum_
  33. #define        THIS_GEN    sgen_
  34. #define        THIS_FT    sft2d_
  35.  
  36. #define        THIS_FFTI    sfft2di
  37. #define        THIS_FFT    sfft2d
  38. #define        THIS_SCAL    sscal2d
  39.  
  40. #endif
  41.  
  42. /*                        */
  43.  
  44. this_half THIS_SUM();
  45.  
  46. void inimat_();
  47. void GetArguments();
  48. void get_values();
  49.  
  50. int size_x, ldx, size_y, n_trials;
  51. this_type *pa, *pb, *pRef, *pSave;
  52.  
  53. main(argc,argv)
  54. int argc;
  55. char *argv[];
  56. {
  57.     int i, j, k, n_total, is_wrong, nerr, inc;
  58.     double x, y, dx, dy, emax, sum, err, maxerr;
  59.     this_half    t;
  60.     this_type *ptr;
  61.  
  62. /* ******************************************************* */
  63.     GetArguments( argc, argv);
  64. /* ******************************************************* */
  65.  
  66.     srandom( (123*getpid()) | 0x01);
  67.  
  68.     for( ; n_trials > 0; n_trials --) { 
  69.     get_values();
  70.  
  71.     printf("\n");
  72.     printf( "%3d <%3d,%3d>", n_trials, size_x, size_y);
  73.     fflush(stdout);
  74.  
  75.     n_total = ((size_x+2)*(size_y+1));
  76.     ldx = size_x + 2;
  77.     pa = (this_type *)malloc
  78.         ( (3 * n_total + size_x + 3 * size_y + 4 * FACTOR_SPACE) * sizeof(this_type));
  79.     if( !(pa) ) {
  80.         fprintf( stderr, "Could not allocate ... Exiting\n");
  81.         exit (-1);
  82.     }
  83.     pb = pa + n_total;
  84.     pRef = pb + n_total;
  85.     pSave = pRef + n_total;
  86.  
  87. /* *******************************************************
  88.     Initialize arrays
  89. ******************************************************* */
  90.     THIS_GEN(pRef, &n_total);
  91.     bcopy( pRef, pa, n_total*sizeof(this_type));
  92.     bcopy( pRef, pb, n_total*sizeof(this_type));
  93.  
  94. /* *******************************************************
  95.     PACKED --- direct Fourier Transform
  96. ******************************************************* */
  97.         j = -1;
  98.     THIS_FT( &j, &size_x, &size_y, pb, &ldx);
  99.     pSave = THIS_FFTI( size_x, size_y, pSave);
  100.     bcopy( pRef, pa, n_total*sizeof(this_type));
  101.     is_wrong = THIS_FFT( -1, size_x, size_y, pa, ldx, pSave);
  102.  
  103.     emax = TOLERANCE*(size_x * size_y);
  104.     for(i = 0, nerr=0 ; i < n_total ; i ++) {
  105.         x = pa[i] - pb[i];
  106.         if( (t= x*x) > (emax)) {
  107.         nerr++;
  108.         }
  109.     }
  110.     if( !(nerr)){
  111.         fprintf( stderr, ".");
  112.     }
  113.     else { 
  114.         fprintf( stderr, "@%d@", nerr);
  115.     }
  116.     inc = 1;
  117.     x = y = 0;
  118.     for( j= 0, ptr = pRef ; j < size_y ; j++, ptr += ldx) { 
  119.         x +=  THIS_SUM( &size_x, ptr, &inc);
  120.     }
  121.     dx = x - pa[0];
  122.     if( (t= dx *dx) > TOLERANCE){
  123.         fprintf( stderr, 
  124.         "\nError direct FFT(%d,%d) at index #0 : (%f)<->(%f)error(%f)",
  125.         size_x, size_y, pa[0], x, dx);
  126.     }
  127.     dx = x - pb[0];
  128.     if( (t= dx *dx) > TOLERANCE){
  129.         fprintf( stderr, 
  130.         "\nError direct DFT(%d,%d) at index #0 : (%f)<->(%f)error(%f)",
  131.         size_x, size_y, pb[0],x,dx);
  132.     }
  133. /* *******************************************************
  134.     PACKED --- inverse Fourier Transform
  135. ******************************************************* */
  136.     is_wrong = THIS_FFT( 1, size_x, size_y, pa, ldx, pSave);
  137.  
  138.     x = 0;
  139.     j = (size_y -1) / 2;
  140.     inc = 2 * ldx;
  141.  
  142.     t = 1. / (double)(size_x * size_y);
  143.     THIS_SCAL( size_x, size_y, t, pa, ldx);
  144.  
  145.     emax = TOLERANCE;
  146.     emax = emax * emax;
  147.  
  148.     sum = 0.;
  149.     err= 0.;
  150.     nerr= 0;
  151.     maxerr= 0.;
  152.     for(i = 0 ; i < n_total ; i ++) {
  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_x*size_y));
  161.     sum = sqrt(sum / (double)(size_x*size_y));
  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", nerr);
  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_x = random() % MAX_2D + 1;
  219.     size_y = random() % MAX_2D + 1;
  220.     if( !(n_trials % 5))
  221.     size_y = size_x;
  222.   } else{
  223.     printf( "Enter SIZE_X : ");
  224.     scanf("%d", &size_x);
  225.     printf( "Enter SIZE_Y : ");
  226.     scanf("%d", &size_y);
  227.   }
  228. }
  229.