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
|
mail@132
|
24 MonoNoteHMM::MonoNoteHMM(int fixedLag) :
|
mail@132
|
25 SparseHMM(fixedLag),
|
matthiasm@0
|
26 par()
|
matthiasm@0
|
27 {
|
matthiasm@0
|
28 build();
|
matthiasm@0
|
29 }
|
matthiasm@0
|
30
|
Chris@145
|
31 vector<double>
|
Chris@156
|
32 MonoNoteHMM::calculateObsProb(const vector<pair<double, double> > &pitchProb)
|
matthiasm@0
|
33 {
|
matthiasm@0
|
34 // pitchProb is a list of pairs (pitches and their probabilities)
|
matthiasm@0
|
35
|
matthiasm@0
|
36 size_t nCandidate = pitchProb.size();
|
matthiasm@0
|
37
|
matthiasm@0
|
38 // what is the probability of pitched
|
matthiasm@0
|
39 double pIsPitched = 0;
|
mail@133
|
40 for (size_t iCand = 0; iCand < nCandidate; ++iCand)
|
matthiasm@0
|
41 {
|
mail@133
|
42 pIsPitched += pitchProb[iCand].second;
|
matthiasm@0
|
43 }
|
matthiasm@0
|
44
|
mail@133
|
45 pIsPitched = pIsPitched * (1-par.priorWeight) +
|
mail@133
|
46 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;
|
mail@133
|
61 for (size_t iCand = 0; iCand < nCandidate; ++iCand)
|
matthiasm@0
|
62 {
|
mail@133
|
63 double currDist = std::abs(getMidiPitch(i)-
|
mail@133
|
64 pitchProb[iCand].first);
|
matthiasm@0
|
65 if (currDist < minDist)
|
matthiasm@0
|
66 {
|
matthiasm@0
|
67 minDist = currDist;
|
mail@133
|
68 minDistProb = pitchProb[iCand].second;
|
mail@133
|
69 minDistCandidate = iCand;
|
matthiasm@0
|
70 }
|
matthiasm@0
|
71 }
|
matthiasm@101
|
72 tempProb = std::pow(minDistProb, par.yinTrust) *
|
matthiasm@101
|
73 boost::math::pdf(pitchDistr[i],
|
matthiasm@101
|
74 pitchProb[minDistCandidate].first);
|
matthiasm@0
|
75 } else {
|
matthiasm@0
|
76 tempProb = 1;
|
matthiasm@0
|
77 }
|
matthiasm@0
|
78 tempProbSum += tempProb;
|
matthiasm@0
|
79 out[i] = tempProb;
|
matthiasm@0
|
80 }
|
matthiasm@0
|
81 }
|
matthiasm@0
|
82
|
matthiasm@0
|
83 for (size_t i = 0; i < par.n; ++i)
|
matthiasm@0
|
84 {
|
matthiasm@103
|
85 if (i % par.nSPP != 2)
|
matthiasm@0
|
86 {
|
matthiasm@0
|
87 if (tempProbSum > 0)
|
matthiasm@0
|
88 {
|
matthiasm@0
|
89 out[i] = out[i] / tempProbSum * pIsPitched;
|
matthiasm@0
|
90 }
|
matthiasm@0
|
91 } else {
|
matthiasm@0
|
92 out[i] = (1-pIsPitched) / (par.nPPS * par.nS);
|
matthiasm@0
|
93 }
|
matthiasm@0
|
94 }
|
Chris@146
|
95
|
matthiasm@0
|
96 return(out);
|
matthiasm@0
|
97 }
|
matthiasm@0
|
98
|
matthiasm@0
|
99 void
|
matthiasm@0
|
100 MonoNoteHMM::build()
|
matthiasm@0
|
101 {
|
matthiasm@0
|
102 // the states are organised as follows:
|
matthiasm@0
|
103 // 0-2. lowest pitch
|
matthiasm@0
|
104 // 0. attack state
|
matthiasm@0
|
105 // 1. stable state
|
matthiasm@0
|
106 // 2. silent state
|
matthiasm@103
|
107 // 3-5. second-lowest pitch
|
matthiasm@103
|
108 // 3. attack state
|
matthiasm@0
|
109 // ...
|
matthiasm@0
|
110
|
mail@130
|
111 m_nState = par.n;
|
mail@130
|
112
|
matthiasm@0
|
113 // observation distributions
|
matthiasm@0
|
114 for (size_t iState = 0; iState < par.n; ++iState)
|
matthiasm@0
|
115 {
|
matthiasm@0
|
116 pitchDistr.push_back(boost::math::normal(0,1));
|
matthiasm@103
|
117 if (iState % par.nSPP == 2)
|
matthiasm@0
|
118 {
|
matthiasm@0
|
119 // silent state starts tracking
|
mail@130
|
120 m_init.push_back(1.0/(par.nS * par.nPPS));
|
matthiasm@0
|
121 } else {
|
mail@130
|
122 m_init.push_back(0.0);
|
matthiasm@0
|
123 }
|
matthiasm@0
|
124 }
|
matthiasm@0
|
125
|
matthiasm@0
|
126 for (size_t iPitch = 0; iPitch < (par.nS * par.nPPS); ++iPitch)
|
matthiasm@0
|
127 {
|
matthiasm@0
|
128 size_t index = iPitch * par.nSPP;
|
matthiasm@0
|
129 double mu = par.minPitch + iPitch * 1.0/par.nPPS;
|
matthiasm@0
|
130 pitchDistr[index] = boost::math::normal(mu, par.sigmaYinPitchAttack);
|
matthiasm@0
|
131 pitchDistr[index+1] = boost::math::normal(mu, par.sigmaYinPitchStable);
|
matthiasm@0
|
132 pitchDistr[index+2] = boost::math::normal(mu, 1.0); // dummy
|
matthiasm@0
|
133 }
|
matthiasm@0
|
134
|
matthiasm@0
|
135 boost::math::normal noteDistanceDistr(0, par.sigma2Note);
|
matthiasm@0
|
136
|
matthiasm@0
|
137 for (size_t iPitch = 0; iPitch < (par.nS * par.nPPS); ++iPitch)
|
matthiasm@0
|
138 {
|
matthiasm@0
|
139 // loop through all notes and set sparse transition probabilities
|
matthiasm@0
|
140 size_t index = iPitch * par.nSPP;
|
matthiasm@0
|
141
|
matthiasm@0
|
142 // transitions from attack state
|
mail@130
|
143 m_from.push_back(index);
|
mail@130
|
144 m_to.push_back(index);
|
mail@130
|
145 m_transProb.push_back(par.pAttackSelftrans);
|
matthiasm@0
|
146
|
mail@130
|
147 m_from.push_back(index);
|
mail@130
|
148 m_to.push_back(index+1);
|
mail@130
|
149 m_transProb.push_back(1-par.pAttackSelftrans);
|
matthiasm@0
|
150
|
matthiasm@0
|
151 // transitions from stable state
|
mail@130
|
152 m_from.push_back(index+1);
|
mail@130
|
153 m_to.push_back(index+1); // to itself
|
mail@130
|
154 m_transProb.push_back(par.pStableSelftrans);
|
matthiasm@0
|
155
|
mail@130
|
156 m_from.push_back(index+1);
|
mail@130
|
157 m_to.push_back(index+2); // to silent
|
mail@130
|
158 m_transProb.push_back(par.pStable2Silent);
|
matthiasm@0
|
159
|
matthiasm@0
|
160 // the "easy" transitions from silent state
|
mail@130
|
161 m_from.push_back(index+2);
|
mail@130
|
162 m_to.push_back(index+2);
|
mail@130
|
163 m_transProb.push_back(par.pSilentSelftrans);
|
matthiasm@0
|
164
|
matthiasm@0
|
165
|
matthiasm@106
|
166 // the more complicated transitions from the silent
|
matthiasm@0
|
167 double probSumSilent = 0;
|
matthiasm@106
|
168
|
matthiasm@0
|
169 vector<double> tempTransProbSilent;
|
matthiasm@0
|
170 for (size_t jPitch = 0; jPitch < (par.nS * par.nPPS); ++jPitch)
|
matthiasm@0
|
171 {
|
matthiasm@0
|
172 int fromPitch = iPitch;
|
matthiasm@0
|
173 int toPitch = jPitch;
|
matthiasm@101
|
174 double semitoneDistance =
|
matthiasm@101
|
175 std::abs(fromPitch - toPitch) * 1.0 / par.nPPS;
|
matthiasm@0
|
176
|
mail@133
|
177
|
matthiasm@101
|
178 if (semitoneDistance == 0 ||
|
matthiasm@101
|
179 (semitoneDistance > par.minSemitoneDistance
|
matthiasm@101
|
180 && semitoneDistance < par.maxJump))
|
matthiasm@0
|
181 {
|
matthiasm@0
|
182 size_t toIndex = jPitch * par.nSPP; // note attack index
|
matthiasm@0
|
183
|
matthiasm@101
|
184 double tempWeightSilent = boost::math::pdf(noteDistanceDistr,
|
matthiasm@101
|
185 semitoneDistance);
|
matthiasm@0
|
186 probSumSilent += tempWeightSilent;
|
matthiasm@0
|
187
|
matthiasm@0
|
188 tempTransProbSilent.push_back(tempWeightSilent);
|
matthiasm@0
|
189
|
mail@130
|
190 m_from.push_back(index+2);
|
mail@130
|
191 m_to.push_back(toIndex);
|
matthiasm@0
|
192 }
|
matthiasm@0
|
193 }
|
matthiasm@0
|
194 for (size_t i = 0; i < tempTransProbSilent.size(); ++i)
|
matthiasm@0
|
195 {
|
mail@133
|
196 m_transProb.push_back((1-par.pSilentSelftrans) *
|
mail@133
|
197 tempTransProbSilent[i]/probSumSilent);
|
matthiasm@0
|
198 }
|
matthiasm@0
|
199 }
|
mail@130
|
200 m_nTrans = m_transProb.size();
|
mail@130
|
201 m_delta = vector<double>(m_nState);
|
mail@130
|
202 m_oldDelta = vector<double>(m_nState);
|
matthiasm@0
|
203 }
|
matthiasm@0
|
204
|
matthiasm@0
|
205 double
|
matthiasm@0
|
206 MonoNoteHMM::getMidiPitch(size_t index)
|
matthiasm@0
|
207 {
|
matthiasm@0
|
208 return pitchDistr[index].mean();
|
matthiasm@0
|
209 }
|
matthiasm@0
|
210
|
matthiasm@0
|
211 double
|
matthiasm@0
|
212 MonoNoteHMM::getFrequency(size_t index)
|
matthiasm@0
|
213 {
|
matthiasm@0
|
214 return 440 * pow(2, (pitchDistr[index].mean()-69)/12);
|
matthiasm@0
|
215 }
|