Log(music) with Undertone
Contents
Whether you’re a physicist, biologist, economist, data scientist, whatever, the chances are you’ll meet the Markov chains sooner or later. And today we’ll try them on… music!
If you’re not up to the math part, feel free to skip it.
Math
The system states changes are represented with a help of transition matrix, describing the probabilities of possible transitions. This matrix is a key to determine the state of the model after n steps.
The math behind that is very straightforward  we need just a matrix power. Oh, wait  what if it is fractional, for example, when representing time? There’re many ways to compute it, let’s take a look at some of them.
1. Eigen vectors
This method is based on properties of eigen values decomposition:
$$\begin{array}{1}
\text{V  eigen vectors}\\
A v_j = \lambda_j v_j\\
A V = V D \text{, where } D = diag(\lambda_1, …, \lambda_n)\\
A^n = V (D ^ n) V^{1}
\end{array}$$
That’s actually how Matlab mpower
function looks like:


We can take advantage of Math.NET matrix decompositions to implement this method:


What are the pros/cons?
+ the method is rather efficient
 inverse matrix $V$ might not exist ^{1}
$$A = \begin{bmatrix} 0.78 & 0.2 & 0.12\\ 0 & 0.78 & 0.32\\0 & 0 & 0.78 \end{bmatrix}$$
$$V = \begin{bmatrix} 1.0000 & 1.0000\\ 0.0000 & 0.0000 \end{bmatrix}$$
 some matrices cannot be diagonalised:
$$A = \begin{bmatrix} 3 & 2\\0 & 3 \end{bmatrix}$$
2. Generator matrices
If we can find a matrix $G$, such as $A = \exp(G)$, then (remember series?)
$$A^x = \exp(xG) = I + (xG) + (xG)^2 / 2! + (xG)^3 / 3! + … $$
We are interested in finding a generator $G$ with zero row sums and nonnegative offdiagonal entries. So we’ll use Mercator’s series:
$$\log(1 + x) = x  x^2 / 2 + x^3 / 3  x^4 / 4 + …$$
In matrix case it’s
$$G = \log(I + (A  I)) = (A  I)  (A  I)^2 / 2 + (A  I)^3 / 3  …$$
The common concern is that the series methods are not very efficient and may be not accurate enough. It’s easy to compare the methods by computing the squared error between the actual and expected values (say, compute $(A^\frac{1}{3})^3$).
Another problem is that a generator matrix (and power too) might have negative elements  not really what you want when dealing with probabilities. We can make them nonnegative and keep the rows sums equal to zero… but the power properties won’t be preserved. For example, you can find out that $A^7 <> A^{3.5} * A^{3.5}$.


+ Works for nondiagonizable matrices
 Not very efficient
 May be not accurate enough or even fail to converge
These are just the most simple algorithms  would be interesting to check the others, but let’s move on.
References
 19 Dubious Ways to Compute the Exponential of a Matrix
 Finding Generators for Markov Chains via empirical transition matrices, with applications to credit ratings
Music
It’s not a secret, that Markov models are often used for generative music, here’s one of the examples: Programming Instrumental Music From Scratch (there’re also some interesting references in the comments). We’ll do a different thing, though: given notes, we can calculate the transition probabilities and… generate new melodies.
But everything in its time.
We need to somehow define the sequences of notes and play them. And thanks to Undertone that’s pretty easy! First of all, let’s download the piano samples  University of Iowa has a collection of recordings here. You can get them manually or using this script. It’s also ok to generate the notes programmatically  but somewhat a bit more realistic is more fun ^_^


The note is defined with its symbol (C = Do, F# = Fis = Fadiesis = F sharp), octave and time (♪  ^{1}⁄_{8}, ♬  2 notes * ^{1}⁄_{16}). The sequence of notes gives a melody, which can be played and even saved later. Can you guess the following one?
Sounds recognizable, but awkwardly stumbling  makes me feel much better about my piano skills. I’d recommend to watch this performace, it’s amazing! (and that pianistfeeling suddenly goes down again)). I believe there’s a way to make the generated sound smoother and so on, but that’s not the goal now.


(Note, the example above has 3 times faster tempo that the one you’ll get with downloaded notes)
The next thing to do is to calculate the transition probabilities. Consider the following sequence: E3  ^{1}⁄_{16}, C4  ^{1}⁄_{8}, E3  ^{1}⁄_{16}, C4  ^{3}⁄_{8}, C4  ^{1}⁄_{16}.
The first option is to simply count transitions between notes and then normalize the rows, so the sum are equal to 1:
$$
\begin{matrix} & E3 & C4 \\ E3 & 0 & 0 \\ C4 & 0 & 0 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 0 & 1 \\ C4 & 0 & 0 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 0 & 1 \\ C4 & 1 & 0 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 0 & 2 \\ C4 & 1 & 0 \end{matrix} \rightarrow$$
$$
\begin{matrix} & E3 & C4 \\ E3 & 0 & 2 \\ C4 & 1 & 0 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 0 & 2 \\ C4 & 1 & 1 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 0 & 1 \\ C4 & 0.5 & 0.5 \end{matrix} $$
But wait, what about time? There’re different notes, we can take that into account too: if the ‘smallest’ length is ^{1}⁄_{16}, then we can count, how many such intervals each note takes, multiplying by 16^{2}:
E3  ^{1}⁄_{16}, C4  ^{1}⁄_{8}, E3  ^{1}⁄_{16}, C4  ^{3}⁄_{8}, C4  ^{1}⁄_{16}
$$
\begin{matrix} & E3 & C4 \\ E3 & 0 & 0 \\ C4 & 0 & 0 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 0 & 1 \\ C4 & 0 & 0 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 0 & 1 \\ C4 & 1 & 1 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 0 & 2 \\ C4 & 1 & 1 \end{matrix} \rightarrow$$
$$
\begin{matrix} & E3 & C4 \\ E3 & 0 & 2 \\ C4 & 1 & 7 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 0 & 1 \\ C4 & 0.125 & 0.875 \end{matrix}$$
So, we know the transitions and times when the next note should be played. A uniform random number generator will choose this next note: e.g. if transitions are 0.4 0.2 0.1 0.3 and generated number is 0.65, that’s the 3rd one (this operation is called choose
below).
The note at time t
, given transition matrix P
is
$$n_t = choose(P^t _{n _{t  1}})$$


I tried to compute the transition matrix  and both method failed! Turns out this is the reallife ‘unlucky’ matrix. To be sure that doesn’t work, checked R’s expm package:


Fortunately, we don’t need fraction power here: if we count time in 1/16th, it becomes integer! ^{1}⁄_{16} is 1, ^{3}⁄_{8} is 6 and so on. Good old multiplication saves the situation.
Here’s several examples of generated melodies  there’re only 10 notes are in the game, but the results are already pretty different from the original:
Generated1
Generated2
Generated3
What if we add an ‘extraprobability’ for note to stay in the same state?
E3  ^{1}⁄_{16}, C4  ^{1}⁄_{8}, E3  ^{1}⁄_{16}, C4  ^{3}⁄_{8}, C4  ^{1}⁄_{16}
$$
\begin{matrix} & E3 & C4 \\ E3 & 0 & 0 \\ C4 & 0 & 0 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 1 & 1 \\ C4 & 0 & 0 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 1 & 1 \\ C4 & 1 & 2 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 2 & 2 \\ C4 & 1 & 2 \end{matrix} \rightarrow $$
$$
\begin{matrix} & E3 & C4 \\ E3 & 2 & 2 \\ C4 & 1 & 9 \end{matrix} \rightarrow
\begin{matrix} & E3 & C4 \\ E3 & 0.5 & 0.5 \\ C4 & 0.1 & 0.9 \end{matrix} $$
For this matrix, the generator does exist and we can programmatically compose something like that:
Generatedseries1
Generatedseries2
Why stop here? We could define the similar transitions for note length; or add chords  say, generated according to the current tonality; or switch tonalities/modes… Well, there’s a whole ‘musical math’, solfeggio, full of different functions  plenty of space to experiment!


 the example matrix isn’t actually a transition matrix  the row sums should be equal to 1  it’s here just to demonstrate a possible problem. ^{[return]}
 the sequence C4  ^{1}⁄_{8}, E3  ^{1}⁄_{16} is almost the same as C4  ^{1}⁄_{16}, C4  ^{1}⁄_{16}, E3  ^{1}⁄_{16}. That gives us ^{50}⁄_{50} transitions: after each period of time C4 can either stay as C4 or transform to E3. ^{[return]}
Author Natallie
LastMod 20131226