struct fftfilt *fftfilt_init(gdouble f1, gdouble f2, gint len) { struct fftfilt *s; s = g_new0(struct fftfilt, 1); if ((s->fft = fft_init(len, FFT_FWD)) == NULL) { fftfilt_free(s); return NULL; } if ((s->ift = fft_init(len, FFT_REV)) == NULL) { fftfilt_free(s); return NULL; } if ((s->tmpfft = fft_init(len, FFT_FWD)) == NULL) { fftfilt_free(s); return NULL; } s->ovlbuf = g_new0(complex, len / 2); s->filter = g_new0(complex, len); s->filterlen = len; s->inptr = 0; fftfilt_set_freqs(s, f1, f2); return s; }
//-------------------------------------------------------------- // init vom Oszi //-------------------------------------------------------------- void oszi_init(void) { uint32_t check; //--------------------------------------------- // Hardware init //--------------------------------------------- check=p_oszi_hw_init(); p_oszi_send_uart("OSZI 4 STM32F429 [UB]"); if(check==1) { // Touch init error UB_LCD_FillLayer(BACKGROUND_COL); UB_Font_DrawString(10,10,"Touch ERR",&Arial_7x10,FONT_COL,BACKGROUND_COL); while(1); } else if(check==2) { // Fehler in den Defines UB_LCD_FillLayer(BACKGROUND_COL); UB_Font_DrawString(10,10,"Wrong ADC Array-LEN",&Arial_7x10,FONT_COL,BACKGROUND_COL); while(1); } //--------------------------------------------- // FFT init //--------------------------------------------- fft_init(); //--------------------------------------------- // Software init //--------------------------------------------- p_oszi_sw_init(); ADC_change_Frq(Menu.timebase.value); }
// Tests the FFT implementation against the naive DFT, returning the base-10 logarithm of the RMS error. This number should be less than -10. static double test_fft_log_error(int n) { double *inputreal, *inputimag; double *refoutreal, *refoutimag; double *actualoutreal, *actualoutimag; inputreal = random_reals(n); inputimag = random_reals(n); refoutreal = malloc(n * sizeof(double)); refoutimag = malloc(n * sizeof(double)); naive_dft(inputreal, inputimag, refoutreal, refoutimag, 0, n); actualoutreal = memdup(inputreal, n * sizeof(double)); actualoutimag = memdup(inputimag, n * sizeof(double)); void *fftTables = fft_init(n); if (fftTables == NULL) return 99; fft_transform(fftTables, actualoutreal, actualoutimag); fft_destroy(fftTables); double result = log10_rms_err(refoutreal, refoutimag, actualoutreal, actualoutimag, n); free(inputreal); free(inputimag); free(refoutreal); free(refoutimag); free(actualoutreal); free(actualoutimag); return result; }
/* * init MDCT or IMDCT computation */ int ff_mdct_init(MDCTContext *s, int nbits, int inverse) { int n, n4, i; float alpha; memset(s, 0, sizeof(*s)); n = 1 << nbits; s->nbits = nbits; s->n = n; n4 = n >> 2; s->tcos = av_malloc(n4 * sizeof(FFTSample)); if (!s->tcos) goto fail; s->tsin = av_malloc(n4 * sizeof(FFTSample)); if (!s->tsin) goto fail; for(i=0;i<n4;i++) { alpha = 2 * M_PI * (i + 1.0 / 8.0) / n; s->tcos[i] = -cos(alpha); s->tsin[i] = -sin(alpha); } if (fft_init(&s->fft, s->nbits - 2, inverse) < 0) goto fail; return 0; fail: av_freep(&s->tcos); av_freep(&s->tsin); return -1; }
/* ** CZT計算用構造体に対し、標本数 n, 出力データ数 no 用の数表データを ** 作成する。 ** ** cztp = CZT計算用構造体へのポインタ ** n = 標本点の数 ** no = 出力するデータの数 ** return = 0:正常終了 1:nが無効な数 2:メモリ不足 */ int czt_init(czt_struct *cztp, int n, int no) { int i, nx; if (n <= 1) return 1; if (no <= 1 || n < no) no = n; nx = n + no; /* (n + no)を2の整数乗まで拡張する(nx) */ for (i = 1; i < nx; i *= 2) ; nx = i; if (fft_init(&cztp->fft, nx) != 0) return 2; cztp->samples = n; cztp->samples_out = no; cztp->samples_ex = nx; cztp->wr = (REAL*)malloc(2 * n * sizeof(REAL)); cztp->vr = (REAL*)malloc(2 * nx * sizeof(REAL)); cztp->tr = (REAL*)malloc(2 * nx * sizeof(REAL)); if (cztp->wr == NULL || cztp->vr == NULL || cztp->tr == NULL) { czt_end(cztp); return 2; } cztp->wi = cztp->wr + n; cztp->vi = cztp->vr + nx; cztp->ti = cztp->tr + nx; make_cztdata(n, no, nx, cztp->wr, cztp->wi, cztp->vr, cztp->vi); fft(&cztp->fft, 0, cztp->vr, cztp->vi); return 0; }
void send_data (int channels, int size, short *buf) { int i; struct timeval time; guint32 latency; if (!vis) { return; } latency = xmms_output_latency (vis->output); fft_init (); gettimeofday (&time, NULL); time.tv_sec += (latency / 1000); time.tv_usec += (latency % 1000) * 1000; if (time.tv_usec > 1000000) { time.tv_sec++; time.tv_usec -= 1000000; } g_mutex_lock (&vis->clientlock); for (i = 0; i < vis->clientc; ++i) { if (vis->clientv[i]) { package_write (vis->clientv[i], i, &time, channels, size, buf); } } g_mutex_unlock (&vis->clientlock); }
int main () { int i; int N, n; int nTimes; float secs; float *results = malloc ((MAXPOW2 - MINPOW2) * sizeof (float)); timestamp_t t0, t1; for (N = (1 << MINPOW2), n = 0; N < (1 << MAXPOW2); N = N << 1, n++) { complex *in = malloc ((N) * sizeof (complex)); complex *out = malloc ((N) * sizeof (complex)); for (i = 0; i < N; i++) in[i].r = i, in[i].i = 0; fft_init (N); nTimes = (int) ceil ((MFLOPS * 1e6F) / (float) flops_fft (N)); t0 = get_timestamp(); for (i = 0; i < nTimes; i++) { memcpy (out, in, (N) * sizeof (complex)); fft_exec (N, out); } t1 = get_timestamp(); secs = (t1 - t0) / 1000000.0L; free (in); free (out); fft_end (); fprintf (stderr, "nTimes=%d N=%d: (flops %f : time:%g us)\n", nTimes, N, (flops_fft (N) * nTimes) / secs * 1e-6F, secs); results[n] = (flops_fft (N) * nTimes) / secs * 1e-6F; } for (n = 0; n < (MAXPOW2 - MINPOW2); ++n) printf ("%d, %f\n", 1 << (MINPOW2 + n), results[n]); free (results); }
static void calc_freq(gint16 *dest, gint16 *src) { static fft_state *state = NULL; gfloat tmp_out[257]; gint i; if(!state) state = fft_init(); fft_perform(src,tmp_out,state); for(i = 0; i < 256; i++) dest[i] = ((gint)sqrt(tmp_out[i + 1])) >> 8; }
int main(void) { DisableDog(); CPUinit(); EALLOW; outputEnable(); LCDinit(); LCDclear(); initADC(); DAC_init(); // SRAMwrite(0); // SRAMaddress = 0x260000; //shouldn't need SRAM here fft_init(); initBuffers(); timerINIT(ISRvalue, samplingRate); while(1){ if(sampleBufferFull){ fft.InBuf = &sampleBuffer[0]; int i; for(i = 0;i<FFT_SIZE;i++){ outBuffer[i] = 0; } for(i=0;i<FFT_SIZE/2;i++){ MagBuffer[i] = 0; } RFFT_f32(&fft); //fft.MagBuf = &sampleBuffer[0]; RFFT_f32_mag(&fft); sampleBufferFull = 0; EINT; } else{ //do nothing } } return 0; }
void fft_init(int k) { int current_k = (int)twiddle_table.size() - 1; if (current_k >= k) return; if (k - 1 > current_k){ fft_init(k - 1); } size_t length = 1 << k; double omega = 2 * M_PI / length; length /= 2; std::vector<complex> sub_table; for (size_t c = 0; c < length; c++){ double angle = omega * c; auto twiddle_factor = complex(cos(angle), sin(angle)); sub_table.push_back(twiddle_factor); } twiddle_table.push_back(std::move(sub_table)); }
static int process_fft(char* data, int size) { static fft_state *left_fftstate; static int init = 0; static char buff_samples[1024]; static int buff_sample_count; if(!(options.opt & MPG321_PRINT_FFT)) return 0; if(!init) { buff_sample_count = 0; left_fftstate = fft_init(); init = 1; } // Size is at least 2304 bytes, so we can eat 1024 of those. if (buff_sample_count && 0) { memcpy(&buff_samples[buff_sample_count], data, 1024 - buff_sample_count); data = &data[buff_sample_count]; size = size - buff_sample_count; perform_and_print(buff_samples, left_fftstate); buff_sample_count = 0; } int samples; for(samples = 0; (samples + 1030) < size; samples += 1024) { perform_and_print(&data[samples], left_fftstate); } if (samples < size && 0) { memcpy(buff_samples, &data[samples], size - samples); buff_sample_count = size - samples; } }
fft_real_object fft_real_init(int N, int sgn) { fft_real_object obj = NULL; fft_type PI, theta; int k; PI = 3.1415926535897932384626433832795; obj = (fft_real_object) malloc (sizeof(struct fft_real_set) + sizeof(fft_data)* (N/2)); obj->cobj = fft_init(N/2,sgn); for (k = 0; k < N/2;++k) { theta = PI2*k/N; obj->twiddle2[k].re = cos(theta); obj->twiddle2[k].im = sin(theta); } return obj; }
void vspectra(void) { int i, j, k, kk, num, numm, i4, maxi, r; int m, mm, blsiz, blsiz2, nsam, n_read; double avsig, av, max, min, noise, wid; uint8_t *bufferRead = malloc((NSAM) * sizeof(uint8_t)); static double vspec[NSPEC]; static int wtt[NSPEC]; static double re[NSPEC * 2], am[NSPEC * 2]; double smax; fftwf_plan p0; float *reamin0, *reamout0; blsiz = NSPEC * 2; blsiz2 = blsiz / 2; d1.bw = 2.4; // fixed 2.4 for TV dongle 10 MHz for ADC card if (d1.fbw == 0.0) d1.fbw = 2.0; // use bandwidth default if not set in srt.cat d1.f1 = 0.5 - (d1.fbw / d1.bw) * 0.5; d1.f2 = 0.5 + (d1.fbw / d1.bw) * 0.5; d1.fc = (d1.f1 + d1.f2) * 0.5; d1.lofreq = 0; // not used but needs to be set to zero to flag the use of the dongle d1.efflofreq = d1.freq - d1.bw * 0.5; if (!d1.fftsim) { fft_init(blsiz2, &p0, &reamin0, &reamout0); } num = d1.nblk; //was 20 // was 100 nsam = NSAM; d1.nsam = NSAM * num; avsig = 0; numm = 0; smax = 0; max = -1e99; min = 1e99; for (i = 0; i < blsiz2; i++) vspec[i] = 0.0; for (k = 0; k < num; k++) { if (!d1.radiosim) // Read the raw data from the RTL Dongle r = rtlsdr_read_sync(dev, bufferRead, nsam, &n_read); else { av = 5.0; if (d1.elnow < 5.0) av = av * (d1.tsys + d1.tcal) / d1.tsys; if (strstr(soutrack, "Sun")) { av = sqrt(d1.eloff * d1.eloff + d1.azoff * d1.azoff * cos(d1.elnow * PI / 180.0) * cos(d1.elnow * PI / 180.0) + 1e-6); if (av > d1.beamw) av = d1.beamw; av = 5.0 + 25.0 * cos(av * PI * 0.5 / d1.beamw) * cos(av * PI * 0.5 / d1.beamw); } for (i = 0; i < nsam; i++) bufferRead[i] = (uint8_t) (sqrt(av) * gauss() + 127.397 + 0.5 + 0 * sin(2.0 * PI * 0.5 * (i / 2) / d1.bw)); // simulate data } // for(i=0;i<nsam;i+=0x10000) printf("%d %f\n",i,(double)(bufferRead[i]-127.397)); for (kk = 0; kk < nsam / blsiz; kk++) { if (d1.fftsim) for (i = 0; i < blsiz2; i++) { re[i] = (double) (bufferRead[2 * i + kk * blsiz] - 127.397); am[i] = (double) (bufferRead[2 * i + 1 + kk * blsiz] - 127.397); if (re[i] > smax) smax = re[i]; } else for (i = 0; i < blsiz2; i++) { reamin0[2 * i] = (float) (bufferRead[2 * i + kk * blsiz] - 127.397); reamin0[2 * i + 1] = (float) (bufferRead[2 * i + 1 + kk * blsiz] - 127.397); if (reamin0[2 * i] > smax) smax = reamin0[2 * i]; } if (d1.fftsim) Four(re, am, blsiz2); else { cfft(&p0); for (i = 0; i < blsiz2; i++) { re[i] = reamout0[2 * i]; am[i] = reamout0[2 * i + 1]; } } // for(i = 0; i < blsiz2; i++) if(re[i] > max) max=re[i]; // for(i = 0; i < blsiz2; i++) if(re[i] < min) min=re[i]; for (i = 0; i < blsiz2; i++) { if (i < blsiz2 / 2) j = i + blsiz2 / 2; else j = i - blsiz2 / 2; vspec[j] += re[i] * re[i] + am[i] * am[i]; numm++; } } } // printf("max %f min %f\n",max,min); max = av = 0; maxi = 0; for (i = 0; i < blsiz2; i++) wtt[i] = 1; if (numm > 0) { if (d1.nfreq == blsiz2) { for (i = 0; i < blsiz2; i++) { if (i > 10) spec[i] = vspec[i] / (double) numm; else spec[i] = 0; } } else { m = blsiz2 / d1.nfreq; for (i = 0; i < d1.nrfi; i++) { i4 = (d1.rfi[i] - d1.freq + d1.bw * 0.5) * blsiz2 / d1.bw + 0.5; // index of rfi MHz wid = 0.5 * d1.rfiwid[i] / (d1.bw / NSPEC); for (j = -wid; j <= wid; j++) if ((i4 + j) >= 0 && (i4 + j) < blsiz2) wtt[i4 + j] = 0; } for (j = 0; j < d1.nfreq; j++) { av = mm = 0; for (i = j * m - m / 2; i <= j * m + m / 2; i++) { if (i > 10 && i < blsiz2 && wtt[i]) { // wtt=0 removal of spurs av += vspec[i] / (double) numm; if (vspec[i] > max) { max = vspec[i]; maxi = i; } mm++; } } if (mm > 0) spec[j] = av / mm; else { spec[j] = 0; if (j > 10) printf("check RFI settings in srt.cat data deleted at %8.3f\n", j * d1.bw / d1.nfreq + d1.freq - d1.bw * 0.5); } } max = max / (double) numm; noise = spec[maxi / m] * sqrt(2.0 * blsiz2 / (double) d1.nsam); if (max > spec[maxi / m] + d1.rfisigma * noise && d1.printout) // rfisigma sigma printf("check for RFI at %8.4f MHz max %5.0e av %5.0e smax %5.0f %3.0f sigma\n", maxi * d1.bw / blsiz2 + d1.freq - d1.bw * 0.5, max, spec[maxi / m], smax, (max - spec[maxi / m]) / noise); } } d1.smax = smax; if (!d1.fftsim) fft_free(&p0, &reamin0, &reamout0); free(bufferRead); }
int main(int argc, char **argv) { FFTComplex *tab, *tab1, *tab_ref; FFTSample *tabtmp, *tab2; int it, i, c; int do_speed = 0; int do_mdct = 0; int do_inverse = 0; FFTContext s1, *s = &s1; MDCTContext m1, *m = &m1; int fft_nbits, fft_size; mm_flags = 0; fft_nbits = 9; for(;;) { c = getopt(argc, argv, "hsimn:"); if (c == -1) break; switch(c) { case 'h': help(); break; case 's': do_speed = 1; break; case 'i': do_inverse = 1; break; case 'm': do_mdct = 1; break; case 'n': fft_nbits = atoi(optarg); break; } } fft_size = 1 << fft_nbits; tab = av_malloc(fft_size * sizeof(FFTComplex)); tab1 = av_malloc(fft_size * sizeof(FFTComplex)); tab_ref = av_malloc(fft_size * sizeof(FFTComplex)); tabtmp = av_malloc(fft_size / 2 * sizeof(FFTSample)); tab2 = av_malloc(fft_size * sizeof(FFTSample)); if (do_mdct) { if (do_inverse) printf("IMDCT"); else printf("MDCT"); ff_mdct_init(m, fft_nbits, do_inverse); } else { if (do_inverse) printf("IFFT"); else printf("FFT"); fft_init(s, fft_nbits, do_inverse); fft_ref_init(fft_nbits, do_inverse); } printf(" %d test\n", fft_size); /* generate random data */ for(i=0;i<fft_size;i++) { tab1[i].re = frandom(); tab1[i].im = frandom(); } /* checking result */ printf("Checking...\n"); if (do_mdct) { if (do_inverse) { imdct_ref((float *)tab_ref, (float *)tab1, fft_size); ff_imdct_calc(m, tab2, (float *)tab1, tabtmp); check_diff((float *)tab_ref, tab2, fft_size); } else { mdct_ref((float *)tab_ref, (float *)tab1, fft_size); ff_mdct_calc(m, tab2, (float *)tab1, tabtmp); check_diff((float *)tab_ref, tab2, fft_size / 2); } } else { memcpy(tab, tab1, fft_size * sizeof(FFTComplex)); fft_permute(s, tab); fft_calc(s, tab); fft_ref(tab_ref, tab1, fft_nbits); check_diff((float *)tab_ref, (float *)tab, fft_size * 2); } /* do a speed test */ if (do_speed) { int64_t time_start, duration; int nb_its; printf("Speed test...\n"); /* we measure during about 1 seconds */ nb_its = 1; for(;;) { time_start = gettime(); for(it=0;it<nb_its;it++) { if (do_mdct) { if (do_inverse) { ff_imdct_calc(m, (float *)tab, (float *)tab1, tabtmp); } else { ff_mdct_calc(m, (float *)tab, (float *)tab1, tabtmp); } } else { memcpy(tab, tab1, fft_size * sizeof(FFTComplex)); fft_calc(s, tab); } } duration = gettime() - time_start; if (duration >= 1000000) break; nb_its *= 2; } printf("time: %0.1f us/transform [total time=%0.2f s its=%d]\n", (double)duration / nb_its, (double)duration / 1000000.0, nb_its); } if (do_mdct) { ff_mdct_end(m); } else { fft_end(s); } return 0; }
AUD_Int32s denoise_mmse( AUD_Int16s *pInBuf, AUD_Int16s *pOutBuf, AUD_Int32s inLen ) { Fft_16s *hFft = NULL; Ifft_16s *hIfft = NULL; AUD_Window16s *hWin = NULL; AUD_Int32s frameSize = 512; AUD_Int32s frameStride = 256; AUD_Int32s frameOverlap = 256; AUD_Int32s nFFT = frameSize; AUD_Int32s nSpecLen = nFFT / 2 + 1; AUD_Int32s nNoiseFrame = (AUD_Int32s)( ( 0.25 * SAMPLE_RATE - frameSize ) / frameStride + 1 );; AUD_Int32s i, j, k; AUD_Int32s cleanLen = 0; // pre-emphasis // sig_preemphasis( pInBuf, pInBuf, inLen ); // init hamming module win16s_init( &hWin, AUD_WIN_HAMM, frameSize, 14 ); AUD_ASSERT( hWin ); // init fft handle fft_init( &hFft, nFFT, 15 ); AUD_ASSERT( hFft ); // init ifft handle ifft_init( &hIfft, nFFT, 15 ); AUD_ASSERT( hIfft ); AUD_Int16s *pFrame = (AUD_Int16s*)calloc( frameSize * sizeof(AUD_Int16s), 1 ); AUD_ASSERT( pFrame ); // FFT AUD_Int32s *pFFTMag = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTMag ); AUD_Int32s *pFFTRe = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTRe ); AUD_Int32s *pFFTIm = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTIm ); AUD_Int32s *pFFTCleanRe = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTCleanRe ); AUD_Int32s *pFFTCleanIm = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTCleanIm ); AUD_Double *pNoiseMean = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pNoiseMean ); AUD_Int32s *pFFTCleanMag = (AUD_Int32s*)calloc( nSpecLen * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTCleanMag ); AUD_Int16s *pxOld = (AUD_Int16s*)calloc( frameSize * sizeof(AUD_Int16s), 1 ); AUD_ASSERT( pxOld ); AUD_Int16s *pxClean = (AUD_Int16s*)calloc( frameSize * sizeof(AUD_Int16s), 1 ); AUD_ASSERT( pxClean ); for ( i = 0; (i < nNoiseFrame) && ( (i * frameStride + frameSize) < inLen ); i++ ) { win16s_calc( hWin, pInBuf + i * frameSize, pFrame ); fft_mag( hFft, pFrame, frameSize, pFFTMag ); for ( j = 0; j < nSpecLen; j++ ) { pNoiseMean[j] += (AUD_Double)pFFTMag[j]; } } // compute noise mean for ( j = 0; j < nSpecLen; j++ ) { pNoiseMean[j] /= nNoiseFrame; } AUD_Double thres = 3.; AUD_Double alpha = 2.; AUD_Double flr = 0.002; AUD_Double G = 0.9; AUD_Double snr, beta; AUD_Double tmp; AUD_Double sigEnergy, noiseEnergy; k = 0; // start processing for ( i = 0; (i * frameStride + frameSize) < inLen; i++ ) { win16s_calc( hWin, pInBuf + i * frameStride, pFrame ); fft_calc( hFft, pFrame, frameSize, pFFTRe, pFFTIm ); // compute SNR sigEnergy = 0.; noiseEnergy = 0.; for ( j = 0; j < nSpecLen; j++ ) { tmp = pFFTRe[j] / 32768. * pFFTRe[j] / 32768. + pFFTIm[j] / 32768. * pFFTIm[j] / 32768.; sigEnergy += tmp; noiseEnergy += pNoiseMean[j] / 32768. * pNoiseMean[j] / 32768.; tmp = sqrt( tmp ) * 32768.; if ( tmp > MAX_INT32S ) { AUD_ECHO pFFTMag[j] = MAX_INT32S; } else { pFFTMag[j] = (AUD_Int32s)round( tmp ); } } snr = 10. * log10( sigEnergy / noiseEnergy ); beta = berouti( snr ); // AUDLOG( "signal energy: %.2f, noise energy: %.2f, snr: %.2f, beta: %.2f\n", sigEnergy, noiseEnergy, snr, beta ); #if 0 AUDLOG( "noisy FFT:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%d, ", pFFTMag[j] ); } AUDLOG( "\n" ); #endif tmp = 0.; for ( j = 0; j < nSpecLen; j++ ) { tmp = AUD_MAX( pow( pFFTMag[j], alpha ) - beta * pow( pNoiseMean[j], alpha ), flr * pow( pNoiseMean[j], alpha ) ); pFFTCleanMag[j] = (AUD_Int32s)round( pow( tmp, 1. / alpha ) ); } // re-estimate noise if ( snr < thres ) { AUD_Double tmpNoise; for ( j = 0; j < nSpecLen; j++ ) { tmpNoise = G * pow( pNoiseMean[j], alpha ) + ( 1 - G ) * pow( pFFTMag[j], alpha ); pNoiseMean[j] = pow( tmpNoise, 1. / alpha ); } } #if 0 AUDLOG( "clean FFT:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%d, ", pFFTCleanMag[j] ); } AUDLOG( "\n" ); #endif pFFTCleanRe[0] = pFFTCleanMag[0]; pFFTCleanIm[0] = 0; AUD_Double costheta, sintheta; for ( j = 1; j < nSpecLen; j++ ) { if ( pFFTMag[j] != 0 ) { costheta = (AUD_Double)pFFTRe[j] / (AUD_Double)pFFTMag[j]; sintheta = (AUD_Double)pFFTIm[j] / (AUD_Double)pFFTMag[j]; pFFTCleanRe[nFFT - j] = pFFTCleanRe[j] = (AUD_Int32s)round( costheta * pFFTCleanMag[j] ); pFFTCleanIm[j] = (AUD_Int32s)round( sintheta * pFFTCleanMag[j] ); pFFTCleanIm[nFFT - j] = -pFFTCleanIm[j]; } else { pFFTCleanRe[nFFT - j] = pFFTCleanRe[j] = pFFTCleanMag[j]; pFFTCleanIm[nFFT - j] = pFFTCleanIm[j] = 0; } } #if 0 AUDLOG( "clean FFT with phase:\n" ); for ( j = 0; j < nFFT; j++ ) { AUDLOG( "%d + j%d, ", pFFTCleanRe[j], pFFTCleanIm[j] ); } AUDLOG( "\n" ); #endif ifft_real( hIfft, pFFTCleanRe, pFFTCleanIm, nFFT, pxClean ); #if 0 AUDLOG( "clean speech:\n" ); for ( j = 0; j < nFFT; j++ ) { AUDLOG( "%d, ", pxClean[j] ); } AUDLOG( "\n" ); #endif for ( j = 0; j < frameStride; j++ ) { pOutBuf[k + j] = pxOld[j] + pxClean[j]; pxOld[j] = pxClean[frameOverlap + j]; } k += frameStride; cleanLen += frameStride; } // de-emphasis // sig_deemphasis( pOutBuf, pOutBuf, cleanLen ); win16s_free( &hWin ); fft_free( &hFft ); ifft_free( &hIfft ); free( pFrame ); free( pNoiseMean ); free( pFFTMag ); free( pFFTRe ); free( pFFTIm ); free( pFFTCleanMag ); free( pFFTCleanRe ); free( pFFTCleanIm ); free( pxOld ); free( pxClean ); return cleanLen; }
void dominoex_init(struct trx *trx) { struct dominoex *s; double bw, cf, flo, fhi; s = g_new0(struct dominoex, 1); s->numtones = 18; switch (trx->mode) { /* 8kHz modes */ case MODE_DOMINOEX4: s->symlen = 2048; s->basetone = 256; /* 1000 Hz */ s->doublespaced = 1; trx->samplerate = 8000; break; case MODE_DOMINOEX8: s->symlen = 1024; s->basetone = 128; /* 1000 Hz */ s->doublespaced = 1; trx->samplerate = 8000; break; case MODE_DOMINOEX16: s->symlen = 512; s->basetone = 64; /* 1000 Hz */ s->doublespaced = 0; trx->samplerate = 8000; break; /* 11.025kHz modes */ case MODE_DOMINOEX5: s->symlen = 2048; s->basetone = 186; /* 1001.3 Hz */ s->doublespaced = 1; trx->samplerate = 11025; break; case MODE_DOMINOEX11: s->symlen = 1024; s->basetone = 93; /* 1001.3 Hz */ s->doublespaced = 0; trx->samplerate = 11025; break; case MODE_DOMINOEX22: s->symlen = 512; s->basetone = 46; /* 990 Hz */ s->doublespaced = 0; trx->samplerate = 11025; break; default: dominoex_free(s); return; } s->tonespacing = (double) (trx->samplerate * ((s->doublespaced) ? 2 : 1)) / s->symlen; if (!(s->fft = fft_init(s->symlen, FFT_FWD))) { g_warning("dominoex_init: init_fft failed\n"); dominoex_free(s); return; } if (!(s->sfft = sfft_init(s->symlen, s->basetone, s->basetone + s->numtones * ((s->doublespaced) ? 2 : 1)))) { g_warning("dominoex_init: init_sfft failed\n"); dominoex_free(s); return; } if (!(s->hilbert = filter_init_hilbert(37, 1))) { g_warning("dominoex_init: init_hilbert failed\n"); dominoex_free(s); return; } s->pipe = g_new0(struct rxpipe, 2 * s->symlen); bw = (s->numtones - 1) * s->tonespacing; cf = 1000.0 + bw / 2.0; flo = (cf - bw) / trx->samplerate; fhi = (cf + bw) / trx->samplerate; if ((s->filt = filter_init_bandpass(127, 1, flo, fhi)) == NULL) { g_warning("dominoex_init: filter_init failed\n"); dominoex_free(s); return; } trx->modem = s; trx->txinit = dominoex_txinit; trx->rxinit = dominoex_rxinit; trx->txprocess = dominoex_txprocess; trx->rxprocess = dominoex_rxprocess; trx->destructor = dominoex_destructor; trx->fragmentsize = s->symlen; trx->bandwidth = (s->numtones - 1) * s->tonespacing; }
int main(int argc, char **argv) { FFTComplex *tab, *tab1, *tab_ref; FFTSample *tab2; enum tf_transform transform = TRANSFORM_FFT; FFTContext *m, *s; #if FFT_FLOAT RDFTContext *r; DCTContext *d; #endif /* FFT_FLOAT */ int it, i, err = 1; int do_speed = 0, do_inverse = 0; int fft_nbits = 9, fft_size; double scale = 1.0; AVLFG prng; #if !AVFFT s = av_mallocz(sizeof(*s)); m = av_mallocz(sizeof(*m)); #endif #if !AVFFT && FFT_FLOAT r = av_mallocz(sizeof(*r)); d = av_mallocz(sizeof(*d)); #endif av_lfg_init(&prng, 1); for (;;) { int c = getopt(argc, argv, "hsimrdn:f:c:"); if (c == -1) break; switch (c) { case 'h': help(); return 1; case 's': do_speed = 1; break; case 'i': do_inverse = 1; break; case 'm': transform = TRANSFORM_MDCT; break; case 'r': transform = TRANSFORM_RDFT; break; case 'd': transform = TRANSFORM_DCT; break; case 'n': fft_nbits = atoi(optarg); break; case 'f': scale = atof(optarg); break; case 'c': { unsigned cpuflags = av_get_cpu_flags(); if (av_parse_cpu_caps(&cpuflags, optarg) < 0) return 1; av_force_cpu_flags(cpuflags); break; } } } fft_size = 1 << fft_nbits; tab = av_malloc_array(fft_size, sizeof(FFTComplex)); tab1 = av_malloc_array(fft_size, sizeof(FFTComplex)); tab_ref = av_malloc_array(fft_size, sizeof(FFTComplex)); tab2 = av_malloc_array(fft_size, sizeof(FFTSample)); if (!(tab && tab1 && tab_ref && tab2)) goto cleanup; switch (transform) { #if CONFIG_MDCT case TRANSFORM_MDCT: av_log(NULL, AV_LOG_INFO, "Scale factor is set to %f\n", scale); if (do_inverse) av_log(NULL, AV_LOG_INFO, "IMDCT"); else av_log(NULL, AV_LOG_INFO, "MDCT"); mdct_init(&m, fft_nbits, do_inverse, scale); break; #endif /* CONFIG_MDCT */ case TRANSFORM_FFT: if (do_inverse) av_log(NULL, AV_LOG_INFO, "IFFT"); else av_log(NULL, AV_LOG_INFO, "FFT"); fft_init(&s, fft_nbits, do_inverse); if ((err = fft_ref_init(fft_nbits, do_inverse)) < 0) goto cleanup; break; #if FFT_FLOAT # if CONFIG_RDFT case TRANSFORM_RDFT: if (do_inverse) av_log(NULL, AV_LOG_INFO, "IDFT_C2R"); else av_log(NULL, AV_LOG_INFO, "DFT_R2C"); rdft_init(&r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C); if ((err = fft_ref_init(fft_nbits, do_inverse)) < 0) goto cleanup; break; # endif /* CONFIG_RDFT */ # if CONFIG_DCT case TRANSFORM_DCT: if (do_inverse) av_log(NULL, AV_LOG_INFO, "DCT_III"); else av_log(NULL, AV_LOG_INFO, "DCT_II"); dct_init(&d, fft_nbits, do_inverse ? DCT_III : DCT_II); break; # endif /* CONFIG_DCT */ #endif /* FFT_FLOAT */ default: av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n"); goto cleanup; } av_log(NULL, AV_LOG_INFO, " %d test\n", fft_size); /* generate random data */ for (i = 0; i < fft_size; i++) { tab1[i].re = frandom(&prng); tab1[i].im = frandom(&prng); } /* checking result */ av_log(NULL, AV_LOG_INFO, "Checking...\n"); switch (transform) { #if CONFIG_MDCT case TRANSFORM_MDCT: if (do_inverse) { imdct_ref(&tab_ref->re, &tab1->re, fft_nbits); imdct_calc(m, tab2, &tab1->re); err = check_diff(&tab_ref->re, tab2, fft_size, scale); } else { mdct_ref(&tab_ref->re, &tab1->re, fft_nbits); mdct_calc(m, tab2, &tab1->re); err = check_diff(&tab_ref->re, tab2, fft_size / 2, scale); } break; #endif /* CONFIG_MDCT */ case TRANSFORM_FFT: memcpy(tab, tab1, fft_size * sizeof(FFTComplex)); fft_permute(s, tab); fft_calc(s, tab); fft_ref(tab_ref, tab1, fft_nbits); err = check_diff(&tab_ref->re, &tab->re, fft_size * 2, 1.0); break; #if FFT_FLOAT #if CONFIG_RDFT case TRANSFORM_RDFT: { int fft_size_2 = fft_size >> 1; if (do_inverse) { tab1[0].im = 0; tab1[fft_size_2].im = 0; for (i = 1; i < fft_size_2; i++) { tab1[fft_size_2 + i].re = tab1[fft_size_2 - i].re; tab1[fft_size_2 + i].im = -tab1[fft_size_2 - i].im; } memcpy(tab2, tab1, fft_size * sizeof(FFTSample)); tab2[1] = tab1[fft_size_2].re; rdft_calc(r, tab2); fft_ref(tab_ref, tab1, fft_nbits); for (i = 0; i < fft_size; i++) { tab[i].re = tab2[i]; tab[i].im = 0; } err = check_diff(&tab_ref->re, &tab->re, fft_size * 2, 0.5); } else { for (i = 0; i < fft_size; i++) { tab2[i] = tab1[i].re; tab1[i].im = 0; } rdft_calc(r, tab2); fft_ref(tab_ref, tab1, fft_nbits); tab_ref[0].im = tab_ref[fft_size_2].re; err = check_diff(&tab_ref->re, tab2, fft_size, 1.0); } break; } #endif /* CONFIG_RDFT */ #if CONFIG_DCT case TRANSFORM_DCT: memcpy(tab, tab1, fft_size * sizeof(FFTComplex)); dct_calc(d, &tab->re); if (do_inverse) idct_ref(&tab_ref->re, &tab1->re, fft_nbits); else dct_ref(&tab_ref->re, &tab1->re, fft_nbits); err = check_diff(&tab_ref->re, &tab->re, fft_size, 1.0); break; #endif /* CONFIG_DCT */ #endif /* FFT_FLOAT */ } /* do a speed test */ if (do_speed) { int64_t time_start, duration; int nb_its; av_log(NULL, AV_LOG_INFO, "Speed test...\n"); /* we measure during about 1 seconds */ nb_its = 1; for (;;) { time_start = av_gettime_relative(); for (it = 0; it < nb_its; it++) { switch (transform) { case TRANSFORM_MDCT: if (do_inverse) imdct_calc(m, &tab->re, &tab1->re); else mdct_calc(m, &tab->re, &tab1->re); break; case TRANSFORM_FFT: memcpy(tab, tab1, fft_size * sizeof(FFTComplex)); fft_calc(s, tab); break; #if FFT_FLOAT case TRANSFORM_RDFT: memcpy(tab2, tab1, fft_size * sizeof(FFTSample)); rdft_calc(r, tab2); break; case TRANSFORM_DCT: memcpy(tab2, tab1, fft_size * sizeof(FFTSample)); dct_calc(d, tab2); break; #endif /* FFT_FLOAT */ } } duration = av_gettime_relative() - time_start; if (duration >= 1000000) break; nb_its *= 2; } av_log(NULL, AV_LOG_INFO, "time: %0.1f us/transform [total time=%0.2f s its=%d]\n", (double) duration / nb_its, (double) duration / 1000000.0, nb_its); } switch (transform) { #if CONFIG_MDCT case TRANSFORM_MDCT: mdct_end(m); break; #endif /* CONFIG_MDCT */ case TRANSFORM_FFT: fft_end(s); break; #if FFT_FLOAT # if CONFIG_RDFT case TRANSFORM_RDFT: rdft_end(r); break; # endif /* CONFIG_RDFT */ # if CONFIG_DCT case TRANSFORM_DCT: dct_end(d); break; # endif /* CONFIG_DCT */ #endif /* FFT_FLOAT */ } cleanup: av_free(tab); av_free(tab1); av_free(tab2); av_free(tab_ref); av_free(exptab); #if !AVFFT av_free(s); av_free(m); #endif #if !AVFFT && FFT_FLOAT av_free(r); av_free(d); #endif if (err) printf("Error: %d.\n", err); return !!err; }
int main() { scanf("%d", &n); fft_init(n + n + 1); }
void vspectra(void) { int i, j, size, sizep, kk, kkk, num, numm, i4; int k, m, mm, blsiz, blsiz2, nsam, info, maxi; double avsig, av, wid, noise, max; static double vspec[NSPEC]; static int wtt[NSPEC]; static float ream[NSPEC * 4]; static double re[NSPEC * 2], am[NSPEC * 2]; double smax, aam, rre, aam2, rre2; double *comm; d1.bw = 10.0; // 10 MHz d1.lofreq = 1416.0; // Luff 1416 MHz d1.efflofreq = d1.lofreq; d1.f1 = (d1.freq - d1.lofreq) / d1.bw - (d1.fbw / d1.bw) * 0.5; d1.f2 = (d1.freq - d1.lofreq) / d1.bw + (d1.fbw / d1.bw) * 0.5; d1.fc = (d1.f1 + d1.f2) * 0.5; blsiz = NSPEC * 2; info = 0; comm = 0; if (!d1.fftsim) { comm = (double *) malloc((5 * blsiz + 100) * sizeof(double)); fft_init(blsiz, ream, comm, &info); } // printf("comm %x\n",comm); nsam = 0x100000; blsiz2 = blsiz / 2; num = 20; // was 100 d1.nsam = nsam * num; avsig = 0; size = 0; numm = 0; smax = 0; av = 0; for (i = 0; i < blsiz2; i++) vspec[i] = 0.0; size = -1; if (!d1.radiosim) while (size != nsam) size = get_pci(buffer1, nsam); // wait for transfer to complete else { av = 5.0; if (d1.elnow < 5.0) av = av * (d1.tsys + d1.tcal) / d1.tsys; if (strstr(soutrack, "Sun")) { av = sqrt(d1.eloff * d1.eloff + d1.azoff * d1.azoff * cos(d1.elnow * PI / 180.0) * cos(d1.elnow * PI / 180.0) + 1e-6); if (av > d1.beamw) av = d1.beamw; av = 5.0 + 25.0 * cos(av * PI * 0.5 / d1.beamw) * cos(av * PI * 0.5 / d1.beamw); } for (i = 0; i < nsam; i++) buffer1[i] = 2048 + sqrt(av) * gauss(); size = nsam; } // simulate transfer sizep = size; for (k = 0; k < num; k++) { if (k < num - 1) { if (!d1.radiosim) size = get_pci(buffer1, nsam); // start new transfer else { for (i = 0; i < nsam; i++) buffer1[i] = 2048 + sqrt(av) * gauss(); size = nsam; } // simulate transfer } if (sizep == nsam) { // work on previous buffer for (kk = 0; kk < sizep / blsiz; kk++) { avsig = 2048; // should be 2048 // avsig = 2300; kkk = kk * blsiz; for (j = 0; j < blsiz; j++) { // if(j==0 && kkk==0) printf("sam %f\n",(buffer1[j + kkk] & 0xfff) - avsig); if (kk % 2 == 0) ream[2 * j] = ((double) (buffer1[j + kkk] & 0xfff) - avsig); else ream[2 * j + 1] = ((double) (buffer1[j + kkk] & 0xfff) - avsig); if (j && ream[2 * j] > smax) smax = ream[2 * j]; } if (kk % 2 == 1) { if (d1.fftsim) { for (i = 0; i < blsiz2; i++) { re[i] = ream[2 * i]; am[i] = ream[2 * i + 1]; } Four(re, am, blsiz2); } else cfft(blsiz, ream, comm, &info); for (i = 0; i < blsiz2; i++) { if (i >= 1) { rre = ream[2 * i] + ream[2 * (blsiz - i)]; aam = ream[2 * i + 1] - ream[2 * (blsiz - i) + 1]; aam2 = -ream[2 * i] + ream[2 * (blsiz - i)]; rre2 = ream[2 * i + 1] + ream[2 * (blsiz - i) + 1]; } else { rre = ream[2 * i] + ream[0]; aam = ream[2 * i + 1] - ream[1]; aam2 = -ream[2 * i] + ream[0]; rre2 = ream[2 * i + 1] + ream[1]; } vspec[i] += rre * rre + aam * aam + rre2 * rre2 + aam2 * aam2; } } } numm++; } while (size != nsam) if (!d1.radiosim) size = get_pci(buffer1, nsam); // wait for transfer to complete else { for (i = 0; i < nsam; i++) buffer1[i] = 2048 + 10.0 * gauss(); size = nsam; } // simulate transfer sizep = size; } av = max = 0; maxi = 0; for (i = 0; i < blsiz2; i++) wtt[i] = 1; if (numm > 0) { if (d1.nfreq == blsiz2) { for (i = 0; i < blsiz2; i++) { if (i > 10) spec[i] = vspec[i] / (double) numm; else spec[i] = 0; } } else { m = blsiz2 / d1.nfreq; for (i = 0; i < d1.nrfi; i++) { i4 = (d1.rfi[i] - d1.lofreq) * blsiz2 / d1.bw + 0.5; // index of rfi MHz wid = 0.5 * d1.rfiwid[i] / (d1.bw / NSPEC); for (j = -wid; j <= wid; j++) if ((i4 + j) >= 0 && (i4 + j) < blsiz2) wtt[i4 + j] = 0; } for (j = 0; j < d1.nfreq; j++) { av = mm = 0; for (i = j * m - m / 2; i <= j * m + m / 2; i++) { if (i > 10 && i < blsiz2 && wtt[i]) { // wtt=0 removal of spurs av += vspec[i] / (double) numm; if (vspec[i] > max) { max = vspec[i]; maxi = i; } mm++; } } if (mm > 0) spec[j] = av / mm; else { spec[j] = 0; if (j > 10) printf("check RFI settings in srt.cat data deleted at %8.3f\n", j * d1.bw / d1.nfreq + d1.lofreq); } } max = max / (double) numm; noise = spec[maxi / m] * sqrt(2.0 * blsiz2 / (double) d1.nsam); if (max > spec[maxi / m] + d1.rfisigma * noise && d1.printout) // rfisigma sigma printf("check for RFI at %8.4f MHz max %5.0e av %5.0e smax %5.0f %3.0f sigma\n", maxi * d1.bw / blsiz2 + d1.lofreq, max, spec[maxi / m], smax, (max - spec[maxi / m]) / noise); } } d1.smax = smax; if (!d1.fftsim) free(comm); }
int main() { int fd=inotify_init(); int in=inotify_add_watch(fd,"/dev/shm",IN_CLOSE_WRITE); char buf[BUF_LEN]; pgm_init(); fft_init(); if (!glfwInit()) exit(EXIT_FAILURE); GLFWwindow* window = glfwCreateWindow(width[0]+width[1], (int)fmax(2*height[0],2*height[1]), "gig-e-camera", NULL, NULL); glfwMakeContextCurrent(window); const int n_tex=4; GLuint texture[n_tex]; glGenTextures( n_tex, texture ); int i; for(i=0;i<n_tex;i++){ glBindTexture( GL_TEXTURE_2D, texture[i] ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glTexImage2D(GL_TEXTURE_2D,0,GL_LUMINANCE,width[i/2],height[i/2],0, GL_LUMINANCE,GL_UNSIGNED_SHORT,0); } glEnable(GL_TEXTURE_2D); while (1){ int len, i = 0; len = read (fd, buf, BUF_LEN); if(len<=0) printf("error\n"); while (i < len) { struct inotify_event *event; event = (struct inotify_event *) &buf[i]; printf ("wd=%d mask=%u cookie=%u len=%u ", event->wd, event->mask, event->cookie, event->len); if (event->len) printf ("name=%s\n", event->name); else printf("\n"); i += EVENT_SIZE + event->len; } { int i; fft_fill(); fft_run(); for(i=0;i<2;i++){ glBindTexture( GL_TEXTURE_2D, texture[2*i] ); glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width[i],height[i],GL_LUMINANCE,GL_UNSIGNED_SHORT,kspace[i]); glBindTexture( GL_TEXTURE_2D, texture[2*i+1] ); glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width[i],height[i],GL_LUMINANCE,GL_UNSIGNED_SHORT,image[i]); } { int win_width,win_height; glfwGetFramebufferSize(window, &win_width, &win_height); glViewport(0, 0, win_width, win_height); } glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, width[0]+width[1], 0,fmax(2*height[0],2*height[1]), 1.f, -1.f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glColor3f(1.f,1.f,1.f); draw_quad(texture[0],0,0,width[0],height[0]); draw_quad(texture[1],0,height[0],width[0],height[0]); draw_quad(texture[2],width[0],0,width[1],height[1]); draw_quad(texture[3],width[0],height[1],width[1],height[1]); glfwSwapBuffers(window); } } glfwTerminate(); }
AUD_Int32s denoise_aud( AUD_Int16s *pInBuf, AUD_Int16s *pOutBuf, AUD_Int32s inLen ) { Fft_16s *hFft = NULL; Ifft_16s *hIfft = NULL; AUD_Window16s *hWin = NULL; AUD_Int32s frameSize = 512; AUD_Int32s frameStride = 256; AUD_Int32s frameOverlap = 256; AUD_Int32s nFFT = frameSize; AUD_Int32s nSpecLen = nFFT / 2 + 1; AUD_Int32s nNoiseFrame = 6; // (AUD_Int32s)( ( 0.25 * SAMPLE_RATE - frameSize ) / frameStride + 1 ); AUD_Int32s i, j, k, m, n, ret; AUD_Int32s cleanLen = 0; // pre-emphasis // sig_preemphasis( pInBuf, pInBuf, inLen ); // init hamming module win16s_init( &hWin, AUD_WIN_HAMM, frameSize, 14 ); AUD_ASSERT( hWin ); // init fft handle fft_init( &hFft, nFFT, 15 ); AUD_ASSERT( hFft ); // init ifft handle ifft_init( &hIfft, nFFT, 15 ); AUD_ASSERT( hIfft ); AUD_Int16s *pFrame = (AUD_Int16s*)calloc( frameSize * sizeof(AUD_Int16s), 1 ); AUD_ASSERT( pFrame ); // FFT AUD_Int32s *pFFTMag = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTMag ); AUD_Int32s *pFFTRe = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTRe ); AUD_Int32s *pFFTIm = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTIm ); AUD_Int32s *pFFTCleanRe = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTCleanRe ); AUD_Int32s *pFFTCleanIm = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTCleanIm ); // noise spectrum AUD_Double *pNoiseEn = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pNoiseEn ); AUD_Double *pNoiseB = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pNoiseB ); AUD_Double *pXPrev = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pXPrev ); AUD_Double *pAb = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pAb ); AUD_Double *pH = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pH ); AUD_Double *pGammak = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pGammak ); AUD_Double *pKsi = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pKsi ); AUD_Double *pLogSigmak = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pLogSigmak ); AUD_Double *pAlpha = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pAlpha ); AUD_Int32s *pLinToBark = (AUD_Int32s*)calloc( nSpecLen * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pLinToBark ); AUD_Int16s *pxOld = (AUD_Int16s*)calloc( frameOverlap * sizeof(AUD_Int16s), 1 ); AUD_ASSERT( pxOld ); AUD_Int16s *pxClean = (AUD_Int16s*)calloc( nFFT * sizeof(AUD_Int16s), 1 ); AUD_ASSERT( pxClean ); /* AUD_Int32s critBandEnds[22] = { 0, 100, 200, 300, 400, 510, 630, 770, 920, 1080, 1270, 1480, 1720, 2000, 2320, 2700, 3150, 3700, 4400, 5300, 6400, 7700 }; */ AUD_Int32s critFFTEnds[CRITICAL_BAND_NUM + 1] = { 0, 4, 7, 10, 13, 17, 21, 25, 30, 35, 41, 48, 56, 64, 75, 87, 101, 119, 141, 170, 205, 247, 257 }; // generate linear->bark transform mapping k = 0; for ( i = 0; i < CRITICAL_BAND_NUM; i++ ) { while ( k >= critFFTEnds[i] && k < critFFTEnds[i + 1] ) { pLinToBark[k] = i; k++; } } AUD_Double absThr[CRITICAL_BAND_NUM] = { 38, 31, 22, 18.5, 15.5, 13, 11, 9.5, 8.75, 7.25, 4.75, 2.75, 1.5, 0.5, 0, 0, 0, 0, 2, 7, 12, 15.5 }; AUD_Double dbOffset[CRITICAL_BAND_NUM]; AUD_Double sumn[CRITICAL_BAND_NUM]; AUD_Double spread[CRITICAL_BAND_NUM]; for ( i = 0; i < CRITICAL_BAND_NUM; i++ ) { absThr[i] = pow( 10., absThr[i] / 10. ) / nFFT / ( 65535. * 65535. ); dbOffset[i] = 10. + i; sumn[i] = 0.474 + i; spread[i] = pow( 10., ( 15.81 + 7.5 * sumn[i] - 17.5 * sqrt( 1. + sumn[i] * sumn[i] ) ) / 10. ); } AUD_Double dcGain[CRITICAL_BAND_NUM]; for ( i = 0; i < CRITICAL_BAND_NUM; i++ ) { dcGain[i] = 0.; for ( j = 0; j < CRITICAL_BAND_NUM; j++ ) { dcGain[i] += spread[MABS( i - j )]; } } AUD_Matrix exPatMatrix; exPatMatrix.rows = CRITICAL_BAND_NUM; exPatMatrix.cols = nSpecLen; exPatMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &exPatMatrix ); AUD_ASSERT( ret == 0 ); // excitation pattern AUD_Int32s index = 0; for ( i = 0; i < exPatMatrix.rows; i++ ) { AUD_Double *pExpatRow = exPatMatrix.pDouble + i * exPatMatrix.cols; for ( j = 0; j < exPatMatrix.cols; j++ ) { index = MABS( i - pLinToBark[j] ); pExpatRow[j] = spread[index]; } } AUD_Int32s frameNum = (inLen - frameSize) / frameStride + 1; AUD_ASSERT( frameNum > nNoiseFrame ); // compute noise mean for ( i = 0; i < nNoiseFrame; i++ ) { win16s_calc( hWin, pInBuf + i * frameSize, pFrame ); fft_mag( hFft, pFrame, frameSize, pFFTMag ); for ( j = 0; j < nSpecLen; j++ ) { pNoiseEn[j] += pFFTMag[j] / 32768. * pFFTMag[j] / 32768.; } } for ( j = 0; j < nSpecLen; j++ ) { pNoiseEn[j] /= nNoiseFrame; } // get cirtical band mean filtered noise power AUD_Int32s k1 = 0, k2 = 0; for ( i = 0; i < CRITICAL_BAND_NUM; i++ ) { k1 = k2; AUD_Double segSum = 0.; while ( k2 >= critFFTEnds[i] && k2 < critFFTEnds[i + 1] ) { segSum += pNoiseEn[k2]; k2++; } segSum /= ( k2 - k1 ); for ( m = k1; m < k2; m++ ) { pNoiseB[m] = segSum; } } #if 0 AUDLOG( "noise band spectrum:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pNoiseB[j] ); } AUDLOG( "\n" ); #endif AUD_Matrix frameMatrix; frameMatrix.rows = nSpecLen; frameMatrix.cols = 1; frameMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &frameMatrix ); AUD_ASSERT( ret == 0 ); AUD_Double *pFrameEn = frameMatrix.pDouble; AUD_Matrix xMatrix; xMatrix.rows = nSpecLen; xMatrix.cols = 1; xMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &xMatrix ); AUD_ASSERT( ret == 0 ); AUD_Double *pX = xMatrix.pDouble; AUD_Matrix cMatrix; cMatrix.rows = CRITICAL_BAND_NUM; cMatrix.cols = 1; cMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &cMatrix ); AUD_ASSERT( ret == 0 ); AUD_Double *pC = cMatrix.pDouble; AUD_Matrix tMatrix; tMatrix.rows = 1; tMatrix.cols = CRITICAL_BAND_NUM; tMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &tMatrix ); AUD_ASSERT( ret == 0 ); AUD_Double *pT = tMatrix.pDouble; AUD_Matrix tkMatrix; tkMatrix.rows = 1; tkMatrix.cols = nSpecLen; tkMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &tkMatrix ); AUD_ASSERT( ret == 0 ); AUD_Double *pTk = tkMatrix.pDouble; AUD_Double dB0[CRITICAL_BAND_NUM]; AUD_Double epsilon = pow( 2, -52 ); #define ESTIMATE_MASKTHRESH( sigMatrix, tkMatrix )\ do {\ AUD_Double *pSig = sigMatrix.pDouble; \ for ( m = 0; m < exPatMatrix.rows; m++ ) \ { \ AUD_Double suma = 0.; \ AUD_Double *pExpatRow = exPatMatrix.pDouble + m * exPatMatrix.cols; \ for ( n = 0; n < exPatMatrix.cols; n++ ) \ { \ suma += pExpatRow[n] * pSig[n]; \ } \ pC[m] = suma; \ } \ AUD_Double product = 1.; \ AUD_Double sum = 0.; \ for ( m = 0; m < sigMatrix.rows; m++ ) \ { \ product *= pSig[m]; \ sum += pSig[m]; \ } \ AUD_Double power = 1. / sigMatrix.rows;\ AUD_Double sfmDB = 10. * log10( pow( product, power ) / sum / sigMatrix.rows + epsilon ); \ AUD_Double alpha = AUD_MIN( 1., sfmDB / (-60.) ); \ for ( m = 0; m < tMatrix.cols; m++ ) \ { \ dB0[m] = dbOffset[m] * alpha + 5.5; \ pT[m] = pC[m] / pow( 10., dB0[m] / 10. ) / dcGain[m]; \ pT[m] = AUD_MAX( pT[m], absThr[m] ); \ } \ for ( m = 0; m < tkMatrix.cols; m++ ) \ { \ pTk[m] = pT[pLinToBark[m]]; \ } \ } while ( 0 ) AUD_Double aa = 0.98; AUD_Double mu = 0.98; AUD_Double eta = 0.15; AUD_Double vadDecision; k = 0; // start processing for ( i = 0; i < frameNum; i++ ) { win16s_calc( hWin, pInBuf + i * frameStride, pFrame ); fft_calc( hFft, pFrame, frameSize, pFFTRe, pFFTIm ); // compute SNR vadDecision = 0.; for ( j = 0; j < nSpecLen; j++ ) { pFrameEn[j] = pFFTRe[j] / 32768. * pFFTRe[j] / 32768. + pFFTIm[j] / 32768. * pFFTIm[j] / 32768.; pGammak[j] = AUD_MIN( pFrameEn[j] / pNoiseEn[j], 40. ); if ( i > 0 ) { pKsi[j] = aa * pXPrev[j] / pNoiseEn[j] + ( 1 - aa ) * AUD_MAX( pGammak[j] - 1., 0. ); } else { pKsi[j] = aa + ( 1. - aa ) * AUD_MAX( pGammak[j] - 1., 0. ); } pLogSigmak[j] = pGammak[j] * pKsi[j] / ( 1. + pKsi[j] ) - log( 1. + pKsi[j] ); vadDecision += ( j > 0 ? 2 : 1 ) * pLogSigmak[j]; } vadDecision /= nFFT; #if 0 AUDLOG( "X prev:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pXPrev[j] ); } AUDLOG( "\n" ); #endif #if 0 AUDLOG( "gamma k:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pGammak[j] ); } AUDLOG( "\n" ); #endif #if 0 AUDLOG( "ksi:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pKsi[j] ); } AUDLOG( "\n" ); #endif #if 0 AUDLOG( "log sigma k:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pLogSigmak[j] ); } AUDLOG( "\n" ); #endif // AUDLOG( "vadDecision: %.2f\n", vadDecision ); // re-estimate noise if ( vadDecision < eta ) { for ( j = 0; j < nSpecLen; j++ ) { pNoiseEn[j] = mu * pNoiseEn[j] + ( 1. - mu ) * pFrameEn[j]; } // re-estimate crital band based noise AUD_Int32s k1 = 0, k2 = 0; for ( int band = 0; band < CRITICAL_BAND_NUM; band++ ) { k1 = k2; AUD_Double segSum = 0.; while ( k2 >= critFFTEnds[band] && k2 < critFFTEnds[band + 1] ) { segSum += pNoiseEn[k2]; k2++; } segSum /= ( k2 - k1 ); for ( m = k1; m < k2; m++ ) { pNoiseB[m] = segSum; } } #if 0 AUDLOG( "noise band spectrum:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pNoiseB[j] ); } AUDLOG( "\n" ); #endif } for ( j = 0; j < nSpecLen; j++ ) { pX[j] = AUD_MAX( pFrameEn[j] - pNoiseEn[j], 0.001 ); pXPrev[j] = pFrameEn[j]; } ESTIMATE_MASKTHRESH( xMatrix, tkMatrix ); for ( int iter = 0; iter < 2; iter++ ) { for ( j = 0; j < nSpecLen; j++ ) { pAb[j] = pNoiseB[j] + pNoiseB[j] * pNoiseB[j] / pTk[j]; pFrameEn[j] = pFrameEn[j] * pFrameEn[j] / ( pFrameEn[j] + pAb[j] ); ESTIMATE_MASKTHRESH( frameMatrix, tkMatrix ); #if 0 showMatrix( &tMatrix ); #endif } } #if 0 AUDLOG( "tk:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pTk[j] ); } AUDLOG( "\n" ); #endif pAlpha[0] = ( pNoiseB[0] + pTk[0] ) * ( pNoiseB[0] / pTk[0] ); pH[0] = pFrameEn[0] / ( pFrameEn[0] + pAlpha[0] ); pXPrev[0] *= pH[0] * pH[0]; pFFTCleanRe[0] = 0; pFFTCleanIm[0] = 0; for ( j = 1; j < nSpecLen; j++ ) { pAlpha[j] = ( pNoiseB[j] + pTk[j] ) * ( pNoiseB[j] / pTk[j] ); pH[j] = pFrameEn[j] / ( pFrameEn[j] + pAlpha[j] ); pFFTCleanRe[j] = pFFTCleanRe[nFFT - j] = (AUD_Int32s)round( pH[j] * pFFTRe[j] ); pFFTCleanIm[j] = (AUD_Int32s)round( pH[j] * pFFTIm[j] ); pFFTCleanIm[nFFT - j] = -pFFTCleanIm[j]; pXPrev[j] *= pH[j] * pH[j]; } #if 0 AUDLOG( "denoise transfer function:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pH[j] ); } AUDLOG( "\n" ); #endif #if 0 AUDLOG( "clean FFT with phase:\n" ); for ( j = 0; j < nFFT; j++ ) { AUDLOG( "%d + j%d, ", pFFTCleanRe[j], pFFTCleanIm[j] ); } AUDLOG( "\n" ); #endif ifft_real( hIfft, pFFTCleanRe, pFFTCleanIm, nFFT, pxClean ); #if 0 AUDLOG( "clean speech:\n" ); for ( j = 0; j < nFFT; j++ ) { AUDLOG( "%d, ", pxClean[j] ); } AUDLOG( "\n" ); #endif for ( j = 0; j < frameStride; j++ ) { if ( j < frameOverlap ) { pOutBuf[k + j] = pxOld[j] + pxClean[j]; pxOld[j] = pxClean[frameStride + j]; } else { pOutBuf[k + j] = pxClean[j]; } } k += frameStride; cleanLen += frameStride; } // de-emphasis // sig_deemphasis( pOutBuf, pOutBuf, cleanLen ); win16s_free( &hWin ); fft_free( &hFft ); ifft_free( &hIfft ); free( pFrame ); free( pNoiseEn ); free( pNoiseB ); free( pFFTMag ); free( pFFTRe ); free( pFFTIm ); free( pXPrev ); free( pAb ); free( pH ); free( pFFTCleanRe ); free( pFFTCleanIm ); free( pxOld ); free( pxClean ); free( pGammak ); free( pKsi ); free( pLogSigmak ); free( pAlpha ); free( pLinToBark ); ret = createMatrix( &xMatrix ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &exPatMatrix ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &frameMatrix ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &cMatrix ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &tMatrix ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &tkMatrix ); AUD_ASSERT( ret == 0 ); return cleanLen; }
void create_windows(void) { char *r_a = gettext("realloc issue"); int nr = 0; if (w_stats) { delwin(w_stats); delwin(w_line1); delwin(w_slow); delwin(w_line2); delwin(w_fast); } #ifdef FW fft_free(); fft_init(max_x); #endif if (max_x > history_n) { history = (double *)realloc(history, sizeof(double) * max_x); if (!history) error_exit(r_a); history_temp = (double *)realloc(history_temp, sizeof(double) * max_x); if (!history_temp) error_exit(r_a); /* halve of it is enough really */ history_fft_magn = (double *)realloc(history_fft_magn, sizeof(double) * max_x); if (!history_fft_magn) error_exit(r_a); history_fft_phase = (double *)realloc(history_fft_phase, sizeof(double) * max_x); if (!history_fft_phase) error_exit(r_a); history_set = (char *)realloc(history_set, sizeof(char) * max_x); if (!history_set) error_exit(r_a); memset(&history[history_n], 0x00, (max_x - history_n) * sizeof(double)); memset(&history_set[history_n], 0x00, (max_x - history_n) * sizeof(char)); history_n = max_x; } if ((int)max_y > window_history_n) { slow_history = (char **)realloc(slow_history, sizeof(char *) * max_y); if (!slow_history) error_exit(r_a); fast_history = (char **)realloc(fast_history, sizeof(char *) * max_y); if (!fast_history) error_exit(r_a); memset(&slow_history[window_history_n], 0x00, (max_y - window_history_n) * sizeof(char *)); memset(&fast_history[window_history_n], 0x00, (max_y - window_history_n) * sizeof(char *)); window_history_n = max_y; } w_stats = newwin(stats_h, max_x, 0, 0); scrollok(w_stats, false); w_line1 = newwin(1, max_x, stats_h, 0); scrollok(w_line1, false); wnoutrefresh(w_line1); logs_n = max_y - (stats_h + 1 + 1); fast_n = logs_n * 11 / 20; slow_n = logs_n - fast_n; w_slow = newwin(slow_n, max_x, (stats_h + 1), 0); scrollok(w_slow, true); w_line2 = newwin(1, max_x, (stats_h + 1) + slow_n, 0); scrollok(w_line2, false); wnoutrefresh(w_line2); w_fast = newwin(fast_n, max_x, (stats_h + 1) + slow_n + 1, 0); scrollok(w_fast, true); wattron(w_line1, A_REVERSE); wattron(w_line2, A_REVERSE); for(nr=0; nr<max_x; nr++) { wprintw(w_line1, " "); wprintw(w_line2, " "); } wattroff(w_line2, A_REVERSE); wattroff(w_line1, A_REVERSE); wnoutrefresh(w_line1); wnoutrefresh(w_line2); doupdate(); signal(SIGWINCH, handler); }
int main(int argc, char *argv[]) { int i; int iter; double total_time, mflops; logical verified; char Class; if (argc == 1) { fprintf(stderr, "Usage: %s <kernel directory>\n", argv[0]); exit(-1); } //--------------------------------------------------------------------- // Run the entire problem once to make sure all data is touched. // This reduces variable startup costs, which is important for such a // short benchmark. The other NPB 2 implementations are similar. //--------------------------------------------------------------------- for (i = 1; i <= T_max; i++) { timer_clear(i); } setup(); setup_opencl(argc, argv); init_ui(&m_u0, &m_u1, &m_twiddle, dims[0], dims[1], dims[2]); compute_indexmap(&m_twiddle, dims[0], dims[1], dims[2]); compute_initial_conditions(&m_u1, dims[0], dims[1], dims[2]); fft_init(dims[0]); fft(1, &m_u1, &m_u0); //--------------------------------------------------------------------- // Start over from the beginning. Note that all operations must // be timed, in contrast to other benchmarks. //--------------------------------------------------------------------- for (i = 1; i <= T_max; i++) { timer_clear(i); } timer_start(T_total); if (timers_enabled) timer_start(T_setup); DTIMER_START(T_compute_im); compute_indexmap(&m_twiddle, dims[0], dims[1], dims[2]); DTIMER_STOP(T_compute_im); DTIMER_START(T_compute_ics); compute_initial_conditions(&m_u1, dims[0], dims[1], dims[2]); DTIMER_STOP(T_compute_ics); DTIMER_START(T_fft_init); fft_init(dims[0]); DTIMER_STOP(T_fft_init); if (timers_enabled) timer_stop(T_setup); if (timers_enabled) timer_start(T_fft); fft(1, &m_u1, &m_u0); if (timers_enabled) timer_stop(T_fft); for (iter = 1; iter <= niter; iter++) { if (timers_enabled) timer_start(T_evolve); evolve(&m_u0, &m_u1, &m_twiddle, dims[0], dims[1], dims[2]); if (timers_enabled) timer_stop(T_evolve); if (timers_enabled) timer_start(T_fft); fft(-1, &m_u1, &m_u1); if (timers_enabled) timer_stop(T_fft); if (timers_enabled) timer_start(T_checksum); checksum(iter, &m_u1, dims[0], dims[1], dims[2]); if (timers_enabled) timer_stop(T_checksum); } verify(NX, NY, NZ, niter, &verified, &Class); timer_stop(T_total); total_time = timer_read(T_total); if (total_time != 0.0) { mflops = 1.0e-6 * (double)NTOTAL * (14.8157 + 7.19641 * log((double)NTOTAL) + (5.23518 + 7.21113 * log((double)NTOTAL)) * niter) / total_time; } else { mflops = 0.0; } c_print_results("FT", Class, NX, NY, NZ, niter, total_time, mflops, " floating point", verified, NPBVERSION, COMPILETIME, CS1, CS2, CS3, CS4, CS5, CS6, CS7, clu_GetDeviceTypeName(device_type), device_name); if (timers_enabled) print_timers(); release_opencl(); fflush(stdout); return 0; }
static int realmain(void *carg) { unsigned arg = (uintptr_t)carg; /*c------------------------------------------------------------------- c-------------------------------------------------------------------*/ int i, ierr; /*------------------------------------------------------------------ c u0, u1, u2 are the main arrays in the problem. c Depending on the decomposition, these arrays will have different c dimensions. To accomodate all possibilities, we allocate them as c one-dimensional arrays and pass them to subroutines for different c views c - u0 contains the initial (transformed) initial condition c - u1 and u2 are working arrays c - indexmap maps i,j,k of u0 to the correct i^2+j^2+k^2 for the c time evolution operator. c-----------------------------------------------------------------*/ /*-------------------------------------------------------------------- c Large arrays are in common so that they are allocated on the c heap rather than the stack. This common block is not c referenced directly anywhere else. Padding is to avoid accidental c cache problems, since all array sizes are powers of two. c-------------------------------------------------------------------*/ static dcomplex u0[NZ][NY][NX]; static dcomplex pad1[3]; static dcomplex u1[NZ][NY][NX]; static dcomplex pad2[3]; static dcomplex u2[NZ][NY][NX]; static dcomplex pad3[3]; static int indexmap[NZ][NY][NX]; int iter; int nthreads = 1; double total_time, mflops; boolean verified; char class; omp_set_num_threads(arg); /*-------------------------------------------------------------------- c Run the entire problem once to make sure all data is touched. c This reduces variable startup costs, which is important for such a c short benchmark. The other NPB 2 implementations are similar. c-------------------------------------------------------------------*/ for (i = 0; i < T_MAX; i++) { timer_clear(i); } setup(); #pragma omp parallel { compute_indexmap(indexmap, dims[2]); #pragma omp single { compute_initial_conditions(u1, dims[0]); fft_init (dims[0][0]); } fft(1, u1, u0); } /* end parallel */ /*-------------------------------------------------------------------- c Start over from the beginning. Note that all operations must c be timed, in contrast to other benchmarks. c-------------------------------------------------------------------*/ for (i = 0; i < T_MAX; i++) { timer_clear(i); } timer_start(T_TOTAL); if (TIMERS_ENABLED == TRUE) timer_start(T_SETUP); #pragma omp parallel private(iter) firstprivate(niter) { compute_indexmap(indexmap, dims[2]); #pragma omp single { compute_initial_conditions(u1, dims[0]); fft_init (dims[0][0]); } if (TIMERS_ENABLED == TRUE) { #pragma omp master timer_stop(T_SETUP); } if (TIMERS_ENABLED == TRUE) { #pragma omp master timer_start(T_FFT); } fft(1, u1, u0); if (TIMERS_ENABLED == TRUE) { #pragma omp master timer_stop(T_FFT); } for (iter = 1; iter <= niter; iter++) { if (TIMERS_ENABLED == TRUE) { #pragma omp master timer_start(T_EVOLVE); } evolve(u0, u1, iter, indexmap, dims[0]); if (TIMERS_ENABLED == TRUE) { #pragma omp master timer_stop(T_EVOLVE); } if (TIMERS_ENABLED == TRUE) { #pragma omp master timer_start(T_FFT); } fft(-1, u1, u2); if (TIMERS_ENABLED == TRUE) { #pragma omp master timer_stop(T_FFT); } if (TIMERS_ENABLED == TRUE) { #pragma omp master timer_start(T_CHECKSUM); } checksum(iter, u2, dims[0]); if (TIMERS_ENABLED == TRUE) { #pragma omp master timer_stop(T_CHECKSUM); } } #pragma omp single verify(NX, NY, NZ, niter, &verified, &class); #if defined(_OPENMP) #pragma omp master nthreads = omp_get_num_threads(); #endif /* _OPENMP */ } /* end parallel */ timer_stop(T_TOTAL); total_time = timer_read(T_TOTAL); if( total_time != 0.0) { mflops = 1.0e-6*(double)(NTOTAL) * (14.8157+7.19641*log((double)(NTOTAL)) + (5.23518+7.21113*log((double)(NTOTAL)))*niter) /total_time; } else { mflops = 0.0; } #ifdef BOMP backend_create_time(arg); #endif printf("Computetime %d %f\n", arg, total_time); printf("client done\n"); /* c_print_results("FT", class, NX, NY, NZ, niter, nthreads, */ /* total_time, mflops, " floating point", verified, */ /* NPBVERSION, COMPILETIME, */ /* CS1, CS2, CS3, CS4, CS5, CS6, CS7); */ if (TIMERS_ENABLED == TRUE) print_timers(); }
int main(int argc, char **argv) { // Self-test to check correct computation of values srand(time(NULL)); int i; for (i = 2; i <= 10; i++) { // Test FFT sizes 4, 8, 16, ..., 512, 1024 if (test_fft_log_error(1 << i) > -10) { printf("Self-test failed\n"); return 1; } } printf("Self-test passed\n"); // Speed benchmark const int64_t TARGET_TIME = 100000000; // In nanoseconds const int TRIALS = 10; printf("%9s %s\n", "Size", "Time per FFT (ns)"); size_t n; for (n = 4; n <= (size_t)1 << 26; n *= 2) { // Initialize data sets void *fftTables = fft_init(n); double *real = random_reals(n); double *imag = random_reals(n); if (fftTables == NULL || real == NULL || imag == NULL) { printf("Memory allocation failed\n"); return 1; } // Determine number of iterations to run to spend TARGET_TIME uint64_t iterations = 1; while (1) { int64_t time = benchmark_time(fftTables, real, imag, iterations); if (time >= TARGET_TIME) { iterations = (uint64_t)((double)TARGET_TIME / time * iterations + 0.5); if (iterations == 0) iterations = 1; break; } iterations *= 2; } // Run trials and store timing double *runtimes = malloc(TRIALS * sizeof(double)); int i; for (i = 0; i < TRIALS; i++) runtimes[i] = (double)benchmark_time(fftTables, real, imag, iterations) / iterations; fft_destroy(fftTables); free(real); free(imag); // Compute statistics double min = 1e300; double sum = 0; for (i = 0; i < TRIALS; i++) { double t = runtimes[i]; if (t < min) min = t; sum += t; } double mean = sum / TRIALS; double sqrdiffsum = 0; for (i = 0; i < TRIALS; i++) { double t = runtimes[i]; sqrdiffsum += (t - mean) * (t - mean); } double stddev = sqrt(sqrdiffsum / TRIALS); free(runtimes); printf("%9zu min=%"PRIu64" mean=%"PRIu64" sd=%.2f%%\n", n, (uint64_t)(min + 0.5), (uint64_t)(mean + 0.5), stddev / mean * 100); } return 0; }
/* Manage the whole stuff. */ void makeit() { checkstruct *check; picstruct *dfield, *field,*pffield[MAXFLAG], *wfield,*dwfield; catstruct *imacat; tabstruct *imatab; patternstruct *pattern; static time_t thetime1, thetime2; struct tm *tm; unsigned int modeltype; int nflag[MAXFLAG], nparam2[2], i, nok, ntab, next, ntabmax, forcextflag, nima0,nima1, nweight0,nweight1, npsf0,npsf1, npat,npat0; next = 0; nok = 1; /* Processing start date and time */ dtime = counter_seconds(); thetimet = time(NULL); tm = localtime(&thetimet); sprintf(prefs.sdate_start,"%04d-%02d-%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday); sprintf(prefs.stime_start,"%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); NFPRINTF(OUTPUT, ""); QPRINTF(OUTPUT, "----- %s %s started on %s at %s with %d thread%s\n\n", BANNER, MYVERSION, prefs.sdate_start, prefs.stime_start, prefs.nthreads, prefs.nthreads>1? "s":""); /* Initialize globals variables */ initglob(); NFPRINTF(OUTPUT, "Setting catalog parameters"); readcatparams(prefs.param_name); useprefs(); /* update things accor. to prefs parameters */ /* Check if a specific extension should be loaded */ /* Never true for an NDF, although we could through all NDFs in a container, */ /* so we make selectext go away. */ nima0 = -1; forcextflag = 0; /* Do the same for other data (but do not force single extension mode) */ nima1 = -1; /* selectext(prefs.image_name[1]) */ nweight0 = -1; /* selectext(prefs.wimage_name[0]) */ nweight1 = -1; /* selectext(prefs.wimage_name[1]) */ if (prefs.dpsf_flag) { npsf0 = -1; /* selectext(prefs.psf_name[0]) */ npsf1 = -1; /* selectext(prefs.psf_name[1]) */ } else npsf0 = -1; /* selectext(prefs.psf_name[0]) */ for (i=0; i<prefs.nfimage_name; i++) nflag[i] = -1; /* selectext(prefs.fimage_name[i]) */ if (prefs.psf_flag) { /*-- Read the first PSF extension to set up stuff such as context parameters */ NFPRINTF(OUTPUT, "Reading PSF information"); if (prefs.dpsf_flag) { thedpsf = psf_load(prefs.psf_name[0],nima0<0? 1 :(npsf0<0? 1:npsf0)); thepsf = psf_load(prefs.psf_name[1], nima1<0? 1 :(npsf1<0? 1:npsf1)); } else thepsf = psf_load(prefs.psf_name[0], nima0<0? 1 :(npsf0<0? 1:npsf0)); /*-- Need to check things up because of PSF context parameters */ updateparamflags(); useprefs(); } if (prefs.prof_flag) { #ifdef USE_MODEL fft_init(prefs.nthreads); /* Create profiles at full resolution */ NFPRINTF(OUTPUT, "Preparing profile models"); modeltype = (FLAG(obj2.prof_offset_flux)? MODEL_BACK : MODEL_NONE) |(FLAG(obj2.prof_dirac_flux)? MODEL_DIRAC : MODEL_NONE) |(FLAG(obj2.prof_spheroid_flux)? (FLAG(obj2.prof_spheroid_sersicn)? MODEL_SERSIC : MODEL_DEVAUCOULEURS) : MODEL_NONE) |(FLAG(obj2.prof_disk_flux)? MODEL_EXPONENTIAL : MODEL_NONE) |(FLAG(obj2.prof_bar_flux)? MODEL_BAR : MODEL_NONE) |(FLAG(obj2.prof_arms_flux)? MODEL_ARMS : MODEL_NONE); theprofit = profit_init(thepsf, modeltype); changecatparamarrays("VECTOR_MODEL", &theprofit->nparam, 1); changecatparamarrays("VECTOR_MODELERR", &theprofit->nparam, 1); nparam2[0] = nparam2[1] = theprofit->nparam; changecatparamarrays("MATRIX_MODELERR", nparam2, 2); if (prefs.dprof_flag) thedprofit = profit_init(thedpsf, modeltype); if (prefs.pattern_flag) { npat0 = prefs.prof_disk_patternvectorsize; if (npat0<prefs.prof_disk_patternmodvectorsize) npat0 = prefs.prof_disk_patternmodvectorsize; if (npat0<prefs.prof_disk_patternargvectorsize) npat0 = prefs.prof_disk_patternargvectorsize; /*---- Do a copy of the original number of pattern components */ prefs.prof_disk_patternncomp = npat0; pattern = pattern_init(theprofit, prefs.pattern_type, npat0); if (FLAG(obj2.prof_disk_patternvector)) { npat = pattern->size[2]; changecatparamarrays("DISK_PATTERN_VECTOR", &npat, 1); } if (FLAG(obj2.prof_disk_patternmodvector)) { npat = pattern->ncomp*pattern->nfreq; changecatparamarrays("DISK_PATTERNMOD_VECTOR", &npat, 1); } if (FLAG(obj2.prof_disk_patternargvector)) { npat = pattern->ncomp*pattern->nfreq; changecatparamarrays("DISK_PATTERNARG_VECTOR", &npat, 1); } pattern_end(pattern); } QPRINTF(OUTPUT, "Fitting model: "); for (i=0; i<theprofit->nprof; i++) { if (i) QPRINTF(OUTPUT, "+"); QPRINTF(OUTPUT, "%s", theprofit->prof[i]->name); } QPRINTF(OUTPUT, "\n"); if (FLAG(obj2.prof_concentration)|FLAG(obj2.prof_concentration)) { thepprofit = profit_init(thepsf, MODEL_DIRAC); theqprofit = profit_init(thepsf, MODEL_EXPONENTIAL); } #else error(EXIT_FAILURE, "*Error*: model-fitting is not supported in this build.\n", " Please check your configure options"); #endif } if (prefs.filter_flag) { NFPRINTF(OUTPUT, "Reading detection filter"); getfilter(prefs.filter_name); /* get the detection filter */ } if (FLAG(obj2.sprob)) { NFPRINTF(OUTPUT, "Initializing Neural Network"); neurinit(); NFPRINTF(OUTPUT, "Reading Neural Network Weights"); getnnw(); } if (prefs.somfit_flag) { int margin; thesom = som_load(prefs.som_name); if ((margin=(thesom->inputsize[1]+1)/2) > prefs.cleanmargin) prefs.cleanmargin = margin; if (prefs.somfit_vectorsize>thesom->neurdim) { prefs.somfit_vectorsize = thesom->neurdim; sprintf(gstr,"%d", prefs.somfit_vectorsize); warning("Dimensionality of the SOM-fit vector limited to ", gstr); } } /* Prepare growth-curve buffer */ if (prefs.growth_flag) initgrowth(); /* Allocate memory for multidimensional catalog parameter arrays */ alloccatparams(); useprefs(); /*-- Init the CHECK-images */ if (prefs.check_flag) { checkenum c; NFPRINTF(OUTPUT, "Initializing check-image(s)"); for (i=0; i<prefs.ncheck_type; i++) if ((c=prefs.check_type[i]) != CHECK_NONE) { if (prefs.check[c]) error(EXIT_FAILURE,"*Error*: 2 CHECK_IMAGEs cannot have the same ", " CHECK_IMAGE_TYPE"); prefs.check[c] = initcheck(prefs.check_name[i], prefs.check_type[i], next); free(prefs.check_name[i]); } } NFPRINTF(OUTPUT, "Initializing catalog"); initcat(); /* Initialize XML data */ if (prefs.xml_flag || prefs.cat_type==ASCII_VO) init_xml(next); /* Go through all images */ /* for all images in an MEF */ /*---- Initial time measurement*/ time(&thetime1); thecat.currext = nok+1; dfield = field = wfield = dwfield = NULL; /*---- Init the Detection and Measurement-images */ if (prefs.dimage_flag) { dfield = newfield(prefs.image_name[0], DETECT_FIELD, nok); field = newfield(prefs.image_name[1], MEASURE_FIELD, nok); if ((field->width!=dfield->width) || (field->height!=dfield->height)) error(EXIT_FAILURE, "*Error*: Frames have different sizes",""); /*---- Prepare interpolation */ if (prefs.dweight_flag && prefs.interp_type[0] == INTERP_ALL) init_interpolate(dfield, -1, -1); if (prefs.interp_type[1] == INTERP_ALL) init_interpolate(field, -1, -1); } else { field = newfield(prefs.image_name[0], DETECT_FIELD | MEASURE_FIELD, nok); /*-- Prepare interpolation */ if ((prefs.dweight_flag || prefs.weight_flag) && prefs.interp_type[0] == INTERP_ALL) init_interpolate(field, -1, -1); /* 0.0 or anything else */ } /*-- Init the WEIGHT-images */ if (prefs.dweight_flag || prefs.weight_flag) { weightenum wtype; PIXTYPE interpthresh; if (prefs.nweight_type>1) { /*------ Double-weight-map mode */ if (prefs.weight_type[1] != WEIGHT_NONE) { /*-------- First: the "measurement" weights */ wfield = newweight(prefs.wimage_name[1],field,prefs.weight_type[1], nok); wtype = prefs.weight_type[1]; interpthresh = prefs.weight_thresh[1]; /*-------- Convert the interpolation threshold to variance units */ weight_to_var(wfield, &interpthresh, 1); wfield->weight_thresh = interpthresh; if (prefs.interp_type[1] != INTERP_NONE) init_interpolate(wfield, prefs.interp_xtimeout[1], prefs.interp_ytimeout[1]); } /*------ The "detection" weights */ if (prefs.weight_type[0] != WEIGHT_NONE) { interpthresh = prefs.weight_thresh[0]; if (prefs.weight_type[0] == WEIGHT_FROMINTERP) { dwfield=newweight(prefs.wimage_name[0],wfield,prefs.weight_type[0], nok); weight_to_var(wfield, &interpthresh, 1); } else { dwfield = newweight(prefs.wimage_name[0], dfield?dfield:field, prefs.weight_type[0], nok); weight_to_var(dwfield, &interpthresh, 1); } dwfield->weight_thresh = interpthresh; if (prefs.interp_type[0] != INTERP_NONE) init_interpolate(dwfield, prefs.interp_xtimeout[0], prefs.interp_ytimeout[0]); } } else { /*------ Single-weight-map mode */ wfield = newweight(prefs.wimage_name[0], dfield?dfield:field, prefs.weight_type[0], nok); wtype = prefs.weight_type[0]; interpthresh = prefs.weight_thresh[0]; /*------ Convert the interpolation threshold to variance units */ weight_to_var(wfield, &interpthresh, 1); wfield->weight_thresh = interpthresh; if (prefs.interp_type[0] != INTERP_NONE) init_interpolate(wfield, prefs.interp_xtimeout[0], prefs.interp_ytimeout[0]); } } /*-- Init the FLAG-images */ for (i=0; i<prefs.nimaflag; i++) { pffield[i] = newfield(prefs.fimage_name[i], FLAG_FIELD, nok); if ((pffield[i]->width!=field->width) || (pffield[i]->height!=field->height)) error(EXIT_FAILURE, "*Error*: Incompatible FLAG-map size in ", prefs.fimage_name[i]); } /*-- Compute background maps for `standard' fields */ QPRINTF(OUTPUT, dfield? "Measurement image:" : "Detection+Measurement image: "); makeback(field, wfield, prefs.wscale_flag[1]); QPRINTF(OUTPUT, (dfield || (dwfield&&dwfield->flags^INTERP_FIELD))? "(M) " "Background: %-10g RMS: %-10g / Threshold: %-10g \n" : "(M+D) " "Background: %-10g RMS: %-10g / Threshold: %-10g \n", field->backmean, field->backsig, (field->flags & DETECT_FIELD)? field->dthresh: field->thresh); if (dfield) { QPRINTF(OUTPUT, "Detection image: "); makeback(dfield, dwfield? dwfield : (prefs.weight_type[0] == WEIGHT_NONE?NULL:wfield), prefs.wscale_flag[0]); QPRINTF(OUTPUT, "(D) " "Background: %-10g RMS: %-10g / Threshold: %-10g \n", dfield->backmean, dfield->backsig, dfield->dthresh); } else if (dwfield && dwfield->flags^INTERP_FIELD) { makeback(field, dwfield, prefs.wscale_flag[0]); QPRINTF(OUTPUT, "(D) " "Background: %-10g RMS: %-10g / Threshold: %-10g \n", field->backmean, field->backsig, field->dthresh); } /*-- For interpolated weight-maps, copy the background structure */ if (dwfield && dwfield->flags&(INTERP_FIELD|BACKRMS_FIELD)) copyback(dwfield->reffield, dwfield); if (wfield && wfield->flags&(INTERP_FIELD|BACKRMS_FIELD)) copyback(wfield->reffield, wfield); /*-- Prepare learn and/or associations */ if (prefs.assoc_flag) init_assoc(field); /* initialize assoc tasks */ /*-- Update the CHECK-images */ if (prefs.check_flag) for (i=0; i<MAXCHECK; i++) if ((check=prefs.check[i])) reinitcheck(field, check); if (!forcextflag && nok>1) { if (prefs.psf_flag) { /*------ Read other PSF extensions */ NFPRINTF(OUTPUT, "Reading PSF information"); psf_end(thepsf, thepsfit); if (prefs.dpsf_flag) { psf_end(thedpsf, thedpsfit); thedpsf = psf_load(prefs.psf_name[0], nok); thepsf = psf_load(prefs.psf_name[1], nok); } else thepsf = psf_load(prefs.psf_name[0], nok); } #ifdef USE_MODEL if (prefs.prof_flag) { /*------ Create profiles at full resolution */ profit_end(theprofit); theprofit = profit_init(thepsf, modeltype); if (prefs.dprof_flag) { profit_end(thedprofit); thedprofit = profit_init(thedpsf, modeltype); } if (prefs.pattern_flag) { pattern = pattern_init(theprofit, prefs.pattern_type, npat0); pattern_end(pattern); } if (FLAG(obj2.prof_concentration)|FLAG(obj2.prof_concentration)) { profit_end(thepprofit); profit_end(theqprofit); thepprofit = profit_init(thepsf, MODEL_DIRAC); theqprofit = profit_init(thepsf, MODEL_EXPONENTIAL); } } #endif } /*-- Initialize PSF contexts and workspace */ if (prefs.psf_flag) { psf_readcontext(thepsf, field); psf_init(); if (prefs.dpsf_flag) { psf_readcontext(thepsf, dfield); psf_init(); } } /*-- Copy field structures to static ones (for catalog info) */ if (dfield) { thefield1 = *field; thefield2 = *dfield; } else thefield1 = thefield2 = *field; if (wfield) { thewfield1 = *wfield; thewfield2 = dwfield? *dwfield: *wfield; } else if (dwfield) thewfield2 = *dwfield; reinitcat(field); /*-- Start the extraction pipeline */ NFPRINTF(OUTPUT, "Scanning image"); scanimage(field, dfield, pffield, prefs.nimaflag, wfield, dwfield); NFPRINTF(OUTPUT, "Closing files"); /*-- Finish the current CHECK-image processing */ if (prefs.check_flag) for (i=0; i<MAXCHECK; i++) if ((check=prefs.check[i])) reendcheck(field, check); /*-- Final time measurements*/ if (time(&thetime2)!=-1) { if (!strftime(thecat.ext_date, 12, "%d/%m/%Y", localtime(&thetime2))) error(EXIT_FAILURE, "*Internal Error*: Date string too long ",""); if (!strftime(thecat.ext_time, 10, "%H:%M:%S", localtime(&thetime2))) error(EXIT_FAILURE, "*Internal Error*: Time/date string too long ",""); thecat.ext_elapsed = difftime(thetime2, thetime1); } reendcat(); /* Update XML data */ if (prefs.xml_flag || prefs.cat_type==ASCII_VO) update_xml(&thecat, dfield? dfield:field, field, dwfield? dwfield:wfield, wfield); /*-- Close ASSOC routines */ end_assoc(field); for (i=0; i<prefs.nimaflag; i++) endfield(pffield[i]); endfield(field); if (dfield) endfield(dfield); if (wfield) endfield(wfield); if (dwfield) endfield(dwfield); QPRINTF(OUTPUT, " Objects: detected %-8d / sextracted %-8d \n\n", thecat.ndetect, thecat.ntotal); /* End look around all images in an MEF */ if (nok<0) error(EXIT_FAILURE, "Not enough valid FITS image extensions in ", prefs.image_name[0]); NFPRINTF(OUTPUT, "Closing files"); /* End CHECK-image processing */ if (prefs.check_flag) for (i=0; i<MAXCHECK; i++) { if ((check=prefs.check[i])) endcheck(check); prefs.check[i] = NULL; } if (prefs.filter_flag) endfilter(); if (prefs.somfit_flag) som_end(thesom); if (prefs.growth_flag) endgrowth(); #ifdef USE_MODEL if (prefs.prof_flag) { profit_end(theprofit); if (prefs.dprof_flag) profit_end(thedprofit); if (FLAG(obj2.prof_concentration)|FLAG(obj2.prof_concentration)) { profit_end(thepprofit); profit_end(theqprofit); } fft_end(); } #endif if (prefs.psf_flag) psf_end(thepsf, thepsfit); if (prefs.dpsf_flag) psf_end(thedpsf, thedpsfit); if (FLAG(obj2.sprob)) neurclose(); /* Processing end date and time */ thetimet2 = time(NULL); tm = localtime(&thetimet2); sprintf(prefs.sdate_end,"%04d-%02d-%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday); sprintf(prefs.stime_end,"%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); prefs.time_diff = counter_seconds() - dtime; /* Write XML */ if (prefs.xml_flag) write_xml(prefs.xml_name); endcat((char *)NULL); if (prefs.xml_flag || prefs.cat_type==ASCII_VO) end_xml(); /* Free FITS headers (now catalogues are closed). */ if (field->fitsheadsize > 0) { free(field->fitshead); } return; }
void initialize() { int i,j ; idum = -long( time(0) ) ; // 9 ; // // read_input() ; if ( phiP + phiHA + phiHB + phiHC> 1.0 ) die("Invalid volume fractions!\n") ; for(i =0 ; i<4 ; i++) wall_lamb[i] *= kappa; lagrange_weights = 0 ; spline_weights = 1 ; mem_use = 0. ; M = 1 ; V = 1.0 ; grid_per_partic = 1 ; for ( j=0 ; j<Dim ; j++ ) { V *= L[j] ; M *= Nx[j] ; dx[j] = L[j] / double( Nx[j] ) ; Lh[j] = 0.5 * L[j] ; grid_per_partic *= ( pmeorder + 1 ) ; printf("dx: %lf\n" , dx[j] ) ; } gvol = V / double( M ) ; // This is used for density fields // ntypes = Nsp+1 ; cout<<"ntypes "<<ntypes<<endl; Rg = sqrt( double( Nda + Ndb ) / 6.0 ) ; Rg3 = Rg * Rg * Rg ; Range = Rg *0.2; cout<<"Range "<<Range<<endl; Range2 = Range *Range; rho0 = C * double( Nda + Ndb ) / Rg3 ; chiAB = chiAB / double( Nda + Ndb ) ; chiAC = chiAC / double( Nda + Ndb ) ; chiBC = chiBC / double( Nda + Ndb ) ; kappa = kappa / double( Nda + Ndb ) ; kappa_p = kappa_p / double( Nda + Ndb ) ; cout<<"kN "<<kappa*( Nda + Ndb )<<" chiABN "<<( Nda + Ndb )*chiAB<<endl; double rdc_V = V * (L[2]-wall_thick*2)/L[2]; nD = int( ( 1.0 - phiHA - phiHB - phiP -phiHC) * rho0 * rdc_V / ( Nda + Ndb ) * CG_ratio ) ; nA = int( phiHA * rho0 * rdc_V / Nha * CG_ratio ) ; nB = int( phiHB * rho0 * rdc_V / Nhb * CG_ratio ) ; nC = int( phiHC * rho0 * rdc_V / Nhc * CG_ratio ) ; Vp = rho0 ; if ( Dim == 2 ) Vp *= PI * Rp * Rp ; else if ( Dim == 3 ) Vp *= 4.0 * PI * Rp * Rp * Rp / 3.0 ; //////////////////////////////////////// // Initialize nanoparticle parameters // //////////////////////////////////////// Diff[2] *= 1.0 / Vp ; p_m = Vp; Diff_rot *= 1.0 / Vp / Rp / Rp ; nP = nsP = 0.0 ; if ( phiP > 0.0 ) { if ( sigma > 0.0 ) { if ( Dim == 2 ) ng_per_partic = int( sigma * PI * Rp * 2.0 ) ; else if ( Dim == 3 ) ng_per_partic = int ( sigma * 4.0 * PI * Rp * Rp ) ; } else ng_per_partic = 0 ; nP = int( phiP * rho0 * rdc_V /(Vp+ Ng * ng_per_partic * CG_ratio )) ; //nP = int( phiP * rho0 * V /(Vp)); printf("nP = %d particles with %d grafted chains per particle\n" , nP , ng_per_partic ) ; } //nD = int( (( 1.0 - phiHA - phiHB - phiP ) * rho0 * V - nP*Ng*ng_per_partic )/ ( Nda + Ndb ) * CG_ratio ) ; //nA = int( phiHA * rho0 * V / Nha * CG_ratio ) ; //nB = int( phiHB * rho0 * V / Nhb * CG_ratio ) ; nsD = nD * (Nda + Ndb) ; nsA = nA * Nha ; nsB = nB * Nhb ; nsC = nC * Nhc ; nsP = nP * ( 1 + ng_per_partic * ( Ng + 1 ) ) ; // + 1 because of the graft site printf("Input rho0: %lf , " , rho0 ) ; rho0 = ( nD * (Nda + Ndb ) + nA * Nha + nB * Nhb + nC*Nhc+nP * (Vp + ng_per_partic * Ng ) ) / rdc_V / CG_ratio ; printf("actual rho0: %lf\n" , rho0 ) ; printf("\nnD: %d\nnA: %d\nnB: %d\nnC: %d\nnP: %d\n\n" , nD, nA, nB, nC, nP ) ; // Derived quantities // nstot = nA * Nha + nB * Nhb + nD * ( Nda + Ndb ) + nC*Nhc + nP * ( 1 + ng_per_partic * ( Ng + 1 ) ) ; step = 0 ; num_averages = 0.0 ; buff_ind = 0 ; if ( stress_freq > print_freq ) stress_freq = print_freq ; if ( stress_freq > 0 ) buff_size = print_freq / stress_freq + 1; else buff_size = 0 ; I = complex<double>( 0.0 , 1.0 ) ; printf("Total segments: %d\n" , nstot ) ; printf("grid_vol: %lf\n" , gvol ) ; printf("Particles per grid point: %lf\n" , double(nstot) / double(M) ) ; fft_init() ; printf("FFTW-MPI Initialized\n") ; fflush( stdout ) ; allocate() ; for(j=0; j<ntypes; j++){ if(Diff[j] > 0 ){ verlet_a[j] = (1 - delt/2.0/Diff[j]/(j==2 ? p_m: 1.0))/(1 + delt/2.0/Diff[j]/(j==2 ? p_m: 1.0)); verlet_b[j] = 1.0 / (1 + delt/2.0/Diff[j]/(j==2 ? p_m: 1.0)); } else{ verlet_a[j] = -1; verlet_b[j] = 0; } printf("verlet_a: %lf and verlet_b: %lf for type: %d\n" , verlet_a[j],verlet_b[j], j ) ;fflush(stdout) ; }//ntypes printf("Memory allocated: %lf MB\n" , mem_use / 1.0E6 ) ; fflush(stdout) ; initialize_configuration() ; //calc_A();//calc A and dAdang printf("Initial config generated\n") ; fflush(stdout) ; charge_grid() ; printf("grid charged\n"); fflush(stdout); initialize_potential() ; printf("potentials initialized, written\n") ; fflush( stdout ) ; }
AUD_Int32s denoise_wiener( AUD_Int16s *pInBuf, AUD_Int16s *pOutBuf, AUD_Int32s inLen ) { Fft_16s *hFft = NULL; Ifft_16s *hIfft = NULL; AUD_Window16s *hWin = NULL; _VAD_Nest vadState; AUD_Int32s frameSize = 512; AUD_Int32s frameStride = 256; AUD_Int32s nFFT = frameSize; AUD_Int32s nSpecLen = nFFT / 2 + 1; AUD_Int32s nNoiseFrame = (AUD_Int32s)( ( 0.25 * SAMPLE_RATE - frameSize ) / frameStride + 1 ); AUD_Int32s i, j, k; AUD_Int32s cleanLen = 0; vadState.meanEn = 280; vadState.nbSpeechFrame = 0; vadState.hangOver = 0; AUD_Int16s *pFrame = (AUD_Int16s*)calloc( frameSize * sizeof(AUD_Int16s), 1 ); AUD_ASSERT( pFrame ); AUD_Double *pNoiseMean = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pNoiseMean ); AUD_Double *pLamdaD = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pLamdaD ); AUD_Double *pXi = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pXi ); AUD_Double *pGamma = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pGamma ); AUD_Double *pG = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pG ); AUD_Double *pGammaNew = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pGammaNew ); for ( j = 0; j < nSpecLen; j++ ) { pG[j] = 1.; pGamma[j] = 1.; } // FFT AUD_Int32s *pFFTMag = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTMag ); AUD_Int32s *pFFTRe = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTRe ); AUD_Int32s *pFFTIm = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTIm ); AUD_Double *pFFTCleanMag = (AUD_Double*)calloc( nFFT * sizeof(AUD_Double), 1 ); AUD_ASSERT( pFFTCleanMag ); AUD_Int32s *pFFTCleanRe = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTCleanRe ); AUD_Int32s *pFFTCleanIm = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTCleanIm ); AUD_Int16s *pxClean = (AUD_Int16s*)calloc( nFFT * sizeof(AUD_Int16s), 1 ); AUD_ASSERT( pxClean ); memset( pOutBuf, 0, inLen * sizeof(AUD_Int16s) ); // init hamming module win16s_init( &hWin, AUD_WIN_HAMM, frameSize, 14 ); AUD_ASSERT( hWin ); // init fft handle fft_init( &hFft, nFFT, 15 ); AUD_ASSERT( hFft ); // init ifft handle ifft_init( &hIfft, nFFT, 15 ); AUD_ASSERT( hIfft ); // noise manipulation for ( i = 0; (i < nNoiseFrame) && ( (i * frameStride + frameSize) < inLen ); i++ ) { win16s_calc( hWin, pInBuf + i * frameStride, pFrame ); fft_mag( hFft, pFrame, frameSize, pFFTMag ); for ( j = 0; j < nSpecLen; j++ ) { pNoiseMean[j] += (AUD_Double)pFFTMag[j]; pLamdaD[j] += (AUD_Double)pFFTMag[j] * (AUD_Double)pFFTMag[j]; } } // compute noise mean for ( j = 0; j < nSpecLen; j++ ) { pNoiseMean[j] /= nNoiseFrame; pLamdaD[j] /= nNoiseFrame; } AUD_Int32s vadFlag = 0; AUD_Double noiseLen = 9.; AUD_Double alpha = 0.99; k = 0; for ( i = 0; (i * frameStride + frameSize) < inLen; i++ ) { win16s_calc( hWin, pInBuf + i * frameStride, pFrame ); fft_calc( hFft, pFrame, frameSize, pFFTRe, pFFTIm ); for ( j = 0; j < nSpecLen; j++ ) { pFFTMag[j] = (AUD_Int32s)round( sqrt( (AUD_Double)pFFTRe[j] * pFFTRe[j] + (AUD_Double)pFFTIm[j] * pFFTIm[j] ) ); } #if 0 AUDLOG( "noisy FFT:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%d, ", pFFTMag[j] ); } AUDLOG( "\n" ); #endif vadFlag = vad_nest( &vadState, pFrame, frameSize ); if ( vadFlag == 0 ) { for ( j = 0; j < nSpecLen; j++ ) { pNoiseMean[j] = ( noiseLen * pNoiseMean[j] + (AUD_Double)pFFTMag[j] ) / ( noiseLen + 1. ); pLamdaD[j] = ( noiseLen * pLamdaD[j] + (AUD_Double)pFFTMag[j] * pFFTMag[j] ) / ( noiseLen + 1. ); } } for ( j = 0; j < nSpecLen; j++ ) { pGammaNew[j] = (AUD_Double)pFFTMag[j] * pFFTMag[j] / pLamdaD[j]; pXi[j] = alpha * pG[j] * pG[j] * pGamma[j] + ( 1. - alpha ) * AUD_MAX( pGammaNew[j] - 1., 0. ); pGamma[j] = pGammaNew[j]; pG[j] = pXi[j] / ( pXi[j] + 1. ); pFFTCleanMag[j] = pG[j] * pFFTMag[j]; } #if 0 AUDLOG( "clean FFT:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pFFTCleanMag[j] ); } AUDLOG( "\n" ); #endif // combine to real/im part of IFFT pFFTCleanRe[0] = pFFTCleanMag[0]; pFFTCleanIm[0] = 0; AUD_Double costheta, sintheta; for ( j = 1; j < nSpecLen; j++ ) { if ( pFFTMag[j] != 0 ) { costheta = (AUD_Double)pFFTRe[j] / (AUD_Double)pFFTMag[j]; sintheta = (AUD_Double)pFFTIm[j] / (AUD_Double)pFFTMag[j]; pFFTCleanRe[nFFT - j] = pFFTCleanRe[j] = (AUD_Int32s)round( costheta * pFFTCleanMag[j] ); pFFTCleanIm[j] = (AUD_Int32s)round( sintheta * pFFTCleanMag[j] ); pFFTCleanIm[nFFT - j] = -pFFTCleanIm[j]; } else { pFFTCleanRe[nFFT - j] = pFFTCleanRe[j] = pFFTCleanMag[j]; pFFTCleanIm[nFFT - j] = pFFTCleanIm[j] = 0; } } ifft_real( hIfft, pFFTCleanRe, pFFTCleanIm, nFFT, pxClean ); #if 0 AUDLOG( "clean FFT with phase:\n" ); for ( j = 0; j < nFFT; j++ ) { AUDLOG( "%d + j%d, ", pFFTCleanRe[j], pFFTCleanIm[j] ); } AUDLOG( "\n" ); #endif for ( j = 0; j < frameSize; j++ ) { pOutBuf[k + j] = pOutBuf[k + j] + pxClean[j]; } k += frameStride; cleanLen += frameStride; } win16s_free( &hWin ); fft_free( &hFft ); ifft_free( &hIfft ); free( pFrame ); free( pNoiseMean ); free( pLamdaD ); free( pXi ); free( pGamma ); free( pG ); free( pGammaNew ); free( pFFTMag ); free( pFFTRe ); free( pFFTIm ); free( pFFTCleanMag ); free( pFFTCleanRe ); free( pFFTCleanIm ); free( pxClean ); return cleanLen; }