int main () { bool (*pointerToComparator) (int,int); //pointerToComparator std::cout << compareGt (1,2); //std::cout << (long*)compareGt << std::endl; //std::cout << (long*)compareLt << std::endl; pointerToComparator = compareLt; std::cout << pointerToComparator (2,1) << std::endl; int arr [] = {25,3563,473845,733,7,457,567,63,5}; std::cout << arr[findExtremum (arr,9,compareGt)] << std::endl << arr[findExtremum (arr,9,compareLt)] << std::endl; double darr [] = {42342.4,234234.5757,535.2423,545.435,346.34}; std::cout << darr[findExtremum (darr,5,compareGt)] << std::endl << std::setprecision (3) << darr[findExtremum (darr,5,compareLt)] << std::endl; std::cout << "Sum of all ints = " << fold (arr,9,0,plus) << std::endl << "Product of all ints = " << fold (arr,9,1,mult) << std::endl << "Count ints = " << fold (arr,9,0,sumones) << std::endl << "Find even = " << fold (arr,9,false,checkAtLeastOneEven<int>) << std::endl; printArray(arr,9); std::cout << std::endl; map (arr,9,inc); printArray(arr,9); map (arr,9,mult2); std::cout << std::endl; printArray(arr,9); std::cout << std::endl; size_t newSize = 0; int *newarr = filter (arr,9,check500,newSize); printArray (newarr,newSize); delete newarr; return 0; }
int main () { int a[] = {67,8967,69,8,7,9,89924,3,7,5}; std::cout << findExtremum (a,10,[](int a, int b){return a > b;}) << std::endl; std::cout << findExtremum (a,10,compareLt) << std::endl; std::cout << findExtremum (a,10,more9s) << std::endl; /* Comparator pComparator = compareLt; std::cout << pComparator (1,2) << std::endl; std::cout << (long)(compareGt) << std::endl; std::cout << (long)(compareLt) << std::endl; std::cout << "Please enter address:"; long tmp; std::cin >> tmp; pComparator = (Comparator)tmp; std::cout << pComparator (1,2) << std::endl; */ }
inline void Audio::analyzePing() { // Determine extrema int botAt = findExtremum(_echoSamplesLeft, PING_SAMPLES_TO_ANALYZE, -1); if (botAt == -1) { qDebug("Audio Ping: Minimum not found.\n"); return; } int topAt = findExtremum(_echoSamplesLeft, PING_SAMPLES_TO_ANALYZE, 1); if (topAt == -1) { qDebug("Audio Ping: Maximum not found.\n"); return; } // Determine peak amplitude - warn if low int ampli = (_echoSamplesLeft[topAt] - _echoSamplesLeft[botAt]) / 2; if (ampli < PING_MIN_AMPLI) { qDebug("Audio Ping unreliable - low amplitude %d.\n", ampli); } // Determine period - warn if doesn't look like our signal int halfPeriod = abs(topAt - botAt); if (abs(halfPeriod-PING_HALF_PERIOD) > PING_MAX_PERIOD_DIFFERENCE) { qDebug("Audio Ping unreliable - peak distance %d vs. %d\n", halfPeriod, PING_HALF_PERIOD); } // Ping is sent: // // ---[ record ]--[ play ]--- audio in space/time ---> // : : : // : : ping: ->X<- // : : : // : : |+| (buffer end - signal center = t1-t0) // : |<----------+ // : : : : // : ->X<- (corresponding input buffer position t0) // : : : : // : : : : // : : : : // Next frame (we're recording from now on): // : : : // : - - --[ record ]--[ play ]------------------> // : : : : // : : |<-- (start of recording t1) // : : : // : : : // At some frame, the signal is picked up: // : : : : // : : : : // : : : V // : : : - - --[ record ]--[ play ]----------> // : V : : // : |<--------->| // |+|<------->| period + measured samples // // If we could pick up the signal at t0 we'd have zero round trip // time - in this case we had recorded the output buffer instantly // in its entirety (we can't - but there's the proper reference // point). We know the number of samples from t1 and, knowing that // data is streaming continuously, we know that t1-t0 is the distance // of the characterisic point from the end of the buffer. int delay = (botAt + topAt) / 2 + PING_PERIOD; qDebug("\n| Audio Ping results:\n+----- ---- --- - - - - -\n\n" "Delay = %d samples (%d ms)\nPeak amplitude = %d\n\n", delay, (delay * 1000) / int(SAMPLE_RATE), ampli); }
OneDimensionalDeviate::OneDimensionalDeviate(const FluxDensity& fluxDensity, std::vector<double>& range, bool isRadial, const GSParamsPtr& gsparams) : _fluxDensity(fluxDensity), _positiveFlux(0.), _negativeFlux(0.), _isRadial(isRadial), _gsparams(gsparams) { dbg<<"Start ODD constructor\n"; dbg<<"Input range has "<<range.size()<<" entries\n"; dbg<<"radial? = "<<isRadial<<std::endl; // Typedef for indices of standard containers, which don't like int values typedef std::vector<double>::size_type Index; // First calculate total flux so we know when an interval is a small amt of flux for (Index iRange = 0; iRange < range.size()-1; iRange++) { xdbg<<"range "<<iRange<<" = "<<range[iRange]<<" ... "<<range[iRange+1]<<std::endl; // Integrate total flux (and sign) in each range Interval segment(fluxDensity, range[iRange], range[iRange+1], _isRadial, _gsparams); double rangeFlux = segment.getFlux(); if (rangeFlux >= 0.) _positiveFlux += rangeFlux; else _negativeFlux += std::abs(rangeFlux); } dbg<<"posFlux = "<<_positiveFlux<<std::endl; dbg<<"negFlux = "<<_negativeFlux<<std::endl; double totalAbsoluteFlux = _positiveFlux + _negativeFlux; dbg<<"totFlux = "<<totalAbsoluteFlux<<std::endl; // Now break each range into Intervals for (Index iRange = 0; iRange < range.size()-1; iRange++) { // See if there is an extremum to split this range: double extremum; if (findExtremum(_fluxDensity, range[iRange], range[iRange+1], extremum, _gsparams->range_division_for_extrema)) { xdbg<<"range "<<iRange<<" = "<<range[iRange]<<" ... "<<range[iRange+1]<< " has an extremum at "<<extremum<<std::endl; // Do 2 ranges { Interval splitit(_fluxDensity, range[iRange], extremum, _isRadial, _gsparams); std::list<Interval> leftList = splitit.split( _gsparams->small_fraction_of_flux * totalAbsoluteFlux); xdbg<<"Add "<<leftList.size()<<" intervals on left of extremem\n"; _pt.insert(_pt.end(), leftList.begin(), leftList.end()); } { Interval splitit(_fluxDensity, extremum, range[iRange+1], _isRadial, _gsparams); std::list<Interval> rightList = splitit.split( _gsparams->small_fraction_of_flux * totalAbsoluteFlux); xdbg<<"Add "<<rightList.size()<<" intervals on right of extremem\n"; _pt.insert(_pt.end(), rightList.begin(), rightList.end()); } } else { // Just single Interval in this range, no extremum: Interval splitit( _fluxDensity, range[iRange], range[iRange+1], _isRadial, _gsparams); std::list<Interval> leftList = splitit.split( _gsparams->small_fraction_of_flux * totalAbsoluteFlux); xdbg<<"Add "<<leftList.size()<<" intervals\n"; _pt.insert(_pt.end(), leftList.begin(), leftList.end()); } } dbg<<"Total of "<<_pt.size()<<" intervals\n"; // Build the ProbabilityTree _pt.buildTree(); }
fextract_series_t *pulses_calc(pitch_t *p, int nframes, mx_real_t *signal, int nsamples){ int i, x=0; mx_real_t globalPeak=signal[0]; mx_real_t t = 0; mx_real_t peak; double addedRight = -1e300; fextract_series_t *pulses = series_create(MAX_SERIES); for (i=1;i<nsamples;i++) { mx_real_t this = fabs(signal[i]); if (this > globalPeak) globalPeak=this; } /* * Cycle over all voiced intervals. */ for (;;) { mx_real_t tleft, tright, tmiddle, f0middle, tmax, tsave; x++; if (! getVoicedInterval (p, nframes, t, & tleft, & tright)) break; /* * Go to the middle of the voice stretch. */ tmiddle = (tleft + tright) / 2; f0middle = getPitchValueAt(p,nframes,tmiddle); /* * Our first point is near this middle. */ tmax = findExtremum (signal, nsamples, tmiddle - 0.5 / f0middle, tmiddle + 0.5 / f0middle); series_add(pulses,tmax); //find pulses backward tsave = tmax; for (;;) { mx_real_t f0 , correlation, last_tmax; f0 = getPitchValueAt(p,nframes,tmax); if (f0==-1) break; last_tmax=tmax; correlation = findMaximumCorrelation (signal, nsamples, tmax, 1.0 / f0, tmax - 1.25 / f0, tmax - 0.8 / f0, & tmax, & peak); if (correlation == -1 || tmax >last_tmax) /*break*/ tmax -= 1.0 / f0; /* This one period will drop out. */ if (tmax < tleft) { if (correlation > 0.7 && peak > 0.023333 * globalPeak && tmax - addedRight > 0.8 / f0) { series_add(pulses,tmax); } break; } if (correlation > 0.3 && (peak == 0.0 || peak > 0.01 * globalPeak)) { if (tmax - addedRight > 0.8 / f0) { /* Do not fill in a short originally unvoiced interval twice. */ series_add(pulses,tmax); } } } //find pulses forward tmax = tsave; for (;;) { mx_real_t f0 , correlation, last_tmax; f0 = getPitchValueAt(p,nframes,tmax); if (f0==-1) break; last_tmax=tmax; correlation = findMaximumCorrelation (signal, nsamples, tmax, 1.0 / f0, tmax + 0.8 / f0, tmax + 1.25 / f0, & tmax, & peak); if (correlation == -1 || tmax <last_tmax) /*break*/ tmax += 1.0 / f0; if (tmax > tright) { if (correlation > 0.7 && peak > 0.023333 * globalPeak) { series_add(pulses,tmax); addedRight = tmax; } break; } if (correlation > 0.3 && (peak == 0.0 || peak > 0.01 * globalPeak)) { series_add(pulses,tmax); addedRight = tmax; } } t = tright; } qsort(pulses->series,pulses->nSeries,sizeof(mx_real_t),_cmp_mx_real); return pulses; }