// ---------------------------------------------------------------------------
//	selectMagnitudePeaks (private)
// ---------------------------------------------------------------------------
Peaks
SpectralPeakSelector::selectMagnitudePeaks( ReassignedSpectrum & spectrum,
                                            double minFrequency )
{
	using namespace std; // for abs and fabs

	const double sampsToHz = mSampleRate / spectrum.size();
	const double oneOverSR = 1. / mSampleRate;
	const double minFreqSample = minFrequency / sampsToHz;
	const double maxCorrectionSamples = mMaxTimeOffset * mSampleRate;
	
	Peaks peaks;
	
	int start_j = 1, end_j = (spectrum.size() / 2) - 2;
	
	double fsample = start_j;
	do 
	{
	    fsample = spectrum.reassignedFrequency( start_j++ );
	} while( fsample < minFreqSample );
	
	for ( int j = start_j; j < end_j; ++j ) 
	{	 
		if ( spectrum.reassignedMagnitude(j) > spectrum.reassignedMagnitude(j-1) && 
			 spectrum.reassignedMagnitude(j) > spectrum.reassignedMagnitude(j+1) ) 
		{				
			//	skip low-frequency peaks:
			double fsample = spectrum.reassignedFrequency( j );
			if ( fsample < minFreqSample )
				continue;

			//	skip peaks with large time corrections:
			double timeCorrectionSamps = spectrum.reassignedTime( j );
			if ( fabs(timeCorrectionSamps) > maxCorrectionSamples )
				continue;
				
			double mag = spectrum.reassignedMagnitude( j );
			double phase = spectrum.reassignedPhase( j );
			
			//	this will be overwritten later in analysis, 
			//	might be ignored altogether, only used if the
			//	mixed derivative convergence indicator is stored
			//	as bandwidth in Analyzer:
			double bw = spectrum.convergence( j );
			
			//	also store the corrected peak time in seconds, won't
			//	be able to compute it later:
			double time = timeCorrectionSamps * oneOverSR;
			Breakpoint bp ( fsample * sampsToHz, mag, bw, phase );
			peaks.push_back( SpectralPeak( time, bp ) );
						
		}	//	end if itsa peak
	}
	
    /*
	debugger << "SpectralPeakSelector::selectMagnitudePeaks: found " 
             << peaks.size() << " peaks" << endl;
    */         		
	return peaks;
}
// ---------------------------------------------------------------------------
//	selectReassignmentMinima (private)
// ---------------------------------------------------------------------------
Peaks
SpectralPeakSelector::selectReassignmentMinima( ReassignedSpectrum & spectrum, 
                                                double minFrequency )
{
	using namespace std; // for abs and fabs

	const double sampsToHz = mSampleRate / spectrum.size();
	const double oneOverSR = 1. / mSampleRate;
	const double minFreqSample = minFrequency / sampsToHz;
	const double maxCorrectionSamples = mMaxTimeOffset * mSampleRate;
	
	Peaks peaks;
	
	int start_j = 1, end_j = (spectrum.size() / 2) - 2;
	
	double fsample = start_j;
	do 
	{
	    fsample = spectrum.reassignedFrequency( start_j++ );
	} while( fsample < minFreqSample );
	
	for ( int j = start_j; j < end_j; ++j ) 
	{	 

	    // look for changes in the frequency reassignment,
	    // from positive to negative correction, indicating
	    // a concentration of energy in the spectrum:
	    double next_fsample = spectrum.reassignedFrequency( j+1 );
	    if ( fsample > j && next_fsample < j + 1 )
	    {
	        //  choose the smaller correction of fsample or next_fsample:
	        // (could also choose the larger magnitude?)
	        double freq;
	        int peakidx;
	        if ( (fsample-j) < (j+1-next_fsample) )
	        {
	            freq = fsample * sampsToHz;
	            peakidx = j;
	        }
	        else
	        {
	            freq = next_fsample * sampsToHz;
	            peakidx = j+1;
	        }
            
            //  still possible that the frequency winds up being
            //  below the specified minimum
            if ( freq >= minFrequency )
            {            	         
                //	keep only peaks with small time corrections:
                double timeCorrectionSamps = spectrum.reassignedTime( peakidx );
                if ( fabs(timeCorrectionSamps) < maxCorrectionSamples )
                {
                    double mag = spectrum.reassignedMagnitude( peakidx );
                    double phase = spectrum.reassignedPhase( peakidx );    			

                    //	this will be overwritten later in analysis, 
                    //	might be ignored altogether, only used if the
                    //	mixed derivative convergence indicator is stored
                    //	as bandwidth in Analyzer:
                    double bw = spectrum.convergence( j );


                    //	also store the corrected peak time in seconds, won't
                    //	be able to compute it later:
                    double time = timeCorrectionSamps * oneOverSR;
                    Breakpoint bp( freq, mag, bw, phase );
                    peaks.push_back( SpectralPeak( time, bp ) );
                }
            }
                	        
	    }
	    fsample = next_fsample;
	}
    
	/*
	debugger << "SpectralPeakSelector::selectReassignmentMinima: found " 
             << peaks.size() << " peaks" << endl;
	*/
    	
	return peaks;

}