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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
pYIN - A fundamental frequency estimator for monophonic audio
Centre for Digital Music, Queen Mary, University of London.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "MonoNoteHMM.h"
#include <boost/math/distributions.hpp>
#include <cstdio>
#include <cmath>
using std::vector;
using std::pair;
MonoNoteHMM::MonoNoteHMM() :
par()
{
build();
}
const vector<double>
MonoNoteHMM::calculateObsProb(const vector<pair<double, double> > pitchProb)
{
// pitchProb is a list of pairs (pitches and their probabilities)
size_t nCandidate = pitchProb.size();
// what is the probability of pitched
double pIsPitched = 0;
for (size_t iCandidate = 0; iCandidate < nCandidate; ++iCandidate)
{
// pIsPitched = pitchProb[iCandidate].second > pIsPitched ? pitchProb[iCandidate].second : pIsPitched;
pIsPitched += pitchProb[iCandidate].second;
}
// pIsPitched = std::pow(pIsPitched, (1-par.priorWeight)) * std::pow(par.priorPitchedProb, par.priorWeight);
pIsPitched = pIsPitched * (1-par.priorWeight) + par.priorPitchedProb * par.priorWeight;
vector<double> out = vector<double>(par.n);
double tempProbSum = 0;
for (size_t i = 0; i < par.n; ++i)
{
if (i % par.nSPP != 2)
{
// std::cerr << getMidiPitch(i) << std::endl;
double tempProb = 0;
if (nCandidate > 0)
{
double minDist = 10000.0;
double minDistProb = 0;
size_t minDistCandidate = 0;
for (size_t iCandidate = 0; iCandidate < nCandidate; ++iCandidate)
{
double currDist = std::abs(getMidiPitch(i)-pitchProb[iCandidate].first);
if (currDist < minDist)
{
minDist = currDist;
minDistProb = pitchProb[iCandidate].second;
minDistCandidate = iCandidate;
}
}
tempProb = std::pow(minDistProb, par.yinTrust) *
boost::math::pdf(pitchDistr[i],
pitchProb[minDistCandidate].first);
} else {
tempProb = 1;
}
tempProbSum += tempProb;
out[i] = tempProb;
}
}
for (size_t i = 0; i < par.n; ++i)
{
if (i % par.nSPP != 2)
{
if (tempProbSum > 0)
{
out[i] = out[i] / tempProbSum * pIsPitched;
}
} else {
out[i] = (1-pIsPitched) / (par.nPPS * par.nS);
}
}
return(out);
}
void
MonoNoteHMM::build()
{
// the states are organised as follows:
// 0-2. lowest pitch
// 0. attack state
// 1. stable state
// 2. silent state
// 3-5. second-lowest pitch
// 3. attack state
// ...
// observation distributions
for (size_t iState = 0; iState < par.n; ++iState)
{
pitchDistr.push_back(boost::math::normal(0,1));
if (iState % par.nSPP == 2)
{
// silent state starts tracking
init.push_back(1.0/(par.nS * par.nPPS));
} else {
init.push_back(0.0);
}
}
for (size_t iPitch = 0; iPitch < (par.nS * par.nPPS); ++iPitch)
{
size_t index = iPitch * par.nSPP;
double mu = par.minPitch + iPitch * 1.0/par.nPPS;
pitchDistr[index] = boost::math::normal(mu, par.sigmaYinPitchAttack);
pitchDistr[index+1] = boost::math::normal(mu, par.sigmaYinPitchStable);
pitchDistr[index+2] = boost::math::normal(mu, 1.0); // dummy
}
boost::math::normal noteDistanceDistr(0, par.sigma2Note);
for (size_t iPitch = 0; iPitch < (par.nS * par.nPPS); ++iPitch)
{
// loop through all notes and set sparse transition probabilities
size_t index = iPitch * par.nSPP;
// transitions from attack state
from.push_back(index);
to.push_back(index);
transProb.push_back(par.pAttackSelftrans);
from.push_back(index);
to.push_back(index+1);
transProb.push_back(1-par.pAttackSelftrans);
// transitions from stable state
from.push_back(index+1);
to.push_back(index+1); // to itself
transProb.push_back(par.pStableSelftrans);
from.push_back(index+1);
to.push_back(index+2); // to silent
transProb.push_back(par.pStable2Silent);
// the "easy" transitions from silent state
from.push_back(index+2);
to.push_back(index+2);
transProb.push_back(par.pSilentSelftrans);
// the more complicated transitions from the silent
double probSumSilent = 0;
vector<double> tempTransProbSilent;
for (size_t jPitch = 0; jPitch < (par.nS * par.nPPS); ++jPitch)
{
int fromPitch = iPitch;
int toPitch = jPitch;
double semitoneDistance =
std::abs(fromPitch - toPitch) * 1.0 / par.nPPS;
// if (std::fmod(semitoneDistance, 1) == 0 && semitoneDistance > par.minSemitoneDistance)
if (semitoneDistance == 0 ||
(semitoneDistance > par.minSemitoneDistance
&& semitoneDistance < par.maxJump))
{
size_t toIndex = jPitch * par.nSPP; // note attack index
double tempWeightSilent = boost::math::pdf(noteDistanceDistr,
semitoneDistance);
probSumSilent += tempWeightSilent;
tempTransProbSilent.push_back(tempWeightSilent);
from.push_back(index+2);
to.push_back(toIndex);
}
}
for (size_t i = 0; i < tempTransProbSilent.size(); ++i)
{
transProb.push_back((1-par.pSilentSelftrans) * tempTransProbSilent[i]/probSumSilent);
}
}
}
double
MonoNoteHMM::getMidiPitch(size_t index)
{
return pitchDistr[index].mean();
}
double
MonoNoteHMM::getFrequency(size_t index)
{
return 440 * pow(2, (pitchDistr[index].mean()-69)/12);
}
|