Loris 1.5

Fundamental.h

00001 #ifndef INCLUDE_FUNDAMENTAL_H
00002 #define INCLUDE_FUNDAMENTAL_H
00003 /*
00004  * This is the Loris C++ Class Library, implementing analysis, 
00005  * manipulation, and synthesis of digitized sounds using the Reassigned 
00006  * Bandwidth-Enhanced Additive Sound Model.
00007  *
00008  * Loris is Copyright (c) 1999-2010 by Kelly Fitz and Lippold Haken
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY, without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  *
00025  * Fundamental.h
00026  *
00027  * Definition of classes for computing an estimate of time-varying
00028  * fundamental frequency from either a sequence of samples or a
00029  * collection of Partials using a frequency domain maximum likelihood 
00030  * algorithm adapted from Quatieri's speech signal processing textbook. 
00031  *
00032  * Kelly Fitz, 25 March 2008
00033  * loris@cerlsoundgroup.org
00034  *
00035  * http://www.cerlsoundgroup.org/Loris/
00036  *
00037  */
00038 
00039 #include "LinearEnvelope.h"
00040 #include "PartialList.h"
00041 #include "F0Estimate.h" 
00042 
00043 #include <memory>
00044 #include <vector>
00045 
00046 //  begin namespace
00047 namespace Loris {
00048 
00049 class ReassignedSpectrum;
00050 
00051 // ---------------------------------------------------------------------------
00052 //  class FundamentalEstimator
00053 //
00056 
00057 class FundamentalEstimator
00058 {
00059 public:
00060 
00061 //  -- types --
00062 
00063     typedef F0Estimate value_type;
00064 
00065 //  -- constants --    
00066 
00067     enum {
00068     
00069         DefaultAmpFloor = -60,          
00070         
00071         DefaultAmpRange = 30,           
00072     
00073         DefaultFreqCeiling = 4000,      
00074     
00075         DefaultPrecisionOver100 = 10,   
00076 
00077         DefaultMinConfidencePct = 90    
00078 
00079     };
00080     
00081 
00082 //  -- lifecycle --
00083 
00084 protected:
00085 
00094     FundamentalEstimator( double precisionHz );
00095                                                         
00096 public: 
00097 
00099     virtual ~FundamentalEstimator( void );    
00100 
00101 
00102     //  compiler-generated copy and assignment are OK
00103 
00104   
00105 //  -- parameter access --
00106 
00110     double ampFloor( void ) const;
00111 
00117     double ampRange( void ) const;
00118 
00123     double freqCeiling( void ) const;
00124 
00128     double precision( void ) const;
00129      
00130 //  -- parameter mutation --
00131 
00137     void setAmpFloor( double x );
00138 
00146     void setAmpRange( double x );
00147 
00155     void setFreqCeiling( double x );
00156 
00162     void setPrecision( double x );
00163 
00164 
00165 protected:
00166 
00167 //  -- parameter member variables --
00168 
00169 
00170     double m_precision;         
00171 
00172                                 
00173     double m_ampFloor;          
00174 
00175 
00176 
00177     
00178     double m_ampRange;          
00179 
00180 
00181 
00182         
00183     double m_freqCeiling;       
00184 
00185 
00186 
00187 
00188 };  //  end of base class FundamentalEstimator
00189 
00190 
00191 
00192 // ---------------------------------------------------------------------------
00193 //  class FundamentalFromSamples
00194 //
00205 //
00206 class FundamentalFromSamples : public FundamentalEstimator
00207 {
00208 //  -- public interface --
00209 
00210 public:
00211 
00212 //  -- lifecycle --
00213 
00228     FundamentalFromSamples( double winWidthHz, 
00229                             double precisionHz = DefaultPrecisionOver100 * 0.01 );
00230                             
00231                             
00232 
00234     ~FundamentalFromSamples( void );
00235 
00236 //  -- fundamental frequency estimation --
00237 
00238     //  buildEnvelope
00239     //
00268     LinearEnvelope buildEnvelope( const double * sampsBeg, 
00269                                   const double * sampsEnd, 
00270                                   double sampleRate, 
00271                                   double tbeg, double tend, 
00272                                   double interval,
00273                                   double lowerFreqBound, double upperFreqBound, 
00274                                   double confidenceThreshold );
00275                                  
00276     //  buildEnvelope
00277     //
00306     LinearEnvelope buildEnvelope( const double * sampsBeg, 
00307                                   unsigned long nsamps,
00308                                   double sampleRate, 
00309                                   double tbeg, double tend, 
00310                                   double interval,
00311                                   double lowerFreqBound, double upperFreqBound, 
00312                                   double confidenceThreshold )
00313     {
00314         return buildEnvelope( sampsBeg, sampsBeg + nsamps, sampleRate, 
00315                               tbeg, tend, interval, 
00316                               lowerFreqBound, upperFreqBound,
00317                               confidenceThreshold );
00318     }
00319 
00320 
00321     //  buildEnvelope
00322     //
00350     LinearEnvelope buildEnvelope( const std::vector< double > & samps, 
00351                                   double sampleRate, 
00352                                   double tbeg, double tend, 
00353                                   double interval,
00354                                   double lowerFreqBound, double upperFreqBound, 
00355                                   double confidenceThreshold )
00356     {
00357         return buildEnvelope( &samps[0], &samps[0] + samps.size(), sampleRate, 
00358                               tbeg, tend, interval, 
00359                               lowerFreqBound, upperFreqBound,
00360                               confidenceThreshold );
00361     }
00362 
00363                                  
00364     //  estimateAt
00365     //
00386     
00387     value_type estimateAt( const double * sampsBeg, 
00388                            const double * sampsEnd, 
00389                            double sampleRate, 
00390                            double time,
00391                            double lowerFreqBound, double upperFreqBound );
00392 
00393     //  estimateAt
00394     //
00415     
00416     value_type estimateAt( const double * sampsBeg, 
00417                            unsigned long nsamps,
00418                            double sampleRate, 
00419                            double time,
00420                            double lowerFreqBound, double upperFreqBound )
00421     {
00422         return estimateAt( sampsBeg, sampsBeg + nsamps, sampleRate, 
00423                            time, lowerFreqBound, upperFreqBound );
00424     }
00425                            
00426     //  estimateAt
00427     //
00447     
00448     value_type estimateAt( const std::vector< double > & samps, 
00449                            double sampleRate, 
00450                            double time,
00451                            double lowerFreqBound, double upperFreqBound )
00452     {
00453         return estimateAt( &samps[0], &samps[0] + samps.size(), sampleRate, 
00454                            time, lowerFreqBound, upperFreqBound );
00455     }
00456                               
00457 
00458 
00459 //  -- spectral analysis parameter access/mutation --
00460 
00464     double windowWidth( void ) const;
00465     
00471     void setWindowWidth( double w );         
00472 
00473     
00474 
00475 //  -- private auxiliary functions --
00476 
00477 private: 
00478 
00479     //  buildSpectrumAnalyzer
00480     //
00488     void buildSpectrumAnalyzer( double srate );
00489         
00490 
00491     //  collectFreqsAndAmps
00492     //
00511     
00512     void collectFreqsAndAmps( const double * samps,
00513                               unsigned long nsamps,
00514                               double sampleRate,
00515                               std::vector< double > & frequencies, 
00516                               std::vector< double > & amplitudes,
00517                               double time );
00518 
00519 
00520 //  -- private member variables --
00521 
00522     std::auto_ptr< ReassignedSpectrum > m_spectrum;
00524                                 
00525     double m_cacheSampleRate;   
00526 
00527     double m_windowWidth;       
00528 
00529     
00530 //  disallow these until they are implemented
00531 
00532     FundamentalFromSamples( const FundamentalFromSamples & );
00533     FundamentalFromSamples & operator= ( const FundamentalFromSamples & );
00534 
00535 };   //  end of class FundamentalFromSamples
00536 
00537 
00538 
00539 // ---------------------------------------------------------------------------
00540 //  class FundamentalFromPartials
00541 //
00546 
00547 class FundamentalFromPartials : public FundamentalEstimator
00548 {
00549 //  -- public interface --
00550 
00551 public:
00552 
00553 //  -- lifecycle --
00554 
00563     FundamentalFromPartials( double precisionHz = DefaultPrecisionOver100 * 0.01 );
00564                                                         
00565 
00567     ~FundamentalFromPartials( void );
00568     
00571     FundamentalFromPartials( const FundamentalFromPartials & );
00572     
00574     FundamentalFromPartials & operator= ( const FundamentalFromPartials & );
00575 
00576 //  -- fundamental frequency estimation --
00577 
00578     //  buildEnvelope
00579     //
00605     LinearEnvelope buildEnvelope( PartialList::const_iterator begin_partials, 
00606                                   PartialList::const_iterator end_partials,
00607                                   double tbeg, double tend, 
00608                                   double interval,
00609                                   double lowerFreqBound, double upperFreqBound, 
00610                                   double confidenceThreshold );
00611                                   
00612     //  buildEnvelope
00613     //
00638     LinearEnvelope buildEnvelope( const PartialList & partials, 
00639                                   double tbeg, double tend, 
00640                                   double interval,
00641                                   double lowerFreqBound, double upperFreqBound, 
00642                                   double confidenceThreshold )
00643     {
00644         return buildEnvelope( partials.begin(), partials.end(),
00645                               tbeg, tend, interval,
00646                               lowerFreqBound, upperFreqBound,
00647                               confidenceThreshold );
00648     }
00649     
00650                                  
00651     //  estimateAt
00652     //
00670     value_type estimateAt( PartialList::const_iterator begin_partials, 
00671                            PartialList::const_iterator end_partials,
00672                            double time,
00673                            double lowerFreqBound, double upperFreqBound );
00674                            
00675     //  estimateAt
00676     //
00693     value_type estimateAt( const PartialList & partials, 
00694                            double time,
00695                            double lowerFreqBound, double upperFreqBound )
00696     {
00697             return estimateAt( partials.begin(), partials.end(),
00698                                time,
00699                                lowerFreqBound, upperFreqBound );
00700     }
00701 
00702 
00703 
00704 //  -- private auxiliary functions --
00705 
00706 private: 
00707 
00708     //  collectFreqsAndAmps
00709     //
00723     void collectFreqsAndAmps( PartialList::const_iterator begin_partials, 
00724                               PartialList::const_iterator end_partials,
00725                               std::vector< double > & frequencies, 
00726                               std::vector< double > & amplitudes,
00727                               double time );
00728 
00729 
00730 
00731 };   //  end of class FundamentalFromPartials
00732 
00733 
00734 
00735 }   //  end of namespace Loris
00736 
00737 #endif  // ndef INCLUDE_FUNDAMENTAL_H
 All Classes Functions Variables Typedefs Enumerator Friends