Skip to main content

seq2markov

seq2markov(
@sequence ## llll (required)
@order 1
@prepad '^'
@postpad '$'
) -> llll

Constructs a Markov chain from a given sequence of data. See markov2seq.


Arguments

  • @sequence [llll]: Input sequence from which the Markov model will be built. (required)
  • @order [int]: Order of the Markov chain, determining the length of history considered for state transitions. (default: 1).
  • @prepad [symbol]: Padding symbol used at the beginning of the sequence to handle initial states. (default: '^').
  • @postpad [symbol]: Padding symbol used at the end of the sequence to handle final states (default: '$').

Output

Transition probability matrix in sparse form [llll]


Usage

$events = importmidi('bach.mid'); ## import MIDI file
## get pitch from each event
$pitches = for $e in $events collect $e.getkey('pitch');
## build 2nd-order Markov model from pitch sequence
$matrix = seq2markov(@sequence $pitches @order 2);
## generate pitch sequence of size 50 from Markov model
$pitchseq = markov2seq(@matrix $matrix @maxlength 50);
$t = 0;
## Transcribe pitches
for $pitch in $pitchseq do (
ezsampler(@pitch $pitch).transcribe($t);
$t += 120
);
render(@play 1)