home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <sys/time.h>
-
- #include "fft.h"
- #include "constant.h"
-
- /* */
- /* Precision dependant declarations */
- /* */
- #ifdef DOUBLE
-
- #define TOLERANCE DTOLERANCE
- typedef double this_type;
-
- #define THIS_SUM dsum_
- #define THIS_GEN dgen_
- #define THIS_FT dft1d_
-
- #define THIS_FFTI dfft1di
- #define THIS_FFT dfft1d
- #define THIS_SCAL dscal1d
-
- #endif
- #ifdef SINGLE
-
- typedef float this_type;
-
- #define TOLERANCE STOLERANCE
-
- #define THIS_SUM ssum_
- #define THIS_GEN sgen_
- #define THIS_FT sft1d_
-
- #define THIS_FFTI sfft1di
- #define THIS_FFT sfft1d
- #define THIS_SCAL sscal1d
-
- #endif
-
- /* */
- this_type THIS_SUM();
-
- void inimat_();
- void GetArguments();
- void get_values();
-
- int size, inc, n_trials;
- this_type *pa, *pb, *pRef, *pWork;
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- int i, ia, j, k, n_total, is_wrong, nerr;
- double x, y, dx, dy, emax, sum, err, maxerr;
- this_type t;
-
- /* ******************************************************* */
- GetArguments( argc, argv);
- /* ******************************************************* */
-
- srandom( (123*getpid()) | 0x01);
-
- for( k = 0; k < n_trials ; k++) {
- get_values();
-
- if( !(k%1) && (k)) printf("\n");
- printf( "<%3d,%3d>", size, inc);
- fflush(stdout);
-
- n_total = ((size+1)*inc);
- pa = (this_type *)malloc( (3 * (n_total+2) + size + FACTOR_SPACE) * sizeof(this_type));
- if( !(pa) ) {
- fprintf( stdout, "Could not allocate ... Exiting\n");
- exit (-1);
- }
- pb = pa + n_total + 2;
- pRef = pb + n_total + 2;
- pWork = pRef + n_total + 2;
-
- /* *******************************************************
- Initialize arrays
- ******************************************************* */
- i = n_total+2;
- THIS_GEN(pRef, &n_total);
- pRef[n_total] = HUGE;
- pRef[n_total+1] = -HUGE;
- bcopy( pRef, pa, i*sizeof(this_type));
- bcopy( pRef, pb, i*sizeof(this_type));
-
- /*---PACKED VERSION---*/
- /* *******************************************************
- direct Fourier Transform
- ******************************************************* */
- j = -1;
- THIS_FT( &j, &size, pb, &inc);
- pWork = THIS_FFTI( size, pWork);
- is_wrong = THIS_FFT( -1, size, pa, inc, pWork);
-
- emax = TOLERANCE*sqrt(size);
- for(i = 0, nerr=0 ; i < (size*inc) ; i ++) {
- x = pa[i] - pb[i];
- if( (t= x*x) > (emax)) {
- fprintf( stderr, " !!! - %d ", i);
- nerr++;
- }
- }
- if( !(nerr)){
- fprintf( stdout, ".", size, i);
- }
- x = THIS_SUM( &size, pRef, &inc);
- dx = x - pa[0];
- if( (t= dx *dx ) > TOLERANCE){
- fprintf( stdout,
- "\nError direct FFT(%d) at index #0 : (%f)<->(%f)error(%f)",
- size, pa[0], x, dx);
-
- }
- dx = x - pb[0];
- if( (t= dx *dx) > TOLERANCE){
- fprintf( stdout,
- "\nError direct DFT(%d) at index #0 : (%f)<->(%f)error(%f)",
- size, pa[0], x, dx);
- }
- /* *******************************************************
- inverse Fourier Transform
- ******************************************************* */
- is_wrong = THIS_FFT( 1, size, pa, inc, pWork);
-
- i = 2 * inc;
- j = (size -1) / 2;
- x = pb[0] + 2. * THIS_SUM( &j, pb+inc, &i);
- if ( !(size & 1))
- x += *(pb + (size -1) * inc);
- dx = x - pa[0];
- if( (t= dx *dx ) > TOLERANCE){
- fprintf( stderr, "#");
- } else {
- fprintf( stderr, ".");
- }
-
- t = 1. / (double)size;
- THIS_SCAL( size, t, pa, inc);
-
- emax = TOLERANCE;
- emax = emax * emax;
- sum = 0.;
- err= 0.;
- nerr= 0;
- maxerr= 0.;
-
- for(i = 0 ; i < (size*inc) ; i += inc) {
- sum += (pRef[i] * pRef[i]) + (pRef[i] * pRef[i]);
- x = pa[i] - pRef[i];
- if( (t= x*x) > (emax))
- nerr++;
- err += t;
- if( t > maxerr) maxerr = t;
- }
- err = sqrt(err / (double)size);
- sum = sqrt(sum / (double)size);
- maxerr = sqrt(maxerr);
- printf(": avg:(%8.3g /%8.3g)= %8.3g <> maxerr: (%8.3g /%8.3g)= %8.3g",
- err, sum, err/sum, maxerr, sum, maxerr/sum);
- if(nerr){
- printf("\n%d errors detected \n");
- exit(-2);
- }
- free(pa);
- }
- printf("\n");
- return(0);
- }
-
- int is_random;
-
- void GetArguments( argc, argv)
- int argc;
- char *argv[];
- {
- int i, j, k;
- int nerror = 0;
-
- #define ON 1
-
- n_trials = DEF_TRIALS;
- is_random = 1;
-
- /* ******************************************************* */
- for ( i = 1 ; (i < argc) && (nerror != ON) ; i ++ ) {
- if( argv[i][0] == '-') {
- switch ( argv[i][1]) {
- case 'i' :
- case 'I' :
- is_random = 0;
- break;
- default : nerror = ON;
- }
- }
- else {
- if( i+1 > argc)
- nerror = ON;
- else {
- n_trials = atoi( argv[i]);
- }
- }
- }
- if( nerror == ON) {
- fprintf( stderr,
- "Usage : %s [-p(arallel)] [n_trials]\n", argv[0]);
- exit(-1);
- }
- }
-
- void get_values()
- {
- if( is_random){
- size = random() % MAX_SIZE + 1;
- inc = random() % MAX_STRIDE + 1;
- } else{
- printf( "Enter SIZE : ");
- scanf("%d", &size);
- printf( "Enter STRIDE : ");
- scanf("%d", &inc);
- }
- }
-