Chris@9
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@9
|
2
|
Chris@9
|
3 /*
|
Chris@9
|
4 pYIN - A fundamental frequency estimator for monophonic audio
|
Chris@9
|
5 Centre for Digital Music, Queen Mary, University of London.
|
Chris@9
|
6
|
Chris@9
|
7 This program is free software; you can redistribute it and/or
|
Chris@9
|
8 modify it under the terms of the GNU General Public License as
|
Chris@9
|
9 published by the Free Software Foundation; either version 2 of the
|
Chris@9
|
10 License, or (at your option) any later version. See the file
|
Chris@9
|
11 COPYING included with this distribution for more information.
|
Chris@9
|
12 */
|
Chris@9
|
13
|
matthiasm@0
|
14 #include "MonoNoteHMM.h"
|
matthiasm@0
|
15
|
matthiasm@0
|
16 #include <boost/math/distributions.hpp>
|
matthiasm@0
|
17
|
matthiasm@0
|
18 #include <cstdio>
|
matthiasm@0
|
19 #include <cmath>
|
matthiasm@0
|
20
|
matthiasm@0
|
21 using std::vector;
|
matthiasm@0
|
22 using std::pair;
|
matthiasm@0
|
23
|
matthiasm@0
|
24 MonoNoteHMM::MonoNoteHMM() :
|
matthiasm@0
|
25 par()
|
matthiasm@0
|
26 {
|
matthiasm@0
|
27 build();
|
matthiasm@0
|
28 }
|
matthiasm@0
|
29
|
matthiasm@0
|
30 const vector<double>
|
matthiasm@0
|
31 MonoNoteHMM::calculateObsProb(const vector<pair<double, double> > pitchProb)
|
matthiasm@0
|
32 {
|
matthiasm@0
|
33 // pitchProb is a list of pairs (pitches and their probabilities)
|
matthiasm@0
|
34
|
matthiasm@0
|
35 size_t nCandidate = pitchProb.size();
|
matthiasm@0
|
36
|
matthiasm@0
|
37 // what is the probability of pitched
|
matthiasm@0
|
38 double pIsPitched = 0;
|
matthiasm@0
|
39 for (size_t iCandidate = 0; iCandidate < nCandidate; ++iCandidate)
|
matthiasm@0
|
40 {
|
matthiasm@0
|
41 // pIsPitched = pitchProb[iCandidate].second > pIsPitched ? pitchProb[iCandidate].second : pIsPitched;
|
matthiasm@0
|
42 pIsPitched += pitchProb[iCandidate].second;
|
matthiasm@0
|
43 }
|
matthiasm@0
|
44
|
matthiasm@0
|
45 // pIsPitched = std::pow(pIsPitched, (1-par.priorWeight)) * std::pow(par.priorPitchedProb, par.priorWeight);
|
matthiasm@0
|
46 pIsPitched = pIsPitched * (1-par.priorWeight) + par.priorPitchedProb * par.priorWeight;
|
matthiasm@0
|
47
|
matthiasm@0
|
48 vector<double> out = vector<double>(par.n);
|
matthiasm@0
|
49 double tempProbSum = 0;
|
matthiasm@0
|
50 for (size_t i = 0; i < par.n; ++i)
|
matthiasm@0
|
51 {
|
matthiasm@103
|
52 if (i % par.nSPP != 2)
|
matthiasm@0
|
53 {
|
matthiasm@0
|
54 // std::cerr << getMidiPitch(i) << std::endl;
|
matthiasm@0
|
55 double tempProb = 0;
|
matthiasm@0
|
56 if (nCandidate > 0)
|
matthiasm@0
|
57 {
|
matthiasm@0
|
58 double minDist = 10000.0;
|
matthiasm@0
|
59 double minDistProb = 0;
|
matthiasm@0
|
60 size_t minDistCandidate = 0;
|
matthiasm@0
|
61 for (size_t iCandidate = 0; iCandidate < nCandidate; ++iCandidate)
|
matthiasm@0
|
62 {
|
matthiasm@0
|
63 double currDist = std::abs(getMidiPitch(i)-pitchProb[iCandidate].first);
|
matthiasm@0
|
64 if (currDist < minDist)
|
matthiasm@0
|
65 {
|
matthiasm@0
|
66 minDist = currDist;
|
matthiasm@0
|
67 minDistProb = pitchProb[iCandidate].second;
|
matthiasm@0
|
68 minDistCandidate = iCandidate;
|
matthiasm@0
|
69 }
|
matthiasm@0
|
70 }
|
matthiasm@101
|
71 tempProb = std::pow(minDistProb, par.yinTrust) *
|
matthiasm@101
|
72 boost::math::pdf(pitchDistr[i],
|
matthiasm@101
|
73 pitchProb[minDistCandidate].first);
|
matthiasm@0
|
74 } else {
|
matthiasm@0
|
75 tempProb = 1;
|
matthiasm@0
|
76 }
|
matthiasm@0
|
77 tempProbSum += tempProb;
|
matthiasm@0
|
78 out[i] = tempProb;
|
matthiasm@0
|
79 }
|
matthiasm@0
|
80 }
|
matthiasm@0
|
81
|
matthiasm@0
|
82 for (size_t i = 0; i < par.n; ++i)
|
matthiasm@0
|
83 {
|
matthiasm@103
|
84 if (i % par.nSPP != 2)
|
matthiasm@0
|
85 {
|
matthiasm@0
|
86 if (tempProbSum > 0)
|
matthiasm@0
|
87 {
|
matthiasm@0
|
88 out[i] = out[i] / tempProbSum * pIsPitched;
|
matthiasm@0
|
89 }
|
matthiasm@0
|
90 } else {
|
matthiasm@0
|
91 out[i] = (1-pIsPitched) / (par.nPPS * par.nS);
|
matthiasm@0
|
92 }
|
matthiasm@0
|
93 }
|
matthiasm@0
|
94
|
matthiasm@0
|
95 return(out);
|
matthiasm@0
|
96 }
|
matthiasm@0
|
97
|
matthiasm@0
|
98 void
|
matthiasm@0
|
99 MonoNoteHMM::build()
|
matthiasm@0
|
100 {
|
matthiasm@0
|
101 // the states are organised as follows:
|
matthiasm@0
|
102 // 0-2. lowest pitch
|
matthiasm@0
|
103 // 0. attack state
|
matthiasm@0
|
104 // 1. stable state
|
matthiasm@0
|
105 // 2. silent state
|
matthiasm@103
|
106 // 3-5. second-lowest pitch
|
matthiasm@103
|
107 // 3. attack state
|
matthiasm@0
|
108 // ...
|
matthiasm@0
|
109
|
matthiasm@0
|
110 // observation distributions
|
matthiasm@0
|
111 for (size_t iState = 0; iState < par.n; ++iState)
|
matthiasm@0
|
112 {
|
matthiasm@0
|
113 pitchDistr.push_back(boost::math::normal(0,1));
|
matthiasm@103
|
114 if (iState % par.nSPP == 2)
|
matthiasm@0
|
115 {
|
matthiasm@0
|
116 // silent state starts tracking
|
matthiasm@0
|
117 init.push_back(1.0/(par.nS * par.nPPS));
|
matthiasm@0
|
118 } else {
|
matthiasm@0
|
119 init.push_back(0.0);
|
matthiasm@0
|
120 }
|
matthiasm@0
|
121 }
|
matthiasm@0
|
122
|
matthiasm@0
|
123 for (size_t iPitch = 0; iPitch < (par.nS * par.nPPS); ++iPitch)
|
matthiasm@0
|
124 {
|
matthiasm@0
|
125 size_t index = iPitch * par.nSPP;
|
matthiasm@0
|
126 double mu = par.minPitch + iPitch * 1.0/par.nPPS;
|
matthiasm@0
|
127 pitchDistr[index] = boost::math::normal(mu, par.sigmaYinPitchAttack);
|
matthiasm@0
|
128 pitchDistr[index+1] = boost::math::normal(mu, par.sigmaYinPitchStable);
|
matthiasm@0
|
129 pitchDistr[index+2] = boost::math::normal(mu, 1.0); // dummy
|
matthiasm@0
|
130 }
|
matthiasm@0
|
131
|
matthiasm@0
|
132 boost::math::normal noteDistanceDistr(0, par.sigma2Note);
|
matthiasm@0
|
133
|
matthiasm@0
|
134 for (size_t iPitch = 0; iPitch < (par.nS * par.nPPS); ++iPitch)
|
matthiasm@0
|
135 {
|
matthiasm@0
|
136 // loop through all notes and set sparse transition probabilities
|
matthiasm@0
|
137 size_t index = iPitch * par.nSPP;
|
matthiasm@0
|
138
|
matthiasm@0
|
139 // transitions from attack state
|
matthiasm@0
|
140 from.push_back(index);
|
matthiasm@0
|
141 to.push_back(index);
|
matthiasm@0
|
142 transProb.push_back(par.pAttackSelftrans);
|
matthiasm@0
|
143
|
matthiasm@0
|
144 from.push_back(index);
|
matthiasm@0
|
145 to.push_back(index+1);
|
matthiasm@0
|
146 transProb.push_back(1-par.pAttackSelftrans);
|
matthiasm@0
|
147
|
matthiasm@0
|
148 // transitions from stable state
|
matthiasm@0
|
149 from.push_back(index+1);
|
matthiasm@0
|
150 to.push_back(index+1); // to itself
|
matthiasm@0
|
151 transProb.push_back(par.pStableSelftrans);
|
matthiasm@0
|
152
|
matthiasm@0
|
153 from.push_back(index+1);
|
matthiasm@0
|
154 to.push_back(index+2); // to silent
|
matthiasm@0
|
155 transProb.push_back(par.pStable2Silent);
|
matthiasm@0
|
156
|
matthiasm@0
|
157 // the "easy" transitions from silent state
|
matthiasm@0
|
158 from.push_back(index+2);
|
matthiasm@0
|
159 to.push_back(index+2);
|
matthiasm@0
|
160 transProb.push_back(par.pSilentSelftrans);
|
matthiasm@0
|
161
|
matthiasm@0
|
162
|
matthiasm@106
|
163 // the more complicated transitions from the silent
|
matthiasm@0
|
164 double probSumSilent = 0;
|
matthiasm@106
|
165
|
matthiasm@0
|
166 vector<double> tempTransProbSilent;
|
matthiasm@0
|
167 for (size_t jPitch = 0; jPitch < (par.nS * par.nPPS); ++jPitch)
|
matthiasm@0
|
168 {
|
matthiasm@0
|
169 int fromPitch = iPitch;
|
matthiasm@0
|
170 int toPitch = jPitch;
|
matthiasm@101
|
171 double semitoneDistance =
|
matthiasm@101
|
172 std::abs(fromPitch - toPitch) * 1.0 / par.nPPS;
|
matthiasm@0
|
173
|
matthiasm@0
|
174 // if (std::fmod(semitoneDistance, 1) == 0 && semitoneDistance > par.minSemitoneDistance)
|
matthiasm@101
|
175 if (semitoneDistance == 0 ||
|
matthiasm@101
|
176 (semitoneDistance > par.minSemitoneDistance
|
matthiasm@101
|
177 && semitoneDistance < par.maxJump))
|
matthiasm@0
|
178 {
|
matthiasm@0
|
179 size_t toIndex = jPitch * par.nSPP; // note attack index
|
matthiasm@0
|
180
|
matthiasm@101
|
181 double tempWeightSilent = boost::math::pdf(noteDistanceDistr,
|
matthiasm@101
|
182 semitoneDistance);
|
matthiasm@0
|
183 probSumSilent += tempWeightSilent;
|
matthiasm@0
|
184
|
matthiasm@0
|
185 tempTransProbSilent.push_back(tempWeightSilent);
|
matthiasm@0
|
186
|
matthiasm@0
|
187 from.push_back(index+2);
|
matthiasm@0
|
188 to.push_back(toIndex);
|
matthiasm@0
|
189 }
|
matthiasm@0
|
190 }
|
matthiasm@0
|
191 for (size_t i = 0; i < tempTransProbSilent.size(); ++i)
|
matthiasm@0
|
192 {
|
matthiasm@0
|
193 transProb.push_back((1-par.pSilentSelftrans) * tempTransProbSilent[i]/probSumSilent);
|
matthiasm@0
|
194 }
|
matthiasm@0
|
195 }
|
matthiasm@0
|
196 }
|
matthiasm@0
|
197
|
matthiasm@0
|
198 double
|
matthiasm@0
|
199 MonoNoteHMM::getMidiPitch(size_t index)
|
matthiasm@0
|
200 {
|
matthiasm@0
|
201 return pitchDistr[index].mean();
|
matthiasm@0
|
202 }
|
matthiasm@0
|
203
|
matthiasm@0
|
204 double
|
matthiasm@0
|
205 MonoNoteHMM::getFrequency(size_t index)
|
matthiasm@0
|
206 {
|
matthiasm@0
|
207 return 440 * pow(2, (pitchDistr[index].mean()-69)/12);
|
matthiasm@0
|
208 }
|