annotate MonoPitch.cpp @ 59:b13d82111c8f tony

merge
author matthiasm
date Thu, 06 Mar 2014 16:48:27 +0000
parents b8cc6a9720a0
children 080fe18f5ebf
rev   line source
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 "MonoPitch.h"
matthiasm@0 15 #include "MonoPitchHMM.h"
matthiasm@0 16 #include <vector>
matthiasm@0 17
matthiasm@0 18 #include <cstdio>
matthiasm@0 19 #include <cmath>
matthiasm@0 20 #include <complex>
matthiasm@0 21
matthiasm@0 22 using std::vector;
matthiasm@0 23 using std::pair;
matthiasm@0 24
matthiasm@0 25 MonoPitch::MonoPitch() :
matthiasm@0 26 hmm()
matthiasm@0 27 {
matthiasm@0 28 }
matthiasm@0 29
matthiasm@0 30 MonoPitch::~MonoPitch()
matthiasm@0 31 {
matthiasm@0 32 }
matthiasm@0 33
matthiasm@0 34 const vector<float>
matthiasm@0 35 MonoPitch::process(const vector<vector<pair<double, double> > > pitchProb)
matthiasm@0 36 {
matthiasm@0 37 // std::cerr << "before observation prob calculation" << std::endl;
matthiasm@0 38 vector<vector<double> > obsProb;
matthiasm@0 39 for (size_t iFrame = 0; iFrame < pitchProb.size(); ++iFrame)
matthiasm@0 40 {
matthiasm@0 41 obsProb.push_back(hmm.calculateObsProb(pitchProb[iFrame]));
matthiasm@0 42 }
matthiasm@0 43
matthiasm@33 44 vector<double> *scale = new vector<double>(0);
matthiasm@0 45
matthiasm@0 46 vector<float> out;
matthiasm@0 47
matthiasm@0 48 // std::cerr << "before Viterbi decoding" << obsProb.size() << "ng" << obsProb[1].size() << std::endl;
matthiasm@0 49 vector<int> path = hmm.decodeViterbi(obsProb, scale);
matthiasm@0 50 // std::cerr << "after Viterbi decoding" << std::endl;
matthiasm@0 51
matthiasm@0 52 for (size_t iFrame = 0; iFrame < path.size(); ++iFrame)
matthiasm@0 53 {
matthiasm@0 54 // std::cerr << path[iFrame] << " " << hmm.m_freqs[path[iFrame]] << std::endl;
matthiasm@0 55 float hmmFreq = hmm.m_freqs[path[iFrame]];
matthiasm@0 56 float bestFreq = 0;
matthiasm@0 57 float leastDist = 10000;
matthiasm@0 58 if (hmmFreq > 0)
matthiasm@0 59 {
matthiasm@0 60 // This was a Yin estimate, so try to get original pitch estimate back
matthiasm@0 61 // ... a bit hacky, since we could have direclty saved the frequency
matthiasm@0 62 // that was assigned to the HMM bin in hmm.calculateObsProb -- but would
matthiasm@0 63 // have had to rethink the interface of that method.
matthiasm@0 64 for (size_t iPitch = 0; iPitch < pitchProb[iFrame].size(); ++iPitch)
matthiasm@0 65 {
matthiasm@0 66 float freq = 440. * std::pow(2, (pitchProb[iFrame][iPitch].first - 69)/12);
matthiasm@0 67 float dist = std::abs(hmmFreq-freq);
matthiasm@0 68 if (dist < leastDist)
matthiasm@0 69 {
matthiasm@0 70 leastDist = dist;
matthiasm@0 71 bestFreq = freq;
matthiasm@0 72 }
matthiasm@0 73 }
matthiasm@0 74 } else {
matthiasm@0 75 bestFreq = hmmFreq;
matthiasm@0 76 }
matthiasm@0 77 out.push_back(bestFreq);
matthiasm@0 78 }
matthiasm@0 79 delete scale;
matthiasm@0 80 return(out);
Chris@9 81 }