File: CumulativeDiffAlgo.cpp

package info (click to toggle)
fmit 1.3.2-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,356 kB
  • sloc: cpp: 9,515; sh: 69; makefile: 12
file content (179 lines) | stat: -rw-r--r-- 4,617 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2004 "Gilles Degottex"

// This file is part of "Music"

// "Music" is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// "Music" is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


#include "CumulativeDiffAlgo.h"

#include <cassert>
#include <cmath>
#include <deque>
#include <iostream>
#include <algorithm>
#include <limits>
using namespace std;
#include <CppAddons/CAMath.h>
using namespace Math;

#include "Music.h"

//#define MUSIC_DEBUG
#ifdef MUSIC_DEBUG
#define LOG(a)	a
#else
#define LOG(a)
#endif

namespace Music
{
	void CumulativeDiffAlgo::init()
	{
		setMinMaxLength(int(GetSamplingRate()/h2f(GetSemitoneMax())), int(GetSamplingRate()/h2f(GetSemitoneMin())));
	}

	CumulativeDiffAlgo::CumulativeDiffAlgo(double noise_treshold)
	: m_noise_threshold(noise_treshold)
	, m_wave_length(0)
	{
		init();
	}

	//! return the average differance on the sample delimited by [0,size]
	// - ne pas utiliser tout size
	// - sauter des données
	double diff(const deque<double>& buff, size_t size, size_t s)
	{
		double r = 0.0;
		for(size_t i=0; i<size; i++)
			r += abs(buff[i] - buff[i+s]);
		return r / size;
	}

	void CumulativeDiffAlgo::apply(const deque<double>& buff)
	{
		if(buff.size()<2*m_max_length)
		{
			m_wave_length = 0;
			return;
		}

		double max_vol = 0.0;
		for(size_t i=0; i<m_max_length; i++)
            max_vol = std::max(max_vol, buff[i]);

		// use a relative threshold
		double threshold = m_noise_threshold*max_vol;

//	cout << "min=" << min_length << " max=" << m_max_length << " threshold=" << treshold << endl;

		double r = 0.0;
		size_t s;
		for(s=m_min_length; r<=threshold && s<m_max_length; s++)
			r = diff(buff, m_max_length, s);

		while((r=diff(buff, m_max_length, s+1))>threshold && s<m_max_length)
			s++;

//	cout << "s=" << s << " r=" << r << endl;
		double old_r = r;
		while(s+1<m_max_length && (r=diff(buff, m_max_length, s+1))<old_r)
		{
//	cout << "s=" << s << " r=" << r << endl;
			s++;
			old_r = r;
		}

//	cout << "s=" << s << " r=" << old_r << endl;

//	cout << "absolute threshold=" << m_noise_threshold << " max volume="<<max_vol<<" relative threshold="<<threshold << " s="<<s << " m_max_length="<<m_max_length << endl;

		m_wave_length = (s<m_max_length)?s:0;
	}

/*
 * ~YIN version, unused because too costly for the CPU: O(n^3) ...
	double CumulativeDiffAlgo::squar_diff(const deque<double>& buff, size_t s)
	{
		double r = 0.0;
		for(size_t i=0; i<s; i++)
		{
			double d = buff[i] - buff[i+s];
			r += d*d;
		}

		return r;
	}
	double CumulativeDiffAlgo::norm_squar_diff(const deque<double>& buff, size_t s)
	{
		double r = 0.0;
		for(size_t i=0; i<s; i++)
			r += squar_diff(buff, i);

		return squar_diff(buff, s) / (r/s);
	}
	void CumulativeDiffAlgo::receive(const deque<double>& buff)
	{
		assert(m_sampling_rate>0);
		size_t min_length = size_t(m_sampling_rate * m_min_wave_length);
		size_t max_length = size_t(m_sampling_rate * m_max_wave_length);

		if(buff.size()<2*max_length)	return;

    cout << "min=" << min_length << " max=" << max_length << " noise=" << m_noise_treshold << endl;

		double r = 1.0;
		double max_vol = 0.0;
		size_t s;
		for(s=min_length; r>m_noise_treshold && s<max_length; s++)
		{
			max_vol = max(max_vol, buff[s]);

			r = norm_squar_diff(buff, s);
//	cout << "s=" << s << " r=" << r << endl;
		}
		s--;
    cout << "s=" << s << " r=" << r << endl;
//	cout << "---" << endl;
		double old_r;
		do
		{
			s++;
			old_r = r;

			r = norm_squar_diff(buff, s);
//	cout << "s=" << s << " r=" << r << endl;
		}
		while(r<old_r && s<max_length);

		r = old_r;
		s--;
    cout << "s=" << s << " r=" << r << endl;

    cout << "m_noise_treshold=" << m_noise_treshold << " max_vol=" << max_vol << " r=" << r << endl;

		m_freq = 0.0;
		if(max_vol>m_noise_treshold && s<max_length)
		{
                        cout << "r=" << r << " m_noise_treshold=" << m_noise_treshold << endl;
			m_freq = double(m_sampling_rate)/s;
		}

    cout << "m_sampling_rate=" << m_sampling_rate << " freq=" << m_freq << endl;
	}
*/
}