hmm.h File Reference

Implementation of HMM base structure. More...

`#include <stdio.h>`
`#include <sphinxbase/fixpoint.h>`
`#include <sphinxbase/listelem_alloc.h>`
`#include "bin_mdef.h"`

Go to the source code of this file.

## Data Structures

struct | hmm_context_s
struct | hmm_s

## Macros

#define | MAX_N_FRAMES MAX_INT32
| Maximum number of frames in index, should be in sync with above.
#define | SENSCR_SHIFT 10
| Shift count for senone scores.
#define | WORST_SCORE ((int)0xE0000000)
| Large "bad" score.
#define | TMAT_WORST_SCORE (-255)
| Watch out, though! Transition matrix entries that are supposed to be "zero" don't actually get that small due to quantization.
#define | BETTER_THAN >
| Is one score better than another?
#define | WORSE_THAN <
| Is one score worse than another?
#define | HMM_MAX_NSTATE 5
| Hard-coded limit on the number of emitting states.
#define | hmm_context(h) (h)->ctx
| Access macros.
#define | hmm_is_mpx(h) (h)->mpx
#define | hmm_in_score(h) (h)->score[0]
#define | hmm_score(h, st) (h)->score[st]
#define | hmm_out_score(h) (h)->out_score
#define | hmm_in_history(h) (h)->history[0]
#define | hmm_history(h, st) (h)->history[st]
#define | hmm_out_history(h) (h)->out_history
#define | hmm_bestscore(h) (h)->bestscore
#define | hmm_frame(h) (h)->frame
#define | hmm_mpx_ssid(h, st) (h)->senid[st]
#define | hmm_nonmpx_ssid(h) (h)->ssid
#define | hmm_ssid(h, st)
#define | hmm_mpx_senid(h, st)
#define | hmm_nonmpx_senid(h, st) ((h)->senid[st])
#define | hmm_senid(h, st)
#define | hmm_senscr(h, st)
#define | hmm_tmatid(h) (h)->tmatid
#define | hmm_tprob(h, i, j) (-(h)->ctx->tp[hmm_tmatid(h)][i][j])
#define | hmm_n_emit_state(h) ((h)->n_emit_state)
#define | hmm_n_state(h) ((h)->n_emit_state + 1)
#define | hmm_context_set_senscore(ctx, senscr) ((ctx)->senscore = (senscr))
| Change the senone score array for a context.

## Typedefs

typedef int32 | frame_idx_t
| Type for frame index values.
typedef struct hmm_context_s | hmm_context_t
typedef struct hmm_s | hmm_t

## Functions

hmm_context_t * | hmm_context_init (int32 n_emit_state, uint8 **const *tp, int16 const *senscore, uint16 *const *sseq)
| Create an HMM context.
void | hmm_context_free (hmm_context_t *ctx)
| Free an HMM context.
void | hmm_init (hmm_context_t *ctx, hmm_t *hmm, int mpx, int ssid, int tmatid)
| Populate a previously-allocated HMM structure, allocating internal data.
void | hmm_deinit (hmm_t *hmm)
| Free an HMM structure, releasing internal data (but not the HMM structure itself).
void | hmm_clear (hmm_t *h)
| Reset the states of the HMM to the invalid condition.
void | hmm_clear_scores (hmm_t *h)
| Reset the scores of the HMM.
void | hmm_normalize (hmm_t *h, int32 bestscr)
| Renormalize the scores in this HMM based on the given best score.
void | hmm_enter (hmm_t *h, int32 score, int32 histid, int frame)
| Enter an HMM with the given path score and history ID.
int32 | hmm_vit_eval (hmm_t *hmm)
| Viterbi evaluation of given HMM.
int32 | hmm_dump_vit_eval (hmm_t *hmm, FILE *fp)
| Like hmm_vit_eval, but dump HMM state and relevant senscr to fp first, for debugging;.
void | hmm_dump (hmm_t *h, FILE *fp)
| For debugging, dump the whole HMM out.

## Detailed Description

Implementation of HMM base structure.

HMM data structure and operation.

Hidden Markov Model base structures.

For efficiency, this version is hardwired for two possible HMM topologies, but will fall back to others:

5-state left-to-right HMMs: (0 is the emitting entry state and E is a non-emitting exit state; the x's indicate allowed transitions between source and destination states):

                  0   1   2   3   4   E (destination-states)
              0   x   x   x
              1       x   x   x
              2           x   x   x
              3               x   x   x
              4                   x   x
       (source-states)

5-state topologies that contain a subset of the above transitions should work as well.

3-state left-to-right HMMs (similar notation as the 5-state topology above):

                  0   1   2   E (destination-states)
              0   x   x   x
              1       x   x   x
              2           x   x
       (source-states)

3-state topologies that contain a subset of the above transitions should work as well.

Definition in file hmm.h.

## Macro Definition Documentation

## BETTER_THAN

#define BETTER_THAN >

Is one score better than another?

Definition at line 95 of file hmm.h.

## hmm_bestscore

#define hmm_bestscore | ( |  | h | ) | (h)->bestscore

Definition at line 197 of file hmm.h.

## hmm_context

#define hmm_context | ( |  | h | ) | (h)->ctx

Access macros.

Definition at line 186 of file hmm.h.

## hmm_context_set_senscore

#define hmm_context_set_senscore | ( |  | ctx,
|  |  | senscr
| ) |  | ((ctx)->senscore = (senscr))

Change the senone score array for a context.

Definition at line 227 of file hmm.h.

## hmm_frame

#define hmm_frame | ( |  | h | ) | (h)->frame

Definition at line 198 of file hmm.h.

## hmm_history

#define hmm_history | ( |  | h,
|  |  | st
| ) |  | (h)->history[st]

Definition at line 194 of file hmm.h.

## hmm_in_history

#define hmm_in_history | ( |  | h | ) | (h)->history[0]

Definition at line 193 of file hmm.h.

## hmm_in_score

#define hmm_in_score | ( |  | h | ) | (h)->score[0]

Definition at line 189 of file hmm.h.

## hmm_is_mpx

#define hmm_is_mpx | ( |  | h | ) | (h)->mpx

Definition at line 187 of file hmm.h.

## HMM_MAX_NSTATE

#define HMM_MAX_NSTATE 5

Hard-coded limit on the number of emitting states.

Definition at line 160 of file hmm.h.

## hmm_mpx_senid

#define hmm_mpx_senid | ( |  | h,
|  |  | st
| ) |  |

Value:
(hmm_mpx_ssid(h,st) == BAD_SENID \
? BAD_SENID : (h)->ctx->sseq[hmm_mpx_ssid(h,st)][st])
BAD_SENID
#define BAD_SENID
Invalid senone ID (limited to 16 bits for PocketSphinx).
Definition bin_mdef.h:98

Definition at line 203 of file hmm.h.

## hmm_mpx_ssid

#define hmm_mpx_ssid | ( |  | h,
|  |  | st
| ) |  | (h)->senid[st]

Definition at line 199 of file hmm.h.

## hmm_n_emit_state

#define hmm_n_emit_state | ( |  | h | ) | ((h)->n_emit_state)

Definition at line 213 of file hmm.h.

## hmm_n_state

#define hmm_n_state | ( |  | h | ) | ((h)->n_emit_state + 1)

Definition at line 214 of file hmm.h.

## hmm_nonmpx_senid

#define hmm_nonmpx_senid | ( |  | h,
|  |  | st
| ) |  | ((h)->senid[st])

Definition at line 205 of file hmm.h.

## hmm_nonmpx_ssid

#define hmm_nonmpx_ssid | ( |  | h | ) | (h)->ssid

Definition at line 200 of file hmm.h.

## hmm_out_history

#define hmm_out_history | ( |  | h | ) | (h)->out_history

Definition at line 195 of file hmm.h.

## hmm_out_score

#define hmm_out_score | ( |  | h | ) | (h)->out_score

Definition at line 191 of file hmm.h.

## hmm_score

#define hmm_score | ( |  | h,
|  |  | st
| ) |  | (h)->score[st]

Definition at line 190 of file hmm.h.

## hmm_senid

#define hmm_senid | ( |  | h,
|  |  | st
| ) |  |

Value:
(hmm_is_mpx(h) \
? hmm_mpx_senid(h,st) : hmm_nonmpx_senid(h,st))

Definition at line 206 of file hmm.h.

## hmm_senscr

#define hmm_senscr | ( |  | h,
|  |  | st
| ) |  |

Value:
(hmm_senid(h,st) == BAD_SENID \
? WORST_SCORE \
: -(h)->ctx->senscore[hmm_senid(h,st)])
WORST_SCORE
#define WORST_SCORE
Large "bad" score.
Definition hmm.h:84

Definition at line 208 of file hmm.h.

## hmm_ssid

#define hmm_ssid | ( |  | h,
|  |  | st
| ) |  |

Value:
(hmm_is_mpx(h) \
? hmm_mpx_ssid(h,st) : hmm_nonmpx_ssid(h))

Definition at line 201 of file hmm.h.

## hmm_tmatid

#define hmm_tmatid | ( |  | h | ) | (h)->tmatid

Definition at line 211 of file hmm.h.

## hmm_tprob

#define hmm_tprob | ( |  | h,
|  |  | i,
|  |  | j
| ) |  | (-(h)->ctx->tp[hmm_tmatid(h)][i][j])

Definition at line 212 of file hmm.h.

## MAX_N_FRAMES

#define MAX_N_FRAMES MAX_INT32

Maximum number of frames in index, should be in sync with above.

Definition at line 69 of file hmm.h.

## SENSCR_SHIFT

#define SENSCR_SHIFT 10

Shift count for senone scores.

Definition at line 73 of file hmm.h.

## TMAT_WORST_SCORE

#define TMAT_WORST_SCORE (-255)

Watch out, though! Transition matrix entries that are supposed to be "zero" don't actually get that small due to quantization.

Definition at line 90 of file hmm.h.

## WORSE_THAN

#define WORSE_THAN <

Is one score worse than another?

Definition at line 100 of file hmm.h.

## WORST_SCORE

#define WORST_SCORE ((int)0xE0000000)

Large "bad" score.

This number must be "bad" enough so that 4 times WORST_SCORE will not overflow. The reason for this is that the search doesn't check the scores in a model before evaluating the model and it may require as many was 4 plies before the new 'good' score can wipe out the initial WORST_SCORE initialization.

Definition at line 84 of file hmm.h.

## Typedef Documentation

## frame_idx_t

typedef int32 frame_idx_t

Type for frame index values.

Used in HMM indexes and backpointers and affects memory required.Due to limitations of FSG search implementation this value needs to be signed.

Definition at line 64 of file hmm.h.

## Function Documentation

## hmm_clear()

void hmm_clear | ( | hmm_t * | h | ) |

Reset the states of the HMM to the invalid condition.

i.e., scores to WORST_SCORE and hist to undefined.

Definition at line 183 of file hmm.c.

References WORST_SCORE.

## hmm_clear_scores()

void hmm_clear_scores | ( | hmm_t * | h | ) |

Reset the scores of the HMM.

Definition at line 170 of file hmm.c.

References WORST_SCORE.

## hmm_context_free()

void hmm_context_free | ( | hmm_context_t * | ctx | ) |

Free an HMM context.

Note
  The transition matrices, senone scores, and senone sequence mapping are all assumed to be allocated externally, and will NOT be freed by this function.

Definition at line 80 of file hmm.c.

## hmm_context_init()

hmm_context_t * hmm_context_init | ( | int32 | n_emit_state,
|  | uint8 **const * | tp,
|  | int16 const * | senscore,
|  | uint16 *const * | sseq
| ) |  |

Create an HMM context.

Definition at line 56 of file hmm.c.

References HMM_MAX_NSTATE.

## hmm_deinit()

void hmm_deinit | ( | hmm_t * | hmm | ) |

Free an HMM structure, releasing internal data (but not the HMM structure itself).

Definition at line 111 of file hmm.c.

## hmm_dump()

void hmm_dump | ( | hmm_t * | h,
|  | FILE * | fp
| ) |  |

For debugging, dump the whole HMM out.

Parameters

  h | In/Out: HMM being updated
  fp | An output file pointer

Definition at line 116 of file hmm.c.

## hmm_dump_vit_eval()

int32 hmm_dump_vit_eval | ( | hmm_t * | hmm,
|  | FILE * | fp
| ) |  |

Like hmm_vit_eval, but dump HMM state and relevant senscr to fp first, for debugging;.

Parameters

  hmm | In/Out: HMM being updated
  fp | An output file pointer

Definition at line 810 of file hmm.c.

## hmm_enter()

void hmm_enter | ( | hmm_t * | h,
|  | int32 | score,
|  | int32 | histid,
|  | int | frame
| ) |  |

Enter an HMM with the given path score and history ID.

Definition at line 201 of file hmm.c.

## hmm_init()

void hmm_init | ( | hmm_context_t * | ctx,
|  | hmm_t * | hmm,
|  | int | mpx,
|  | int | ssid,
|  | int | tmatid
| ) |  |

Populate a previously-allocated HMM structure, allocating internal data.

Definition at line 89 of file hmm.c.

References BAD_SSID.

## hmm_normalize()

void hmm_normalize | ( | hmm_t * | h,
|  | int32 | bestscr
| ) |  |

Renormalize the scores in this HMM based on the given best score.

Definition at line 209 of file hmm.c.

References BETTER_THAN, and WORST_SCORE.

## hmm_vit_eval()

int32 hmm_vit_eval | ( | hmm_t * | hmm | ) |

Viterbi evaluation of given HMM.

Note
  If this module were being used for tracking state segmentations, the dummy, non-emitting exit state would have to be updated separately. In the Viterbi DP diagram, transitions to the exit state occur from the current time; they are vertical transitions. Hence they should be made only after the history has been logged for the emitting states. But we're not bothered with state segmentations, for now. So, we update the exit state as well.

Definition at line 789 of file hmm.c.