int main() { complex temp, z, prod; double radius; FILE *outfile; int n; char filename[100]; temp.re = 1.0; temp.im = .05; z = rdiv(temp,cnorm(temp)); prod.re = 1.0; prod.im = 0.0; printf("Enter radius parameter:\n"); scanf("%lf", &radius); printf("radius = %f\n", radius); z = rmul(radius,z); sprintf(filename,"cmultest_r%.2f.dat", radius); printf("Data stored in %s\n", filename); outfile = fopen(filename, "w"); for(n=0; n<120; n++) { fprintf(outfile,"%f\t%f\n", prod.re, prod.im); /* print results */ prod = cmul(prod, z); } return(0); }
int plane_intersection(const Object *object, const Ray *ray, Ray *reflect, Vector *inter, Vector *n) { const Plane *plane = (const Plane*) object->priv; // ri: 面上的点, ni: 面的法向量 // pi: ray的起点, ki: ray的方向 const Vector *ri = &object->pos; const Vector *ni = &object->front; const Vector *pi = &ray->pos; const Vector *ki = &ray->front; Vector ri_minus_pi = sub(ri, pi); double t = dot(&ri_minus_pi, ni) / dot(ki, ni); Vector mul_result = rmul(ki, t); *inter = add(&mul_result, pi); *reflect = get_reflection_by_normal_and_ray(ray, ni, inter); *n = *ni; //if (plane->unlimited) { Vector v = sub(inter, &ray->pos); if (dot(&ray->front, &v) < 0) { return 0; } return 1; //} }
/** @brief ハウスホルダー・ベクトルへの変換. @details h[0]=0; ...; h[k-1]=0; h[k]=-s*xi; h[k+1]=x[k+1]; ...; h[n-1]=x[n-1]; @param[in] h 初期化済みのベクトル.サイズはn. @param[in] x 初期化済みのベクトル.サイズはn. @param[in] n ベクトルのサイズ. @param[in] k 第k要素が基準. @param[out] h ハウスホルダー・ベクトル. */ void rhouseholder_vec(int n, int k, rmulti **h, rmulti *alpha, rmulti **x) { int p0,p1,prec; rmulti *eta=NULL,*zeta=NULL,*xi=NULL,*axk=NULL; // allocate p0=rget_prec(alpha); p1=rvec_get_prec_max(n,h); prec=MAX2(p0,p1); eta=rallocate_prec(prec); zeta=rallocate_prec(prec); xi=rallocate_prec(prec); axk=rallocate_prec(prec); //----------- norm rvec_sum_pow2(xi,n-k-1,&x[k+1]); // xi=sum(abs(x((k+1):end)).^2); rmul(axk,x[k],x[k]); // axk=|x[k]|^2 radd(eta,axk,xi); // eta=|x[k]|^2+... rsqrt(axk,axk); // axk=|x[k]| rsqrt(eta,eta); // eta=sqrt(|x[k]|^2+...) if(req_d(eta,0)){rsub(xi,eta,axk);} // xi=eta-|x(k)| else{ // xi=xi/(|x(k)|+eta) radd(zeta,axk,eta); rdiv(xi,xi,zeta); } //----------- h rvec_set_zeros(k,h); rvec_copy(n-k-1,&h[k+1],&x[k+1]); // h((k+1):end)=x((k+1):end); if(ris_zero(x[k])){ rcopy(h[k],xi); rneg(h[k],h[k]); // h[k]=-xi }else{ rdiv(zeta,xi,axk); rneg(zeta,zeta); // zeta=-xi/axk; rmul(h[k],x[k],zeta); // h[k]=zeta*x[k]; } //----------- alpha if(req_d(xi,0) || req_d(eta,0)){ rset_d(alpha,0); }else{ rmul(alpha,xi,eta); // alpha=1/(xi*eta) rinv(alpha,alpha); } // free eta=rfree(eta); zeta=rfree(zeta); xi=rfree(xi); axk=rfree(axk); }
IExprNode* rmul(const std::vector<Token>& tokens, int &curToken, IExprNode *left) { if (curToken == tokens.size()) return left; const Token& t = tokens[curToken]; if (t.type == Token::TT_notation && (t.value == "*" || t.value == "/")) { ++curToken; IExprNode *opNode = new ExprNode_BinaryOp(t.value, left, factor(tokens, curToken)); return rmul(tokens, curToken, opNode); } else return left; }
//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); }
int sphere_intersection(const Object *object, const Ray *ray, Ray *reflect, Vector *inter, Vector *n) { Sphere *sphere = (Sphere*) object->priv; // ray.pos - sphere.pos Vector sub_result = sub(&ray->pos, &object->pos); // nearest point double t = -dot(&sub_result, &ray->front) / modulation2(&ray->front); Vector v = rmul(&ray->front, t); Vector nearest_point = add(&ray->pos, &v); // 检查交点是否在的正方向上 Vector pb = sub(&nearest_point, &ray->pos); double inner_pb = dot(&pb, &ray->front); if (inner_pb < 0) return 0; // nearest distance Vector nearest_v = sub(&nearest_point, &object->pos); double nearest_dis2 = modulation2(&nearest_v); if (nearest_dis2 <= sphere->radius * sphere->radius) { double dis_nearest_to_inter = sqrt(sphere->radius * sphere->radius - nearest_dis2); Vector delta; Vector ray_direction = ray->front; normalize(&ray_direction); delta = rmul(&ray_direction, dis_nearest_to_inter); *inter = sub(&nearest_point, &delta); *n = sub(inter, &object->pos); // 计算反射光线方向 *reflect = get_reflection_by_normal_and_ray(ray, &nearest_v, inter); return 1; } else { return 0; } }
int StackMul(void) { if(curstksize < 2){ return 1; } stack[0] = rmul(stack[1], stack[0]); for(int i = 1; i < curstksize - 1; ++i){ stack[i] = stack[i + 1]; } --curstksize; return 0; }
//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); }
//reduce noise using noise buffer void noise_reduction(){ int k; float a,b,gain; for (k=0;k<FFTLEN;k++) { P[k]=(1-lpf_weight)*cabs(buffer[k])+lpf_weight*P[k]; switch(gain_sel){ case 1://enhancement4 trial 1 a=lambda*N[k]/cabs(buffer[k]); b=1-N[k]/cabs(buffer[k]); break; case 2: a=lambda*P[k]/cabs(buffer[k]); b=1-N[k]/cabs(buffer[k]); break; case 3: a=lambda*N[k]/P[k]; b=1-N[k]/P[k]; break; case 4: a=lambda; b=1-N[k]/P[k]; break; case 5: //enhancement5 a=lambda; b=sqrt(1-N[k]*N[k]/cabs(cmul(buffer[k],buffer[k]))); break; default://no enhancement a=lambda; b=1-N[k]/cabs(buffer[k]); break; } gain=a; if(a<b){ gain=b; } buffer[k] = rmul(gain,buffer[k]); } ifft(FFTLEN,buffer); }
/** @brief ハウスホルダー変換によるQR分解の変則版. @param[in] n ベクトルxのサイズ.ベクトルhのサイズ. @param[in] k0 Hの最初のハウスホルダー・ベクトルの基準は第k0要素. @param[in] nH 配列Alphaのサイズ.行列Hの列の個数. @param[in] k 第k要素が基準. @param[in] H ハウスホルダー・ベクトルの格納用の行列.サイズは(n,nH). @param[in] Alpha ハウスホルダー・ベクトルの規格化定数の格納用の配列.サイズはnH. @param[in] x 変換されるx. @param[out] h 生成されたハウスホルダー・ベクトル. @param[out] alpha 生成されたハウスホルダー・ベクトルの規格化定数. */ void rhouseholder(int n, int k0, int nH, int k, rmulti **h, rmulti *alpha, rmulti **H, int LDH, rmulti **Alpha, rmulti **x) { int j,l,p0,p1,prec; rmulti *value=NULL,**R=NULL; // allocate p0=rget_prec(alpha); p1=rvec_get_prec_max(n,h); prec=MAX2(p0,p1); value=rallocate_prec(prec); R=rvec_allocate_prec(n,prec); rvec_copy(n,R,x); // R=(I-alpha*h*h')*R=R-alpha*h*(h'*R) for(j=0; j<nH; j++){ // R=R-alpha[j]*(H(:,j)'*R)*H(:,j) l=k0+j; rvec_sum_mul(value,n-l,&MAT(H,l,j,LDH),&R[l]); rmul(value,value,Alpha[j]); rvec_sub_mul_r(n-l,&R[l],&MAT(H,l,j,LDH),value); } rhouseholder_vec(n,k,h,alpha,R); // free value=rfree(value); R=rvec_free(n,R); }
int sphere_refraction_func(const struct TObject *object, const Ray *ray, const Ray *reflect, const Vector *pt, const Vector *n, Ray *refract, Vector *attenuation) { // 入射角i const Sphere *sp = (const Sphere*)object->priv; double cosi = dot(n, &ray->front) / modulation(n) / modulation(&ray->front); double sini = sqrt(1 - cosi*cosi); double sinr; Vector vin = *n; if (cosi < 0) { // 空气到介质 sinr = sini / sp->refractive; } else { // 介质到空气 sinr = sini * sp->refractive; vin = rmul(n, -1); // 全反射 if (sinr >= 1) return 0; } double r = asin(sinr); Vector left = vcross(vcross(*n, ray->front), *n); normalize(&left); Vector nn = *n; normalize(&nn); refract->front = vadd(vrmul(left, sin(r)), vrmul(nn, cos(r))); refract->pos = *pt; *attenuation = sp->refract_attenuation; return 1; }
/******************************** process_frame() ***********************************/ void process_frame(void) { int k, m; int io_ptr0; float *Mptr; /* work out fraction of available CPU time used by algorithm */ cpufrac = ((float) (io_ptr & (FRAMEINC - 1)))/FRAMEINC; /* wait until io_ptr is at the start of the current frame */ while((io_ptr/FRAMEINC) != frame_ptr); /* then increment the framecount (wrapping if required) */ if (++frame_ptr >= (CIRCBUF/FRAMEINC)) frame_ptr=0; /* save a pointer to the position in the I/O buffers (inbuffer/outbuffer) where the data should be read (inbuffer) and saved (outbuffer) for the purpose of processing */ io_ptr0=frame_ptr * FRAMEINC; /* copy input data from inbuffer into inframe (starting from the pointer position) */ m=io_ptr0; for (k=0;k<FFTLEN;k++) { inframe[k] = inbuffer[m] * inwin[k]; if (++m >= CIRCBUF) m=0; /* wrap if required */ } /************************* DO PROCESSING OF FRAME HERE **************************/ //##################################################################################### //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //|||||||||||||||||||||||||||||||||||||Added Code Here||||||||||||||||||||||||||||||||| //copy values to complex variable to do fft for (k=0;k<FFTLEN;k++) { C[k] = cmplx(inframe[k],0);/* copy input to complex */ } //do the fft fft(FFTLEN,C); #if optim6a == 1 snr_count++; //reset input power variables to 0 S_Power = 0; if (snr_count >= f_count_loop/3) { N_Power = 0; } #endif //now start processing on those values for (k=0;k<FFTLEN/2;k++) { //init X(w) X[k] = cabs(C[k]); #if optim6a //calculate power S_Power += X[k]*X[k]; #endif #if optim1 == 1 X[k] = X[k]*(1-K)+K*Ptm1[k]; //Ptm1[k] = X[k]; #elif optim2 == 1 X[k] = (X[k]*X[k])*(1-K)+K*(Ptm1[k]*Ptm1[k]); X[k] = sqrt(X[k]); //Ptm1[k] = X[k]; #endif //M1(w) = min(|X(w)|,M1(w)) if (M1[k]>X[k]){ M1[k] = X[k]; } //N(w) = alpha*min(M1,M2,M3,M4) #if optim3 == 1 N[k] = (1-K)*alpha*lpfcoef[k]*min(min(M1[k],M2[k]),min(M3[k],M4[k]))+K*Ntm1[k]; Ntm1[k] = N[k]; #else N[k] = alpha*lpfcoef[k]*min(min(M1[k],M2[k]),min(M3[k],M4[k])); #endif #if (optim6a == 1) if (snr_count >= f_count_loop/3) { N_Power += N[k]*N[k]/(alpha*alpha*lpfcoef[k]*lpfcoef[k]); } #endif //Y(w) variations #if optim4a == 1 C[k] = rmul(max(lambda*(N[k]/X[k]),1-(N[k]/X[k])),C[k]); #elif optim4b == 1 C[k] = rmul(max(lambda*(Ptm1[k]/X[k]),1-(N[k]/X[k])),C[k]); #elif optim4c == 1 C[k] = rmul(max(lambda*(N[k]/Ptm1[k]),1-(N[k]/Ptm1[k])),C[k]); #elif optim4d == 1 C[k] = rmul(max(lambda,1-(N[k]/Ptm1[k])),C[k]); #elif optim5 == 1 final =sqrt(1-((N[k]*N[k])/(X[k]*X[k]))); C[k] = rmul(max(lambda,final),C[k]); #else //Y(w) = X(w)*max(lambda,g(w)) C[k] = rmul(max(lambda,1-(N[k]/X[k])),C[k]); #endif #if ((optim1)||(optim2) == 1) Ptm1[k] = X[k]; #endif }
/******************* Wait for buffer of data to be input/output **********************/ void wait_buffer(void) { float *p; int k,n; double w; double angle = (2*PI)/(double)BUFFLEN; //complex *dftBuffer; //complex complexBuffer[BUFFLEN]; int i = 0; /* wait for array index to be set to zero by ISR */ while(index); /* rotate data arrays */ p = input; input = output; output = intermediate; intermediate = p; /************************* DO PROCESSING OF FRAME HERE **************************/ /*for (i =0; i<BUFFLEN; i++) { *(complexBuffer+i) = cmplx(intermediate[i],0); } */ //dft1(BUFFLEN, complexBuffer); for(k=0; k<BUFFLEN; k++){ outBuffer[k] = cmplx(0,0); for (n=0; n<BUFFLEN; n++){ w=angle*n*k; outBuffer[k] = cadd(outBuffer[k],rmul(intermediate[n], cexp(cmplx(0,-w)))); //outBuffer[k].r += complexBuffer[n].r*cos(w)+complexBuffer[n].i*sin(w); //outBuffer[k].i += complexBuffer[n].i*cos(w)-complexBuffer[n].r*sin(w); } } //memcpy(complexBuffer, outBuffer, BUFFLEN); //fft(BUFFLEN,complexBuffer); //this is really bad // for (i=0;i<BUFFLEN;i++){ // //*(mag+i) = 3; // *(mag+i) = cabs(*(complexBuffer+i)); // } ifft(BUFFLEN, outBuffer); for (i =0; i<BUFFLEN; i++) { *(intermediate+i) = outBuffer[i].r; } // for(n=0; n<BUFFLEN; n++){ // intermediate[n] = 0; // for (k=0; k<BUFFLEN; k++){ // w=angle*n*k; // intermediate[n] += (cmul(outBuffer[k], cexp(cmplx(0,w)))).r / BUFFLEN; // //outBuffer[k].r += complexBuffer[n].r*cos(w)+complexBuffer[n].i*sin(w); // //outBuffer[k].i += complexBuffer[n].i*cos(w)-complexBuffer[n].r*sin(w); // } // } //free(complexBuffer); /**********************************************************************************/ /* wait here in case next sample has not yet been read in */ while(!index); }
///vswitch is original setting with no filtering,G = max(lambda,1-|N|/|X|), void ProcVswitch(void) { int k; ////////////////////////////////// //compute FFT of incoming frame // ////////////////////////////////// fft(FFTLEN, inframe); for (k = 0; k < FFTLEN; k++) { /* calculate absolute of fft bin of signal as phase unknown */ frame_mag[k] = cabs(inframe[k]); /** * enchancement 2: in_filt 2 * low pass filter |X(\omega)|^2, then sqrt * Thereotically closest to paper approach as the estimator is estimating noise power and not noise magnitude. */ if (in_filt == 2) { P[k] = (1 - K) * (frame_mag[k] * frame_mag[k]) + K * last_P[k]; last_P[k] = P[k]; } else { /** * enchancement 1: in_filt 1 * P_t(\omega)=(1-k) x abs(X(\omega))+k x P_t-1(\omega) * LPF on noise estimator, used to smooth subbands * Current frame * times + last frame due to decimation */ if (in_filt == 1) { P[k] = (1 - K) * frame_mag[k] + K * last_P[k]; last_P[k] = P[k]; } } } //downcounter to keep track of which frame for processing //execute at margins between adjacent frames if (--frame_count == -1) { frame_count = min_frame_rate; //handle wraparound of min_buffer_index so 0->3 if (++min_buffer_index > OVERSAMP - 1)min_buffer_index = 0; for (k = 0; k < FFTLEN; k++) { if (in_filt == 2) { frame_min_buffer[min_buffer_index * FFTLEN + k] = sqrt(P[k]); } else { if (in_filt == 1) { frame_min_buffer[min_buffer_index * FFTLEN + k] = P[k]; } else { frame_min_buffer[min_buffer_index * FFTLEN + k] = frame_mag[k]; } } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //main objective: implement the spectral subtraction technique // //Assuming non-musical noise, so no convolution between speech and noise. // //Therefore, all noise is in the background and statistically independent, therefore treat as additive noise, so the subtraction technique works. // //Peaks in FFT correspond to speech, therefore find lowest valley (local minimum thereotically be noise). // //In C, compare minimum by looping through frequency bins, and find minimum frequency bins // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// else { for (k = 0; k < FFTLEN; k++) { if (in_filt == 2) { if (sqrt(P[k]) < frame_min_buffer[min_buffer_index * FFTLEN + k])frame_min_buffer[min_buffer_index * FFTLEN + k] = sqrt(P[k]); } else { if ( in_filt == 1) { if (P[k] < frame_min_buffer[min_buffer_index * FFTLEN + k])frame_min_buffer[min_buffer_index * FFTLEN + k] = P[k]; } else { if (frame_mag[k] < frame_min_buffer[min_buffer_index * FFTLEN + k])frame_min_buffer[min_buffer_index * FFTLEN + k] = frame_mag[k]; } } } } for (k = 0; k < FFTLEN; k++) { { //enchancement 6, calc snr, and adjust alpha if (enchance6) { SNRtmp = (frame_mag[k]*frame_mag[k]) / (N[k]*N[k]); //SNR = 10 * ilog10c(SNRtmp); //log10 lookup table alphatmp = ilog10c(SNRtmp); } } N[k] = alphatmp * min(min(frame_min_buffer[k], frame_min_buffer[FFTLEN + k]), min(frame_min_buffer[k + FFTLEN * 2], frame_min_buffer[k + FFTLEN * 3])); /** * enchancement 3: * LPF the noise estimate to prevent subtracting discontinuties * Smooth noise estimator using LPF */ if (out_filt == 1) { PP[k] = (1 - K) * N[k] + K * last_PP[k]; last_PP[k] = PP[k]; N[k] = PP[k]; } //no knowledge of phase, therefore subtraction is done by multiplying by a gain //N/X = inverse SNR //1 - N/X = magnitude - noise if (RNR_flag == 1) { if ((last_N[k] / last_frame_mag[k]) > RNR_threhold) { RNRbuffer[k] = complex_min(inframe[k], complex_min(last_frame[k], last_last_frame[k])); } else { RNRbuffer[k] = last_frame[k]; switch (gain_var) { case 0: inframe[k] = rmul(max(lambda, (1 - N[k] / frame_mag[k])), inframe[k]); break; case 1: inframe[k] = rmul(max(lambda * N[k] / frame_mag[k], (1 - N[k] / frame_mag[k])), inframe[k]); break; case 2: inframe[k] = rmul(max(lambda * P[k] / frame_mag[k], (1 - N[k] / frame_mag[k])), inframe[k]); break; case 3: inframe[k] = rmul(max(lambda * N[k] / P[k], (1 - N[k] / P[k])), inframe[k]); break; case 4: inframe[k] = rmul(max(lambda, (1 - N[k] / P[k])), inframe[k]); break; case 5: inframe[k] = rmul(max(lambda, sqrt(1 - (N[k] * N[k]) / (frame_mag[k] * frame_mag[k]))), inframe[k]); break; case 6: inframe[k] = rmul(max(lambda * N[k] / frame_mag[k], sqrt((1 - (N[k] * N[k]) / (frame_mag[k] * frame_mag[k])))), inframe[k]); break; case 7: inframe[k] = rmul(max(lambda * P[k] / frame_mag[k], sqrt((1 - (N[k] * N[k]) / (frame_mag[k] * frame_mag[k])))), inframe[k]); break; case 8: inframe[k] = rmul(max(lambda * N[k] / P[k], sqrt((1 - (N[k] * N[k]) / (P[k] * P[k])))), inframe[k]); break; case 9: inframe[k] = rmul(max(lambda, sqrt((1 - (N[k] * N[k]) / (P[k] * P[k])))), inframe[k]); break; default: inframe[k] = rmul(max(lambda, (1 - N[k] / frame_mag[k])), inframe[k]); break; } } last_frame_mag[k] = frame_mag[k]; last_last_frame[k] = last_frame[k]; last_frame[k] = inframe[k]; last_N[k] = N[k]; } else { switch (gain_var) { case 0: inframe[k] = rmul(max(lambda, (1 - N[k] / frame_mag[k])), inframe[k]); break; case 1: inframe[k] = rmul(max(lambda * N[k] / frame_mag[k], (1 - N[k] / frame_mag[k])), inframe[k]); break; case 2: inframe[k] = rmul(max(lambda * P[k] / frame_mag[k], (1 - N[k] / frame_mag[k])), inframe[k]); break; case 3: inframe[k] = rmul(max(lambda * N[k] / P[k], (1 - N[k] / P[k])), inframe[k]); break; case 4: inframe[k] = rmul(max(lambda, (1 - N[k] / P[k])), inframe[k]); break; case 5: inframe[k] = rmul(max(lambda, sqrt(1 - (N[k] * N[k]) / (frame_mag[k] * frame_mag[k]))), inframe[k]); break; case 6: inframe[k] = rmul(max(lambda * N[k] / frame_mag[k], sqrt((1 - (N[k] * N[k]) / (frame_mag[k] * frame_mag[k])))), inframe[k]); break; case 7: inframe[k] = rmul(max(lambda * P[k] / frame_mag[k], sqrt((1 - (N[k] * N[k]) / (frame_mag[k] * frame_mag[k])))), inframe[k]); break; case 8: inframe[k] = rmul(max(lambda * N[k] / P[k], sqrt((1 - (N[k] * N[k]) / (P[k] * P[k])))), inframe[k]); break; case 9: inframe[k] = rmul(max(lambda, sqrt((1 - (N[k] * N[k]) / (P[k] * P[k])))), inframe[k]); break; //extra improvements case 10: inframe[k] = rmul(max(sqrt(lambda) * N[k], 1-N[k]/P[k]), inframe[k]); case 11: { delta = (k<32)?1:(k<64)?1.5:2.5; inframe[k] = rmul(max(lambda, 1-sqrt((P[k]*P[k]-alphatmp*delta*N[k]*N[k])/(frame_mag[k]*frame_mag[k]))), inframe[k]); } default: inframe[k] = rmul(max(lambda, (1 - N[k] / frame_mag[k])), inframe[k]); break; } } } //////////////////////////////////////////////////// //compute ifft of frame to go back to time domain // //////////////////////////////////////////////////// if (RNR_flag == 0) { ifft(FFTLEN, inframe); } else { ifft(FFTLEN, RNRbuffer); } }
void riep_dhToda_TN(int m, int M, rmulti **A, int LDA, rmulti **Q, int LDQ, rmulti **E, rmulti **lambda, rmulti **c, int debug) { int prec=0,k=0,n=0,f_size=0,n_size=0,*Q0_size=NULL,*E0_size=NULL,LDQ1; rmulti *a=NULL,**f=NULL,***Q0=NULL,***E0=NULL,**sigma=NULL,**Q1=NULL,**E1=NULL; // init n_size=(M+1)*(m-1)+2*M; // precision prec=rmat_get_prec_max(m,m,A,LDA); // allocate if(Q==NULL) { LDQ1=m; Q1=rmat_allocate_prec(m,M,prec); } else { LDQ1=LDQ; Q1=Q; } if(E==NULL) { E1=rvec_allocate_prec(m,prec); } else { E1=E; } sigma=rvec_allocate_prec(m,prec); a=rallocate_prec(prec); Q0_size=ivec_allocate(m); Q0=malloc(m*sizeof(rmulti**)); for(k=0; k<m; k++) { Q0_size[k]=n_size-k*(M+1); Q0[k]=rvec_allocate_prec(Q0_size[k],prec); } E0_size=ivec_allocate(m); E0=malloc(m*sizeof(rmulti**)); for(k=0; k<m; k++) { E0_size[k]=n_size-(k+1)*(M+1)+1; E0[k]=rvec_allocate_prec(E0_size[k],prec); } f_size=Q0_size[0]+1; f=rvec_allocate_prec(f_size,prec); // generate sigma[n] rinv_d(a,M); rvec_pow_r(m,sigma,lambda,a); // generate f[n] for(n=0; n<f_size; n++) { rset_d(f[n],0); for(k=0; k<m; k++) { rpow_si(a,sigma[k],n); // a=(sigma[i])^n radd_mul(f[n],c[k],a); // f[i]=f[i]+c[i]*(sigma[i])^n } } // Q[n][0]=f[n+1]/f[n] for(n=0; n<Q0_size[0]; n++) { if(n+1<f_size) { rdiv(Q0[0][n],f[n+1],f[n]); } else { rset_nan(Q0[0][n]); } } // E[0][n]=Q[0][n+M]-Q[0][n]; k=0; for(n=0; n<E0_size[k]; n++) { if(n+M<Q0_size[k] && n<Q0_size[k]) { rsub(E0[k][n],Q0[k][n+M],Q0[k][n]); } else { rset_nan(E0[k][n]); } } // loop for QE-table for(k=1; k<m; k++) { // Q[k][n]=(E[k-1][n+1]*Q[k-1][n+M])/E[k-1][n]; for(n=0; n<Q0_size[k]; n++) { rdiv(a,E0[k-1][n+1],E0[k-1][n]); rmul(Q0[k][n],a,Q0[k-1][n+M]); } // E[k][n]=Q[k][n+M]-Q[k][n]+E[k-1][n+1] for(n=0; n<E0_size[k]; n++) { rsub(a,Q0[k][n+M],Q0[k][n]); radd(E0[k][n],a,E0[k-1][n+1]); } } // debug if(debug>0) { printf("Q=\n"); for(n=0; n<n_size; n++) { for(k=0; k<m; k++) { if(n<Q0_size[k]) { mpfr_printf("%.3Re ",Q0[k][n]); } } printf("\n"); } printf("E=\n"); for(n=0; n<n_size; n++) { for(k=0; k<m; k++) { if(n<E0_size[k]) { mpfr_printf("%.3Re ",E0[k][n]); } } printf("\n"); } } // generate vector E for(k=0; k<m; k++) { rcopy(E1[k],E0[k][0]); } // generate matrix Q for(n=0; n<M; n++) { for(k=0; k<m; k++) { rcopy(MAT(Q1,k,n,LDQ1),Q0[k][n]); } } // genrate matrix A if(A!=NULL) { riep_dhToda_QE_to_A(m,M,A,LDA,Q1,LDQ1,E1,debug); } // done if(Q==NULL) { Q1=rmat_free(LDQ1,M,Q1); } else { Q1=NULL; } if(E==NULL) { E1=rvec_free(m,E1); } else { E1=NULL; } a=rfree(a); f=rvec_free(f_size,f); sigma=rvec_free(m,sigma); for(k=0; k<m; k++) { Q0[k]=rvec_free(Q0_size[k],Q0[k]); } free(Q0); Q0=NULL; for(k=0; k<m; k++) { E0[k]=rvec_free(E0_size[k],E0[k]); } free(E0); E0=NULL; Q0_size=ivec_free(Q0_size); E0_size=ivec_free(E0_size); return; }
/******************************** process_frame() ***********************************/ void process_frame(void) { int k, m; int io_ptr0; float cabsInput; float cabs2Input; float *tmpM; float tmpMinNoise; float tmpResult; //static float noiseRatio; //static float prevNoiseRatio; complex tmpMin3Frames; /* work out fraction of available CPU time used by algorithm */ cpuFrac = ((float) (io_ptr & (FRAMEINC - 1)))/FRAMEINC; /* wait until io_ptr is at the start of the current frame */ while((io_ptr/FRAMEINC) != frame_ptr); /* then increment the framecount (wrapping if required) */ if (++frame_ptr >= (CIRCBUFFLEN/FRAMEINC)) frame_ptr=0; /* save a pointer to the position in the I/O buffers (inBuffer/outBuffer) where the data should be read (inBuffer) and saved (outBuffer) for the purpose of processing */ io_ptr0=frame_ptr * FRAMEINC; /* copy input data from inBuffer into inFrame (starting from the pointer position) */ m=io_ptr0; //start from current io_ptr for (k=0;k<FFTLEN;k++) { inFrame[k] = inBuffer[m] * inWin[k]; //windows the input samples if (++m >= CIRCBUFFLEN) m=0; /* wrap if required */ } /************************* DO PROCESSING OF FRAME HERE **************************/ /* please add your code, at the moment the code simply copies the input to the ouptut with no processing */ for (k=0;k<FFTLEN;k++){ //copy inFrame to working set for FFT fft_input[k].r = inFrame[k]; fft_input[k].i = 0; } fft(FFTLEN,fft_input); //************** shuffle noise buffers *******************// if(++numFrames >= FRAMESLIMIT){ numFrames = 0; tmpM = M4; M4 = M3; M3 = M2; M2 = M1; M1 = tmpM; } //*************** processing *****************************// for(k=0; k<NFREQ; k++){ cabsInput = cabs(fft_input[k]); //|X(w)| cabs2Input = cabsInput*cabsInput; //|X(w)|**2 //************** ENHANCEMENT 1/2 *****************// if (enhancement1or2 == 1) lowPass[k] = (1-inputLPF_k)*cabsInput +inputLPF_k*lowPass[k]; //lowpass formula from notes else if (enhancement1or2 == 2) lowPass[k] = sqrt((1-inputLPF_k)*cabs2Input+ inputLPF_k*lowPass[k]*lowPass[k]); //lowpass on power else lowPass[k] = cabsInput; //doesnt LPF at all if(numFrames == 0){ // if first frame after swapping noise buffer pointers M1[k] = lowPass[k]; //then youre *hapless laughter* so youre filling the entire M1 with the whole input frame. lowPass[k] is either the whole input frame OR lowpassed version based off the enhancement case //min noise is just the frame tmpMinNoise = M4[k]; //this finds the min noise out of the the buffers M4-M2 + a tiny bit M1 cos its still doing it?!! for the particular frequency k if(tmpMinNoise > M3[k]) tmpMinNoise = M3[k]; if(tmpMinNoise > M2[k]) tmpMinNoise = M2[k]; if(tmpMinNoise > M1[k]) tmpMinNoise = M1[k]; } else if (lowPass[k] < M1[k]){ //else if not the first frame then and the the current noise is smaller than whats in M1 M1[k] = lowPass[k]; //replace with the smaller one if(minM[k] > M1[k]) tmpMinNoise = M1[k]; //so if this is smaller than the current minimum, then udpate else tmpMinNoise = minM[k]; //tmpMinNoise is for enhancement 3 - single minimum(for this k because of the for loop but is declared as a float) } else tmpMinNoise = minM[k]; //************* ENHANCEMENT 3 *************// if(enhancement3 == 1) //low pass filter noise estimate minM[k] = (1- noiseLPF_k)*tmpMinNoise + noiseLPF_k*minM[k]; //lowpass formula from notes else minM[k] = tmpMinNoise; //************* ENHANCEMENT 6 *************// if(enhancement6 == 1) /* if (pureNoiseRatio < 0.1) alpha = 1; else if (pureNoiseRatio > 0.562) alpha = 5; else alpha = 8.658*pureNoiseRatio + 0.134;*/ if (pureNoiseRatio < 0.1) alpha = 1; else if (pureNoiseRatio > 1.78) alpha = 5; else alpha = 3*log10f(pureNoiseRatio) +4; prevNoiseRatio = noiseRatio; pureNoiseRatio = (minM[k]/cabsInput) ; noiseRatio = (alpha*pureNoiseRatio); //|N(w)|/|X(w)| //************* ENHANCEMENT 4 *************// if (enhancement4or5 == 4) { switch(enhancement4){ case 1: fft_gain[k] = 1.0 - noiseRatio; minNR = MIN_NR*(alpha*(minM[k])/cabsInput); break; case 2: pOverX = (lowPass[k]/cabsInput); fft_gain[k] = 1.0 - noiseRatio; minNR = MIN_NR*pOverX; break; case 3: fft_gain[k] = 1.0 - (alpha*(minM[k])/lowPass[k]); minNR = MIN_NR*(alpha*(minM[k])/lowPass[k]); break; case 4: fft_gain[k] = 1.0 - (alpha*(minM[k])/lowPass[k]); minNR = MIN_NR; break; default: fft_gain[k] = 1.0 - (noiseRatio); minNR = MIN_NR; break; } } else if (enhancement4or5 == 5){ switch(enhancement5){ case 1: // tmpResult = ((alpha*alpha*(minM[k])*(minM[k]))/cabs2Input); tmpResult = pureNoiseRatio*pureNoiseRatio; fft_gain[k] = sqrt(1.0 - tmpResult); minNR = MIN_NR*tmpResult; break; case 2: //fft_gain[k] = sqrt(1.0 - ((alpha*alpha*(minM[k])*(minM[k]))/cabs2Input)); fft_gain[k] = sqrt(1.0 - ((lowPass[k]/cabsInput)*(lowPass[k]/cabsInput)) ); minNR = MIN_NR*(lowPass[k]/cabsInput); break; case 3: tmpResult = ((minM[k]*minM[k])/(lowPass[k]*lowPass[k])); fft_gain[k] = sqrt(1.0 - tmpResult); minNR = MIN_NR*tmpResult; break; case 4: fft_gain[k] = sqrt(1.0 - ((minM[k]*minM[k])/(lowPass[k]*lowPass[k]))); minNR = MIN_NR; break; default: fft_gain[k] = sqrt(1.0 - (minM[k]*minM[k])/cabs2Input); minNR = MIN_NR; break; } } else { fft_gain[k] = 1.0 - noiseRatio; minNR = MIN_NR; } //vanilla g(w) which is the gain for each frequency fft_gain[k] = maxOfFloats(minNR,fft_gain[k]); //where minNR is lambda in note, //************* ENHANCEMENT 8 ***************// if (enhancement8==1) { autoRecent3Frames[k].s[2] = autoRecent3Frames[k].s[1]; autoRecent3Frames[k].s[1] = autoRecent3Frames[k].s[0]; autoRecent3Frames[k].s[0] = rmul(fft_gain[k], fft_input[k]); if ( prevNoiseRatio > noiseRatioThresh) { tmpMin3Frames = autoRecent3Frames[k].s[0]; if (cabs(tmpMin3Frames) > cabs(autoRecent3Frames[k].s[1])) tmpMin3Frames = autoRecent3Frames[k].s[1]; if (cabs(tmpMin3Frames) > cabs(autoRecent3Frames[k].s[2])) tmpMin3Frames = autoRecent3Frames[k].s[2]; fft_input[k] = tmpMin3Frames; } else { fft_input[k] = autoRecent3Frames[k].s[1]; } } //enhancement 8 off else{ fft_input[k] = rmul(fft_gain[k], fft_input[k]); } if (k!=0 || k!=NFREQ-1) fft_input[FFTLEN-k] = conjg(fft_input[k]); //symmetric copy }//end of giantfor loop iterating over frequencies ifft(FFTLEN,fft_input); //TODO ask about the fft and why its half, and what do we do with the other empty array size. if (original == 1) { for (k=0;k<FFTLEN;k++) //loop over the current frame outFrame[k] = inFrame[k];// copy input straight into output } else{ for (k=0;k<FFTLEN;k++){ //loop over the current frame outFrame[k] = (fft_input[k].r);// copy input straight into output } } /********************************************************************************/ /* multiply outFrame by output window and overlap-add into output buffer */ m=io_ptr0;//start from current io_ptr for (k=0;k<(FFTLEN-FRAMEINC);k++) { /* this loop adds into outBuffer */ outBuffer[m] = outBuffer[m]+outFrame[k]*outWin[k]; if (++m >= CIRCBUFFLEN) m=0; /* wrap if required */ } for (;k<FFTLEN;k++) { outBuffer[m] = outFrame[k]*outWin[k]; /* this loop over-writes outBuffer */ m++; } }
IExprNode* mul(const std::vector<Token>& tokens, int &curToken) { IExprNode *left = factor(tokens, curToken); return rmul(tokens, curToken, left); }