int main(int argc, char **argv) { fftw_complex *mem; if (argc <= 4) { printf("usage: ./construct_data FILENAME N M Z\n"); return 1; } mem = (fftw_complex*) nfft_malloc(sizeof(fftw_complex) * atoi(argv[2]) * atoi(argv[2]) * atoi(argv[4])); read_data(atoi(argv[2]),atoi(argv[3]),atoi(argv[4]), mem); fft(atoi(argv[2]),atoi(argv[3]),atoi(argv[4]), mem); construct(argv[1],atoi(argv[2]),atoi(argv[3]),atoi(argv[4]), mem); nfft_free(mem); return 1; }
/*! * This is the objective function for objects that have a cnls-like form * n coupled waves where the best solution is for each one to be a pulse. * It takes the energy of each pulse, and divides by the kurtosis of the * waveform of the laser pulse. This optimizes for energetic yet stable pulses */ double n_pulse_score::score(comp* ucur){ //This is not an optimal solution, //but isn't too slow and this code should never be a bottleneck //This verison is far easier to read/understand than the optimal version for(size_t i = 0; i < nts; i++){ help[i] = _sqabs(ucur[i]); for(size_t j = 1; j < n_pulse; j++){ help[i] += _sqabs(ucur[i+j*nts]); } help[i] = sqrt(help[i]); kurtosis_help[i] = help[i]; } double ener = energy(help, nts); fft(kurtosis_help, kurtosis_help, nts); for(size_t i = 0; i < nts; i++){ help[i] = abs(kurtosis_help[i]); } double kurtosis_v = 1.0/(kurtosis(help, nts)); double score = kurtosis_v* ener; return score; }
int main(int argc, char ** argv) { (void)argc; long n = atol(argv[1]); int low,high; long i; double t; if (!pow2check(n)) { fprintf(stderr, "error : n (%ld) not a power of two\n", n); exit(1); } sample_t * buf = calloc(sizeof(sample_t), n); complex double * X = calloc(sizeof(complex double), n); complex double * Y = calloc(sizeof(complex double), n); low = atoi(argv[2]); high = atoi(argv[3]); while (1) { /* 標準入力からn個標本を読む */ ssize_t m = read_n(0, n * sizeof(sample_t), buf); if (m == 0) break; /* 複素数の配列に変換 */ sample_to_complex(buf, X, n); /* FFT -> Y */ fft(X, Y, n); // Yのバンド外を0にする // t = (double) n / SAMPLING_FREQEUENCY; // for (i = 0; i < n; ++i){ // if(i/t<low || i/t>high){ // Y[i] = 0; // } // } /* IFFT -> Z */ ifft(Y, X, n); /* 標本の配列に変換 */ complex_to_sample(Y, buf, n); /* 標準出力へ出力 */ write_n(1, m, buf); } return 0; }
void Minim::FFT::inverse( float * buffer ) { // if (buffer.length > real.length) // { // Minim // .error("FFT.inverse: the passed array's length must equal FFT.timeSize()."); // return; // } // conjugate for (int i = 0; i < m_timeSize; i++) { m_imag[i] *= -1; } bitReverseComplex(); fft(); // copy the result in real into buffer, scaling as we do for (int i = 0; i < m_timeSize; i++) { buffer[i] = m_real[i] / m_timeSize; } }
/* IFFT */ void ifft(int N, double (*x)[2], double (*X)[2]) { int N2 = N/2; /* half the number of points in IFFT */ int i; /* generic index */ double tmp0, tmp1; /* temporary storage */ /* Calculate IFFT via reciprocity property of DFT. */ fft(N, X, x); x[0][0] = x[0][0]/N; x[0][1] = x[0][1]/N; x[N2][0] = x[N2][0]/N; x[N2][1] = x[N2][1]/N; for(i=1; i<N2; i++) { tmp0 = x[i][0]/N; tmp1 = x[i][1]/N; x[i][0] = x[N-i][0]/N; x[i][1] = x[N-i][1]/N; x[N-i][0] = tmp0; x[N-i][1] = tmp1; } }
void ccm_fft(complex *cdata, int n1, int n2, int ld1, int sign) { int i, j; double *real, *imag; if (NINT(pow(2.0, (double)NINT(log((double)n1)/log(2.0)))) != n1) { if (npfa(n1) == n1) pfamcc(sign, n1, n2, 1, ld1, cdata); else { for (i = 0; i < n2; i++) ccdft(&cdata[i*ld1],n1,sign); } } else { real = (double *)malloc(n1*sizeof(double)); if (real == NULL) fprintf(stderr,"ccm_fft: memory allocation error\n"); imag = (double *)malloc(n1*sizeof(double)); if (imag == NULL) fprintf(stderr,"ccm_fft: memory allocation error\n"); for (i = 0; i < n2; i++) { for (j = 0; j < n1; j++) { real[j] = (double)cdata[i*ld1+j].r; imag[j] = (double)cdata[i*ld1+j].i; } if (sign < 0) fft(n1, real, imag); else ifft(n1, real, imag); for (j = 0; j < n1; j++) { cdata[i*ld1+j].r = (REAL)real[j]; cdata[i*ld1+j].i = (REAL)imag[j]; } } free(real); free(imag); } return; }
// SV Data Types - int real[] real[] real[] void syn_calc_fft(int num_samples, const svOpenArrayHandle data_in_arry, const svOpenArrayHandle data_out_re_arry, const svOpenArrayHandle data_out_im_arry) { int i; double (*x)[2]; /* pointer to time-domain samples */ double (*X)[2]; /* pointer to frequency-domain samples */ x = malloc(2 * num_samples * sizeof(double)); X = malloc(2 * num_samples * sizeof(double)); printf("\n \n Data In Array Left %d, Data In Array Right %d \n\n", svLeft(data_in_arry,1), svRight(data_in_arry, 1) ); for (i= svLeft(data_in_arry,1); i <= svRight(data_in_arry,1); i++) { //packing to double type x[i][0] = *(double*)svGetArrElemPtr1(data_in_arry, i); x[i][1] = 0; //printf("[syn_calc_fft - C] i :%d\treal : %f\tim : %f\n",i,x[i][0],x[i][1]); } /* Calculate FFT. */ fft(num_samples, x, X); printf("\n \n Data Out Real Array Left %d, Data Out Real Array Right %d \n\n", svLeft(data_out_re_arry,1), svRight(data_out_re_arry, 1) ); for(i= svLeft(data_out_re_arry,1); i <= svRight(data_out_re_arry,1); i++) { //packing real arry *(double*)svGetArrElemPtr1(data_out_re_arry, i) = X[i][0]; } printf("\n \n Data Out Imaginary Array Left %d, Data Out Imaginary Array Right %d \n\n", svLeft(data_out_im_arry,1), svRight(data_out_im_arry, 1) ); for(i= svLeft(data_out_im_arry,1); i <= svRight(data_out_im_arry,1); i++) { //packing im arry *(double*)svGetArrElemPtr1(data_out_im_arry, i) = X[i][1]; } // for(i=0;i<num_samples;i++) { // printf("[syn_calc_fft - C] i :%d\treal : %f\tim : %f\n",i,X[i][0],X[i][1]); // } return; }
void test_nr(int n){ double *space = new double[4*n]; double *v = space; double *w = space + 2*n; for(int i=0; i < n; i++){ w[2*i] = v[2*i] = rand()*1.0/RAND_MAX - 0.5; w[2*i+1] = v[2*i+1] = rand()*1.0/RAND_MAX - 0.5; } fft_mkl fft(n); nrbwd(v, n); fft.bwd(w); array_diff(v, w, 2*n); std::cout<<"\n\tNR error relative to MKL"<<std::endl; std::cout<<"\tn = "<<n<<std::endl; std::cout<<"\trel error = "<<array_max(v, 2*n)/array_max(w,2*n) <<std::endl; delete[] space; }
//Processing when import frame void frame_in(){ int k; float mag; for(k=0;k<FFTLEN;k++){ buffer[k]=cmplx(inframe[k],0); } //perform FFT fft(FFTLEN,buffer); //rotate every 2.5s if(i== 312){ M4=M3; M3=M2; M2=M1; M1=M4; i=0; } //Import to M1 if(i==0){ for(k=0;k<FFTLEN;k++){ M1[k]=cabs(buffer[k]); } } else{ if(lpf_switch==0){ for(k=0;k<FFTLEN;k++){ mag=cabs(buffer[k]); if(M1[k]>mag){ M1[k]=mag; } } } else{ for(k=0;k<FFTLEN;k++){ mag=cabs(buffer[k]); M1[k]=(1-lpf_weight)*mag+lpf_weight*M1[k]; } } } }
void demod_channel_profile_calc(signed short re[128], signed short im[128], struct dibDemodChannelProfile *profile) { signed long lu = 8192 / (profile->fft == 0 ? 4 : profile->fft); signed long i,j; double bmwin[128], tmp_re[512] = { 0.0 }, tmp_im[512] = { 0.0 }; for (i = 0; i < 128; i++) { double p = (i - 127) / (128.0*2.0) * 6.2831853071796; double tmp = ( 0.40217 - 0.49703 * cos(p) + 0.09892 * cos(p * 2) - 0.00183 * cos(p * 3)); bmwin[i] = tmp * (569 / (569.0 - i)); } //--- symetrisation for (i = 0; i < 128; i++) { tmp_re[i] = re[i] / 8192.0 * bmwin[i]; tmp_im[i] = im[i] / 8192.0 * bmwin[i]; if (i > 0) { tmp_re[512-i] = tmp_re[i]; tmp_im[512-i] = -tmp_im[i]; } } fft(tmp_re,tmp_im,512); i = (profile->pha_shift % (lu / 3)) * 512 / (lu / 3); // DibDbgPrint("-D- profile shift: %d %d\n", profile->pha_shift, i); j = 0; while (j < 512) { profile->profile[j] = 10 * log10(fabs(tmp_re[i]) / 512.0); if (++i >= 512) i = 0; j++; } }
/* *.....complex forward fft */ int fftcxf(float *xrr, float *xri, int isize, int ntot, int rotate) { int i, idum; /* * .....zerofill if necessary, between isize and ntot */ if (ntot > isize) { memset(xrr + isize, 0, sizeof(float) * (ntot - isize)); memset(xri + isize, 0, sizeof(float) * (ntot - isize)); } /* * Left rotate data, if requested */ if (rotate) { xxrotate(xrr, 1, ntot, rotate); xxrotate(xri, 1, ntot, rotate); } /* * .....normal forward fft */ fft(1.0,ntot,xrr,xri); /* * .....complex fft, transform x(0....f) to x(-0.5f....0.5f) */ idum = ntot/2; for (i=0;i<idum;i++) { float xdum; xdum = xrr[i]; xrr[i] = xrr[i+idum]; xrr[i+idum] = xdum; xdum = xri[i]; xri[i] = xri[i+idum]; xri[i+idum] = xdum; } return ntot; }
/* gets no. of points from the user, initialize the points and roots of unity lookup table * and lets fft go. finally bit-reverses the results and outputs them into a file. * n should be a power of 2. */ int main(int argc, char *argv[]) { int n; double *A_re, *A_im, *W_re, *W_im; long long start, end; struct timespec ts; if (argc <= 2) { fprintf(stderr, "Usage: ./fft n outfile\n"); exit(-1); } n = atoi(argv[1]); A_re = (double*)_mm_malloc(sizeof(double)*n, 32); A_im = (double*)_mm_malloc(sizeof(double)*n, 32); W_re = (double*)_mm_malloc(sizeof(double)*n/2, 32); W_im = (double*)_mm_malloc(sizeof(double)*n/2, 32); assert(A_re != NULL && A_im != NULL && W_re != NULL && W_im != NULL); init_array(n, A_re, A_im); compute_W(n, W_re, W_im); clock_gettime(CLOCK_MONOTONIC, &ts); start = ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000; fft(n, A_re, A_im, W_re, W_im); clock_gettime(CLOCK_MONOTONIC, &ts); end = ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000; permute_bitrev(n, A_re, A_im); // output_array(n, A_re, A_im, argv[2]); printf ("The total CPU time of FFT is: %lld microseconds.\n", end - start); printf ("The number of errors is %lld \n.", errors); _mm_free(A_re); _mm_free(A_im); _mm_free(W_re); _mm_free(W_im); }
void ifft(complex *fd, complex *td, int r) { // 付立叶变换点数 long count; // 循环变量 int i; complex *x; // 计算付立叶变换点数 count = 1 << r; // 分配运算所需存储器 x = (complex *)malloc(sizeof(complex)*count); // 将频域点写入X memcpy(x, fd, sizeof(complex) * count); // 求共轭 for(i = 0; i < count; i++) { x[i].r = x[i].r; x[i].i = -x[i].i; } // 调用快速付立叶变换 fft(x, td, r); // 求时域点的共轭 for(i = 0; i < count; i++) { td[i].r = td[i].r/count; td[i].i = -td[i].i/count; } // 释放内存 free(x); }
/* *.....complex backward fft */ int fftcxb(float *xrr, float *xri, int isize, int ntot) { int i, idum; float scale; /* * .....zerofill if necessary, between isize and ntot */ if (ntot > isize) { memset(xrr + isize, 0, sizeof(float) * (ntot - isize)); memset(xri + isize, 0, sizeof(float) * (ntot - isize)); } /* * .....complex fft, transform x(-0.5f....0.5f) to x(0....f) */ idum = ntot/2; for (i=0;i<idum;i++) { float xdum; xdum = xrr[i]; xrr[i] = xrr[i+idum]; xrr[i+idum] = xdum; xdum = xri[i]; xri[i] = xri[i+idum]; xri[i+idum] = xdum; } /* * .....reverse fft */ fft(-1.0, ntot, xrr, xri); scale = 1.0/ntot; for (i=0;i<ntot;i++) { xrr[i] *= scale; xri[i] *= scale; } return ntot; }
Array<T> fbp(const Array<T>& A, const Array<T>& gamma, size_t N) { Array<T> I = zeros(N,N); Array<T> x = linspace(-0.5,0.5,N); Array<T> y = linspace(-0.5,0.5,N); size_t nrDetPix = shape(A,0); size_t nrAngles = shape(A,1); Array<T> filter = abs( range(-nrDetPix/2, nrDetPix/2) ); //abs(linspace(-nrDetPix/2, nrDetPix/2,nrDetPix))'; for(size_t t=0; t<size(gamma); t++) { auto fhat = fftshift(fft(A(full,t))); fhat = fhat * filter; A(full,t) = real(ifft(fftshift(fhat))); //Todo -> ifftshift } //gamma = gamma / 360.0 * 2 * pi; for(size_t m=0; m<N; m++) for(size_t n=0; n<N; n++) for(size_t t=0; t<nrAngles; t++) { T xi = x(m)*cos(gamma(t))+y(n)*sin(gamma(t)); size_t index = round((xi/sqrt(2.0) + 0.5) * (nrDetPix-1.0)); I(m,n) += A(index,t); } //I = real(I/(2*nrAngles)*pi); //I = rot90(I); %flipud(fliplr(I)); return I; }
void ifft( Matrix& RealA, Matrix& ImagA) { // Routine to compute inverse Fourier transform using FFT algorithm // Inputs // RealA, ImagA Real and imaginary parts of transform // Outputs // RealA, ImagA Real and imaginary parts of time series int i, N = RealA.nRow(); // Number of data points //* Take complex conjugate of input transform for( i=1; i<=N; i++ ) ImagA(i) *= -1.0; // Complex conjugate //* Evaluate fast fourier transform fft( RealA, ImagA ); //* Take complex conjugate and normalize by N double invN = 1.0/N; for( i=1; i<=N; i++ ) { RealA(i) *= invN; ImagA(i) *= -invN; // Normalize and complex conjugate } }
void realft(double* s, int len) { int n, n2, i, i1, i2, i3, i4; double xr1, xi1, xr2, xi2, wrs, wis; double yr, yi, yr2, yi2, yr0, theta, x; n = len / 2; n2 = n / 2; theta = PI / n; fft(s, len, 0); x = sin(0.5 * theta); yr2 = -2.0 * x * x; yi2 = sin(theta); yr = 1.0 + yr2; yi = yi2; for (i = 2; i <= n2; ++i) { i1 = i + i - 1; i2 = i1 + 1; i3 = n + n + 3 - i2; i4 = i3 + 1; wrs = yr; wis = yi; xr1 = (s[i1 - 1] + s[i3 - 1]) / 2.0; xi1 = (s[i2 - 1] - s[i4 - 1]) / 2.0; xr2 = (s[i2 - 1] + s[i4 - 1]) / 2.0; xi2 = (s[i3 - 1] - s[i1 - 1]) / 2.0; s[i1 - 1] = xr1 + wrs * xr2 - wis * xi2; s[i2 - 1] = xi1 + wrs * xi2 + wis * xr2; s[i3 - 1] = xr1 - wrs * xr2 + wis * xi2; s[i4 - 1] = -xi1 + wrs * xi2 + wis * xr2; yr0 = yr; yr = yr * yr2 - yi * yi2 + yr; yi = yi * yr2 + yr0 * yi2 + yi; } xr1 = s[0]; s[0] = xr1 + s[1]; s[1] = 0.0; }
/* * ....imag backward fft */ int fftixb(float *xrr, float *xri, int isize, int ntot) { int i, ntotp1; float scale; /* * .....zerofill if necessary, between isize and ntot */ if (ntot > isize) memset(xri + isize, 0, sizeof(float) * (ntot - isize)); /* * .....fill imaginary array with zeros */ memset(xrr, 0, sizeof(float) * ntot * 2); /* * .....imag fft, transform x(0...0.5f) to x(0....f) */ ntotp1 = 2 * ntot - 1; for (i=0;i<ntot;i++) { xrr[ntotp1-i] = xrr[i]; xri[ntotp1-i] = -xri[i]; } /* * .....reverse fft */ fft(-1.0, ntot*2, xrr, xri); for (i=0;i<ntot;i++) { xrr[i] = xrr[i+ntot]; xri[i] = xri[i+ntot]; } scale = 1.0/ntot; for (i=0;i<ntot;i++) { xrr[i] *= scale; xri[i] *= scale; } return ntot; }
int idct(double *pReal, double *pImag, const double *pInReal, const double *pInImag, int PowerOf2) { int k, n; double rtemp, itemp; for (n = 1; n < dct_table_size; n++) { pLocalReal[n] = pInReal[n]; pLocalImag[n] = pInImag[n]; pLocalReal[dct_table_size + n] = -pInReal[dct_table_size - n]; pLocalImag[dct_table_size + n] = -pInImag[dct_table_size - n]; } pLocalReal[0] = pInReal[0]; pLocalImag[0] = pInImag[0]; pLocalReal[dct_table_size] = 0.0; pLocalImag[dct_table_size] = 0.0; /* 1/2 sample shift in the temporal domain */ for (k = 0; k < 2 * dct_table_size; k++) { rtemp = pLocalReal[k]; itemp = pLocalImag[k]; pLocalReal[k] = rtemp * pWeightReal[k] - itemp * pWeightImag[k]; pLocalImag[k] = rtemp * pWeightImag[k] + itemp * pWeightReal[k]; } if (size != PowerOf2 || dftmode) { dft(pLocalReal, pLocalImag, dct_table_size * 2); } else { fft(pLocalReal, pLocalImag, dct_table_size * 2); } for (k = 0; k < dct_table_size; k++) { pReal[k] = pLocalReal[k]; pImag[k] = pLocalImag[k]; } }
void gft_1dComplex64(double *signal, unsigned int N, double *win, int *pars, int stride) { int fstart, fend, fcount; double *fband; int i; // Do the initial FFT of the signal fft(N, signal, stride); // Apply the windows for (i=0; i<N*2; i+=2) { cmul(signal+i*stride,win+i); } // For each of the GFT frequency bands fcount = 0; fstart = 0; while (pars[fcount] >= 0) { fend = pars[fcount]; // frequency band that we're working with fband = signal+fstart*2*stride; // inverse FFT to transform to S-space ifft(fend-fstart, fband, stride); fstart = pars[fcount]; fcount++; } }
void doppler_process(vector<valarray<complex<double> > > &y, vector<valarray<complex<double> > > &yp, vector<double> &w, int Lfft) { // vector<valarray<complex<double> > > yp; int My = y.size(); int Ny = y[0].size(); int N = (Lfft <= Ny) ? Ny : Lfft; for(int i = 0; i < My; i++) { valarray<complex<double> > in(N); vector<double> YdBi; YdBi.clear(); for(int j = 0; j < Ny; j++) { in[j] = conj(y[i][j]) * w[j]; } fft(in, IsPowerofTwo(N)); in = in * in.apply(std::conj); yp.push_back(in); } git_rotate(yp, Lfft/2); }
int main(void) { void *sound_data; fft_t trans_data[N]; /* Check our parameters */ #ifndef NDEBUG LOGV("sizeof(fft_t) = %lu", (long unsigned) sizeof(fft_t)); assert(ao_format.bits == FORMAT_BITS); assert(ao_format.channels == FORMAT_CHANNELS); assert(ao_format.rate == FORMAT_RATE); assert(sf_format.frames == FORMAT_BITS); assert(sf_format.samplerate == FORMAT_RATE); assert(sf_format.channels == FORMAT_CHANNELS); #else /* DEBUG */ set_log_level(LOG_LEVEL_QUIET); #endif /* !DEBUG */ /* Process available data */ if ((sound_data = DATA(SAMPLES))) { /* Compute the FFT of the first N bytes */ fft((fft_t *) (sound_data), N, trans_data); save_sampled(sound_data, N, SOUND_FILE); save_sampled(trans_data, sizeof(trans_data), TRANS_FILE); /* Use the sound system to play samples */ ao_initialize(); play_sampled(sound_data, SAMPLES); play_sampled((char *) (trans_data), sizeof(trans_data)); ao_shutdown(); /* Cleanup memory */ free(sound_data); return EXIT_SUCCESS; } return EXIT_FAILURE; }
void begin() { int i; float *A, *W_re, *W_im; A = (float*)malloc(sizeof(float)*2*n); W_re = (float*)malloc(sizeof(float)*n/2); W_im = (float*)malloc(sizeof(float)*n/2); /* assert(A_re != NULL && A_im != NULL && W_re != NULL && W_im != NULL); */ compute_W(W_re, W_im); while (numiters == -1 || numiters-- > 0) { init_array(A); fft(A, W_re, W_im); #ifdef FFT2 permute_bitrev(A); #endif output_array(A); } free(A); free(W_re); free(W_im); }
void mul_any(const int *x, int lx, const int *y, int ly, int *z) { static Complex a[kN], b[kN], c[kN], d[kN]; for (int i = 0; i < lx; ++ i) a[i] = Complex(x[i] >> 15, 0.), b[i] = Complex(x[i] & 32767, 0.); for (int i = lx; i < L; ++ i) a[i] = b[i] = Complex(0., 0.); for (int i = 0; i < ly; ++ i) c[i] = Complex(y[i] >> 15, 0.), d[i] = Complex(y[i] & 32767, 0.); for (int i = ly; i < L; ++ i) c[i] = d[i] = Complex(0., 0.); fft(a, L, 1); fft(b, L, 1); fft(c, L, 1); fft(d, L, 1); for (int i = 0; i < L; ++ i) { Complex ta = a[i], tb = b[i], tc = c[i], td = d[i]; a[i] = ta * tc; b[i] = ta * td + tb * tc; c[i] = tb * td; } fft(a, L, -1); fft(b, L, -1); fft(c, L, -1); for (int i = 0; i < L; ++ i) z[i] = ((LL(a[i].x + .5) % MOD << 30) + (LL(b[i].x + .5) % MOD << 15) + LL(c[i].x + .5) % MOD) % MOD; }
void decodeDTMF(char * filename) { const unsigned int n_samples=30000; unsigned int samplesize, i; SNDFILE * f; FILE *plotfile; SF_INFO info; short sample; unsigned int rn_samples; float sec; float complex x[n_samples], r[n_samples]; samplesize=0; samplesize=samplesize; for(i=0;i<n_samples;i++) { x[i] = 0.0 + _Complex_I*0.0; r[i] = 0.0 + _Complex_I*0.0; } f=sf_open(filename,SFM_READ,&info); if(f) { printf("Frames: %u\n",(unsigned int)info.frames); printf("Sample rate: %u\n",info.samplerate); printf("Channels: %u\n",info.channels); printf("Format: 0x%X\n",info.format); if((info.format & SF_FORMAT_WAV)==SF_FORMAT_WAV){ printf("Format: SF_FORMAT_WAV\n"); } if((info.format & SF_FORMAT_PCM_S8)==SF_FORMAT_PCM_S8){ printf("\tSigned 8 bit data\n"); } if((info.format & SF_FORMAT_PCM_U8)==SF_FORMAT_PCM_U8){ printf("\tUnsigned 8 bit data\n"); } if((info.format & SF_FORMAT_PCM_16)==SF_FORMAT_PCM_16){ printf("\tSigned 16 bit data\n"); samplesize=sizeof(int16_t); } if((info.format & SF_FORMAT_PCM_24)==SF_FORMAT_PCM_24){ printf("\tSigned 24 bit data\n"); } if((info.format & SF_FORMAT_PCM_32)==SF_FORMAT_PCM_32){ printf("\tSigned 32 bit data\n"); } if((info.format & SF_FORMAT_FLOAT)==SF_FORMAT_FLOAT){ printf("\t32 bit float data\n"); } if((info.format & SF_FORMAT_DOUBLE)==SF_FORMAT_DOUBLE){ printf("\t64 bit float data\n"); } printf("Sections: %u\n",info.sections); printf("Seekable: %u\n",info.seekable); rn_samples = info.samplerate / 1; sec = ((float)rn_samples)/((float)info.samplerate); printf("Seconds: %f\n",sec); printf("FPS: %f\n",1.0/sec); plotfile = fopen("dtmf_decode/plotfile.txt","w"); if(plotfile){ for(i=0;i<(rn_samples/10);i++) { sf_readf_short(f,&sample, 1); x[rn_samples-i] = (float)sample; /*fprintf(o,"%u\t%i\n",i,sample);*/ } for(i=0;i<(rn_samples/2);i++) { fft(&r[i],x,rn_samples,i); fprintf(plotfile,"%f\t%.10f\n",i/sec,(cabs(r[i])*(2.0/rn_samples))); } } sf_close(f); } }
void BufferProcessor::run(){ // spectrum amplitude qreal amplitude; qreal SpectrumAnalyserMultiplier = 0.15e-3; // tells when all chunks has been processed if(pass == chunks){ emit allDone(); return; } // we does not calc spectra when array is too small if(array.size() < SPECSIZE){ return; } // prepare complex frame for fft calculations for(uint i=0; i<SPECSIZE; i++){ complexFrame[i] = Complex(window[i]*array[i+pass*SPECSIZE],0); } // do the magic fft(complexFrame); // some scaling/windowing is needed for displaying the fourier spectrum somewhere for(uint i=0; i<SPECSIZE/2;i++){ amplitude = SpectrumAnalyserMultiplier*std::abs(complexFrame[i]); amplitude = qMax(qreal(0.0), amplitude); amplitude = qMin(qreal(1.0), amplitude); complexFrame[i] = amplitude; } // audio spectrum is usually compressed for better displaying if(compressed){ for (int i = 0; i <SPECSIZE/2; i ++){ /* sum up values in freq array between logscale[i] and logscale[i + 1], including fractional parts */ int a = ceilf (logscale[i]); int b = floorf (logscale[i+1]); float sum = 0; if (b < a) sum += complexFrame[b].real()*(logscale[i+1]-logscale[i]); else{ if (a > 0) sum += complexFrame[a-1].real()*(a-logscale[i]); for (; a < b; a++) sum += complexFrame[a].real(); if (b < SPECSIZE/2) sum += complexFrame[b].real()*(logscale[i+1] - b); } /* fudge factor to make the graph have the same overall height as a 12-band one no matter how many bands there are */ sum *= (float) SPECSIZE/24; /* convert to dB */ float val = 20*log10f (sum); /* scale (-DB_RANGE, 0.0) to (0.0, 1.0) */ val = 1 + val / 40; spectrum[i] = CLAMP (val, 0, 1); } } else{ // if not compressed, just copy the real part clamped between 0 and 1 for(int i=0; i<SPECSIZE/2; i++){ spectrum[i] = CLAMP(complexFrame[i].real()*100,0,1); } } // emit the spectrum emit calculatedSpectrum(spectrum); // count the pass pass++; }
int main(int argc, char *argv[]) { FILE *fp; char *s, *infile = NULL, c; int size = SIZE, size2, k, nd = -1, out = ' '; double *x, *y; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) { if (*(s = *++argv) == '-') { c = *++s; switch (c) { case 'l': size = atoi(*++argv); argc--; break; case 'm': nd = atoi(*++argv) + 1; argc--; break; case 'i': case 'p': case 'r': c -= ('a' - 'A'); case 'A': case 'I': case 'P': case 'R': out = c; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, c); usage(1); } } else infile = s; } if (nd == -1) nd = size; if (nd > size) { fprintf(stderr, "%s : Order of sequence %d should be less than the FFT size %d!\n", cmnd, nd, size); return (1); } fp = stdin; if (infile) { fp = getfp(infile, "rb"); } x = dgetmem(size2 = size + size); y = x + size; while (!feof(fp)) { fillz(x, size2, sizeof(double)); if (freadf(x, sizeof(*x), nd, fp) == 0) break; if (freadf(y, sizeof(*y), nd, fp) == 0) break; fft(x, y, size); if (out == 'P') for (k = 0; k < size; k++) x[k] = x[k] * x[k] + y[k] * y[k]; else if (out == 'A') for (k = 0; k < size; k++) x[k] = sqrt(x[k] * x[k] + y[k] * y[k]); if (out != 'I') fwritef(x, sizeof(*x), size, stdout); if (out == ' ' || out == 'I') fwritef(y, sizeof(*y), size, stdout); } if (infile) { fclose(fp); } return (0); }
void overlp(float *input, int npts, float *output, float *c, int nc, int nfft, float *buffer, float *cbuff) { int i, iptr, nbad, ngood, nload1, nload2, nrem; float ci, cr, scale, scale1, scale2, xi, xr; float *const Buffer = &buffer[0] - 1; float *const C = &c[0] - 1; float *const Cbuff = &cbuff[0] - 1; float *const Input = &input[0] - 1; float *const Output = &output[0] - 1; nrem = npts; nbad = nc - 1; ngood = nfft - nbad; iptr = 1; /* DFT of filter sequence * */ zero( &Cbuff[1], 2*nfft ); /* copy( (int*)&C[1], (int*)&Cbuff[1], nc ); */ copy_float(&(C[1]), &(Cbuff[1]), nc ); fft( &Cbuff[1], &Cbuff[nfft + 1], nfft, -1 ); /* Initial conditions in buffer * */ zero( &Buffer[ngood + 1], nbad ); L_6: ; if( nrem <= 0 ) goto L_7; /* Load data into buffer * */ nload2 = 0; nload1 = min( ngood, nrem ); /* copy( (int*)&Input[iptr], (int*)&Buffer[1], nload1 ); */ copy_float(&(Input[iptr]), &(Buffer[1]), nload1 ); nrem = nrem - nload1; /* Load second buffer if the available data is not exhausted * */ if( nrem > 0 ){ nload2 = min( ngood, nrem ); /* Data */ /* copy( (int*)&Input[iptr + nload1], (int*)&Buffer[nfft + 1], nload2 ); */ copy_float( &(Input[iptr + nload1]), &(Buffer[nfft + 1]), nload2 ); /* Initial condition */ /* copy( (int*)&Input[iptr + ngood - nbad], (int*)&Buffer[nfft + 1 + ngood], nbad ); */ copy_float( &(Input[iptr + ngood - nbad]), &(Buffer[nfft + 1 + ngood]), nbad ); nrem = nrem - nload2; } else{ zero( &Buffer[nfft + 1], nfft ); } /* Scale buffers when both contain data * */ if( !(nload2 == 0) ){ scale1 = 0.; scale2 = 0.; for( i = 1; i <= nfft; i++ ){ scale1 = scale1 + fabs( Buffer[i] ); scale2 = scale2 + fabs( Buffer[nfft + i] ); } if( scale1 == 0. ){ scale1 = 1.; } scale = scale2/scale1; if( scale == 0. ){ scale = 1.; } for( i = 1; i <= nfft; i++ ){ Buffer[i] = Buffer[i]*scale; } } /* Transform data * */ fft( &Buffer[1], &Buffer[nfft + 1], nfft, -1 ); /* Product of data transform with filter transform * */ for( i = 1; i <= nfft; i++ ){ xr = Buffer[i]; xi = Buffer[nfft + i]; cr = Cbuff[i]; ci = Cbuff[nfft + i]; Buffer[i] = xr*cr - xi*ci; Buffer[nfft + i] = xr*ci + xi*cr; } /* Inverse transform * */ fft( &Buffer[1], &Buffer[nfft + 1], nfft, 1 ); /* Load initial conditions from input sequence * */ if( !(nrem <= 0) ){ /* copy( (int*)&Input[iptr + 2*ngood - nbad], (int*)&Buffer[ngood + 1], nbad ); */ copy_float( &(Input[iptr + 2*ngood - nbad]), &(Buffer[ngood + 1]), nbad ); } /* Save filtered data to output sequence * */ if( !(nload2 == 0) ){ for( i = 1; i <= nload1; i++ ){ Buffer[i] = Buffer[i]/scale; } } /* copy( (int*)&Buffer[1], (int*)&Output[iptr], nload1 ); */ copy_float( &(Buffer[1]), &(Output[iptr]), nload1 ); iptr = iptr + nload1; if( !(nload2 == 0) ){ /* copy( (int*)&Buffer[nfft + 1], (int*)&Output[iptr], nload2 ); */ copy_float( &(Buffer[nfft + 1]), &(Output[iptr]), nload2 ); iptr = iptr + nload2; } goto L_6; L_7: ; return; }
/* Fast Fourier transform, using the Tukey-Cooley algorithm. * * inputs: * xr: real input vector * xi: imaginary input vector * samples: number of samples in the clip * * outputs: * real: real output vector * imag: imaginary output vector * */ int fft( double* real, double* imag, const double* xr, const double* xi, int samples ) { int result; if( samples <= 1 ) { /*base case. Fourier transform of a single sample is itself*/ memcpy( real, xr, samples ); memcpy( imag, xi, samples ); } else { /*declare variables*/ double* evenSamplesr; double* evenSamplesi; double* oddSamplesr; double* oddSamplesi; double* evenResultsr; double* evenResultsi; double* oddResultsr; double* oddResultsi; int numEvenSamples; int i; /*split the samples into even and odd indexed elements*/ if( samples % 2 == 0 ) { numEvenSamples = samples / 2; } else { numEvenSamples = samples / 2 + 1; } evenSamplesr = ( double* )malloc( sizeof( double ) * numEvenSamples ); evenSamplesi = ( double* )malloc( sizeof( double ) * numEvenSamples ); oddSamplesr = ( double* )malloc( sizeof( double ) * samples / 2 ); oddSamplesi = ( double* )malloc( sizeof( double ) * samples / 2 ); for( i = 0; i < samples; i++ ) { if( i % 2 == 0 ) { evenSamplesr[ i / 2 ] = xr[ i ]; evenSamplesi[ i / 2 ] = xi[ i ]; } else { oddSamplesr[ i / 2 ] = xr[ i ]; oddSamplesi[ i / 2 ] = xi[ i ]; } } /*take the Fourier transform of each set*/ evenResultsr = ( double* )malloc( sizeof( double ) * numEvenSamples ); evenResultsi = ( double* )malloc( sizeof( double ) * numEvenSamples ); oddResultsr = ( double* )malloc( sizeof( double ) * samples / 2 ); oddResultsi = ( double* )malloc( sizeof( double ) * samples / 2 ); fft( evenResultsr, evenResultsi, evenSamplesr, evenSamplesi, samples / 2 ); fft( oddResultsr, oddResultsi, oddSamplesr, oddSamplesi, samples / 2 ); /*compute the real and imaginary results*/ real = realloc( real, sizeof( double ) * samples ); imag = realloc( imag, sizeof( double ) * samples ); for( i = 0; i < samples; i++ ) { double zr; double zi; int k; cmplxExp( &zr, &zi, 0, -2 * M_PI * k / samples ); if( i < samples / 2 ) { k = i; real[ i ] = evenSamplesr[ k ] + zr * oddSamplesr[ k ] - zi * oddSamplesi[ k ]; imag[ i ] = evenSamplesi[ k ] + zr * oddSamplesi[ k ] + zi * oddSamplesr[ k ]; } else { k = i % ( samples / 2 ); real[ i ] = evenSamplesr[ k ] - zr * oddSamplesr[ k ] + zi * oddSamplesr[ k ]; imag[ i ] = evenSamplesr[ k ] - zr * oddSamplesr[ k ] - zi * oddSamplesr[ k ]; } } free( evenSamplesr ); free( evenSamplesi ); free( oddSamplesr ); free( oddSamplesi ); free( evenResultsr ); free( evenResultsi ); free( oddResultsr ); free( oddResultsi ); } return 0; }
int main(int argc, char *argv[]) { if (argc < 4) { std::cout << "Usage:\n" "\tsine_peak_detect <SIZE> <SAMPLEFREQUENCY> <SIGNALFREQUENCY>\n"; return 1; } std::size_t SIZE = std::atoi(argv[1]); Aquila::FrequencyType sampleFrequency = std::atoi(argv[2]); Aquila::FrequencyType signalFrequency = std::atoi(argv[3]); // generate test signal (a sine wave) double dt = 1.0 / sampleFrequency; double* x = new double[SIZE]; for (std::size_t i = 0; i < SIZE; ++i) { x[i] = 64 * std::sin(2 * M_PI * signalFrequency * i * dt); } // calculate the FFT and magnitude spectrum Aquila::ComplexType* spectrum = new Aquila::ComplexType[SIZE]; Aquila::OouraFft fft(SIZE); fft.fft(x, spectrum); double* absSpectrum = new double[SIZE / 2]; for (std::size_t i = 0; i < SIZE / 2; ++i) { absSpectrum[i] = std::abs(spectrum[i]); } // "iterator" pointing to highest spectrum peak // the pointer difference is the number of spectral peak double* peakPos = std::max_element(absSpectrum, absSpectrum + SIZE / 2); std::cout << std::distance(absSpectrum, peakPos); delete [] absSpectrum; delete [] spectrum; delete [] x; return 0; }