void ifft(int n, struct complex A[], struct complex F[]) { int k,j=0; struct complex omegak, omega; int sft = n/2; if(n==1) { F[0] = A[0]; return; } struct complex E[n/2], O[n/2], EF[n/2], OF[n/2]; for(k=0; k<n; k+=2) { E[j]=A[k]; O[j]=A[k+1]; j++; } ifft(n/2, E, EF); ifft(n/2, O, OF); omega.a = cos(-2*PI/n); omega.b = sin(-2*PI/n); omegak.a = 1.0; omegak.b = 0.0; for (k=0; k<n/2; k++, omegak = multiply(omegak,omega)) { F[k] = add(EF[k] , multiply(omegak,OF[k])); F[k + sft] = subtract(EF[k] , multiply(omegak,OF[k])); } }
int main(void) { complex v[N], v1[N], scratch[N]; int k; /* Fill v[] with a function of known FFT: */ for(k=0; k<N; k++) { v[k].Re = 0.125*cos(2*PI*k/(double)N); v[k].Im = 0.125*sin(2*PI*k/(double)N); v1[k].Re = 0.3*cos(2*PI*k/(double)N); v1[k].Im = -0.3*sin(2*PI*k/(double)N); } /* FFT, iFFT of v[]: */ print_vector("Orig", v, N); fft( v, N, scratch ); print_vector(" FFT", v, N); ifft( v, N, scratch ); print_vector("iFFT", v, N); /* FFT, iFFT of v1[]: */ print_vector("Orig", v1, N); fft( v1, N, scratch ); print_vector(" FFT", v1, N); ifft( v1, N, scratch ); print_vector("iFFT", v1, N); exit(EXIT_SUCCESS); }
/* ifft(v,N): [0] If N==1 then return. [1] For k = 0 to N/2-1, let ve[k] = v[2*k] [2] Compute ifft(ve, N/2); [3] For k = 0 to N/2-1, let vo[k] = v[2*k+1] [4] Compute ifft(vo, N/2); [5] For m = 0 to N/2-1, do [6] through [9] [6] Let w.re = cos(2*PI*m/N) [7] Let w.im = sin(2*PI*m/N) [8] Let v[m] = ve[m] + w*vo[m] [9] Let v[m+N/2] = ve[m] - w*vo[m] */ void ifft( complex *v, int n, complex *tmp ) { if(n>1) { /* otherwise, do nothing and return */ int k,m; complex z, w, *vo, *ve; ve = tmp; vo = tmp+n/2; for(k=0; k<n/2; k++) { ve[k] = v[2*k]; vo[k] = v[2*k+1]; } ifft( ve, n/2, v ); /* FFT on even-indexed elements of v[] */ ifft( vo, n/2, v ); /* FFT on odd-indexed elements of v[] */ for(m=0; m<n/2; m++) { w.Re = cos(2*PI*m/(double)n); w.Im = sin(2*PI*m/(double)n); z.Re = w.Re*vo[m].Re - w.Im*vo[m].Im; /* Re(w*vo[m]) */ z.Im = w.Re*vo[m].Im + w.Im*vo[m].Re; /* Im(w*vo[m]) */ v[ m ].Re = ve[m].Re + z.Re; v[ m ].Im = ve[m].Im + z.Im; v[m+n/2].Re = ve[m].Re - z.Re; v[m+n/2].Im = ve[m].Im - z.Im; } } return; }
int rhs_CNLS::dxdt(ptr_passer x, ptr_passer _dx, double t){ comp* restr dx = _dx; uf1= x; uf2=uf1+NUM_TIME_STEPS; //take the inverse fourier transform ifft(uf1, u1, NUM_TIME_STEPS); ifft(uf2, u2, NUM_TIME_STEPS); //Inform compiler of alignment, if supported ALIGNED(uf1); ALIGNED(uf2); ALIGNED(u1); ALIGNED(u2); ALIGNED(sq1); ALIGNED(sq2); ALIGNED(comp_in_r); ALIGNED(comp_in); ALIGNED(comp_out); ALIGNED(comp_out_r); ALIGNED(k); ALIGNED(ksq); for(size_t i = 0; i < NUM_TIME_STEPS; i++){ sq1[i] = _sqabs(u1[i]); sq2[i] = _sqabs(u2[i]); comp_in_r[i] = sq1[i] + sq2[i]; } comp expr1 = Id*(2.0*g0/(1.0+trap(comp_in_r, NUM_TIME_STEPS)*dt/e0)); //calculate the ffts for the rhs for(size_t i = 0; i < NUM_TIME_STEPS; i++){ comp_in_r[i] = (sq1[i] + A*sq2[i])*u1[i]; comp_in[i] = u2[i] * u2[i] * _conj(u1[i]); } //fourier transform forwards nonlinear equations fft(comp_in, comp_out, NUM_TIME_STEPS); fft(comp_in_r, comp_out_r, NUM_TIME_STEPS); for(size_t i = 0; i < NUM_TIME_STEPS; i++){ dx[i] = (((D/2) * ksq[i] + K) * uf1[i] - comp_out_r[i] - B*comp_out[i] + expr1*uf1[i]*(1-tau*ksq[i]) - Gamma*uf1[i])/Id; } //Do the fft work for the other half of the calculations for(size_t i = 0; i < NUM_TIME_STEPS; i++){ comp_in_r[i] = (A*sq1[i] + sq2[i])*u2[i]; comp_in[i] = u1[i] * u1[i] * _conj(u2[i]); } fft(comp_in, comp_out, NUM_TIME_STEPS); fft(comp_in_r, comp_out_r, NUM_TIME_STEPS); for(size_t i = 0; i < NUM_TIME_STEPS; i++){ dx[i+NUM_TIME_STEPS] = (((D/2) * ksq[i] - K) * uf2[i] - comp_out_r[i] - B*comp_out[i] + expr1*(uf2[i]-tau*ksq[i]*uf2[i]) - Gamma*uf2[i])/Id; } return 0; }
int ifft2(double x[], double y[], const int n) { double *xq, *yq; static double *xb = NULL, *yb; double *xp, *yp; int i, j; static int size_f; if (xb == NULL) { size_f = 2 * n; xb = dgetmem(size_f); yb = xb + n; } if (2 * n > size_f) { free(xb); size_f = 2 * n; xb = dgetmem(size_f); yb = xb + n; } for (i = 0; i < n; i++) { xp = xb; xq = x + i; yp = yb; yq = y + i; for (j = n; --j >= 0; xq += n, yq += n) { *xp++ = *xq; *yp++ = *yq; } if (ifft(xb, yb, n) < 0) return (-1); xp = xb; xq = x + i; yp = yb; yq = y + i; for (j = n; --j >= 0; xq += n, yq += n) { *xq = *xp++; *yq = *yp++; } } for (i = n, xp = x, yp = y; --i >= 0; xp += n, yp += n) { if (ifft(xp, yp, n) < 0) return (-1); } return (0); }
/////////////////////////////////////////////// // // // Cepstrum // // // /////////////////////////////////////////////// void cep_FE(void){ int k; double Fin[FFT_SIZE+1]={0}, Env[FFT_SIZE+1]={0}; fft(); for(k=0;k<FFT_SIZE;k++){ if(Xr[k]!=0){ Xphs[k]=atan2(Xi[k],Xr[k]); // 位相記録.-πからπのラジアン値. } Xr[k]=log(Xamp[k]); // 振幅の対数をとる Xi[k]=0; // 虚部は0 } ifft(); // IFFTでCepを作成 for(k=0;k<FFT_SIZE;k++){ c[k]=z[k]/FFT_SIZE; // ケプストラムをcとして格納 Fin[k]=0; // 微細構造ケプストラムをFinとして格納 Env[k]=c[k]; // スペクトル包絡ケプストラムをFinとして格納 if(k>=L && k<=FFT_SIZE-L){ Fin[k]=c[k]; Env[k]=0; } } for(k=0;k<FFT_SIZE;k++) xin[k] = Fin[k]; // FFT入力を微細構造Cepとする fft(); for(k=0;k<FFT_SIZE;k++) XFin[k]= Fr[k]; // 微細構造対数スペクトル for(k=0;k<FFT_SIZE;k++) xin[k] = Env[k]; // FFT入力をスペクトル包絡Cepとする fft(); for(k=0;k<FFT_SIZE;k++) XEnv[k]= Fr[k]; // スペクトル包絡対数スペクトル }
double * synthesis(double (*magPhase)[2]) { int index; double (*x)[2] = malloc(windowSize*2*sizeof(double)); //frequency domain double (*X)[2] = malloc(windowSize*2*sizeof(double)); //time domain double * outputFrame = malloc(windowSize*sizeof(double)); for(index = 0; index <windowSize;index++) { x[index][0] = magPhase[index][0]*cos(phaseCumulative[index]); x[index][1] = magPhase[index][0]*sin(phaseCumulative[index]); if(debug == 1) { printf("%lg",x[index][0]); printf("%s","\t"); printf("%lg",x[index][1]); printf("%s","\n"); } }; ifft(windowSize,X,x); //inverse FFT for(index = 0;index<windowSize;index++) { outputFrame[index] = (X[index][0])*(wn[index]/weird2); } free(X); free(x); if(debug == 1) printf("%s","finishind the synthesis\n"); return outputFrame; }
void cc1_fft(complex *cdata, int n, int sign) { int j; double *real, *imag; if (NINT(pow(2.0, (double)NINT(log((double)n)/log(2.0)))) != n) { if (npfa(n) == n) pfacc(sign, n, cdata); else ccdft(cdata,n,sign); } else { real = (double *)malloc(n*sizeof(double)); if (real == NULL) fprintf(stderr,"cc1_fft: memory allocation error\n"); imag = (double *)malloc(n*sizeof(double)); if (imag == NULL) fprintf(stderr,"cc1_fft: memory allocation error\n"); for (j = 0; j < n; j++) { real[j] = (double)cdata[j].r; imag[j] = (double)cdata[j].i; } if (sign < 0) fft(n, real, imag); else ifft(n, real, imag); for (j = 0; j < n; j++) { cdata[j].r = (REAL)real[j]; cdata[j].i = (REAL)imag[j]; } free(real); free(imag); } return; }
/*! * \brief MainWindow::do_EPI_ifft */ void MainWindow::do_EPI_ifft(){ if (DEBUG) cout << Q_FUNC_INFO << endl; if (EPI_coord_select->size() != 0){ bool res = ifft(EPI_coord_ifft, *EPI_coord_select, EPI_min_ifft, EPI_max_ifft); if(res){ //Plot plot_EPI_ifft_curve(); //Min-Max double ymin = min(EPI_coord_ifft->coords(YCOORD), EPI_coord_ifft->size()); double ymax = max(EPI_coord_ifft->coords(YCOORD), EPI_coord_ifft->size()); //Slider EPI_slider_threshold->setRange(ymin * (double)SLIDER_DOUBLE, ymax * (double)SLIDER_DOUBLE); //Box EPI_box_threshold->setRange(ymin, ymax); EPI_box_threshold->setValue((ymin+ymax)/2.); //Status status_info(""); } else{ status_error(""); } } else{ status_error(""); } }
static void test_normal_fft() { size_t i; double re[FFT_SIZE] = {0}; double ref[FFT_SIZE] = {0}; double im[FFT_SIZE] = {0}; for(i = 0; i < ARRAY_SIZE(re); i++) { re[i] = sin(((double)i/(double)256)* (double)2.0 * (double)3.14); ref[i] = re[i]; im[i] = 0; } fft(re,im,8); ifft(re,im,8); for(i = 0; i < ARRAY_SIZE(re); i++) { if((ref[i] < (re[i] - 0.5)) || ((re[i] + 0.5) < ref[i])) { CU_FAIL("fft input output data not equal"); } } }
Array_d* calcAutoCorrelationFunction( Array_d* original_vector ) { int original_dim = original_vector->length; Array_c *vector = expand( array_dToc( original_vector ) ) , *power_spectrum , *ffted , *iffted ; Array_d *auto_correlation_function; ffted = fft( vector ); power_spectrum = power_old( ffted ); iffted = ifft( power_spectrum ); auto_correlation_function = array_cTod( iffted ); for ( int i = 0; i < auto_correlation_function->length; i++ ) { auto_correlation_function->a[i] /= original_dim; } printArray_cToFile( vector, "debug/vec.txt" ); printArray_cToFile( ffted, "debug/fft.txt" ); printArray_cToFile( power_spectrum, "debug/power.txt" ); printArray_cToFile( iffted, "debug/ifft.txt" ); freeArray_c( vector ); freeArray_c( ffted ); freeArray_c( iffted ); freeArray_c( power_spectrum ); return cloneArray_d( auto_correlation_function, original_dim ); // return calcCorrelationFunction( original_vector, original_vector ); }
void mul(int a[],int b[],int c[],int n) { cmplx *aco,*bco,*af,*bf,*cf,*cco,cmu; aco=(cmplx*)malloc(n*sizeof(cmplx)); bco=(cmplx*)malloc(n*sizeof(cmplx)); af=(cmplx*)malloc(2*n*sizeof(cmplx)); bf=(cmplx*)malloc(2*n*sizeof(cmplx)); cf=(cmplx*)malloc(2*n*sizeof(cmplx)); cco=(cmplx*)malloc(2*n*sizeof(cmplx)); int i; for(i=0;i<n;i++) { aco[i].re=a[i]; aco[i].im=0; bco[i].re=b[i]; bco[i].im=0; } fft(aco,n,af); fft(bco,n,bf); for(i=0;i<2*n;i++) { cmu=cmul(af[i],bf[i]); cf[i].re=cmu.re; cf[i].im=cmu.im; } ifft(cf,2*n,cco); for(i=0;i<n;i++) { cco[i].re/=n; cco[i].im/=n; } for(i=0;i<n;i++) { printf("\n%f %f",cco[i].re,cco[i].im); } }
/** * Computes the complex (inverse) fast Fourier transform. * * @param RE * Pointer to an array containing the real part of the input, will be * overwritten with real part of output. * @param IM * Pointer to an array containing the imaginary part of the input, will * be overwritten with imaginary part of output. * @param nXL * Length of signals, must be a power of 2 (otherwise the function will * return an <code>ERR_MDIM</code> error) * @param bInv * If non-zero the function computes the inverse complex Fourier * transform * @return <code>O_K</code> if successfull, a (begative) error code otherwise * * <h4>Remarks</h4> * <ul> * <li>The function creates the tables by its own, stores them in static arrays and * reuses them for all subsequent calls with the same value of <code>nXL</code>.</li> * </ul> */ INT16 dlm_fft ( FLOAT64* RE, FLOAT64* IM, INT32 nXL, INT16 bInv ) { extern int ifft(FLOAT64*,FLOAT64*,const int); extern int fft(FLOAT64*,FLOAT64*,const int); INT16 order; /* Initialize */ /* --------------------------------- */ order = (INT16)dlm_log2_i(nXL); /* Compute FFT order */ if (order<=0) return ERR_MDIM; /* nXL is not power of 2 --> :// */ #ifndef __NOXALLOC DLPASSERT(dlp_size(RE)>=nXL*sizeof(FLOAT64)); /* Assert RE has the right length */ DLPASSERT(dlp_size(IM)>=nXL*sizeof(FLOAT64)); /* Assert IM has the right length */ #endif if(bInv) { if(ifft(RE,IM,nXL) != 0) return NOT_EXEC; } else { if(fft(RE,IM,nXL) != 0) return NOT_EXEC; } return O_K; /* All done */ }
void lowpassfilter(double *data,double threshold,unsigned long dataLen) { int r=0; unsigned long i,mask=0xffffffff; while(mask&dataLen) { mask<<=1; r++; } unsigned long count = 1<<r; complex *com1,*com2; com1 = (complex *)malloc(sizeof(complex)*count); com2 = (complex *)malloc(sizeof(complex)*count); //Initiallize for (i = 0; i < count; i++) { com1[i].r = i>=dataLen?0:data[i]; com1[i].i = 0.0; } fft(com1,com2, r); for(i = (int)((threshold/2)*count); i< count; i++) { com2[i].r = 0.0; } ifft (com2,com1, r); for(i = 0;i<dataLen;i++) { data[i] = com1[i].r; } }
fourier_transform(void) { long num=0,i,j; long status; REAL ssq=0.0; REAL scale; REAL starttime, runtime; if (num == 0) num = 512; if (num > 512) { prints("\n"); prints("Error:\n"); prints("Number of Points %d exceeds alloted array size\n\n",num); return; } for (i=0;i<num;i++) { real[i] = (REAL)i; imag[i] = 0.0; } scale = 1.0 / (REAL)num; prints("\n"); prints("FFT-Mayer: 04 Oct 1994\n"); prints("FFT Size: %6d\n",num); fft(num, real, imag); ifft(num, real, imag); for (j=0;j<num*2;j++) {real[j]*=scale;imag[j]*=scale;} starttime = (double)get_timer(0) / CONFIG_SYS_HZ; fft(num, real, imag); ifft(num, real, imag); runtime = ((double)get_timer(0) / CONFIG_SYS_HZ) - starttime; for (j=0;j<num*2;j++) {real[j]*=scale;imag[j]*=scale;} runtime = runtime / 2.0; prints("Run Time (sec) = %11.5f\n",runtime); for (ssq=0,i=0;i<num;i++) ssq+=(real[i]-(REAL)i)*(real[i]-(REAL)i); prints("ssq errors %#.2g\n\n",ssq); }
inline nd::array ifft(const nd::array &x, std::vector<intptr_t> shape) { std::vector<intptr_t> axes; for (intptr_t i = 0; i < x.get_ndim(); ++i) { axes.push_back(i); } return ifft(x, shape, axes); }
cvector FFT::ifft(cvector vector, const int size) { const int end = Ub(vector); cxsc::Resize(vector, Lb(vector), Lb(vector)+size-1); for(int i = end+1; i<=Ub(vector); ++i) vector[i] = cxsc::complex(0.0, 0.0); return ifft(vector); }
int main() { // input signal parameters const std::size_t SIZE = 64; const Aquila::FrequencyType sampleFreq = 2000; const Aquila::FrequencyType f1 = 96, f2 = 813; const Aquila::FrequencyType f_lp = 500; Aquila::SineGenerator sineGenerator1 = Aquila::SineGenerator(sampleFreq); sineGenerator1.setAmplitude(32).setFrequency(f1).generate(SIZE); Aquila::SineGenerator sineGenerator2 = Aquila::SineGenerator(sampleFreq); sineGenerator2.setAmplitude(8).setFrequency(f2).setPhase(0.75).generate(SIZE); Aquila::Sum sum(sineGenerator1, sineGenerator2); Aquila::TextPlot plt("Signal waveform before filtration"); plt.plot(sum); // calculate the FFT auto fft = Aquila::FftFactory::getFft(SIZE); Aquila::ComplexType spectrum[SIZE]; fft->fft(sum.toArray(), spectrum); plt.setTitle("Signal spectrum before filtration"); plt.plotSpectrum(spectrum, SIZE); // generate a low-pass filter spectrum Aquila::ComplexType filterSpectrum[SIZE]; for (std::size_t i = 0; i < SIZE; ++i) { if (i < (SIZE * f_lp / sampleFreq)) { // passband filterSpectrum[i] = 1.0; } else { // stopband filterSpectrum[i] = 0.0; } } plt.setTitle("Filter spectrum"); plt.plotSpectrum(filterSpectrum, SIZE); // the following line does the multiplication of two spectra // (which is complementary to convolution in time domain) typedef Aquila::ComplexType cplx; std::transform(spectrum, spectrum + SIZE, filterSpectrum, spectrum, [] (cplx x, cplx y) { return x * y; }); plt.setTitle("Signal spectrum after filtration"); plt.plotSpectrum(spectrum, SIZE); // Inverse FFT moves us back to time domain double x1[SIZE]; fft->ifft(spectrum, x1); plt.setTitle("Signal waveform after filtration"); plt.plot(x1, SIZE); return 0; }
void compute_raised_cosine_filter(double coeffs[], int len, int root, int sinc_compensate, double alpha, double beta) { double f; double x; double f1; double f2; double tau; complex_t vec[SEQ_LEN]; int i; int j; int h; f1 = (1.0 - beta)*alpha; f2 = (1.0 + beta)*alpha; tau = 0.5/alpha; /* (Root) raised cosine */ for (i = 0; i <= SEQ_LEN/2; i++) { f = (double) i/(double) SEQ_LEN; if (f <= f1) vec[i] = complex_set(1.0, 0.0); else if (f <= f2) vec[i] = complex_set(0.5*(1.0 + cos((3.1415926535*tau/beta)*(f - f1))), 0.0); else vec[i] = complex_set(0.0, 0.0); } if (root) { for (i = 0; i <= SEQ_LEN/2; i++) vec[i].re = sqrt(vec[i].re); } if (sinc_compensate) { for (i = 1; i <= SEQ_LEN/2; i++) { x = 3.1415926535*(double) i/(double) SEQ_LEN; vec[i].re *= (x/sin(x)); } } for (i = 0; i <= SEQ_LEN/2; i++) vec[i].re *= tau; for (i = 1; i < SEQ_LEN/2; i++) vec[SEQ_LEN - i] = vec[i]; ifft(vec, SEQ_LEN); h = (len - 1)/2; for (i = 0; i < len; i++) { j = (SEQ_LEN - h + i)%SEQ_LEN; coeffs[i] = vec[j].re/(double) SEQ_LEN; } }
/* * Function NAME: apply_taper */ void matchedfilter::apply_taper (float *taper_buff, float cutoff_freq, float freq_range) { int k; taper_f(taper_buff, time_window_ref, cutoff_freq, freq_range, samp_freq_ref, dt_ref, N_window_ref); fft(ref_ax, N_window_ref); fft(ref_ay, N_window_ref); for (k = 0; k < N_window_ref+2; k++) ref_ax[k] *= taper_buff[k]; for (k = 0; k < N_window_ref+2; k++) ref_ay[k] *= taper_buff[k]; norm_ref_ax = 0.0f; // Use Parceval's theorem from DC to nyquist (+) for (k = 0; k < N_window_ref + 2; k++) norm_ref_ax += ref_ax[k]*ref_ax[k]; // Use Parceval's theorem on the negative frequencies (-) for (k = 2; k < N_window_ref; k++) norm_ref_ax += ref_ax[k]*ref_ax[k]; norm_ref_ax /= (float)N_window_ref; norm_ref_ax = sqrtf(norm_ref_ax); norm_ref_ay = 0.0f; // Use Parceval's theorem from DC to nyquist (+) for (k = 0; k < N_window_ref + 2; k++) norm_ref_ay += ref_ay[k]*ref_ay[k]; // Use Parceval's theorem on the negative frequencies (-) for (k = 2; k < N_window_ref; k++) norm_ref_ay += ref_ay[k]*ref_ay[k]; norm_ref_ay /= (float)N_window_ref; norm_ref_ay = sqrtf(norm_ref_ay); ifft(ref_ax, N_window_ref); ifft(ref_ay, N_window_ref); return; }
void compute_chest_cilk(task *taskX) { symbol_data *symbolData = taskX->symbolData; int rx = taskX->rx; int layer = taskX->layer; int res_power[4]; mf(&symbolData->data->in_data[symbolData->slot][3][rx][symbolData->startSc], &symbolData->data->in_rs[symbolData->slot][symbolData->startSc][layer], symbolData->nmbSc, symbolData->layer_data[layer][rx], &symbolData->pow[rx]); ifft(symbolData->layer_data[layer][rx], symbolData->nmbSc, symbolData->data->fftw[symbolData->slot]); chest(symbolData->layer_data[layer][rx], symbolData->pow[rx], symbolData->nmbSc, symbolData->layer_data[layer][rx], &res_power[rx]); symbolData->R[layer][rx] = cmake(res_power[rx],0); fft(symbolData->layer_data[layer][rx], symbolData->nmbSc, symbolData->data->fftw[symbolData->slot]); }
void phone_recvplay(void *vs){ int* ps = (int *)vs; int s = *ps; int i; FILE *fp_play; if ( (fp_play=popen("play -t raw -b 16 -c 1 -e s -r 44100 - 2> /dev/null ","w")) ==NULL) { die("popen:play"); } int cut_low=300, cut_high=5000; int send_len = (cut_high-cut_low)*N/SAMPLING_FREQEUENCY; double * recv_data = malloc(sizeof(double)*send_len*2); sample_t * play_data = malloc(sizeof(sample_t)*N); sample_t * pre_data = malloc(sizeof(sample_t)*N/2); complex double * X = calloc(sizeof(complex double), N); complex double * Y = calloc(sizeof(complex double), N); complex double * Z = calloc(sizeof(complex double), N); complex double * W = calloc(sizeof(complex double), N); memset(pre_data,0,N); while(1){ memset(W,0.0+0.0*I,N*sizeof(complex double)); memset(Z,0.0+0.0*I,N*sizeof(complex double)); // memset(rec_data,0,sizeof(long)*send_len*2); if(recv_all(s,(char *)recv_data,sizeof(double)*send_len*2)==-1){ die("recv"); } for(i=0; i<send_len; i++){ W[cut_low*N/SAMPLING_FREQEUENCY+i]=(double)recv_data[2*i]+(double)recv_data[2*i+1]*I; } // /* IFFT -> Z */ ifft(W, Z, N); // // 標本の配列に変換 complex_to_sample(Z, play_data, N); // オーバーラップを戻す for(i=0;i<N/2;i++){ play_data[i] += pre_data[i]; } memcpy(pre_data,play_data+N/2,N/2); // 無音状態だったらスキップ int num_low=0; for(i=0;i<N;i++){ if(-10<play_data[i] && play_data[i]<10) num_low++; } if(num_low>80*N/100) continue; // /* 標準出力へ出力 */ // write(1,play_data,N/2); fwrite(play_data,sizeof(sample_t),N/2,fp_play); memset(play_data,0,sizeof(sample_t)*N); } }
int main() { int N; int n, p,q,r; double *y_r, *y_i, *x_r, *x_i; clock_t t1, t2; printf("hello midterm \n"); /* srand(time(NULL)); N=10; v=(int *) malloc(N *sizeof(int)); printf("ori "); for(i=0;i<N;++i){ v[i]=rand() % 100; //printf("%d,",v[i]); } */ printf("input 2^p 3^q 5^r : p q r =>"); scanf("%d %d %d", &p,&q,&r); N = 1 << p; N=N*pow(3,q)*pow(5, r); printf("N=%d\n",N); x_r = (double *) malloc(N*sizeof(double)); x_i = (double *) malloc(N*sizeof(double)); y_r = (double *) malloc(N*sizeof(double)); y_i = (double *) malloc(N*sizeof(double)); //initial data for(n=0;n<N;++n) { x_r[n] = n; x_i[n] = 0; } t1 = clock(); fft(x_r, x_i, y_r, y_i, N); ifft(y_r, y_i, y_r, y_i, N); // t2 = clock(); printf("%f secs\n", 1.0*(t2-t1)/CLOCKS_PER_SEC);//print times print_complex(y_r, y_i, N); free(x_r); free(x_i); free(y_r); free(y_i); //sort(v,N); return 0; }
//reduce noise using noise buffer void noise_reduction(){ int k; float gain; for (k=0;k<FFTLEN;k++) { gain=1-N[k]/cabs(buffer[k]); if (gain<0){ gain=lamda; } buffer[k] = rmul(gain,buffer[k]); } ifft(FFTLEN,buffer); }
void ifft (complex a[],int n,complex f[]) { int j,k; complex omegak,omega,*ef,*of,*e,*o,cmu; ef=(complex*)malloc(n/2*sizeof(complex)); of=(complex*)malloc(n/2*sizeof(complex)); e=(complex*)malloc(n/2*sizeof(complex)); o=(complex*)malloc(n/2*sizeof(complex)); if(n==1){ f[0].re=a[0].re; f[0].im=a[0].im; return; } for(j=k=0;k<n;j++,k+=2){ e[j].re=a[k].re; e[j].im=a[k].im; o[j].re=a[k+1].re; o[j].im=a[k+1].im; } omega.re=cos(2*pi/n); omega.im=-1*sin(2*pi/n); omegak.re=1; omegak.im=0; ifft(e,n/2,ef); ifft(o,n/2,of); for(k=0;k<n/2;k++) { cmu=comp_mul(omegak,of[k]); f[k].re=ef[k].re+cmu.re; f[k].im=ef[k].im+cmu.im; f[k+n/2].re=ef[k].re-cmu.re; f[k+n/2].im=ef[k].im-cmu.im; omegak=comp_mul(omegak,omega); } return; }
//reduce noise using noise buffer void noise_reduction(){ int k; float a,b,gain; for (k=0;k<FFTLEN;k++) { a=1-N[k]/cabs(buffer[k]); b=lambda; gain=a; if(gain<b){ gain=b; } buffer[k] = rmul(gain,buffer[k]); } ifft(FFTLEN,buffer); }
inline typename enable_if2 < (is_arma_type<T1>::value && is_complex_strict<typename T1::elem_type>::value), Mat< std::complex<typename T1::pod_type> > >::result ifft2(const T1& A) { arma_extra_debug_sigprint(); // not exactly efficient, but "better-than-nothing" implementation typedef typename T1::pod_type T; Mat< std::complex<T> > B = ifft(A); // for square matrices, strans() will work out that an inplace transpose can be done, // hence we can potentially avoid creating a temporary matrix B = strans(B); return strans( ifft(B) ); }
/////////////////////////////////////////////// // // // Cepstrum // // // /////////////////////////////////////////////// void cep(void){ int k; fft(); for(k=0;k<FFT_SIZE;k++){ if(Xr[k]!=0){ Xphs[k]=atan2(Xi[k],Xr[k]); // 位相記録.-πからπのラジアン値. } Xr[k]=log(Xamp[k]); // 振幅の対数をとる Xi[k]=0; // 虚部は0 } ifft(); // IFFTでCepを作成 for(k=0;k<FFT_SIZE;k++){ c[k]=z[k]/FFT_SIZE; // 結果をcとして格納 } }
/////////////////////////////////////////////// // // // Inverse Cepstrum // // // /////////////////////////////////////////////// void icep(void){ int k; for(k=0;k<FFT_SIZE;k++){ xin[k]=c[k]; // FFT入力をCepとする } fft(); // Cepから振幅スペクトルを得る for(k=0;k<FFT_SIZE;k++){ Xamp[k]=exp(Fr[k]); // 振幅の算出 } for(k=0;k<FFT_SIZE;k++){ Xr[k]=Xamp[k]*cos(Xphs[k]); // 位相と融合して実部をつくる Xi[k]=Xamp[k]*sin(Xphs[k]); // 位相と融合して虚部をつくる } ifft(); // IFFTにより時間領域信号を得る(z[n]) }
/////////////////////////////////////////////// // // // Inverse Cepstrum // // // /////////////////////////////////////////////// void icep_FE(void){ int k; for(k=0;k<FFT_SIZE;k++){ xin[k]=c[k]; // FFT入力をCepとする } fft(); // Cepから振幅スペクトルを得る for(k=0;k<FFT_SIZE;k++){ Xamp[k]=exp(XFin[k]+XEnv[k]); // スペクトル包絡と微細構造から振幅を計算 } for(k=0;k<FFT_SIZE;k++){ Xr[k]=Xamp[k]*cos(Xphs[k]); // 位相と融合して実部をつくる Xi[k]=Xamp[k]*sin(Xphs[k]); // 位相と融合して虚部をつくる } ifft(); // IFFTにより時間領域信号を得る(z[n]) }