/* Compute spectrum of estimated echo for use in an echo post-filter */ void speex_echo_get_residual(SpeexEchoState *st, spx_word32_t *residual_echo, int len) { int i; spx_word16_t leak2; int N; N = st->window_size; /* Apply hanning window (should pre-compute it)*/ for (i=0;i<N;i++) st->y[i] = MULT16_16_Q15(st->window[i],st->last_y[i]); /* Compute power spectrum of the echo */ spx_fft(st->fft_table, st->y, st->Y); power_spectrum(st->Y, residual_echo, N); #ifdef FIXED_POINT if (st->leak_estimate > 16383) leak2 = 32767; else leak2 = SHL16(st->leak_estimate, 1); #else if (st->leak_estimate>.5) leak2 = 1; else leak2 = 2*st->leak_estimate; #endif /* Estimate residual echo */ for (i=0;i<=st->frame_size;i++) residual_echo[i] = (spx_int32_t)MULT16_32_Q15(leak2,residual_echo[i]); }
int main() { int Num_of_detections 32; // Read the prediction signal and times file_detection prediction = readingfile("data/GWprediction.rat"); signal = prediction.signal; times = prediction.times; str::string dir = 'data/'; // Do the Fourier transform of the signal calculating the power spectrum of the prediction rarray <std::complex<double>,1> signal_hat = fft_fast(signal01); rarray <double,1> ps = power_spectrum(rarray <std::complex<double>,1> signal_hat01); // Read the prediction file sequently //for (i=01; i <= Num_of_detections; i++) { // read one of the GW signal from observations int i=1; fname = "detection"+"01"+".rat"; file_detection detection01 = readingfile("data/detection01.rat"); signal01 = detection01.signal; times01 = detection01.times; rarray <std::complex<double>,1> signal_hat01 = fft_fast(signal01); rarray <double,1> ps01 = power_spectrum(rarray <std::complex<double>,1> signal_hat01); double co_ef_01=correlation_coefficient(ps01, ps); vector <double> co_ef(Num_of_detections); co_ef[0] = co_ef; std::sort(co_ef.begin(), co_ef.end()); std::cout<<co_ef[0]<<co_ef[-1]<<std::endl; //} return 0; }
/** Performs echo cancellation on a frame */ void speex_echo_cancel(SpeexEchoState *st, short *ref, short *echo, short *out, float *Yout) { int i,j,m; int N,M; float scale; float ESR; float SER; float Sry=0,Srr=0,Syy=0,Sey=0,See=0,Sxx=0; float leak_estimate; leak_estimate = .1+(.9/(1+2*st->sum_adapt)); N = st->window_size; M = st->M; scale = 1.0f/N; st->cancel_count++; /* Copy input data to buffer */ for (i=0; i<st->frame_size; i++) { st->x[i] = st->x[i+st->frame_size]; st->x[i+st->frame_size] = echo[i]; st->d[i] = st->d[i+st->frame_size]; st->d[i+st->frame_size] = ref[i]; } /* Shift memory: this could be optimized eventually*/ for (i=0; i<N*(M-1); i++) st->X[i]=st->X[i+N]; /* Copy new echo frame */ for (i=0; i<N; i++) st->X[(M-1)*N+i]=st->x[i]; /* Convert x (echo input) to frequency domain */ spx_drft_forward(st->fft_lookup, &st->X[(M-1)*N]); /* Compute filter response Y */ for (i=0; i<N; i++) st->Y[i] = 0; for (j=0; j<M; j++) spectral_mul_accum(&st->X[j*N], &st->W[j*N], st->Y, N); /* Convert Y (filter response) to time domain */ for (i=0; i<N; i++) st->y[i] = st->Y[i]; spx_drft_backward(st->fft_lookup, st->y); for (i=0; i<N; i++) st->y[i] *= scale; /* Transform d (reference signal) to frequency domain */ for (i=0; i<N; i++) st->D[i]=st->d[i]; spx_drft_forward(st->fft_lookup, st->D); /* Compute error signal (signal with echo removed) */ for (i=0; i<st->frame_size; i++) { float tmp_out; tmp_out = (float)ref[i] - st->y[i+st->frame_size]; st->E[i] = 0; st->E[i+st->frame_size] = tmp_out; /* Saturation */ if (tmp_out>32767) tmp_out = 32767; else if (tmp_out<-32768) tmp_out = -32768; out[i] = tmp_out; } /* This bit of code provides faster adaptation by doing a projection of the previous gradient on the "MMSE surface" */ if (1) { float Sge, Sgg, Syy; float gain; Syy = inner_prod(st->y+st->frame_size, st->y+st->frame_size, st->frame_size); for (i=0; i<N; i++) st->Y2[i] = 0; for (j=0; j<M; j++) spectral_mul_accum(&st->X[j*N], &st->PHI[j*N], st->Y2, N); for (i=0; i<N; i++) st->y2[i] = st->Y2[i]; spx_drft_backward(st->fft_lookup, st->y2); for (i=0; i<N; i++) st->y2[i] *= scale; Sge = inner_prod(st->y2+st->frame_size, st->E+st->frame_size, st->frame_size); Sgg = inner_prod(st->y2+st->frame_size, st->y2+st->frame_size, st->frame_size); /* Compute projection gain */ gain = Sge/(N+.03*Syy+Sgg); if (gain>2) gain = 2; if (gain < -2) gain = -2; /* Apply gain to weights, echo estimates, output */ for (i=0; i<N; i++) st->Y[i] += gain*st->Y2[i]; for (i=0; i<st->frame_size; i++) { st->y[i+st->frame_size] += gain*st->y2[i+st->frame_size]; st->E[i+st->frame_size] -= gain*st->y2[i+st->frame_size]; } for (i=0; i<M*N; i++) st->W[i] += gain*st->PHI[i]; } /* Compute power spectrum of output (D-Y) and filter response (Y) */ for (i=0; i<N; i++) st->D[i] -= st->Y[i]; power_spectrum(st->D, st->Rf, N); power_spectrum(st->Y, st->Yf, N); /* Compute frequency-domain adaptation mask */ for (j=0; j<=st->frame_size; j++) { float r; r = leak_estimate*st->Yf[j] / (1+st->Rf[j]); if (r>1) r = 1; st->fratio[j] = r; } /* Compute a bunch of correlations */ Sry = inner_prod(st->y+st->frame_size, st->d+st->frame_size, st->frame_size); Sey = inner_prod(st->y+st->frame_size, st->E+st->frame_size, st->frame_size); See = inner_prod(st->E+st->frame_size, st->E+st->frame_size, st->frame_size); Syy = inner_prod(st->y+st->frame_size, st->y+st->frame_size, st->frame_size); Srr = inner_prod(st->d+st->frame_size, st->d+st->frame_size, st->frame_size); Sxx = inner_prod(st->x+st->frame_size, st->x+st->frame_size, st->frame_size); /* Compute smoothed cross-correlation and energy */ st->Sey = .98*st->Sey + .02*Sey; st->Syy = .98*st->Syy + .02*Syy; st->See = .98*st->See + .02*See; /* Check if filter is completely mis-adapted (if so, reset filter) */ if (st->Sey/(1+st->Syy + .01*st->See) < -1) { /*fprintf (stderr, "reset at %d\n", st->cancel_count);*/ speex_echo_state_reset(st); return; } SER = Srr / (1+Sxx); ESR = leak_estimate*Syy / (1+See); if (ESR>1) ESR = 1; #if 1 /* If over-cancellation (creating echo with 180 phase) damp filter */ if (st->Sey/(1+st->Syy) < -.1 && (ESR > .3)) { for (i=0; i<M*N; i++) st->W[i] *= .95; st->Sey *= .5; /*fprintf (stderr, "corrected down\n");*/ } #endif #if 1 /* If under-cancellation (leaving echo with 0 phase) scale filter up */ if (st->Sey/(1+st->Syy) > .1 && (ESR > .1 || SER < 10)) { for (i=0; i<M*N; i++) st->W[i] *= 1.05; st->Sey *= .5; /*fprintf (stderr, "corrected up %d\n", st->cancel_count);*/ } #endif /* We consider that the filter is adapted if the following is true*/ if (ESR>.6 && st->sum_adapt > 1) { /*if (!st->adapted) fprintf(stderr, "Adapted at %d %f\n", st->cancel_count, st->sum_adapt);*/ st->adapted = 1; } /* Update frequency-dependent energy ratio with the total energy ratio */ for (i=0; i<=st->frame_size; i++) { st->fratio[i] = (.2*ESR+.8*min(.005+ESR,st->fratio[i])); } if (st->adapted) { st->adapt_rate = .95f/(2+M); } else { /* Temporary adaption rate if filter is not adapted correctly */ if (SER<.1) st->adapt_rate =.8/(2+M); else if (SER<1) st->adapt_rate =.4/(2+M); else if (SER<10) st->adapt_rate =.2/(2+M); else if (SER<30) st->adapt_rate =.08/(2+M); else st->adapt_rate = 0; } /* How much have we adapted so far? */ st->sum_adapt += st->adapt_rate; /* Compute echo power in each frequency bin */ { float ss = 1.0f/st->cancel_count; if (ss < .3/M) ss=.3/M; power_spectrum(&st->X[(M-1)*N], st->Xf, N); /* Smooth echo energy estimate over time */ for (j=0; j<=st->frame_size; j++) st->power[j] = (1-ss)*st->power[j] + ss*st->Xf[j]; /* Combine adaptation rate to the the inverse energy estimate */ if (st->adapted) { /* If filter is adapted, include the frequency-dependent ratio too */ for (i=0; i<=st->frame_size; i++) st->power_1[i] = st->adapt_rate*st->fratio[i] /(1.f+st->power[i]); } else { for (i=0; i<=st->frame_size; i++) st->power_1[i] = st->adapt_rate/(1.f+st->power[i]); } } /* Convert error to frequency domain */ spx_drft_forward(st->fft_lookup, st->E); /* Do some regularization (prevents problems when system is ill-conditoned) */ for (m=0; m<M; m++) for (i=0; i<N; i++) st->W[m*N+i] *= 1-st->regul[i]*ESR; /* Compute weight gradient */ for (j=0; j<M; j++) { weighted_spectral_mul_conj(st->power_1, &st->X[j*N], st->E, st->PHI+N*j, N); } /* Gradient descent */ for (i=0; i<M*N; i++) st->W[i] += st->PHI[i]; /* AUMDF weight constraint */ for (j=0; j<M; j++) { /* Remove the "if" to make this an MDF filter */ if (st->cancel_count%M == j) { spx_drft_backward(st->fft_lookup, &st->W[j*N]); for (i=0; i<N; i++) st->W[j*N+i]*=scale; for (i=st->frame_size; i<N; i++) { st->W[j*N+i]=0; } spx_drft_forward(st->fft_lookup, &st->W[j*N]); } } /* Compute spectrum of estimated echo for use in an echo post-filter (if necessary)*/ if (Yout) { if (st->adapted) { /* If the filter is adapted, take the filtered echo */ for (i=0; i<st->frame_size; i++) st->last_y[i] = st->last_y[st->frame_size+i]; for (i=0; i<st->frame_size; i++) st->last_y[st->frame_size+i] = st->y[st->frame_size+i]; } else { /* If filter isn't adapted yet, all we can do is take the echo signal directly */ for (i=0; i<N; i++) st->last_y[i] = st->x[i]; } /* Apply hanning window (should pre-compute it)*/ for (i=0; i<N; i++) st->Yps[i] = (.5-.5*cos(2*M_PI*i/N))*st->last_y[i]; /* Compute power spectrum of the echo */ spx_drft_forward(st->fft_lookup, st->Yps); power_spectrum(st->Yps, st->Yps, N); /* Estimate residual echo */ for (i=0; i<=st->frame_size; i++) Yout[i] = 2*leak_estimate*st->Yps[i]; } }
void irphase_process_internal (t_irphase *x, t_symbol *sym, short argc, t_atom *argv) { FFT_SETUP_D fft_setup; FFT_SPLIT_COMPLEX_D spectrum_1; FFT_SPLIT_COMPLEX_D spectrum_2; FFT_SPLIT_COMPLEX_D spectrum_3; float *in; float *filter_in; double *out_buf; t_symbol *filter = filter_retriever(x->deconvolve_filter_specifier); t_symbol *target = atom_getsym(argv++); t_symbol *source = atom_getsym(argv++); double filter_specifier[HIRT_MAX_SPECIFIER_ITEMS]; double range_specifier[HIRT_MAX_SPECIFIER_ITEMS]; double phase = atom_getfloat(argv++); double time_mul = atom_getfloat(argv++); double sample_rate = buffer_sample_rate(source); double deconvolve_delay; double deconvolve_phase; t_phase_type mode = (t_phase_type) atom_getlong(argv++); AH_UIntPtr fft_size; AH_UIntPtr fft_size_log2; AH_UIntPtr i; t_buffer_write_error error; long deconvolve_mode; // Get input buffer lengths AH_SIntPtr source_length_1 = buffer_length(source); AH_SIntPtr filter_length = buffer_length(filter); AH_SIntPtr max_length = source_length_1; // Check input buffers if (buffer_check((t_object *) x, source)) return; // Calculate fft size time_mul = time_mul == 0. ? 1 : time_mul; if (time_mul < 1) { object_warn((t_object *) x, " time multiplier cannot be less than 1 (using 1)"); time_mul = 1; } fft_size = calculate_fft_size((long) (max_length * time_mul), &fft_size_log2); if (fft_size < 8) { object_error((t_object *) x, "buffers are too short, or have no length"); return; } deconvolve_mode = x->deconvolve_mode; deconvolve_phase = phase_retriever(x->deconvolve_phase); deconvolve_delay = delay_retriever(x->deconvolve_delay, fft_size, sample_rate); // Allocate momory fft_setup = hisstools_create_setup_d(fft_size_log2); spectrum_1.realp = ALIGNED_MALLOC(sizeof(double) * fft_size * (mode == MODE_ALLPASS ? 6 : 3)); spectrum_1.imagp = spectrum_1.realp + fft_size; spectrum_2.realp = spectrum_1.imagp + fft_size; spectrum_2.imagp = mode == MODE_ALLPASS ? spectrum_2.realp + fft_size : 0; spectrum_3.realp = mode == MODE_ALLPASS ? spectrum_2.imagp + fft_size : 0; spectrum_3.imagp = mode == MODE_ALLPASS ? spectrum_3.realp + fft_size : 0; filter_in = filter_length ? ALIGNED_MALLOC(sizeof(float *) * filter_length) : 0; out_buf = mode == MODE_ALLPASS ? spectrum_3.realp : spectrum_2.realp; in = (float *) out_buf; if (!spectrum_1.realp || !fft_setup || (filter_length && !filter_in)) { object_error((t_object *) x, "could not allocate temporary memory for processing"); hisstools_destroy_setup_d(fft_setup); ALIGNED_FREE(spectrum_1.realp); ALIGNED_FREE(filter_in); return; } // Get input - convert to frequency domain - get power spectrum - convert phase buffer_read(source, x->read_chan - 1, in, fft_size); time_to_spectrum_float(fft_setup, in, source_length_1, spectrum_1, fft_size); power_spectrum(spectrum_1, fft_size, SPECTRUM_FULL); variable_phase_from_power_spectrum(fft_setup, spectrum_1, fft_size, phase, false); if (mode == MODE_ALLPASS) { // Copy minimum phase spectrum to spectrum_2 for (i = 0; i < fft_size; i++) { spectrum_2.realp[i] = spectrum_1.realp[i]; spectrum_2.imagp[i] = spectrum_1.imagp[i]; } // Get input again time_to_spectrum_float(fft_setup, in, source_length_1, spectrum_1, fft_size); // Fill deconvolution filter specifiers - read filter from buffer (if specified) - deconvolve input by minimum phase spectrum fill_power_array_specifier(filter_specifier, x->deconvolve_filter_specifier, x->deconvolve_num_filter_specifiers); fill_power_array_specifier(range_specifier, x->deconvolve_range_specifier, x->deconvolve_num_range_specifiers); buffer_read(filter, 0, filter_in, fft_size); deconvolve(fft_setup, spectrum_1, spectrum_2, spectrum_3, filter_specifier, range_specifier, 0, filter_in, filter_length, fft_size, SPECTRUM_FULL, deconvolve_mode, deconvolve_phase, deconvolve_delay, sample_rate); } // Convert to time domain - copy out to buffer spectrum_to_time(fft_setup, out_buf, spectrum_1, fft_size, SPECTRUM_FULL); error = buffer_write(target, out_buf, fft_size, x->write_chan - 1, x->resize, sample_rate, 1); buffer_write_error((t_object *) x, target, error); // Free memory hisstools_destroy_setup_d(fft_setup); ALIGNED_FREE(spectrum_1.realp); ALIGNED_FREE(filter_in); if (!error) outlet_bang(x->process_done); }
int main(int argc, char *argv[]){ SID_init(&argc,&argv,NULL,NULL); // Parse arguments and initialize double z; if(argc<2 || argc>3){ fprintf(stderr,"\n Syntax: %s z [gbpCosmo_file.txt]\n",argv[0]); fprintf(stderr," ------\n\n"); return(ERROR_SYNTAX); } else z=(double)atof(argv[1]); SID_log("Computing clustering information for z=%.2lf...",SID_LOG_OPEN,z); // Initialize cosmology cosmo_info *cosmo=NULL; if(argc==2) init_cosmo_default(&cosmo); else if(argc==3) read_gbpCosmo_file(&cosmo,argv[2]); // Initialize int mode =PSPEC_LINEAR_TF; int component=PSPEC_ALL_MATTER; init_sigma_M(&cosmo,z,mode,component); int n_k =((int *)ADaPS_fetch(cosmo,"n_k"))[0]; double *lk_P = (double *)ADaPS_fetch(cosmo,"lk_P"); double h_Hubble=((double *)ADaPS_fetch(cosmo,"h_Hubble"))[0]; SID_log("Done.",SID_LOG_CLOSE); // Generate file SID_log("Writing table to stdout...",SID_LOG_OPEN); double delta_c =1.686; double m_per_mpc_h=M_PER_MPC/h_Hubble; printf("# Column (01): k [h Mpc^-1]\n"); printf("# (02): R [h^-1 Mpc] \n"); printf("# (03): M [h^-1 M_sol]\n"); printf("# (04): V_max [km/s] \n"); printf("# (05): P_k [(h^-1 Mpc)^3]\n"); printf("# (06): sigma\n"); printf("# (07): nu (peak height)\n"); printf("# (08): b_BPR\n"); printf("# (09): b_TRK\n"); printf("# (10): z-space boost Kaiser '87 (applied to b_TRK)\n"); printf("# (11): b_TRK total (w/ Kaiser boost)\n"); printf("# (12): b_halo_Poole \n"); printf("# (13): z-space boost Poole \n"); printf("# (14): b_total_Poole \n"); printf("# (15): b_halo_Poole (substructure)\n"); printf("# (16): z-space boost Poole (substructure)\n"); printf("# (17): b_total_Poole (substructure)\n"); for(int i_k=0;i_k<n_k;i_k++){ double k_P =take_alog10(lk_P[i_k]); double R_P =R_of_k(k_P); double M_R =M_of_k(k_P,z,cosmo); double P_k =power_spectrum(k_P,z,&cosmo,mode,component); double sigma=sqrt(power_spectrum_variance(k_P,z,&cosmo,mode,component)); double V_max=V_max_NFW(M_R,z,NFW_MODE_DEFAULT,&cosmo); double nu =delta_c/sigma; double bias =1.; if(M_R<1e16*M_SOL){ printf("%10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le\n", k_P*m_per_mpc_h, R_P/m_per_mpc_h, M_R/(M_SOL/h_Hubble), V_max*1e-3, P_k/pow(m_per_mpc_h,3.), sigma, nu, bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_BPR), bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_TRK), bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_TRK|BIAS_MODEL_KAISER_BOOST), bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_TRK|BIAS_MODEL_KAISER), bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_POOLE_HALO), bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_POOLE_ZSPACE), bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_POOLE_TOTAL), bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_POOLE_SUBSTRUCTURE|BIAS_MODEL_POOLE_HALO), bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_POOLE_SUBSTRUCTURE|BIAS_MODEL_POOLE_ZSPACE), bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_POOLE_SUBSTRUCTURE|BIAS_MODEL_POOLE_TOTAL)); } } SID_log("Done.",SID_LOG_CLOSE); // Clean-up free_cosmo(&cosmo); SID_log("Done.",SID_LOG_CLOSE); SID_exit(ERROR_NONE); }