mTransitionTable
for implementing markov model processes
Description
A class that can be used to create a probability-based transition table.
Typical usage is to give data to the transition table using its constructor, then call step whenever you want to move to a new state. If you want to change the table’s probablilites, send the updated table to your object with updateTable
Template Parameters
template <int NUM_STATES>NUM_STATES: number of states in the transition table. For example, if the transition table represents the notes in one octave of a major scale, NUM_STATES would be 8.
Class Methods
Constructor
mTransitionTable(float transition_table[NUM_STATES][NUM_STATES], int initial_state = 0)transition_table: 2D array representing the relative probability of moving between states. Internally, mTransitionTable will normalize each row of the table so that the sum of all probabilities in each row is equal to one.
For example:
the first row of the transition table represents the relative probability that the table will move to each state from state 0.
if the first row of a transition table of size 4 is [1, 2, 1, 0] this means there is a 0.25 chance of staying in state 0, a 0.5 chance of moving to state 1, a 0.25 chance of moving to state 2 and a 0 chance of moving to state 3.
initial_state: state the transition table will begin in. Defaults to 0
void updateTable(float transition_table[NUM_STATES][NUM_STATES]Calculates probabilities of transistion table based on a new array, like in the constructor.
int step()Randomly transitions to a new state according to the probabilities given to the table by the constructor or updateTable
void setState(int state)Manually moves to a certain state of the transition table. state must be between 0 and NUM_STATES-1
int getState()Returns the current state.
Example
// transition table derived by analyzing transitions between notes in the song Happy Birthday
int note_numbers[8] = {60, 62, 64, 65, 67, 69, 70, 72}; // MIDI note numbers corresponding to each state
float note_tallies[8][8] = // number of occurences of each note-pair transition
{
{3, 2, 0, 1, 1, 0, 0, 1}, // from C
{2, 0, 0, 0, 0, 0, 1, 0}, // from D
{2, 0, 0, 0, 0, 0, 1, 0}, // from E
{1, 0, 2, 0, 1, 0, 0, 0}, // from F
{0, 0, 0, 2, 0, 0, 0, 0}, // from G
{0, 0, 0, 2, 0, 0, 0, 0}, // from A
{0, 0, 0, 0, 0, 1, 1, 0}, // from B
{0, 0, 0, 0, 1, 0, 0, 0} // from C^
//to C D E F G A B C^
};
mTransitionTable<8> note_table(note_tallies);
// PUT THE FOLLOWING LINE WHEREVER YOU WANT TO MOVE TO THE NEXT STATE, assuming you have defined an mOscil named osc
osc.setFreq(mtof(note_numbers[note_table.step()]));