Esempio n. 1
0
        private void MakeStep(int Pos, HMM State, int ArrayStateIndex)
        {
            double PrevMaxStateValue = 0;
            string PrevStateName = "";
            for (int I = 0; I < m_WorkingStates.Count; I++)
            {
                HMM PrevState = (Pos == 1) ? m_BeginingState : m_WorkingStates[I];
                double PrevStateValue = Results[Pos - 1, I].Possibility *
                    PrevState.GetTransitionStrength(State.Name);

                if (PrevStateValue > PrevMaxStateValue)
                {
                    PrevMaxStateValue = PrevStateValue;
                    PrevStateName = PrevState.Name;
                }
            }

            double Emission =
                (Pos == m_Sequence.Count + 1) ?
                    1 : State.GetEmmisionStrength(m_Sequence[Pos - 1].Sequence);

            Results[Pos, ArrayStateIndex] = new ViterbiResult(
                Pos, State.Name, PrevStateName, Emission * PrevMaxStateValue);
        }
Esempio n. 2
0
        private double CalcForwardState(HMM Hmm, int SeqPos)
        {
            double Result = 0;
            int State = 0;
            double[] LogSums = new double[m_WorkingStatesQty];

            double EmmisionStr = 1; // for endingState
            if (SeqPos < m_Sequence.Count)
                EmmisionStr = Hmm.GetEmmisionStrength(m_Sequence[SeqPos].Sequence);

            foreach(HMM HmmKm1 in m_WorkingStates)
            {
                // explog trick
                LogSums[State] = Math.Log(EmmisionStr) +
                    Math.Log(HmmKm1.GetTransitionStrength(Hmm.Name)) +
                    Math.Log(ForwardValues[SeqPos,State]); // seqpos - 1 + 1 to shift for begining state
                State++;
            }

            double MaxVal = LogSums[0];
            for(int I = 1; I < m_WorkingStatesQty; I++)
                MaxVal = Math.Max(MaxVal,LogSums[I]);

            for(int I = 0; I < m_WorkingStatesQty; I++)
                Result += Math.Exp(MaxVal + Math.Log(Math.Exp(LogSums[I] - MaxVal)));

            return Result;
        }