/* * Copyright (c) 2007 - 2015 Joseph Gaeddert * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ // // Test Rader's alternate algorithm for FFT of prime numbers by // computing a larger, but simpler FFT potentially of the form 2^m // // References: // [Rader:1968] Charles M. Rader, "Discrete Fourier Transforms When // the Number of Data Samples Is Prime," Proceedings of the IEEE, // vol. 56, number 6, pp. 1107--1108, June 1968 // #include #include #include #include #include #include #define DEBUG 0 #define DFT_FORWARD (-1) #define DFT_REVERSE ( 1) #define MAX_FACTORS (40) // print usage/help message void usage() { printf("fft_rader_prime_radix2_test -- test Rader's alternate prime FFT\n"); printf(" algorithm, compare to slow DFT method\n"); printf("options (default values in []):\n"); printf(" h : print usage/help\n"); printf(" n : fft size (must be prime and greater than 2)\n"); } // super slow DFT, but functionally correct void dft_run(unsigned int _nfft, float complex * _x, float complex * _y, int _dir, int _flags); // determine if number is prime (slow, simple method) int is_prime(unsigned int _n); // find smallest primitive root of _n (assuming _n is prime) unsigned int primitive_root(unsigned int _n); // compute c = base^exp (mod n) unsigned int modpow(unsigned int _base, unsigned int _exp, unsigned int _n); int main(int argc, char*argv[]) { // transform size (must be prime) unsigned int nfft = 17; int dopt; while ((dopt = getopt(argc,argv,"uhn:")) != EOF) { switch (dopt) { case 'h': usage(); return 0; case 'n': nfft = atoi(optarg); break; default: exit(1); } } // validate input if ( nfft <= 2 || !is_prime(nfft)) { fprintf(stderr,"error: %s, input transform size must be prime and greater than two\n", argv[0]); exit(1); } unsigned int i; // create and initialize data arrays float complex * x = (float complex *) malloc(nfft * sizeof(float complex)); float complex * y = (float complex *) malloc(nfft * sizeof(float complex)); float complex * y_test = (float complex *) malloc(nfft * sizeof(float complex)); if (x == NULL || y == NULL || y_test == NULL) { fprintf(stderr,"error: %s, not enough memory for allocation\n", argv[0]); exit(1); } for (i=0; i 0) { nfft_prime >>= 1; m++; } nfft_prime = 1 << m; printf("nfft_prime = %u\n", nfft_prime); #else // compute larger FFT length greater than 2*nfft-4 // NOTE: while any length greater than 2*nfft-4 will work, use // nfft_prime as smallest 'simple' FFT (mostly small factors) // // score(n) = n / sum(factors(n).^2) float gamma_max = 0.0f; // score unsigned int nfft_prime_opt = 0; for (i=1; i<=10 + nfft/4; i++) { unsigned int n_hat = 2*nfft - 4 + i; // compute factors printf("%3u : ", n_hat); unsigned int k; unsigned int num_factors = 0; unsigned int m = n_hat; float gamma = 0.0f; do { for (k=2; k<=m; k++) { if ( (m % k) == 0) { printf("%-4u", k); m /= k; num_factors++; gamma += k*k; break; } } } while (m > 1); for (k=12; k>num_factors; k--) printf(" "); // compute score: //float gamma = (float)n_hat / (float)num_factors; //float gamma = 1e3f * (float)num_factors / (float)n_hat; gamma = (float)n_hat / gamma; printf(" score : %12.8f%s\n", gamma, gamma > gamma_max ? " *" : ""); if (gamma > gamma_max) { gamma_max = gamma; nfft_prime_opt = n_hat; } } printf("best nfft_prime : %u\n", nfft_prime_opt); unsigned int nfft_prime = nfft_prime_opt; #endif // compute DFT of sequence { exp(-j*2*pi*g^i/nfft }, size: nfft_prime // NOTE: R[0] = -1, |R[k]| = sqrt(nfft) for k != 0 // NOTE: R can be pre-computed float complex * r = (float complex*)malloc((nfft_prime)*sizeof(float complex)); float complex * R = (float complex*)malloc((nfft_prime)*sizeof(float complex)); for (i=0; i 1 && num_unique_factors < MAX_FACTORS); #if 0 // print unique factors printf("found %u unique factors of n-1 = %u\n", num_unique_factors, _n-1); for (k=0; k