void cnoise_generate_colored_noise( double *x, int N, double alpha, double std ){ gsl_fft_complex_wavetable * wavetable; gsl_fft_complex_workspace * workspace; int i, j; double tmp; double gauss_second, gauss_u, gauss_v; double * fh = malloc( 4*N*sizeof(double) ); double * fw = malloc( 4*N*sizeof(double) ); fh[0] = 1.0; fh[1] = 0.0; for( i=1; i<N; i++ ){ fh[2*i] = fh[2*(i-1)] * ( 0.5 * alpha + (double)(i-1) ) / ((double) i ); fh[2*i+1] = 0.0; }; for( i=0; i<N; i+=2 ){ gauss_u = cnoise_uniform01(); gauss_v = cnoise_uniform01(); fw[2*i] = std * sqrt( - 2 * log( gauss_u ) ) * cos( 2 * _CNOISE_PI * gauss_v ); fw[2*i+1] = 0.0; fw[2*i+2] = std * sqrt( - 2 * log( gauss_u ) ) * sin( 2 * _CNOISE_PI * gauss_v ); fw[2*i+3] = 0.0; }; for( i=2*N; i<4*N; i++ ){ fh[i] = 0.0; fw[i] = 0.0; }; wavetable = gsl_fft_complex_wavetable_alloc(2*N); workspace = gsl_fft_complex_workspace_alloc(2*N); gsl_fft_complex_forward (fh, 1, 2*N, wavetable, workspace); gsl_fft_complex_forward (fw, 1, 2*N, wavetable, workspace); for( i=0; i<N+1; i++ ){ tmp = fw[2*i]; fw[2*i] = tmp*fh[2*i] - fw[2*i+1]*fh[2*i+1]; fw[2*i+1] = tmp*fh[2*i+1] + fw[2*i+1]*fh[2*i]; }; fw[0] /= 2.0; fw[1] /= 2.0; fw[2*N] /= 2.0; fw[2*N+1] /= 2.0; for( i=N+1; i<2*N; i++ ){ fw[2*i] = 0.0; fw[2*i+1] = 0.0; }; gsl_fft_complex_inverse( fw, 1, 2*N, wavetable, workspace); for( i=0; i<N; i++ ){ x[i] = 2.0*fw[2*i]; }; free( fh ); free( fw ); gsl_fft_complex_wavetable_free (wavetable); gsl_fft_complex_workspace_free (workspace); };
void cnoise_generate_colored_noise_truncated( double *x, int N, double alpha, double std, double range ){ gsl_fft_complex_wavetable * wavetable; gsl_fft_complex_workspace * workspace; int i, j; double tmp; double gauss_second, gauss_u, gauss_v; double * fh = malloc( 4*N*sizeof(double) ); double * fw = malloc( 4*N*sizeof(double) ); fh[0] = 1.0; fh[1] = 0.0; for( i=1; i<N; i++ ){ fh[2*i] = fh[2*(i-1)] * ( 0.5 * alpha + (double)(i-1) ) / ((double) i ); fh[2*i+1] = 0.0; }; for( i=0; i<N; i+=2 ){ cnoise_two_gaussian_truncated( &fw[2*i], &fw[2*i+2], std, range ); }; for( i=2*N; i<4*N; i++ ){ fh[i] = 0.0; fw[i] = 0.0; }; wavetable = gsl_fft_complex_wavetable_alloc(2*N); workspace = gsl_fft_complex_workspace_alloc(2*N); gsl_fft_complex_forward (fh, 1, 2*N, wavetable, workspace); gsl_fft_complex_forward (fw, 1, 2*N, wavetable, workspace); for( i=0; i<N+1; i++ ){ tmp = fw[2*i]; fw[2*i] = tmp*fh[2*i] - fw[2*i+1]*fh[2*i+1]; fw[2*i+1] = tmp*fh[2*i+1] + fw[2*i+1]*fh[2*i]; }; fw[0] /= 2.0; fw[1] /= 2.0; fw[2*N] /= 2.0; fw[2*N+1] /= 2.0; for( i=N+1; i<2*N; i++ ){ fw[2*i] = 0.0; fw[2*i+1] = 0.0; }; gsl_fft_complex_inverse( fw, 1, 2*N, wavetable, workspace); for( i=0; i<N; i++ ){ x[i] = 2.0*fw[2*i]; }; free( fh ); free( fw ); gsl_fft_complex_wavetable_free (wavetable); gsl_fft_complex_workspace_free (workspace); };
/* calculates the fast Fourier transform of a complex packed array, and stores * it into fft_results. The length of the array is 2*N, and the storage * convention is that the real and imaginary parts of the complex number are * stored in consecutive locations */ int complex_fft (size_t N, double *data, double *fft_results) { size_t i; int retcode; /* initialize the data for fft */ for (i=0; i<2*N; i++) fft_results [i] = data [i]; /* use the corresponding routine if N is power of 2 */ if (is_power_of_n (N,2)) { /* perform the fft */ retcode = gsl_fft_complex_radix2_forward (fft_results, 1, N); } else { /* alloc memory for wavetable and workspace */ gsl_fft_complex_wavetable * wavetable = gsl_fft_complex_wavetable_alloc (N); gsl_fft_complex_workspace * workspace = gsl_fft_complex_workspace_alloc (N); /* perform the fft */ retcode = gsl_fft_complex_forward (fft_results, 1, N, wavetable, workspace); /* free memory */ gsl_fft_complex_wavetable_free (wavetable); gsl_fft_complex_workspace_free (workspace); } return retcode; }
void FFT::fftTable() { double *amp = (double *)malloc(d_n*sizeof(double)); gsl_fft_complex_wavetable *wavetable = gsl_fft_complex_wavetable_alloc (d_n); gsl_fft_complex_workspace *workspace = gsl_fft_complex_workspace_alloc (d_n); if(!amp || !wavetable || !workspace){ memoryErrorMessage(); return; } double df = 1.0/(double)(d_n*d_sampling);//frequency sampling double aMax = 0.0;//max amplitude if(d_inverse) gsl_fft_complex_inverse (d_y, 1, d_n, wavetable, workspace); else gsl_fft_complex_forward (d_y, 1, d_n, wavetable, workspace); gsl_fft_complex_wavetable_free (wavetable); gsl_fft_complex_workspace_free (workspace); if (d_shift_order) { int n2 = d_n/2; for(int i = 0; i < d_n; i++) { d_x[i] = (i - n2)*df; int j = i + d_n; double aux = d_y[i]; d_y[i] = d_y[j]; d_y[j] = aux; } } else { for(int i = 0; i < d_n; i++) d_x[i] = i*df; } for(int i = 0; i < d_n; i++) { int i2 = 2*i; double a = sqrt(d_y[i2]*d_y[i2] + d_y[i2+1]*d_y[i2+1]); amp[i]= a; if (a > aMax) aMax = a; } ApplicationWindow *app = (ApplicationWindow *)parent(); QLocale locale = app->locale(); int prec = app->d_decimal_digits; for (int i = 0; i < d_n; i++) { int i2 = 2*i; d_result_table->setText(i, 0, locale.toString(d_x[i], 'g', prec)); d_result_table->setText(i, 1, locale.toString(d_y[i2], 'g', prec)); d_result_table->setText(i, 2, locale.toString(d_y[i2 + 1], 'g', prec)); if (d_normalize) d_result_table->setText(i, 3, locale.toString(amp[i]/aMax, 'g', prec)); else d_result_table->setText(i, 3, locale.toString(amp[i], 'g', prec)); d_result_table->setText(i, 4, locale.toString(atan(d_y[i2 + 1]/d_y[i2]), 'g', prec)); } free(amp); }
/* * 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; 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; }
int FFTCalc::performFFT() { return gsl_fft_complex_forward(&data_[0], 1, N_, wavetable_, workspace_); }
QString FFT::fftTable() { int i; double *amp = new double[d_n]; gsl_fft_complex_wavetable *wavetable = gsl_fft_complex_wavetable_alloc (d_n); gsl_fft_complex_workspace *workspace = gsl_fft_complex_workspace_alloc (d_n); if(!amp || !wavetable || !workspace){ QMessageBox::critical((ApplicationWindow *)parent(), tr("MantidPlot") + " - " + tr("Error"), tr("Could not allocate memory, operation aborted!")); d_init_err = true; return ""; } double df = 1.0/(double)(d_n*d_sampling);//frequency sampling double aMax = 0.0;//max amplitude QString text; if(!d_inverse) { d_explanation = tr("Forward") + " " + tr("FFT") + " " + tr("of") + " " + d_table->colName(d_real_col); text = tr("Frequency"); gsl_fft_complex_forward (d_y, 1, d_n, wavetable, workspace); } else { d_explanation = tr("Inverse") + " " + tr("FFT") + " " + tr("of") + " " + d_table->colName(d_real_col); text = tr("Time"); gsl_fft_complex_inverse (d_y, 1, d_n, wavetable, workspace); } gsl_fft_complex_wavetable_free (wavetable); gsl_fft_complex_workspace_free (workspace); if (d_shift_order) { int n2 = d_n/2; for(i=0; i<d_n; i++) { d_x[i] = (i-n2)*df; int j = i + d_n; double aux = d_y[i]; d_y[i] = d_y[j]; d_y[j] = aux; } } else { for(i=0; i<d_n; i++) d_x[i] = i*df; } for(i=0; i<d_n; i++) { int i2 = 2*i; double a = sqrt(d_y[i2]*d_y[i2] + d_y[i2+1]*d_y[i2+1]); amp[i]= a; if (a > aMax) aMax = a; } ApplicationWindow *app = (ApplicationWindow *)parent(); QLocale locale = app->locale(); int prec = app->d_decimal_digits; text += "\t"+tr("Real")+"\t"+tr("Imaginary")+"\t"+tr("Amplitude")+"\t"+tr("Angle")+"\n"; for (i=0; i<d_n; i++) { int i2 = 2*i; text += locale.toString(d_x[i], 'g', prec)+"\t"; text += locale.toString(d_y[i2], 'g', prec)+"\t"; text += locale.toString(d_y[i2+1], 'g', prec)+"\t"; if (d_normalize) text += locale.toString(amp[i]/aMax, 'g', prec)+"\t"; else text += locale.toString(amp[i], 'g', prec)+"\t"; text += locale.toString(atan(d_y[i2+1]/d_y[i2]), 'g', prec)+"\n"; } delete[] amp; return text; }
int main (int argc, char *argv[]) { int status, factor_sum; size_t i, start, end, n; double *complex_data, *complex_tmp; double rms, total; gsl_fft_complex_wavetable * cw; if (argc == 2) { start = strtol (argv[1], NULL, 0); end = start + 1; } else { start = 1 ; end = 1000 ; } for (n = start; n < end; n++) { complex_data = (double *) malloc (n * 2 * sizeof (double)); complex_tmp = (double *) malloc (n * 2 * sizeof (double)); cw = gsl_fft_complex_wavetable_alloc (n); status = gsl_fft_complex_init (n, cw); status = gsl_fft_complex_generate (n, cw); for (i = 0; i < n; i++) { REAL(complex_data,1,i) = urand(); IMAG(complex_data,1,i) = urand(); } memcpy (complex_tmp, complex_data, n * 2 * sizeof (double)); gsl_fft_complex_forward (complex_data, 1, n, cw); gsl_fft_complex_inverse (complex_data, 1, n, cw); total = 0.0; for (i = 0; i < n; i++) { double dr = REAL(complex_data,1,i) - REAL(complex_tmp,1,i); double di = IMAG(complex_data,1,i) - IMAG(complex_tmp,1,i); total += dr * dr + di * di; } rms = sqrt (total / n); factor_sum = 0; for (i = 0; i < cw->nf; i++) { int j = cw->factor[i]; factor_sum += j; } printf ("n = %d factor_sum = %d rms = %e\n", n, factor_sum, rms); free (complex_data); free (complex_tmp); } return 0; }
QList<Column *> FFT::fftTable() { int i; int rows = d_table->numRows(); double *amp = new double[rows]; gsl_fft_complex_wavetable *wavetable = gsl_fft_complex_wavetable_alloc(rows); gsl_fft_complex_workspace *workspace = gsl_fft_complex_workspace_alloc(rows); if (!amp || !wavetable || !workspace) { QMessageBox::critical((ApplicationWindow *)parent(), tr("AlphaPlot") + " - " + tr("Error"), tr("Could not allocate memory, operation aborted!")); d_init_err = true; return QList<Column *>(); } double df = 1.0 / (double)(rows * d_sampling); // frequency sampling double aMax = 0.0; // max amplitude QList<Column *> columns; if (!d_inverse) { columns << new Column(tr("Frequency"), AlphaPlot::Numeric); gsl_fft_complex_forward(d_y, 1, rows, wavetable, workspace); } else { columns << new Column(tr("Time"), AlphaPlot::Numeric); gsl_fft_complex_inverse(d_y, 1, rows, wavetable, workspace); } gsl_fft_complex_wavetable_free(wavetable); gsl_fft_complex_workspace_free(workspace); if (d_shift_order) { int n2 = rows / 2; for (i = 0; i < rows; i++) { d_x[i] = (i - n2) * df; int j = i + rows; double aux = d_y[i]; d_y[i] = d_y[j]; d_y[j] = aux; } } else { for (i = 0; i < rows; i++) d_x[i] = i * df; } for (i = 0; i < rows; i++) { int i2 = 2 * i; double a = sqrt(d_y[i2] * d_y[i2] + d_y[i2 + 1] * d_y[i2 + 1]); amp[i] = a; if (a > aMax) aMax = a; } columns << new Column(tr("Real"), AlphaPlot::Numeric); columns << new Column(tr("Imaginary"), AlphaPlot::Numeric); columns << new Column(tr("Amplitude"), AlphaPlot::Numeric); columns << new Column(tr("Angle"), AlphaPlot::Numeric); for (i = 0; i < rows; i++) { int i2 = 2 * i; columns.at(0)->setValueAt(i, d_x[i]); columns.at(1)->setValueAt(i, d_y[i2]); columns.at(2)->setValueAt(i, d_y[i2 + 1]); if (d_normalize) columns.at(3)->setValueAt(i, amp[i] / aMax); else columns.at(3)->setValueAt(i, amp[i]); columns.at(4)->setValueAt(i, atan(d_y[i2 + 1] / d_y[i2])); } delete[] amp; columns.at(0)->setPlotDesignation(AlphaPlot::X); columns.at(1)->setPlotDesignation(AlphaPlot::Y); columns.at(2)->setPlotDesignation(AlphaPlot::Y); columns.at(3)->setPlotDesignation(AlphaPlot::Y); columns.at(4)->setPlotDesignation(AlphaPlot::Y); return columns; }
QString FFT::fftTable() { int i; int rows = d_table->tableRows(); double *amp = new double[rows]; gsl_fft_complex_wavetable *wavetable = gsl_fft_complex_wavetable_alloc (rows); gsl_fft_complex_workspace *workspace = gsl_fft_complex_workspace_alloc (rows); if(!amp || !wavetable || !workspace) { QMessageBox::critical((ApplicationWindow *)parent(), tr("QtiPlot") + " - " + tr("Error"), tr("Could not allocate memory, operation aborted!")); d_init_err = true; return ""; } double df = 1.0/(double)(rows*d_sampling);//frequency sampling double aMax = 0.0;//max amplitude QString text; if(!d_inverse) { text = tr("Frequency"); gsl_fft_complex_forward (d_y, 1, rows, wavetable, workspace); } else { text = tr("Time"); gsl_fft_complex_inverse (d_y, 1, rows, wavetable, workspace); } gsl_fft_complex_wavetable_free (wavetable); gsl_fft_complex_workspace_free (workspace); if (d_shift_order) { int n2 = rows/2; for(i=0; i<rows; i++) { d_x[i] = (i-n2)*df; int j = i + rows; double aux = d_y[i]; d_y[i] = d_y[j]; d_y[j] = aux; } } else { for(i=0; i<rows; i++) d_x[i] = i*df; } for(i=0; i<rows; i++) { int i2 = 2*i; double a = sqrt(d_y[i2]*d_y[i2] + d_y[i2+1]*d_y[i2+1]); amp[i]= a; if (a > aMax) aMax = a; } text += "\t"+tr("Real")+"\t"+tr("Imaginary")+"\t"+tr("Amplitude")+"\t"+tr("Angle")+"\n"; for (i=0; i<rows; i++) { int i2 = 2*i; text += QString::number(d_x[i])+"\t"; text += QString::number(d_y[i2])+"\t"; text += QString::number(d_y[i2+1])+"\t"; if (d_normalize) text += QString::number(amp[i]/aMax)+"\t"; else text += QString::number(amp[i])+"\t"; text += QString::number(atan(d_y[i2+1]/d_y[i2]))+"\n"; } delete[] amp; return text; }