Debian Codegraph @

libs/Music/CumulativeDiffAlgo.cpp

raw
1 // Copyright 2004 "Gilles Degottex"
2
3 // This file is part of "Music"
4
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.
9 //
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.
14 //
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
18
19
20 #include "CumulativeDiffAlgo.h"
21
22 #include <cassert>
23 #include <cmath>
24 #include <deque>
25 #include <iostream>
26 #include <algorithm>
27 #include <limits>
28 using namespace std;
29 #include <CppAddons/CAMath.h>
30 using namespace Math;
31
32 #include "Music.h"
33
34 //#define MUSIC_DEBUG
35 #ifdef MUSIC_DEBUG
36 #define LOG(a) a
37 #else
38 #define LOG(a)
39 #endif
40
41 namespace Music
42 {
43 void CumulativeDiffAlgo::init()
44 {
45 setMinMaxLength(int(GetSamplingRate()/h2f(GetSemitoneMax())), int(GetSamplingRate()/h2f(GetSemitoneMin())));
46 }
47
48 CumulativeDiffAlgo::CumulativeDiffAlgo(double noise_treshold)
49 : m_noise_threshold(noise_treshold)
50 , m_wave_length(0)
51 {
52 init();
53 }
54
55 //! return the average differance on the sample delimited by [0,size]
56 // - ne pas utiliser tout size
57 // - sauter des données
58 double diff(const deque<double>& buff, size_t size, size_t s)
59 {
60 double r = 0.0;
61 for(size_t i=0; i<size; i++)
62 r += abs(buff[i] - buff[i+s]);
63 return r / size;
64 }
65
66 void CumulativeDiffAlgo::apply(const deque<double>& buff)
67 {
68 if(buff.size()<2*m_max_length)
69 {
70 m_wave_length = 0;
71 return;
72 }
73
74 double max_vol = 0.0;
75 for(size_t i=0; i<m_max_length; i++)
76 max_vol = std::max(max_vol, buff[i]);
77
78 // use a relative threshold
79 double threshold = m_noise_threshold*max_vol;
80
81 // cout << "min=" << min_length << " max=" << m_max_length << " threshold=" << treshold << endl;
82
83 double r = 0.0;
84 size_t s;
85 for(s=m_min_length; r<=threshold && s<m_max_length; s++)
86 r = diff(buff, m_max_length, s);
87
88 while((r=diff(buff, m_max_length, s+1))>threshold && s<m_max_length)
89 s++;
90
91 // cout << "s=" << s << " r=" << r << endl;
92 double old_r = r;
93 while(s+1<m_max_length && (r=diff(buff, m_max_length, s+1))<old_r)
94 {
95 // cout << "s=" << s << " r=" << r << endl;
96 s++;
97 old_r = r;
98 }
99
100 // cout << "s=" << s << " r=" << old_r << endl;
101
102 // cout << "absolute threshold=" << m_noise_threshold << " max volume="<<max_vol<<" relative threshold="<<threshold << " s="<<s << " m_max_length="<<m_max_length << endl;
103
104 m_wave_length = (s<m_max_length)?s:0;
105 }
106
107 /*
108 * ~YIN version, unused because too costly for the CPU: O(n^3) ...
109 double CumulativeDiffAlgo::squar_diff(const deque<double>& buff, size_t s)
110 {
111 double r = 0.0;
112 for(size_t i=0; i<s; i++)
113 {
114 double d = buff[i] - buff[i+s];
115 r += d*d;
116 }
117
118 return r;
119 }
120 double CumulativeDiffAlgo::norm_squar_diff(const deque<double>& buff, size_t s)
121 {
122 double r = 0.0;
123 for(size_t i=0; i<s; i++)
124 r += squar_diff(buff, i);
125
126 return squar_diff(buff, s) / (r/s);
127 }
128 void CumulativeDiffAlgo::receive(const deque<double>& buff)
129 {
130 assert(m_sampling_rate>0);
131 size_t min_length = size_t(m_sampling_rate * m_min_wave_length);
132 size_t max_length = size_t(m_sampling_rate * m_max_wave_length);
133
134 if(buff.size()<2*max_length) return;
135
136 cout << "min=" << min_length << " max=" << max_length << " noise=" << m_noise_treshold << endl;
137
138 double r = 1.0;
139 double max_vol = 0.0;
140 size_t s;
141 for(s=min_length; r>m_noise_treshold && s<max_length; s++)
142 {
143 max_vol = max(max_vol, buff[s]);
144
145 r = norm_squar_diff(buff, s);
146 // cout << "s=" << s << " r=" << r << endl;
147 }
148 s--;
149 cout << "s=" << s << " r=" << r << endl;
150 // cout << "---" << endl;
151 double old_r;
152 do
153 {
154 s++;
155 old_r = r;
156
157 r = norm_squar_diff(buff, s);
158 // cout << "s=" << s << " r=" << r << endl;
159 }
160 while(r<old_r && s<max_length);
161
162 r = old_r;
163 s--;
164 cout << "s=" << s << " r=" << r << endl;
165
166 cout << "m_noise_treshold=" << m_noise_treshold << " max_vol=" << max_vol << " r=" << r << endl;
167
168 m_freq = 0.0;
169 if(max_vol>m_noise_treshold && s<max_length)
170 {
171 cout << "r=" << r << " m_noise_treshold=" << m_noise_treshold << endl;
172 m_freq = double(m_sampling_rate)/s;
173 }
174
175 cout << "m_sampling_rate=" << m_sampling_rate << " freq=" << m_freq << endl;
176 }
177 */
178 }
179