int main (void) { // printf("EPS: %e\n", EPS); size_t size = pow(2, SIZE); size_t i; double data[2*size]; for (i = 0; i < size; i++) { REAL(data,i) = 0.0; IMAG(data,i) = 0.0; } REAL(data,0) = 1.0; for (i = 1; i <= 10; i++) { REAL(data,i) = REAL(data,size - i) = 1.0; } /* for (i = 0; i < size; i++) { printf ("%d %e %e\n", i, REAL(data,i), IMAG(data,i)); } printf ("\n"); */ # pragma adapt begin gsl_fft_complex_radix2_forward (data, 1, size); double norm = 0; for (i = 0; i < size; i++) { // printf ("%d %e %e\n", i, // REAL(data,i)/sqrt(128), // IMAG(data,i)/sqrt(128)); norm += square(REAL(data, i)/sqrt(size)) + square(IMAG(data,i)/sqrt(size)); } norm = sqrt(norm); # pragma adapt output norm EPS # pragma adapt end // printf("norm: %.16f\n", norm); double diff = (ANS-norm); double error = ABS(diff); if ((double)error < (double)EPS) { printf("fft - SUCCESSFUL!\n"); } else { printf("fft - FAILED!\n"); } return 0; }
double beip_wrap(double x) { npy_cdouble Be, Ke, Bep, Kep; int flag = 0; if (x<0) {x=-x; flag=1;} F_FUNC(klvna,KLVNA)(&x, CADDR(Be), CADDR(Ke), CADDR(Bep), CADDR(Kep)); ZCONVINF("beip", Bep); if (flag) return -IMAG(Bep); return IMAG(Bep); }
int FUNCTION(fft_signal,complex_exp) (const int k, const size_t n, const size_t stride, const BASE z_real, const BASE z_imag, BASE data[], BASE fft[]) { size_t j; if (n == 0) { GSL_ERROR ("length n must be positive integer", GSL_EDOM); } /* exponential, data[j] = z * exp(2 pi i j k) */ for (j = 0; j < n; j++) { const double arg = 2 * M_PI * ((double) ((j * k) % n)) / ((double) n); const BASE w_real = (BASE)cos (arg); const BASE w_imag = (BASE)sin (arg); REAL(data,stride,j) = w_real * z_real - w_imag * z_imag; IMAG(data,stride,j) = w_real * z_imag + w_imag * z_real; } /* fourier transform, fft[j] = z * delta{(j - k),0} */ for (j = 0; j < n; j++) { REAL(fft,stride,j) = 0.0; IMAG(fft,stride,j) = 0.0; } { int freq; if (k <= 0) { freq = (n-k) % n ; } else { freq = (k % n); }; REAL(fft,stride,freq) = ((BASE) n) * z_real; IMAG(fft,stride,freq) = ((BASE) n) * z_imag; } return 0; }
void fft4(t_fft *x, int n1, int N1, int r, t_fft *t, int dir) { int i = r+n1; real *x0 = x[i]; i+=N1; real *x1 = x[i]; i+=N1; real *x2 = x[i]; i+=N1; real *x3 = x[i]; real z20 = x0[0] - x2[0]; real z21 = x0[1] - x2[1]; real z00 = x0[0] + x2[0]; real z01 = x0[1] + x2[1]; real z10 = x1[0] + x3[0]; real z11 = x1[1] + x3[1]; x0[0] = z00 + z10; x0[1] = z01 + z11; real y20 = z00 - z10; real y21 = z01 - z11; real *t2 = t[n1<<1]; real t20 = t2[0]; real t21 = t2[1]; x2[0] = REAL(t20,t21,y20,y21); x2[1] = IMAG(t20,t21,y20,y21); real z30; real z31; if(dir==1) { z30 = (x3[0] - x1[0]); z31 = (x3[1] - x1[1]); } else { z30 = (x1[0] - x3[0]); z31 = (x1[1] - x3[1]); } real y10 = z20 - z31; real y11 = z21 + z30; real *t1 = t[n1]; real t10 = t1[0]; real t11 = t1[1]; x1[0] = REAL(t10,t11,y10,y11); x1[1] = IMAG(t10,t11,y10,y11); real y30 = z20 + z31; real y31 = z21 - z30; real *t3 = t[n1*3]; real t30 = t3[0]; real t31 = t3[1]; x3[0] = REAL(t30,t31,y30,y31); x3[1] = IMAG(t30,t31,y30,y31); }
Py_complex cpsi_wrap( Py_complex z) { Py_complex cy; if (IMAG(z)==0.0) { REAL(cy) = cephes_psi(REAL(z)); IMAG(cy) = 0.0; } else { F_FUNC(cpsi,CPSI)(CADDR(z), CADDR(cy)); } return cy; }
npy_cdouble crgamma_wrap( npy_cdouble z) { int kf = 1; npy_cdouble cy; npy_cdouble cy2; double magsq; F_FUNC(cgama,CGAMA)(CADDR(z), &kf, CADDR(cy)); magsq = ABSQ(cy); REAL(cy2) = REAL(cy) / magsq; IMAG(cy2) = -IMAG(cy) / magsq; return cy2; }
Py_complex crgamma_wrap( Py_complex z) { int kf = 1; Py_complex cy; Py_complex cy2; double magsq; F_FUNC(cgama,CGAMA)(CADDR(z), &kf, CADDR(cy)); magsq = ABSQ(cy); REAL(cy2) = REAL(cy) / magsq; IMAG(cy2) = -IMAG(cy) / magsq; return cy2; }
npy_cdouble cpsi_wrap( npy_cdouble z) { npy_cdouble cy; if (IMAG(z)==0.0) { REAL(cy) = cephes_psi(REAL(z)); IMAG(cy) = 0.0; } else { F_FUNC(cpsi,CPSI)(CADDR(z), CADDR(cy)); } return cy; }
int main (void) { int i; const int n = 630; double data[2*n]; gsl_fft_complex_wavetable * wavetable; gsl_fft_complex_workspace * workspace; for (i = 0; i < n; i++) { REAL(data,i) = 0.0; IMAG(data,i) = 0.0; } data[0] = 1.0; for (i = 1; i <= 10; i++) { REAL(data,i) = REAL(data,n-i) = 1.0; } for (i = 0; i < n; i++) { printf ("%d: %e %e\n", i, REAL(data,i), IMAG(data,i)); } printf ("\n"); wavetable = gsl_fft_complex_wavetable_alloc (n); workspace = gsl_fft_complex_workspace_alloc (n); for (i = 0; i < wavetable->nf; i++) { printf ("# factor %d: %d\n", i, wavetable->factor[i]); } gsl_fft_complex_forward (data, 1, n, wavetable, workspace); for (i = 0; i < n; i++) { printf ("%d: %e %e\n", i, REAL(data,i), IMAG(data,i)); } gsl_fft_complex_wavetable_free (wavetable); gsl_fft_complex_workspace_free (workspace); return 0; }
Py_complex chyp2f1_wrap( double a, double b, double c, Py_complex z) { Py_complex outz; int l1, l0; l0 = ((c == floor(c)) && (c < 0)); l1 = ((fabs(1-REAL(z)) < 1e-15) && (IMAG(z) == 0) && (c-a-b <= 0)); if (l0 || l1) { REAL(outz) = NPY_INFINITY; IMAG(outz) = 0.0; return outz; } F_FUNC(hygfz, HYGFZ)(&a, &b, &c, &z, &outz); return outz; }
npy_cdouble chyp2f1_wrap( double a, double b, double c, npy_cdouble z) { npy_cdouble outz; int l1, l0; l0 = ((c == floor(c)) && (c < 0)); l1 = ((fabs(1-REAL(z)) < 1e-15) && (IMAG(z) == 0) && (c-a-b <= 0)); if (l0 || l1) { sf_error("chyp2f1", SF_ERROR_OVERFLOW, NULL); REAL(outz) = NPY_INFINITY; IMAG(outz) = 0.0; return outz; } F_FUNC(hygfz, HYGFZ)(&a, &b, &c, &z, &outz); return outz; }
void aubio_fft_do_complex(aubio_fft_t * s, fvec_t * input, fvec_t * compspec) { uint_t i; for (i=0; i < s->winsize; i++) { s->in[i] = input->data[i]; } #ifdef HAVE_FFTW3 fftw_execute(s->pfw); #ifdef HAVE_COMPLEX_H compspec->data[0] = REAL(s->specdata[0]); for (i = 1; i < s->fft_size -1 ; i++) { compspec->data[i] = REAL(s->specdata[i]); compspec->data[compspec->length - i] = IMAG(s->specdata[i]); } compspec->data[s->fft_size-1] = REAL(s->specdata[s->fft_size-1]); #else /* HAVE_COMPLEX_H */ for (i = 0; i < s->fft_size; i++) { compspec->data[i] = s->specdata[i]; } #endif /* HAVE_COMPLEX_H */ #else /* HAVE_FFTW3 */ rdft(s->winsize, 1, s->in, s->ip, s->w); compspec->data[0] = s->in[0]; compspec->data[s->winsize / 2] = s->in[1]; for (i = 1; i < s->fft_size - 1; i++) { compspec->data[i] = s->in[2 * i]; compspec->data[s->winsize - i] = - s->in[2 * i + 1]; } #endif /* HAVE_FFTW3 */ }
int FUNCTION(fft_signal,real_noise) (const size_t n, const size_t stride, BASE data[], BASE fft[]) { size_t i; int status; if (n == 0) { GSL_ERROR ("length n must be positive integer", GSL_EDOM); } for (i = 0; i < n; i++) { REAL(data,stride,i) = (BASE)urand(); IMAG(data,stride,i) = 0.0; } /* compute the dft */ status = FUNCTION(gsl_dft_complex,forward) (data, stride, n, fft); return status; }
/* _m : number of correlators */ PRESYNC() PRESYNC(_create)(TC * _v, unsigned int _n, float _dphi_max, unsigned int _m) { // validate input if (_n < 1) { fprintf(stderr, "error: bpresync_%s_create(), invalid input length\n", EXTENSION_FULL); exit(1); } else if (_m == 0) { fprintf(stderr, "error: bpresync_%s_create(), number of correlators must be at least 1\n", EXTENSION_FULL); exit(1); } // allocate main object memory and initialize PRESYNC() _q = (PRESYNC()) malloc(sizeof(struct PRESYNC(_s))); _q->n = _n; _q->m = _m; _q->n_inv = 1.0f / (float)(_q->n); unsigned int i; // create internal receive buffers _q->rx_i = WINDOW(_create)(_q->n); _q->rx_q = WINDOW(_create)(_q->n); // create internal array of frequency offsets _q->dphi = (float*) malloc( _q->m*sizeof(float) ); // create internal synchronizers _q->sync_i = (DOTPROD()*) malloc( _q->m*sizeof(DOTPROD()) ); _q->sync_q = (DOTPROD()*) malloc( _q->m*sizeof(DOTPROD()) ); // buffer T vi_prime[_n]; T vq_prime[_n]; for (i=0; i<_q->m; i++) { // generate signal with frequency offset _q->dphi[i] = (float)i / (float)(_q->m-1)*_dphi_max; unsigned int k; for (k=0; k<_q->n; k++) { vi_prime[k] = REAL( _v[k] * cexpf(-_Complex_I*k*_q->dphi[i]) ); vq_prime[k] = IMAG( _v[k] * cexpf(-_Complex_I*k*_q->dphi[i]) ); } _q->sync_i[i] = DOTPROD(_create)(vi_prime, _q->n); _q->sync_q[i] = DOTPROD(_create)(vq_prime, _q->n); } // allocate memory for cross-correlation _q->rxy = (float*) malloc( _q->m*sizeof(float) ); // reset object PRESYNC(_reset)(_q); return _q; }
/* * transform frequency domain into stream */ void BandPassFilterFFT::fft_inverse(double* band_data, uint8_t *stream) { gsl_fft_complex_inverse(band_data, 1, samples, wavetable, workspace); // Then reverse Fourier transform back to the time domain... for (uint32_t i =0; i<samples; i++){ // ...and copy the data back in to the stream. RIGHT((int16_t*)stream,i)=(int16_t)REAL(band_data,i); LEFT((int16_t*)stream,i)=(int16_t)IMAG(band_data,i); } }
double kei_wrap(double x) { Py_complex Be, Ke, Bep, Kep; if (x<0) return NPY_NAN; F_FUNC(klvna,KLVNA)(&x, CADDR(Be), CADDR(Ke), CADDR(Bep), CADDR(Kep)); ZCONVINF(Ke); return IMAG(Ke); }
/* * graphical equaliser using the function eq to boost/cut the stream. */ void BandPassFilterFFT::EQ(uint8_t *stream, double(*eq)(double, void*), void *args) { double band_data[2*samples]; /* * EQ the data */ for(uint32_t i=0; i<=freqs; i++) // Go over each frequency. { double Eq = eq(f[i], args); REAL(band_data,POSITIVE(i,samples)) = Eq * REAL(fcache,POSITIVE(i,samples)); IMAG(band_data,POSITIVE(i,samples)) = Eq * IMAG(fcache,POSITIVE(i,samples)); REAL(band_data,NEGATIVE(i,samples)) = Eq * REAL(fcache,NEGATIVE(i,samples)); IMAG(band_data,NEGATIVE(i,samples)) = Eq * IMAG(fcache,NEGATIVE(i,samples)); } fft_inverse(band_data, stream); }
double bei_wrap(double x) { npy_cdouble Be, Ke, Bep, Kep; if (x<0) x=-x; F_FUNC(klvna,KLVNA)(&x, CADDR(Be), CADDR(Ke), CADDR(Bep), CADDR(Kep)); ZCONVINF("bei", Be); return IMAG(Be); }
double keip_wrap(double x) { npy_cdouble Be, Ke, Bep, Kep; if (x<0) return NPY_NAN; F_FUNC(klvna,KLVNA)(&x, CADDR(Be), CADDR(Ke), CADDR(Bep), CADDR(Kep)); ZCONVINF("keip", Kep); return IMAG(Kep); }
int kelvin_wrap(double x, npy_cdouble *Be, npy_cdouble *Ke, npy_cdouble *Bep, npy_cdouble *Kep) { int flag = 0; if (x<0) {x=-x; flag=1;} F_FUNC(klvna,KLVNA)(&x, F2C_CST(Be), F2C_CST(Ke), F2C_CST(Bep), F2C_CST(Kep)); ZCONVINF("klvna", *Be); ZCONVINF("klvna", *Ke); ZCONVINF("klvna", *Bep); ZCONVINF("klvna", *Kep); if (flag) { REAL(*Bep) = -REAL(*Bep); IMAG(*Bep) = -IMAG(*Bep); REAL(*Ke) = NPY_NAN; IMAG(*Ke) = NPY_NAN; REAL(*Kep) = NPY_NAN; IMAG(*Kep) = NPY_NAN; } return 0; }
void fft3(t_fft *x, int n1, int N1, int r, t_fft *t, int dir) { int i = r+n1; real *x0 = x[i]; i+=N1; real *x1 = x[i]; i+=N1; real *x2 = x[i]; real z00 = x1[0] + x2[0]; real z01 = x1[1] + x2[1]; real z10 = x0[0] - T300*z00; real z11 = x0[1] - T300*z01; real z20; real z21; if(dir==1) { z20 = T301*(x2[0] - x1[0]); z21 = T301*(x2[1] - x1[1]); } else { z20 = T301*(x1[0] - x2[0]); z21 = T301*(x1[1] - x2[1]); } x0[0] = z00 + x0[0]; x0[1] = z01 + x0[1]; real z30 = z10 - z21; real z31 = z11 + z20; real *t1 = t[n1]; real t10 = t1[0]; real t11 = t1[1]; x1[0] = REAL(t10,t11,z30,z31); x1[1] = IMAG(t10,t11,z30,z31); real z40 = z10 + z21; real z41 = z11 - z20; real *t2 = t[n1<<1]; real t20 = t2[0]; real t21 = t2[1]; x2[0] = REAL(t20,t21,z40,z41); x2[1] = IMAG(t20,t21,z40,z41); }
int FUNCTION(gsl_dft_complex,transform) (const BASE data[], const size_t stride, const size_t n, BASE result[], const gsl_fft_direction sign) { size_t i, j, exponent; const double d_theta = 2.0 * ((int) sign) * M_PI / (double) n; /* FIXME: check that input length == output length and give error */ for (i = 0; i < n; i++) { ATOMIC sum_real = 0; ATOMIC sum_imag = 0; exponent = 0; for (j = 0; j < n; j++) { double theta = d_theta * (double) exponent; /* sum = exp(i theta) * data[j] */ ATOMIC w_real = (ATOMIC) cos (theta); ATOMIC w_imag = (ATOMIC) sin (theta); ATOMIC data_real = REAL(data,stride,j); ATOMIC data_imag = IMAG(data,stride,j); sum_real += w_real * data_real - w_imag * data_imag; sum_imag += w_real * data_imag + w_imag * data_real; exponent = (exponent + i) % n; } REAL(result,stride,i) = sum_real; IMAG(result,stride,i) = sum_imag; } return 0; }
int FUNCTION(fft_signal,complex_pulse) (const size_t k, const size_t n, const size_t stride, const BASE z_real, const BASE z_imag, BASE data[], BASE fft[]) { size_t j; if (n == 0) { GSL_ERROR ("length n must be positive integer", GSL_EDOM); } /* gsl_complex pulse at position k, data[j] = z * delta_{jk} */ for (j = 0; j < n; j++) { REAL(data,stride,j) = 0.0; IMAG(data,stride,j) = 0.0; } REAL(data,stride,k % n) = z_real; IMAG(data,stride,k % n) = z_imag; /* fourier transform, fft[j] = z * exp(-2 pi i j k / n) */ for (j = 0; j < n; j++) { const double arg = -2 * M_PI * ((double) ((j * k) % n)) / ((double) n); const BASE w_real = (BASE)cos (arg); const BASE w_imag = (BASE)sin (arg); REAL(fft,stride,j) = w_real * z_real - w_imag * z_imag; IMAG(fft,stride,j) = w_real * z_imag + w_imag * z_real; } return 0; }
/** * Utility function to write data to file. * save the graphs y1 = re[z(x)] and y2 = im[z(x)] */ void BandPassFilterFFT::writef(double* data, char* file) { FILE *fp = fopen(file,"w"); /* write the data to file. Column 1 = x, Column 2 = re(z), Column 3 = im(z).*/ for (uint32_t i =0; i<samples; i++){ fprintf(fp,"%f %f %f\n", f[i], REAL(data,i), IMAG(data,i)); fflush(fp); } fclose(fp); }
/* * transform stream in to frequency domain */ fft_t BandPassFilterFFT::fft_alloc(uint8_t* stream) { for (uint32_t i =0; i<samples; i++){ REAL(fcache,i)=(double)RIGHT((int16_t*)stream,i); IMAG(fcache,i)=(double)LEFT((int16_t*)stream,i); } /* Fourier transform */ gsl_fft_complex_forward(fcache, 1, samples, wavetable, workspace); return fcache; }
int main (void) { int i; for (i = 0; i < PULSE_SAMPLE_LEN; i++) { REAL(data,i) = 0.0; IMAG(data,i) = 0.0; } REAL(data,0) = 1.0; for (i = 1; i <= PULSE_ACTUAL_LEN; i++) { REAL(data,i) = REAL(data,PULSE_SAMPLE_LEN-i) = 1.0; } #if PRINT_RES for (i = 0; i < PULSE_SAMPLE_LEN; i++) { printf ("%d %e %e\n", i, REAL(data,i), IMAG(data,i)); } printf ("\n\n"); #endif gsl_fft_complex_radix2_forward (data, 1, PULSE_SAMPLE_LEN); #if PRINT_RES for (i = 0; i < PULSE_SAMPLE_LEN; i++) { printf ("%d %e %e\n", i, REAL(data,i)/sqrt(PULSE_SAMPLE_LEN), IMAG(data,i)/sqrt(PULSE_SAMPLE_LEN)); } #endif return 0; }
int FUNCTION(fft_signal,complex_constant) (const size_t n, const size_t stride, const BASE z_real, const BASE z_imag, BASE data[], BASE fft[]) { size_t j; if (n == 0) { GSL_ERROR ("length n must be positive integer", GSL_EDOM); } /* constant, data[j] = z */ for (j = 0; j < n; j++) { REAL(data,stride,j) = z_real; IMAG(data,stride,j) = z_imag; } /* fourier transform, fft[j] = n z delta_{j0} */ for (j = 0; j < n; j++) { REAL(fft,stride,j) = 0.0; IMAG(fft,stride,j) = 0.0; } REAL(fft,stride,0) = ((BASE) n) * z_real; IMAG(fft,stride,0) = ((BASE) n) * z_imag; return 0; }
int main (void) { int i; double data[2*128]; for (i = 0; i < 128; i++) { REAL(data,i) = 0.0; IMAG(data,i) = 0.0; } REAL(data,0) = 1.0; for (i = 1; i <= 10; i++) { REAL(data,i) = REAL(data,128-i) = 1.0; } for (i = 0; i < 128; i++) { printf ("%d %e %e\n", i, REAL(data,i), IMAG(data,i)); } printf ("\n"); gsl_fft_complex_radix2_forward (data, 1, 128); for (i = 0; i < 128; i++) { printf ("%d %e %e\n", i, REAL(data,i)/sqrt(128), IMAG(data,i)/sqrt(128)); } return 0; }
void fft2(t_fft *x, int n1, int N1, int r, t_fft *t, int dir) { int i = r+n1; real *x0 = x[i]; i+= N1; real *x1 = x[i]; real y0 = x0[0] - x1[0]; real y1 = x0[1] - x1[1]; x0[0] += x1[0]; x0[1] += x1[1]; real *t1 = t[n1]; real t10 = t1[0]; real t11 = t1[1]; x1[0] = REAL(t10,t11,y0,y1); x1[1] = IMAG(t10,t11,y0,y1); }
int FUNCTION(gsl_dft_complex,inverse) (const BASE data[], const size_t stride, const size_t n, BASE result[]) { gsl_fft_direction sign = backward; int status = FUNCTION(gsl_dft_complex,transform) (data, stride, n, result, sign); /* normalize inverse fft with 1/n */ { const ATOMIC norm = ONE / (ATOMIC)n; size_t i; for (i = 0; i < n; i++) { REAL(result,stride,i) *= norm; IMAG(result,stride,i) *= norm; } } return status; }