1 // Copyright 2004-07 "Gilles Degottex"
3 // This file is part of "Music"
5 // "Music" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
10 // "Music" is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #ifndef _FreqAnalysis_h_
21 #define _FreqAnalysis_h_
28 #include <CppAddons/CAMath.h>
29 //#include <NeuralNet/mlp/LayeredNeuralNet.h>
32 #include "Algorithm.h"
33 #include "Convolution.h"
43 double noise_lvl; // in [0,1]
45 struct Harmonic : Partial
50 inline bool is_peak(double p1, double p2, double p3)
52 return p1<p2 && p2>p3;
54 inline bool is_peak(const std::vector<std::complex<double> > spectrum, int c)
56 assert(c>0 && c<int(spectrum.size())/2-1);
58 return is_peak(Math::mod2(spectrum[c-1]), Math::mod2(spectrum[c]), Math::mod2(spectrum[c+1]));
61 double PeakRefinementLogParabola(const std::vector<std::complex<double> > spectrum, int peak_index);
62 double PeakRefinementLogParabolaUnbiased(const std::vector<std::complex<double> > spectrum, int peak_index, double zp);
64 std::vector<Harmonic> GetHarmonicStruct(const std::vector<std::complex<double> >& spectrum, double approx_f0, int nb_harm, double used_zp, double offset_tresh=0.1);
65 double FundFreqRefinementOfHarmonicStruct(const std::vector<std::complex<double> >& spectrum, double approx_f0, int nb_harm, double used_zp);
67 /*! the simpler: only one big convolution on the whole window
70 class SingleResConvolutionTransform : public Transform
74 virtual void AFreqChanged() {init();}
75 virtual void samplingRateChanged() {init();}
76 virtual void semitoneBoundsChanged() {init();}
77 double m_latency_factor;
78 double m_gauss_factor;
81 std::vector<Convolution*> m_convolutions;
83 SingleResConvolutionTransform(double latency_factor, double gauss_factor);
85 void setLatencyFactor(double latency) {m_latency_factor=latency; init();}
86 double getLatencyFactor() {return m_latency_factor;}
88 void setGaussFactor(double g) {m_gauss_factor=g; init();}
89 double getGaussFactor() {return m_gauss_factor;}
91 virtual void apply(const std::deque<double>& buff);
93 virtual ~SingleResConvolutionTransform();
96 /*! extraction des fondamentales avec un r�aux de neurones
97 * entr�s avec la visualisation dans le plan de Gauss
99 struct NeuralNetGaussAlgo : SingleResConvolutionTransform
101 // typedef Neuron TypeNeuron;
102 // LayeredNeuralNet<TypeNeuron>* m_nn;
106 NeuralNetGaussAlgo(double latency_factor, double gauss_factor);
108 virtual int getSampleAlgoLatency() const {return 0;}
110 virtual void apply(const deque<double>& buff);
112 virtual ~NeuralNetGaussAlgo();
115 /*! Monophonic Algorithm: algo for one voice
118 class MonophonicAlgo : public SingleResConvolutionTransform
121 double m_dominant_treshold;
124 MonophonicAlgo(double latency_factor, double gauss_factor);
126 virtual double getAlgoLatency() const {return 1000.0*m_convolutions[0]->size()/GetSamplingRate();}
127 virtual int getSampleAlgoLatency() const;
129 inline double getDominantTreshold() {return m_dominant_treshold;}
130 inline void setDominantTreshold(double t) {m_dominant_treshold=t;}
132 virtual void apply(const deque<double>& buff);
134 virtual ~MonophonicAlgo() {}
138 /*! algo for two voice
141 struct TwoVoiceMHT : MultiHalfTone
143 // typedef RemoveSyncMHT MHT;
144 typedef SingleResMultiHalfTone MHT;
147 vector< complex<double> > m_last_sol;
149 deque< vector<complex<double> > > fp;
150 deque< vector<double> > argpfp;
151 deque< vector<double> > modfp;
154 TwoVoiceMHT(double AFreq, int dataBySecond, double maxRep, double win_factor, int minHT, int maxHT);
156 virtual void apply(deque<double>& buff);
158 virtual ~TwoVoiceMHT();
161 /*! multiply "usefull" data quantity
164 struct OneDataMultiplierMHT : MultiHalfTone
166 vector< SingleHalfTone* > m_sht;
168 rfftw_plan m_fwd_plan;
169 rfftw_plan m_bck_plan;
177 OneDataMultiplierMHT(){}
178 OneDataMultiplierMHT(double AFreq, int dataBySecond, double rep, double win_factor, int minHT, int maxHT);
180 virtual void apply(deque<double>& buff);
182 virtual ~OneDataMultiplierMHT();
185 /*! une grande convolution qui couvre toute la fen�re et qui sert "d'indicateur" �la petit r�olution se trouvant au d�ut
188 struct IndicMultiHalfTone : SingleResMultiHalfTone
190 vector< SingleHalfTone* > m_small_sht;
192 IndicMultiHalfTone(double AFreq, int dataBySecond, double maxRep, double win_factor, int minHT, int maxHT);
194 virtual void apply(deque<double>& buff);
197 /*! integration sur plusieurs r�olution (Ondelettes)
198 * racourcit consid�ablement la chute d'une note, mais pas l'entr�
201 struct MultiResMultiHalfTone : MultiHalfTone
203 vector< vector<SingleHalfTone*> > m_sht;
205 MultiResMultiHalfTone(){}
206 MultiResMultiHalfTone(double AFreq, int dataBySecond, double maxRep, double win_factor, int minHT, int maxHT);
208 virtual void apply(deque<double>& buff);
210 virtual ~MultiResMultiHalfTone();
213 /*! minimum sur trois classes de r�olution
214 * - une grande qui couvre toute la fen�re (augmente la r�olution en fr�uence)
215 * - des progressivement plus petites qui commence au d�ut de la grande fen�re (augemente la r�olution en temps �la fin d'une note)
216 * - des progressivement plus petites qui finissent �la fin de la grande fen�re (augmente la r�olution en temps au d�ut d'une note)
219 struct TriResMultiHalfTone : MultiResMultiHalfTone
221 TriResMultiHalfTone(double AFreq, int dataBySecond, double maxRep, double win_factor, int minHT, int maxHT);
223 virtual void apply(deque<double>& buff);
226 /*! supprime les fr�uences syncronis�s
230 struct RemoveSyncMHT : MultiHalfTone
232 typedef SingleResMultiHalfTone MHT;
235 vector< complex<double> > m_last_sol;
238 RemoveSyncMHT(double AFreq, int dataBySecond, double maxRep, double win_factor, int minHT, int maxHT);
240 virtual void apply(deque<double>& buff);
242 virtual ~RemoveSyncMHT();
247 #endif // _FreqAnalysis_h_