void fsinh(_MIPD_ flash x,flash y) { /* calculate y=sinh(x) */ int op[5]; #ifdef MR_OS_THREADS miracl *mr_mip=get_mip(); #endif copy(x,y); if (mr_mip->ERNUM || size(y)==0) return; MR_IN(65) fexp(_MIPP_ y,y); op[0]=0xC6; op[2]=op[3]=op[4]=1; op[1]=(-1); flop(_MIPP_ y,y,op,y); MR_OUT }
void ftanh(_MIPD_ flash x,flash y) { /* calculates y=tanh(x) */ int op[5]; #ifndef MR_GENERIC_MT miracl *mr_mip=get_mip(); #endif copy(x,y); if (mr_mip->ERNUM || size(y)==0) return; MR_IN(63) fexp(_MIPP_ y,y); op[0]=0x33; op[1]=op[3]=op[4]=1; op[2]=(-1); flop(_MIPP_ y,y,op,y); MR_OUT }
void fcosh(_MIPD_ flash x,flash y) { /* calculate y=cosh(x) */ int op[5]; #ifdef MR_OS_THREADS miracl *mr_mip=get_mip(); #endif copy(x,y); if (mr_mip->ERNUM || size(y)==0) { convert(_MIPP_ 1,y); return; } MR_IN(67) fexp(_MIPP_ y,y); op[0]=0xC6; op[1]=op[2]=op[3]=op[4]=1; flop(_MIPP_ y,y,op,y); MR_OUT }
int main() { /* Brents example program */ flash x,pi; miracl *mip=mirsys(-35,0); x=mirvar(0); pi=mirvar(0); mip->RPOINT=ON; printf("Calculating pi..\n"); fpi(pi); cotnum(pi,stdout); /* output pi */ printf("Calculating exp(pi*(163/9)^0.5)\n"); fconv(163,9,x); froot(x,2,x); fmul(x,pi,x); fexp(x,x); cotnum(x,stdout); printf("Calculating exp(pi*(163)^0.5)\n"); fpower(x,3,x); cotnum(x,stdout); return 0; }
static BOOL act(int p,int q) { /* act on selected key */ int k,n,c; aprint(PRESSED,4+5*p,6+3*q,keys[q][p]); switch(p+7*q) { case 0: if (degrees) fmul(x,radeg,x); if (hyp) fsinh(x,x); else fsin(x,x); newx=TRUE; break; case 1: if (degrees) fmul(x,radeg,x); if (hyp) fcosh(x,x); else fcos(x,x); newx=TRUE; break; case 2: if (degrees) fmul(x,radeg,x); if (hyp) ftanh(x,x); else ftan(x,x); newx=TRUE; break; case 3: if (lgbase>0) { n=size(x); if (abs(n)<MR_TOOBIG) { convert(lgbase,x); if (n<0) frecip(x,x); fpower(x,abs(n),x); newx=TRUE; break; } if (lgbase==2) fmul(x,loge2,x); if (lgbase==10) fmul(x,loge10,x); } fexp(x,x); newx=TRUE; break; case 4: mip->RPOINT=!mip->RPOINT; newx=TRUE; break; case 5: clrall(); newx=TRUE; break; case 6: return TRUE; case 7: if (hyp) fasinh(x,x); else fasin(x,x); if (degrees) fdiv(x,radeg,x); newx=TRUE; break; case 8: if (hyp) facosh(x,x); else facos(x,x); if (degrees) fdiv(x,radeg,x); newx=TRUE; break; case 9: if (hyp) fatanh(x,x); else fatan(x,x); if (degrees) fdiv(x,radeg,x); newx=TRUE; break; case 10: flog(x,x); if (lgbase==2) fdiv(x,loge2,x); if (lgbase==10) fdiv(x,loge10,x); newx=TRUE; break; case 11: newx=TRUE; k=3; forever { aprint(INVER,2+stptr[k],2,settings[k][option[k]]); curser(2+stptr[k],2); c=arrow(gethit()); if (c==1) { if (option[k]==nops[k]) option[k]=0; else option[k]+=1; continue; } aprint(STATCOL,2+stptr[k],2,settings[k][option[k]]); if (c==0 || c==2) break; if (c==4 && k>0) k--; if (c==3 && k<3) k++; } setopts(); break; case 12: chekit(7); break; case 13: result=FALSE; if (ipt==0) break; ipt--; mybuff[ipt]='\0'; if (ipt==0) clr(); just(mybuff); cinstr(x,mybuff); newx=TRUE; break; case 14: if (!next('7')) putchar(BELL); break; case 15: if (!next('8')) putchar(BELL); break; case 16: if (!next('9')) putchar(BELL); break; case 17: chekit(6); break; case 18: chekit(5); break; case 19: chekit(4); break; case 20: copy(m,x); newx=TRUE; break; case 21: if (!next('4')) putchar(BELL); break; case 22: if (!next('5')) putchar(BELL); break; case 23: if (!next('6')) putchar(BELL); break; case 24: fmul(x,x,x); newx=TRUE; break; case 25: froot(x,2,x); newx=TRUE; break; case 26: chekit(3); break; case 27: brkt=0; chekit(0); flag=OFF; fadd(m,x,m); newx=TRUE; break; case 28: if (!next('1')) putchar(BELL); break; case 29: if (!next('2')) putchar(BELL); break; case 30: if (!next('3')) putchar(BELL); break; case 31: frecip(x,x); newx=TRUE; break; case 32: fpi(x); newx=TRUE; break; case 33: chekit(2); break; case 34: negify(x,x); newx=TRUE; break; case 35: if (!next('0')) putchar(BELL); break; case 36: if (!next('/')) putchar(BELL); break; case 37: if (!next('.')) putchar(BELL); break; case 38: if (ipt>0) { putchar(BELL); result=FALSE; } else { zero(x); brkt+=1; newx=TRUE; } break; case 39: if (brkt>0) { chekit(0); brkt-=1; } else { putchar(BELL); result=FALSE; } break; case 40: chekit(1); break; case 41: brkt=0; equals(0); flag=OFF; break; } return FALSE; }
bool MeshKD::loadObjFromFile ( std::string filename ) { std::vector<Vector3> vectorVector; // SCNR std::vector<Vector3> normalVector; std::cout << "reading " << filename << std::endl; std::ifstream file ( filename.c_str() ); if ( !file.is_open() ) { std::cerr << "Couldn't load mesh " << filename << std::endl; return false; } //build regexes boost::regex vexp ( "^v\\s+(.*?)\\s+(.*?)\\s+(.*?)$" ); boost::regex fexp ( "^f\\s+(.*?)\\s+(.*?)\\s+(.*?)$" ); boost::cmatch match; char line[1024]; while ( !file.eof() ) { file.getline ( line,1024 ); // std::cout << line << std::endl; if ( boost::regex_match ( line, match, vexp ) ) { Vector3 v = Vector3 ( boost::lexical_cast<double> ( match[1] ), boost::lexical_cast<double> ( match[2] ), boost::lexical_cast<double> ( match[3] ) ); vectorVector.push_back ( v ); } else if ( boost::regex_match ( line, match, fexp ) ) { Vector3 a, b, c; int vectorSize = vectorVector.size(); if ( boost::lexical_cast<int> ( match[1] ) < 0 ) a = vectorVector[vectorSize + boost::lexical_cast<int> ( match[1] ) ]; if ( boost::lexical_cast<int> ( match[1] ) > 0 ) a = vectorVector[boost::lexical_cast<int> ( match[1] )-1]; if ( boost::lexical_cast<int> ( match[2] ) < 0 ) b = vectorVector[vectorSize + boost::lexical_cast<int> ( match[2] ) ]; if ( boost::lexical_cast<int> ( match[2] ) > 0 ) b = vectorVector[boost::lexical_cast<int> ( match[2] )-1]; if ( boost::lexical_cast<int> ( match[3] ) < 0 ) c = vectorVector[vectorSize + boost::lexical_cast<int> ( match[3] ) ]; if ( boost::lexical_cast<int> ( match[3] ) > 0 ) c = vectorVector[boost::lexical_cast<int> ( match[3] )-1]; tree.addMeshKDTriangle ( new MeshKDTriangle ( a,b,c ) ); } } /* std::cout << "Finished reading" << std::endl; std::cout << "Loaded " << ( unsigned int ) tris.size() << " faces" << std::endl; std::cout << "Building tree" << std::endl; std::cout << "Finished building tree" << std::endl; tris.dump(); oa <<const_cast<const OctreeNode&> ( tris );*/ this->boundingBoxComputed = true; std::cout << "Building tree" << std::endl; tree.build ( AXIS_X ); std::cout << "Finished building tree" << std::endl; std::cout << "Tree countains " << tree.getMeshKDTriangleCount() << " tris" << std::endl; return true; }
int main() { const Cadena NombreM("Jose Manuel Barba Gonzalez"); Fecha fecM(21,2,1982); Fecha fpubli(1,1,1970); Fecha fexp(3,7,2015); const Fecha fString("1/1/1970"); Fecha f; const char *user = "******"; Cadena art_id("110"); Cadena art_nom("Programación C"); Cadena cadM(user); //Numero Num_tjtM("1234 9840 9482 3847"); // Numero Num_tjtM("378282246310005"); Cadena userid("001"); Cadena userNom("Jose M"); Cadena userApll("Barba Gonzalez"); Cadena userDir("su casa"); // const Clave userPass("holas"); //istringstream isM("2/4/2006"); //ostringstream os(""); cout << "---" << endl; //isM >> f; /* Usuario userM(userid,userNom,userApll,userDir,userPass); Tarjeta TjtM(Num_tjtM,userM,fexp); Autor autM(userNom,userApll,userDir); Articulo::Autores autores = crea_autores(autM); Libro artM(autores,art_id,art_nom,fpubli,50.55,200,100); InformeDigital InfDigM(autores,art_id,art_nom,fpubli,50.55,fexp); */ cout << "Main\n" << NombreM << endl << cadM << "." << endl; cout << "---Cadena---" << endl; cout << "Extraccion: una palabra --- "; istringstream is("bueno bonito barato"); Cadena c("algo"); is >> c; if(c == "bueno" && is.peek() == ' ') cout << "OK." << endl; cout << c << endl; cout << endl << "---Fecha---" << endl; cout << "Fecha de Nacimiento: "; fecM.visualizar(); cout << "Fecha como cadena: "; fString.visualizar(); Fecha g(--fecM);g.visualizar();g.restadias(3);g.visualizar(); ++g;g.visualizar();g.sumadias(2);g.visualizar(); cout << "---" << endl; //f.visualizar(); //cout << f; /* cout << "\n---Articulo Libro---\n" << artM; cout << "\n---Articulo InformeDigital---\n" << "A la venta hasta el "; InfDigM.imp_esp(os); cout << "\n---Tarjeta---\n" << TjtM.tarjeta() << endl; cout << "\n---Usuario---\n" << userM.id() << "|" << userM.nombre() << " " << userM.apellidos() << "|" << userM.direccion() << "|" << userPass.clave() << endl; if(userPass.verifica("holas")) cout << "Verificada" << endl; else cout << "No Verificada" << endl; */ return 0; }
void LeakageSubtractor(const std::vector<float> *time, std::vector<float> *cathode, const std::vector<float> *hv, const std::vector<float> *led, std::string file, std::vector<float>& cathode_out, chi2& fit ){ // 1 -> tries different values // 2 -> uses the value of the last fit as input to the next int type_minimization = 2; string str_volt; gStyle->SetOptFit(1); int skipAtLedChange = 4; int size = 0; float chi2 = 0; int spike = 0; std::vector<float> cathode_ye; float error = (0.5/16.6)*10E-12; size = cathode->size(); //the number pf voltages stored in the cathode vector //cout << "size: " << size<< endl; //declare the values of the voltage to fit std::vector<double> fitX; std::vector<double> fitXe; std::vector<double> fitY; std::vector<double> fitYe; //int nstep = 7; int step[7]={800, 900, 1000, 1200, 1400, 1600, 1800}; //write the endex of the beginning and the end of the hv regions into an array int voltageStep[7] = {0,0,0,0,0,0,0}; int index_begin[7] = {0,0,0,0,0,0,0}; int index_end[7] = {0,0,0,0,0,0,0}; int istep, i; int fcount=0; bool repeate=true; double ini1[5] = {1.0e-12, 1.0e-11, 1.0e-10, 1.0e-9, 1.0e-8}; double shifts_begin[8] = {0, 50, 100, 150, 200, 250, 300, 350}; double shifts_end[6] = {0, 50, 100, 150, 200, 250}; double ini3[5] = {100, 200, 300, 400, 500}; double ini2[6] = {1.0e-12, 1.0e-11, 1.0e-10, 1.0e-9, 1.0e-8, 1.0e-7}; int ini2_count = 0; int ini3_count = 0; int begin_count = 0; int end_count = 0; float better_chi2 = 0.0; int better[5] = {0, 0, 0, 0}; double par[3] = {0, 0, 0}; for (i = 0; i < size; i++) { cathode_ye.push_back(error); cathode_out.push_back(cathode->at(i)); } //for (i = 0; i < size-1; i++) //{ // cout << i << " " << size << " " << time->at(i) << " " << hv->at(i) << " " << led->at(i) << " " << cathode->at(i) << endl; //} //the different hight voltage steps in volt for(istep = 0; istep < 7; istep++){ int index1 = 1; int index2 = 1; //cout<<"trying voltage: "<<step[istep]<<endl; while((abs(hv->at(index1-1)) > step[istep] + 20 or abs(hv->at(index1-1)) < step[istep] - 20) and index1 < size-1){ index1++; //cout << "begin " <<index1-1 << " " << size << " " << abs(hv->at(index1-1)) << endl; } //cout << "out of begin" << endl; index_begin[istep] = index1; //cout << "index_begin " <<index_begin[istep] << endl; if (index1 < size) { index2 = index1; while((abs(hv->at(index2)) < step[istep] + 20 and abs(hv->at(index2)) > step[istep] - 20) and index2 < size-1){ index2++; //cout << "end " << index2 << " " << size << " " << abs(hv->at(index2)) << endl; } index_end[istep] = index2; //cout << "index_end " << index_end[istep] << endl; } voltageStep[istep] = index_end[istep] - index_begin[istep]; //cout << "voltage Step " << voltageStep[istep] << endl; //write the voltages into the vector // copy the x,y values for one voltage set and only where LED was off if (voltageStep[istep] > 20) { for(i = index_begin[istep]; i < index_end[istep]; i++){ if (i < size - skipAtLedChange) { //cout << i << " " << size << " " << time->at(i) << " " << led->at(i) << " " << hv->at(i) << " " << cathode->at(i) << " " << cathode_ye[i] << endl; spike_check(i, cathode, spike); if(led->at(i) == 0 && led->at(i-skipAtLedChange) == 0 && led->at(i+skipAtLedChange) == 0 && abs(hv->at(i)) < step[istep] + 20 && abs(hv->at(i)) > step[istep] - 20 && cathode->at(i) > 0 && spike == 0){ //cout << "in" << endl; if (time->at(i) < 0) { cout << "warning: negative time " << i << " " << time->at(i) << endl; } fitX.push_back(time->at(i)); fitY.push_back(cathode->at(i)); fitYe.push_back(cathode_ye[i]); fitXe.push_back(0.); } spike = 0; } } // cout << "========" << hv[index_begin[istep]] << "===========" << endl // TGraphErrors has to be defined befor TF1 (the fit function) TGraphErrors *gc0 = new TGraphErrors(fitX.size(),&fitX.front(),&fitY.front(),NULL,&fitYe.front()); TCanvas * c = new TCanvas("c","c",800,600); //gPad->SetLogy(); str_volt = str_volt + ";Time [s];Current [V]"; TF1 *ff = 0; TVirtualFitter::SetMaxIterations(3000); if (type_minimization == 2) { float par_temp[3] = {1e-9, 1e-7, 300.}; par[0] = 0; par[1] = 0; par[2] = 0; int max_tries = 70; int tries = 0; while (repeate && tries < max_tries && fitX.size() > 100) { //cout << "fcount = " << fcount << " begin_count = " << begin_count << "end_count = " << end_count << endl; ff = new TF1("ff", fexp, time->at(index_begin[istep]) + 100, time->at(index_end[istep]) - 50, 3); ff->SetLineColor(4); ff->SetParameter(0, par_temp[0]); ff->SetParameter(1, par_temp[1]); ff->SetParameter(2, par_temp[2]); //cout << "ini par[1] = " << ini1[fcount] << " begin_shift = " << shifts_begin[begin_count] << " end_shift = " << - shifts_end[end_count] << endl; gc0->Fit("ff","ERQ"); //cout << "=> " << gMinuit->fCstatu.Data() << endl; chi2 = ff->GetChisquare()/float(ff->GetNDF()); //cout << "chi2 = " << chi2 << endl; repeate = ( (gMinuit->fCstatu.Data()[0]!='S') || (ff->GetParameter(1)<0) || (ff->GetParameter(0)<0) || chi2 > 2.0); if (better_chi2 == 0.0 and gMinuit->fCstatu.Data()[0]=='S' and ff->GetParameter(1)>0 and ff->GetParameter(0)>0) { better_chi2 = chi2; par[0] = ff->GetParameter(0); par[1] = ff->GetParameter(1); par[2] = ff->GetParameter(2); par_temp[0] = ff->GetParameter(0); par_temp[1] = ff->GetParameter(1); par_temp[2] = ff->GetParameter(2); //cout << "Chi2 of the first approach = " << chi2 << endl; } if (better_chi2 > chi2 and gMinuit->fCstatu.Data()[0]=='S' and ff->GetParameter(1)>0 and ff->GetParameter(0)>0) { //cout << "ini par = " << ini1[fcount] << " begin_shift = " << shifts_begin[begin_count] << " end_shift = " << - shifts_end[end_count] << " ini3 = " << ini3[ini3_count]<< endl; //cout << "=> " << gMinuit->fCstatu.Data() << endl; //cout << "chi2 = " << chi2 << endl; better_chi2 = chi2; par[0] = ff->GetParameter(0); par[1] = ff->GetParameter(1); par[2] = ff->GetParameter(2); par_temp[0] = ff->GetParameter(0); par_temp[1] = ff->GetParameter(1); par_temp[2] = ff->GetParameter(2); } else { if (tries%6 == 0) { par_temp[0] = par[0] * 1.1; } if (tries%6 == 1) { par_temp[0] = par[0] * 0.9; } if (tries%6 == 2) { par_temp[1] = par[1] * 1.1; } if (tries%6 == 3) { par_temp[1] = par[1] * 0.9; } if (tries%6 == 4) { par_temp[2] = par[2] * 1.1; } if (tries%6 == 5) { par_temp[2] = par[2] * 0.9; } } tries = tries + 1; //cout << tries << " > " << better_chi2 << " ~ " << par[0] << " ; " << par[1] << " ; " << par[2] << endl; } if (type_minimization == 1 or chi2 > 5.0) { if (chi2 > chi2_threshold) { par[0] = 0.0; par[1] = 0.0; par[2] = 0.0; better_chi2 = 0.0; } while (repeate && fcount < 5 && begin_count < 8 && end_count < 6 && ini3_count < 5 && ini2_count < 6 && fitX.size() > 100) { //cout << "fcount = " << fcount << " begin_count = " << begin_count << "end_count = " << end_count << endl; ff = new TF1("ff", fexp, time->at(index_begin[istep]) + shifts_begin[begin_count], time->at(index_end[istep]) - shifts_end[end_count], 3); ff->SetLineColor(3); ff->SetParameter(0, ini1[fcount]); ff->SetParameter(1, ini2[ini2_count]); ff->SetParameter(2, ini3[ini3_count]); //cout << "ini par[1] = " << ini1[fcount] << " begin_shift = " << shifts_begin[begin_count] << " end_shift = " << - shifts_end[end_count] << endl; gc0->Fit("ff","ERQ"); //cout << "=> " << gMinuit->fCstatu.Data() << endl; chi2 = ff->GetChisquare()/float(ff->GetNDF()); //cout << "chi2 = " << chi2 << endl; repeate = ( (gMinuit->fCstatu.Data()[0]!='S') || (ff->GetParameter(1)<0) || (ff->GetParameter(0)<0) || chi2 > 2.0); if (better_chi2 == 0.0 and gMinuit->fCstatu.Data()[0]=='S' and ff->GetParameter(1)>0 and ff->GetParameter(0)>0) { better_chi2 = chi2; better[0] = fcount; better[1] = begin_count; better[2] = end_count; better[3] = ini3_count; better[4] = ini2_count; par[0] = ff->GetParameter(0); par[1] = ff->GetParameter(1); par[2] = ff->GetParameter(2); cout << "Chi2 of the first approach = " << chi2 << endl; } if (better_chi2 > chi2 and gMinuit->fCstatu.Data()[0]=='S' and ff->GetParameter(1)>0 and ff->GetParameter(0)>0) { //cout << "ini par = " << ini1[fcount] << " begin_shift = " << shifts_begin[begin_count] << " end_shift = " << - shifts_end[end_count] << " ini3 = " << ini3[ini3_count]<< endl; //cout << "=> " << gMinuit->fCstatu.Data() << endl; //cout << "chi2 = " << chi2 << endl; better_chi2 = chi2; better[0] = fcount; better[1] = begin_count; better[2] = end_count; better[3] = ini3_count; better[4] = ini2_count; par[0] = ff->GetParameter(0); par[1] = ff->GetParameter(1); par[2] = ff->GetParameter(2); } ini2_count++; if (ini2_count == 6) { ini3_count++; ini2_count = 0; } if (ini3_count == 5) { end_count++; ini3_count = 0; } if (end_count == 6) { begin_count++; end_count = 0; } if (begin_count == 8) { fcount++; begin_count = 0; } } if (better_chi2 > 2.0) { cout << "none of the fits was very good, using the best one!" << endl; int sb = shifts_begin[better[1]]; int se = shifts_end[better[2]]; float ig = ini1[better[0]]; float i3 = ini3[better[3]]; float i2 = ini2[better[4]]; cout << better[0] << " " << better[1] << " " << better[2] << " " << better[3] << " " << better[4] << endl; cout << ig << " " << sb << " " << se << " " << i3 << " " << i2 << endl; ff = new TF1("ff", fexp, time->at(index_begin[istep]) + sb, time->at(index_end[istep]) - se, 3); ff->SetLineColor(2); if (better_chi2 < 15.0) { ff->SetLineColor(3); } ff->SetParameter(0, ig); ff->SetParameter(1, i2); ff->SetParameter(2, i3); gc0->Fit("ff","ERQ"); cout << "=> " << gMinuit->fCstatu.Data() << endl; chi2 = ff->GetChisquare()/float(ff->GetNDF()); if (chi2 < chi2_threshold and gMinuit->fCstatu.Data()[0]=='S' and ff->GetParameter(1)>0 and ff->GetParameter(0)>0) { ff->SetLineColor(3); better_chi2 = chi2; par[0] = ff->GetParameter(0); par[1] = ff->GetParameter(1); par[2] = ff->GetParameter(2); } else { cout << "the fit was bad!" << endl; } cout << "chi2 = " << chi2 << endl; } } cout << "parameters : " << par[0] << " " << par[1] << " " << par[2] << endl; if (better_chi2 < chi2_threshold and better_chi2 != 0.0 and par[0]>0 ) // and par[1]>0) { cout<<"good fit = " << better_chi2 << " subtracting backgroung now!" << endl; for (i = index_begin[istep]; i < index_end[istep]; i++) { double t = time->at(i); double val = cathode->at(i) - fexp(&t, par); cathode_out.at(i) = val; } } else { cout << "The fit was bad, no background subtraction will be done" << endl; } } if (chi2 == 0) { cout << "No fit was sucessfull at all!" << endl; chi2 = 1000; } str_volt = "unknownvoltage"; if (hv->at(index_begin[istep]) > -820 and hv->at(index_begin[istep]) < -780) { fit.c_800V = chi2; str_volt = "800V"; } if (hv->at(index_begin[istep]) > -920 and hv->at(index_begin[istep]) < -880) { fit.c_900V = chi2; str_volt = "900V"; } if (hv->at(index_begin[istep]) > -1020 and hv->at(index_begin[istep]) < -980) { fit.c_1000V = chi2; str_volt = "1000V"; } if (hv->at(index_begin[istep]) > -1220 and hv->at(index_begin[istep]) < -1180) { fit.c_1200V = chi2; str_volt = "1200V"; } if (hv->at(index_begin[istep]) > -1420 and hv->at(index_begin[istep]) < -1380) { fit.c_1400V = chi2; str_volt = "1400V"; } if (hv->at(index_begin[istep]) > -1620 and hv->at(index_begin[istep]) < -1580) { fit.c_1600V = chi2; str_volt = "1600V"; } if (hv->at(index_begin[istep]) > -1820 and hv->at(index_begin[istep]) < -1780) { fit.c_1800V = chi2; str_volt = "1800V"; } gc0->SetTitle(str_volt.c_str()); gc0->SetMaximum(2*par[0]); gc0->SetMinimum(0); gc0->Draw("AP"); c->Print(file.c_str()); c->Close(); par[0] = 0.0; par[1] = 0.0; par[2] = 0.0; better_chi2 = 0.0; better[0] = 0; better[1] = 0; better[2] = 0; better[3] = 0; fcount = 0; repeate = true; ini3_count = 0; begin_count = 0; end_count = 0; delete(gc0); fitX.clear(); fitY.clear(); fitYe.clear(); fitXe.clear(); } else { cout << "voltage not found!" << endl; } } //for (i = 0; i < size; ++i) //{ // cathode_out.push_back(cathode->at(i)); //} }
// This is the actual Fiddle~ code void pitch_getit(t_pitch *x) { t_int i, j, k; t_peak *pk1; // peaks found t_peakout *pk2; // peaks to output t_histopeak *hp1; t_float power_spec = 0.0f, total_power = 0.0f, total_loudness = 0.0f, total_db = 0.0f; t_float *fp1, *fp2; t_float *spec = x->BufFFT, *powSpec = x->BufPower, threshold, mult; t_int n = x->FFTSize/2; t_int npitch, newphase, oldphase, npeak = 0; t_int logn = pitch_ilog2(n); t_float maxbin = BINPEROCT * (logn-2); t_float hzperbin = x->x_Fs/x->FFTSize; t_float coeff = x->FFTSize/(t_float)x->BufSize; t_float *histogram = x->histBuf + BINGUARD; t_int npeaktot = (x->x_npeakout > x->x_npeakanal ? x->x_npeakout : x->x_npeakanal); t_pitchhist *phist; // Circular buffer for History oldphase = x->x_histphase; newphase = x->x_histphase + 1; if (newphase == HISTORY) newphase = 0; x->x_histphase = newphase; // Get spectrum power for (i=0; i<n; i++) power_spec += powSpec[i]; total_power = 4.0f * power_spec; // Compensate for fiddle~ power estimation (difference of 6 dB) if (total_power > POWERTHRES) { total_db = (FIDDLEDB_REF-DBFUDGE) + LOGTODB*flog(total_power/n); // dB power estimation of fiddle~ total_loudness = fsqrt(fsqrt(power_spec)); // Use the actual real estimation rather than fiddle~'s if (total_db < 0) total_db = 0.0f; } else { total_db = total_loudness = 0.0f; } // Store new db in history vector x->x_dbs[newphase] = total_db; // Not enough power to find anything if (total_db < x->x_amplo) goto nopow; // search for peaks pk1 = x->x_peaklist; for (i=MINBIN, fp1=spec+2*MINBIN, fp2=powSpec+MINBIN; (i<n-3) && (npeak<npeaktot); i++, fp1+=2, fp2++) { t_float height = fp2[0], h1 = fp2[-1], h2 = fp2[1]; // Bin power and adjacents t_float totalfreq, pfreq, f1, f2, m, var, stdev; if (height<h1 || height<h2 || h1*coeff<POWERTHRES*total_power || h2*coeff<POWERTHRES*total_power) continue; // Go to next // Use an informal phase vocoder to estimate the frequency pfreq = ((fp1[-4] - fp1[4]) * (2.0f * fp1[0] - fp1[4] - fp1[-4]) + (fp1[-3] - fp1[5]) * (2.0f * fp1[1] - fp1[5] - fp1[-3])) / (2.0f * height); // Do this for the two adjacent bins too f1 = ((fp1[-6] - fp1[2]) * (2.0f * fp1[-2] - fp1[2] - fp1[-6]) + (fp1[-5] - fp1[3]) * (2.0f * fp1[-1] - fp1[3] - fp1[-5])) / (2.0f * h1) - 1; f2 = ((fp1[-2] - fp1[6]) * (2.0f * fp1[2] - fp1[6] - fp1[-2]) + (fp1[-1] - fp1[7]) * (2.0f * fp1[3] - fp1[7] - fp1[-1])) / (2.0f * h2) + 1; // get sample mean and variance of the three m = 0.333333f * (pfreq + f1 + f2); var = 0.5f * ((pfreq-m)*(pfreq-m) + (f1-m)*(f1-m) + (f2-m)*(f2-m)); totalfreq = i + m; // BAD HACK TO BE CHANGE IN NEXT VERSION !!!! if (coeff > 1) { switch ((t_int)coeff) { case 2: mult = 0.005; break; case 4: mult = 0.125; break; case 8: mult = 0.2; break; case 16: mult = 0.25; // weird values found by trying to get npeak around 6-7 break; default: mult = 0.25; } threshold = KNOCKTHRESH * height * mult; } else { threshold = KNOCKTHRESH * height; } if ((var * total_power) > threshold || (var < 1e-30)) continue; stdev = fsqrt(var); if (totalfreq < 4) totalfreq = 4; // Store the peak info in the list of peaks pk1->p_width = stdev; pk1->p_pow = height; pk1->p_loudness = fsqrt(fsqrt(height)); pk1->p_fp = fp1; pk1->p_freq = totalfreq; npeak++; pk1++; } // end for // prepare the raw peaks for output for (i=0, pk1=x->x_peaklist, pk2=x->peakBuf; i<npeak; i++, pk1++, pk2++) { t_float loudness = pk1->p_loudness; if (i>=x->x_npeakout) break; pk2->po_freq = hzperbin * pk1->p_freq; pk2->po_amp = (2.f/(t_float)n) * loudness * loudness * coeff; } // in case npeak < x->x_npeakout for (; i<x->x_npeakout; i++, pk2++) pk2->po_amp = pk2->po_freq = 0; // now, make a sort of "likelihood" spectrum. Proceeding in 48ths of an octave, // from 2 to n/2 (in bins), the likelihood of each pitch range is contributed // to by every peak in peaklist that's an integer multiple of it in frequency if (npeak > x->x_npeakanal) npeak = x->x_npeakanal; // max # peaks to analyze // Initialize histogram buffer to 0 for (i=0, fp1=histogram; i<maxbin; i++) *fp1++ = 0.0f; for (i=0, pk1=x->x_peaklist; i<npeak; i++, pk1++) { t_float pit = BPEROOVERLOG2 * flog(pk1->p_freq) - 96.0f; t_float binbandwidth = FACTORTOBINS * pk1->p_width/pk1->p_freq; t_float putbandwidth = (binbandwidth < 2 ? 2 : binbandwidth); t_float weightbandwidth = (binbandwidth < 1.0f ? 1.0f : binbandwidth); t_float weightamp = 4.0f * pk1->p_loudness / total_loudness; for (j=0, fp2=pitch_partialonset; j<NPARTIALONSET; j++, fp2++) { t_float bin = pit - *fp2; if (bin<maxbin) { t_float para, pphase, score = BINAMPCOEFF * weightamp / ((j+x->x_npartial) * weightbandwidth); t_int firstbin = bin + 0.5f - 0.5f * putbandwidth; t_int lastbin = bin + 0.5f + 0.5f * putbandwidth; t_int ibw = lastbin - firstbin; if (firstbin < -BINGUARD) break; para = 1.0f / (putbandwidth * putbandwidth); for (k=0, fp1=histogram+firstbin, pphase=firstbin-bin; k<=ibw; k++, fp1++, pphase+=1.0f) *fp1 += score * (1.0f - para * pphase * pphase); } } // end for } // end for //post("npeaks = %d, %f",npeak,mult); // For debugging weird hack!!! // Next we find up to NPITCH strongest peaks in the histogram. // If a peak is related to a stronger one via an interval in // the pitch_partialonset array, we suppress it. for (npitch=0; npitch<x->x_npitch; npitch++) { t_int index; t_float best; if (npitch) { for (best=0, index=-1, j=1; j<maxbin-1; j++) { if ((histogram[j]>best) && (histogram[j]>histogram[j-1]) && (histogram[j]>histogram[j+1])) { for (k=0; k<npitch; k++) if (x->x_histvec[k].h_index == j) goto peaknogood; for (k=0; k<NPARTIALONSET; k++) { if ((j-pitch_intpartialonset[k]) < 0) break; if (histogram[j-pitch_intpartialonset[k]] > histogram[j]) goto peaknogood; } for (k=0; k<NPARTIALONSET; k++) { if (j+ pitch_intpartialonset[k] >= maxbin) break; if (histogram[j+pitch_intpartialonset[k]] > histogram[j]) goto peaknogood; } index = j; best = histogram[j]; } peaknogood: ; } } else { best = 0; index = -1; for (j=0; j<maxbin; j++) if (histogram[j] > best) { index = j; best = histogram[j]; } } if (index < 0) break; x->x_histvec[npitch].h_value = best; x->x_histvec[npitch].h_index = index; } // for each histogram peak, we now search back through the // FFT peaks. A peak is a pitch if either there are several // harmonics that match it, or else if (a) the fundamental is // present, and (b) the sum of the powers of the contributing peaks // is at least 1/100 of the total power. // // A peak is a contributor if its frequency is within 25 cents of // a partial from 1 to 16. // // Finally, we have to be at least 5 bins in frequency, which // corresponds to 2-1/5 periods fitting in the analysis window. for (i=0; i<npitch; i++) { t_float cumpow=0, cumstrength=0, freqnum=0, freqden=0; t_int npartials=0, nbelow8=0; // guessed-at frequency in bins t_float putfreq = fexp((1.0f / BPEROOVERLOG2) * (x->x_histvec[i].h_index + 96.0f)); for (j=0; j<npeak; j++) { t_float fpnum = x->x_peaklist[j].p_freq/putfreq; t_int pnum = fpnum + 0.5f; t_float fipnum = pnum; t_float deviation; if ((pnum>16) || (pnum<1)) continue; deviation = 1.0f - fpnum/fipnum; if ((deviation > -PARTIALDEVIANCE) && (deviation < PARTIALDEVIANCE)) { // we figure this is a partial since it's within 1/4 of // a halftone of a multiple of the putative frequency. t_float stdev, weight; npartials++; if (pnum<8) nbelow8++; cumpow += x->x_peaklist[j].p_pow; cumstrength += fsqrt(fsqrt(x->x_peaklist[j].p_pow)); stdev = (x->x_peaklist[j].p_width > MINBW ? x->x_peaklist[j].p_width : MINBW); weight = 1.0f / ((stdev*fipnum) * (stdev*fipnum)); freqden += weight; freqnum += weight * x->x_peaklist[j].p_freq/fipnum; } // end if } // end for if (((nbelow8<4) || (npartials<DEFNPARTIAL)) && (cumpow < (0.01f * total_power))) { x->x_histvec[i].h_value = 0; } else { t_float pitchpow = (cumstrength * cumstrength * cumstrength * cumstrength); t_float freqinbins = freqnum/freqden; // check for minimum output frequency if (freqinbins < MINFREQINBINS) { x->x_histvec[i].h_value = 0; } else { // we passed all tests... save the values we got x->x_histvec[i].h_pitch = ftom(hzperbin * freqnum/freqden); x->x_histvec[i].h_loud = (FIDDLEDB_REF-DBFUDGE) + LOGTODB*flog(pitchpow*coeff/n); } } // end else } // end for // Now try to find continuous pitch tracks that match the new pitches. // First mark each peak unmatched. for (i=0, hp1=x->x_histvec; i<npitch; i++, hp1++) hp1->h_used = 0; // For each old pitch, try to match a new one to it. for (i=0, phist=x->x_hist; i<x->x_npitch; i++, phist++) { t_float thispitch = phist->h_pitches[oldphase]; phist->h_pitch = 0; // no output, thanks... phist->h_wherefrom = 0; if (thispitch == 0.0f) continue; for (j=0, hp1=x->x_histvec; j<npitch; j++, hp1++) if ((hp1->h_value > 0) && (hp1->h_pitch > thispitch - GLISS) && (hp1->h_pitch < thispitch + GLISS)) { phist->h_wherefrom = hp1; hp1->h_used = 1; } } for (i=0, hp1=x->x_histvec; i<npitch; i++, hp1++) if ((hp1->h_value > 0) && !hp1->h_used) { for (j=0, phist=x->x_hist; j<x->x_npitch; j++, phist++) if (!phist->h_wherefrom) { phist->h_wherefrom = hp1; phist->h_age = 0; phist->h_noted = 0; hp1->h_used = 1; goto happy; } break; happy: ; } // end if // Copy the pitch info into the history vector for (i=0, phist=x->x_hist; i<x->x_npitch; i++, phist++) { if (phist->h_wherefrom) { phist->h_amps[newphase] = phist->h_wherefrom->h_loud; phist->h_pitches[newphase] = phist->h_wherefrom->h_pitch; (phist->h_age)++; } else { phist->h_age = 0; phist->h_amps[newphase] = phist->h_pitches[newphase] = 0; } } // end for // Look for envelope attacks x->x_attackvalue = 0; if (x->x_peaked) { if (total_db > x->x_amphi) { t_int binlook = newphase - x->x_attackbins; if (binlook < 0) binlook += HISTORY; if (total_db > x->x_dbs[binlook] + x->x_attackthresh) { x->x_attackvalue = 1; x->x_peaked = 0; } } } else { t_int binlook = newphase - x->x_attackbins; if (binlook < 0) binlook += HISTORY; if ((x->x_dbs[binlook] > x->x_amphi) && (x->x_dbs[binlook] > total_db)) x->x_peaked = 1; } // For each current frequency track, test for a new note using a // stability criterion. Later perhaps we should also do as in // pitch~ and check for unstable notes a posteriori when // there's a new attack with no note found since the last onset; // but what's an attack &/or onset when we're polyphonic? for (i=0, phist=x->x_hist; i<x->x_npitch; i++, phist++) { // if we've found a pitch but we've now strayed from it, turn it off if (phist->h_noted) { if (phist->h_pitches[newphase] > phist->h_noted + x->x_vibdepth || phist->h_pitches[newphase] < phist->h_noted - x->x_vibdepth) phist->h_noted = 0; } else { if (phist->h_wherefrom && phist->h_age >= x->x_vibbins) { t_float centroid = 0; t_int not = 0; for (j=0, k=newphase; j<x->x_vibbins; j++) { centroid += phist->h_pitches[k]; k--; if (k<0) k = HISTORY-1; } centroid /= x->x_vibbins; for (j=0, k=newphase; j<x->x_vibbins; j++) { // calculate deviation from norm t_float dev = centroid - phist->h_pitches[k]; k--; if (k<0) k = HISTORY-1; if ((dev > x->x_vibdepth) || (-dev > x->x_vibdepth)) not = 1; } if (!not) { phist->h_pitch = phist->h_noted = centroid; } } // end if } // end else } return; nopow: for (i=0; i<x->x_npitch; i++) { x->x_hist[i].h_pitch = x->x_hist[i].h_noted = x->x_hist[i].h_pitches[newphase] = x->x_hist[i].h_amps[newphase] = x->x_hist[i].h_age = 0; } x->x_peaked = 1; x->x_dbage = 0; return; }