Skip to content

Commit

Permalink
New: Enabling the second layer of the Hidden Markov Model
Browse files Browse the repository at this point in the history
  • Loading branch information
napulen committed Jul 24, 2018
1 parent 684be8c commit 8f6cf39
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 83 deletions.
15 changes: 8 additions & 7 deletions include/hiddenmarkovmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,21 @@ class HiddenMarkovModel {
std::map<std::string, double> > m_emissionProbabilities;

public:
typedef std::vector<Key> KeySequence;
HiddenMarkovModel(
std::vector<PitchClass> observations,
Key::KeyVector states,
std::map<Key, double> initialProbabilities,
std::map<Key, std::map<Key, double> > transitionProbabilities,
std::map<Key, std::map<PitchClass, double> > emissionProbabilities);
// HiddenMarkovModel(
// std::vector<Key> observations,
// Key::KeyVector states,
// std::map<Key, double> initialProbabilities,
// std::map<Key, std::map<Key, double> > transitionProbabilities,
// std::map<Key, std::map<Key, double> > emissionProbabilities);
HiddenMarkovModel(
std::vector<Key> observations,
Key::KeyVector states,
std::map<Key, double> initialProbabilities,
std::map<Key, std::map<Key, double> > transitionProbabilities,
std::map<Key, std::map<Key, double> > emissionProbabilities);
void printOutput();
void runViterbi();
KeySequence runViterbi();
};


Expand Down
111 changes: 61 additions & 50 deletions src/hiddenmarkovmodel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,55 +82,55 @@ HiddenMarkovModel::HiddenMarkovModel(
}
}

// HiddenMarkovModel::HiddenMarkovModel(
// std::vector<Key> observations,
// Key::KeyVector states,
// std::map<Key, double> initialProbabilities,
// std::map<Key, std::map<Key, double> > transitionProbabilities,
// std::map<Key, std::map<Key, double> > emissionProbabilities) {
// // Observations
// for (std::vector<Key>::const_iterator itObs = observations.begin();
// itObs != observations.end(); itObs++) {
// m_observations.push_back(itObs->getInt());
// }
// // States
// for (std::vector<Key>::const_iterator itState = states.begin();
// itState != states.end(); itState++) {
// m_states.push_back(itState->getString());
// }
// // Initial probabilities
// for (std::map<Key, double>::const_iterator itInitial =
// initialProbabilities.begin(); itInitial != initialProbabilities.end();
// itInitial++) {
// std::string key = (itInitial->first).getString();
// m_initialProbabilities[key] =
// itInitial->second;
// }
// // Transition probabilities
// for (std::map<Key, std::map<Key,
// double> >::const_iterator itTransition = transitionProbabilities.begin();
// itTransition != transitionProbabilities.end(); itTransition++) {
// std::string fromKey = (itTransition->first).getString();
// std::map<Key, double> toKeyMap = itTransition->second;
// for (std::map<Key, double>::const_iterator itToKey = toKeyMap.begin();
// itToKey != toKeyMap.end(); itToKey++ ) {
// std::string toKey = (itToKey->first).getString();
// m_transitionProbabilities[fromKey][toKey] = itToKey->second;
// }
// }
// // Emission probabilities
// for (std::map<Key, std::map<PitchClass,
// double> >::const_iterator itEmission = emissionProbabilities.begin();
// itEmission != emissionProbabilities.end(); itEmission++) {
// std::string fromKey = (itEmission->first).getString();
// std::map<PitchClass, double> emitPcMap = itEmission->second;
// for (std::map<PitchClass, double>::const_iterator itEmitPc =
// emitPcMap.begin(); itEmitPc != emitPcMap.end(); itEmitPc++) {
// int emitPc = (itEmitPc->first).getInt();
// m_emissionProbabilities[fromKey][emitPc] = itEmitPc->second;
// }
// }
// }
HiddenMarkovModel::HiddenMarkovModel(
std::vector<Key> observations,
Key::KeyVector states,
std::map<Key, double> initialProbabilities,
std::map<Key, std::map<Key, double> > transitionProbabilities,
std::map<Key, std::map<Key, double> > emissionProbabilities) {
// Observations
for (std::vector<Key>::const_iterator itObs = observations.begin();
itObs != observations.end(); itObs++) {
m_observations.push_back(itObs->getString());
}
// States
for (std::vector<Key>::const_iterator itState = states.begin();
itState != states.end(); itState++) {
m_states.push_back(itState->getString());
}
// Initial probabilities
for (std::map<Key, double>::const_iterator itInitial =
initialProbabilities.begin(); itInitial != initialProbabilities.end();
itInitial++) {
std::string key = (itInitial->first).getString();
m_initialProbabilities[key] =
itInitial->second;
}
// Transition probabilities
for (std::map<Key, std::map<Key,
double> >::const_iterator itTransition = transitionProbabilities.begin();
itTransition != transitionProbabilities.end(); itTransition++) {
std::string fromKey = (itTransition->first).getString();
std::map<Key, double> toKeyMap = itTransition->second;
for (std::map<Key, double>::const_iterator itToKey = toKeyMap.begin();
itToKey != toKeyMap.end(); itToKey++ ) {
std::string toKey = (itToKey->first).getString();
m_transitionProbabilities[fromKey][toKey] = itToKey->second;
}
}
// Emission probabilities
for (std::map<Key, std::map<Key,
double> >::const_iterator itEmission = emissionProbabilities.begin();
itEmission != emissionProbabilities.end(); itEmission++) {
std::string fromKey = (itEmission->first).getString();
std::map<Key, double> emitKeyMap = itEmission->second;
for (std::map<Key, double>::const_iterator itEmitKey =
emitKeyMap.begin(); itEmitKey != emitKeyMap.end(); itEmitKey++) {
std::string emitKey = (itEmitKey->first).getString();
m_emissionProbabilities[fromKey][emitKey] = itEmitKey->second;
}
}
}

void HiddenMarkovModel::printOutput() {
// print states
Expand Down Expand Up @@ -178,7 +178,8 @@ void HiddenMarkovModel::printOutput() {
}
}

void HiddenMarkovModel::runViterbi() {
HiddenMarkovModel::KeySequence HiddenMarkovModel::runViterbi() {
KeySequence keySequence;
std::map<std::string, Tracking> T;

for (std::vector<std::string>::iterator state=m_states.begin();
Expand Down Expand Up @@ -232,6 +233,14 @@ void HiddenMarkovModel::runViterbi() {
}
}

final_tracker.v_path.pop_back();

for (std::vector<std::string>::const_iterator itNode =
final_tracker.v_path.begin(); itNode != final_tracker.v_path.end();
itNode++) {
keySequence.push_back(Key(*itNode));
}

std::cout << "Total probability of the observation sequence: "
<< final_tracker.prob << std::endl;
std::cout << "Probability of the Viterbi path: "
Expand All @@ -241,6 +250,8 @@ void HiddenMarkovModel::runViterbi() {
state != final_tracker.v_path.end(); state++) {
std::cout << "VState: " << *state << std::endl;
}

return keySequence;
}

} // namespace justkeydding
43 changes: 17 additions & 26 deletions test/test_hiddenmarkovmodel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,14 @@ using justkeydding::HiddenMarkovModel;


int main(int argc, char *argv[]) {
////////////////////////////
// First Hidden Markov Model
////////////////////////////
// Observation symbols section
std::vector<PitchClass> pitchClassSequence;
pitchClassSequence.push_back(PitchClass("C"));
pitchClassSequence.push_back(PitchClass("D"));
pitchClassSequence.push_back(PitchClass("E"));
pitchClassSequence.push_back(PitchClass("F"));
pitchClassSequence.push_back(PitchClass("G"));
pitchClassSequence.push_back(PitchClass("A"));
pitchClassSequence.push_back(PitchClass("B"));
pitchClassSequence.push_back(PitchClass("A"));
pitchClassSequence.push_back(PitchClass("G"));
pitchClassSequence.push_back(PitchClass("F#"));
pitchClassSequence.push_back(PitchClass("E"));
pitchClassSequence.push_back(PitchClass("D"));
pitchClassSequence.push_back(PitchClass("C#"));
pitchClassSequence.push_back(PitchClass("D"));
pitchClassSequence.push_back(PitchClass("E"));
pitchClassSequence.push_back(PitchClass("F#"));
pitchClassSequence.push_back(PitchClass("G"));
pitchClassSequence.push_back(PitchClass("A"));
pitchClassSequence.push_back(PitchClass("B"));
pitchClassSequence.push_back(PitchClass("C"));
pitchClassSequence.push_back(PitchClass("B"));
pitchClassSequence.push_back(PitchClass("A"));
pitchClassSequence.push_back(PitchClass("G"));
pitchClassSequence.push_back(PitchClass("F"));
pitchClassSequence.push_back(PitchClass("E"));
pitchClassSequence.push_back(PitchClass("D"));
pitchClassSequence.push_back(PitchClass("C"));
// States section
Key::KeyVector keyVector =
Key::getAllKeysVector();
Expand All @@ -90,12 +69,24 @@ int main(int argc, char *argv[]) {
// Emission probabilities
KeyProfile::KeyProfileMap emissionProbabilities =
KeyProfile("temperley", "sapp").getKeyProfileMap();
HiddenMarkovModel::KeySequence keySequence;
HiddenMarkovModel hmm(
pitchClassSequence,
keyVector,
initialProbabilities,
transitionProbabilities,
emissionProbabilities);
// hmm.printOutput();
hmm.runViterbi();
keySequence = hmm.runViterbi();
/////////////////////////////
// Second Hidden Markov Model
/////////////////////////////
KeyTransition::KeyTransitionMap zeroTransiionProbabilities =
KeyTransition("zero").getKeyTransitionMap();
HiddenMarkovModel hmm2(
keySequence,
keyVector,
initialProbabilities,
zeroTransiionProbabilities,
transitionProbabilities);
HiddenMarkovModel::KeySequence mainKeySequence = hmm2.runViterbi();
}

0 comments on commit 8f6cf39

Please sign in to comment.