📄 mp_notes.c
字号:
LWFLOAT mainFreq,partialFreq,partialFreqId; LWFLOAT diffFreq,freqFactor; LWFLOAT sigma2; if(numAtom <0) Errorf("numAtom %d < 0",numAtom); /* initialization */ if (partial == NULL) { partial = NewAtom(); fund = NewAtom(); moleculeSearch = NewMolecule(); } foundAtoms = 0; for (n = numAtom; n<book->size; n++) { /* if (*(flagNotes +n-1)) continue; this atom belongs to another note yet */ moleculeSearch = GetBookMolecule(book,n); /* we take the best partial in this molecule */ GetNearestPartial(moleculeSearch,mainAtom,partial); GetFundamental(moleculeSearch,fund); if (fund->freqId != partial->freqId) continue; /*Printf("%d,mainFreq:%f,partialFreq:%f",n,mainAtom->freqId,partial->freqId);*/ /* we try to mend the octave errors */ partialFreqId = partial->freqId; if (partialFreqId > mainAtom->freqId) { timesFreq = (int) floor(partialFreqId/mainAtom->freqId); if (timesFreq > 1) { if (partialFreqId-timesFreq*mainAtom->freqId < mainAtom->freqId/2) partialFreqId -= (timesFreq-1)*mainAtom->freqId; else partialFreqId -= timesFreq*mainAtom->freqId; /* Printf(",times:%d, diffFreqId:%f\n",timesFreq,mainAtom->freqId-partial->freqId);*/ } else { if (partialFreqId - mainAtom->freqId > mainAtom->freqId/2) { partialFreqId -= mainAtom->freqId; /* Printf(",times:1, diffFreqId:%f\n",mainAtom->freqId-partial->freqId);*/ } /* else Printf("\n");*/ } } /* else Printf("\n"); */ /* Should we mark it ? */ partialSize = partial->windowSize; if (partial->timeId > *beginNoteId-partialSize/4 && partial->timeId < endNoteId) { mainFreq = FreqId2Freq(mainAtom,mainAtom->freqId); partialFreq = FreqId2Freq(mainAtom,partialFreqId); sigma2 = (4096/partial->windowSize)*freqSigmaFact/(16000.0*book->dx); sigma2 = sigma2*sigma2; diffFreq = mainFreq-partialFreq; freqFactor = exp(-(diffFreq*diffFreq)/sigma2); if (freqFactor > markRatio) { foundAtoms++; /* if (partial->timeId < *beginNoteId) *beginNoteId = partial->timeId; */ if (*(flagNotes +n-1)) continue; Printf("\t\t%d:(s:%d,dif:%f,factor:%f)MARKED\n",n,partial->windowSize,diffFreq,freqFactor); *(flagNotes +n-1) = 1; } } } return(foundAtoms);}LWFLOAT GetEnergyMax(SIGNAL energyProfile,ATOM atom){ LWFLOAT maxima; int i; //static SIGNAL atomEnvelope = NULL; //LWFLOAT (*f)(SIGNAL,LWFLOAT); /* if (atomEnvelope == NULL) atomEnvelope = NewSignal(); SizeSignal(atomEnvelope,atom->windowSize,YSIG); GetWindowShapeFunc(atom->windowShape,&f); (*f)(atomEnvelope,0.0); maxima = -1; for (i=0;;i++) { while(maxima < atomEnvelope->Y[i]) maxima = atomEnvelope->Y[i]; } maxima = energyProfile->Y[(int) (i+atom->timeId)]; return(maxima);*/ maxima = 0.0; for (i = (int) atom->timeId ; i < atom->timeId + atom->windowSize/4 ; i++) { if ( energyProfile->Y[i] > maxima) maxima = energyProfile->Y[i]; } return(maxima); /* maxima = 0.0; for (i = 0; i < energyProfile->size ; i++) { if ( energyProfile->Y[i] > maxima) maxima = energyProfile->Y[i]; } return(maxima); */} void GetBeginEndNote(SIGNAL energyProfile,ATOM atom,LWFLOAT energyTh,LWFLOAT *beginNote,LWFLOAT *endNote,LWFLOAT *endMark){ LWFLOAT energy; int timeIdBegin,timeIdEnd; int originSearch; LWFLOAT maxima; int i; maxima = -1e10; for (i= (int) atom->timeId ; i < atom->timeId+atom->windowSize/4; i++) { if (energyProfile->Y[i] > maxima) {originSearch = i; maxima = energyProfile->Y[i];} } timeIdBegin = originSearch; /* loop to find the beginning of the current note */ /* originSearch = (int)(atom->timeId + (atom->windowSize)/4); timeIdBegin = originSearch + 1; */ do { timeIdBegin -= 1; energy = energyProfile->Y[timeIdBegin]; /* Printf("energy1:%f (%d) > %f \n",energy,timeIdBegin,energyTh); */ } while ( (energy > energyTh) && (timeIdBegin > 0) ); *beginNote = timeIdBegin; /* loop to find the end */ timeIdEnd = originSearch - 1; do { timeIdEnd += 1; energy = energyProfile->Y[timeIdEnd]; /* Printf("energy2:%f (%d) > %f \n",energy,timeIdEnd,energyTh); */ } while ( (energy > energyTh) && (timeIdEnd < energyProfile->size -1) ); *endNote = timeIdEnd; *endMark = timeIdEnd;} void AddAtomEnergyEnvelope(SIGNAL energyEnvelope,ATOM atom,ATOM mainAtom){ static SIGNAL atomEnvelope = NULL; static SIGNAL tempSignal = NULL; int i,time; LWFLOAT freqAtom,mainFreq; int atomSize; int windowShape; LWFLOAT diffFreq; LWFLOAT sigma2; LWFLOAT coeff2; LWFLOAT (*f)(SIGNAL,LWFLOAT); LWFLOAT factor,freqFactor; LWFLOAT fr; if (atomEnvelope == NULL) { tempSignal = NewSignal(); atomEnvelope = NewSignal(); } windowShape = atom->windowShape; atomSize = atom->windowSize; /* we use always the normalized coeff2 by the main atom of the note */ coeff2 = atom->coeff2/mainAtom->coeff2; SizeSignal(atomEnvelope,atomSize,YSIG); CopyFieldsSig(energyEnvelope,atomEnvelope); /* Convertion of freqId to Hertz */ mainFreq = FreqId2Freq(atom,mainAtom->freqId); freqAtom = FreqId2Freq(atom,atom->freqId); /*sigma2 = (20*(15-atom->octave)); */ sigma2 = (4096/atom->windowSize)*freqSigmaFact/(16000.0*atom->dx); sigma2 = sigma2*sigma2; /* Printf("%f - %f = %f ---->freqFactor: %f (%f)\n",mainFreq,freqAtom,diffFreq,freqFactor,sigma2); *//* Printf(" coeff2: %f freqFactor: %f --> factor: %f\n",coeff2,freqFactor,factor); */ /* Setting the right enveloppe size */ SizeSignal(tempSignal,atomSize,YSIG); atomEnvelope = tempSignal; GetWindowShapeFunc(windowShape,&f); (*f)(atomEnvelope,0.0); /* adding the atom envelope to the energy profile */ freqFactor = 0; for (fr = mainFreq-deltaFreq; fr<= mainFreq+deltaFreq;fr+=.1) { diffFreq = (fr-freqAtom); freqFactor += exp(-(diffFreq*diffFreq)/sigma2); factor = coeff2*freqFactor; } if (factor < 0.01) return; for (i=0; i<atomSize; i++) { time = (int) (i + atom->timeId); energyEnvelope->Y[time] += (factor*atomEnvelope->Y[i]*atomEnvelope->Y[i]); } }void GetNearestPartial(MOLECULE molecule,ATOM mainAtom,ATOM nearestPartial){ int k; int kMax; int partial; LWFLOAT freqId,mainFreqId,diffFreqId; LWFLOAT minDiffFreqId; ATOM atom = NULL; kMax = molecule->dim; mainFreqId = mainAtom->freqId; minDiffFreqId = 1e5; for (k=1; k<= kMax; k++) { atom = GetMoleculeAtom(molecule,0,k-1); if (atom == NULL) continue; freqId = atom->freqId; diffFreqId = fabs(mainFreqId - freqId); if (diffFreqId < minDiffFreqId) { minDiffFreqId = diffFreqId; CopyAtom(atom,nearestPartial); partial = k; } else break; }}void GetFundamental(MOLECULE molecule,ATOM fundamental) { int k; int kMax; ATOM atom1 = NULL; LWFLOAT maxCoeff2; kMax = molecule->dim; if (kMax > 1) { maxCoeff2 = -1; for (k=0; k< kMax; k++) { atom1 = GetMoleculeAtom(molecule,0,k); if (atom1 == NULL) continue; if (atom1->coeff2 > maxCoeff2) { CopyAtom(atom1,fundamental); maxCoeff2 = atom1->coeff2; } } if (maxCoeff2 == -1) Errorf("GetFundamental() : Weird !"); } else { atom1 = GetMoleculeAtom(molecule,0,0); CopyAtom(atom1,fundamental); }}/*int MarkAtoms(BOOK book,ATOM mainAtom,int numAtom,int *flagNotes,LWFLOAT * beginNoteId,LWFLOAT endNoteId){static ATOM partial = NULL;static MOLECULE moleculeSearch = NULL; int n;int timesFreq;int findedAtoms;int partialSize;LWFLOAT mainFreq,partialFreq;LWFLOAT diffFreq,freqFactor;LWFLOAT sigma2; if (partial == NULL) {partial = NewAtom();moleculeSearch = NewMolecule();} findedAtoms = 0; for (n = numAtom; n<book->size; n++){ moleculeSearch = GetBookMolecule(book,n); GetNearestPartial(moleculeSearch,mainAtom,partial); if (partial->freqId > mainAtom->freqId){ timesFreq = floor((partial->freqId)/(mainAtom->freqId)); if (timesFreq > 1){ if ((partial->freqId - (timesFreq*mainAtom->freqId)) < mainAtom->freqId/2) partial->freqId -= ((timesFreq-1)*mainAtom->freqId); else partial->freqId -= (timesFreq*mainAtom->freqId); } else{ if ((partial->freqId - mainAtom->freqId) > (mainAtom->freqId/2)){ partial->freqId -= mainAtom->freqId; } } } partialSize = partial->windowSize; if ( (partial->timeId > (*beginNoteId-partialSize/4))&&(partial->timeId < endNoteId ) ) { mainFreq = FreqId2Freq(mainAtom,mainAtom->freqId); partialFreq = FreqId2Freq(mainAtom,partial->freqId); sigma2 = (18*(15-partial->octave)); diffFreq = (mainFreq-partialFreq); if ( partial->octave < 11) diffFreq /= ( 12 - partial->octave ); freqFactor = exp(-(diffFreq*diffFreq)/sigma2); if (freqFactor > 0.005) { findedAtoms++; if (*(flagNotes +n-1)) continue; Printf("\t\t%d:(o:%d,dif:%f,factor:%f)MARKED\n",n,partial->octave,diffFreq,freqFactor); *(flagNotes +n-1) = 1; } } } return(findedAtoms);}void GetBestPartial(MOLECULE molecule,ATOM bestPartial){ int k; int kMax; int partialMax; LWFLOAT coeff2,maxCoeff2; ATOM atom = NULL; kMax = molecule->dim; maxCoeff2 = 0.0; partialMax = 0; for (k=1; k<= kMax; k++) { atom = GetMoleculeAtom(molecule,0,k-1); if (atom == NULL) continue; coeff2 = atom->coeff2; if (coeff2 > maxCoeff2) { CopyAtom(atom,bestPartial); maxCoeff2 = coeff2; partialMax = k; } }}void GetFundamental(MOLECULE molecule,ATOM fundamental) { int k; int kMax; ATOM atom1 = NULL; ATOM atom2 = NULL; kMax = molecule->dim; if (kMax > 1) { for (k=1; k<= kMax; k++) { atom1 = GetMoleculeAtom(molecule,0,k-1); atom2 = GetMoleculeAtom(molecule,0,k); if ((atom1 == NULL)||(atom2 == NULL)) Errorf("GetFundamental"); if ( atom1->coeff2 > 0.1*atom2->coeff2) { CopyAtom(atom1,fundamental); break; } } } else { atom1 = GetMoleculeAtom(molecule,0,0); CopyAtom(atom1,fundamental); }}*//* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -