Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Sunday, May 26, 2019

Walsh-Hadamart transformations in supercollider

Problem

Practically the whole world is using Fourier Transforms to decompose sounds into sums of sine waves. The Fourier transform then can be edited, and transformed back to the time domain to hear the effects of the editing. One question that naturally arises is if perhaps ways exist to decompose sounds as sums of something other than sine waves.

Approach

Well, as it turns out there are infinitely many ways to decompose signals into sums of other signals, and one that personally intrigues me is the Walsh-Hadamard transform which decomposes signals into sums of pulses (square waves). Walsh functions were already known and used around 1890, but it took until 1923 for Walsh to formally examine them in a mathematical context. Most of the work on Walsh functions in the context of audio processing was done in the nineteen-seventies. The fact that it is no longer popular may indicate that the results were nothing spectacular, but that shouldn't stop us from experiencing it first-hand.

Walsh functions

With the help of mathematics it can be established that we need only a subset of all possible square waves to decompose and reconstruct any signal. These waves are now known as "Walsh functions" and they can be ordered by sequency. (Note: not frequency!). Sequency is a number that corresponds the number of zero-crossings the pulse makes in the time base. Here's a representation of some Walsh functions ordered by their sequency. Sequency is not expressed in cps (cycles per second, also known as Hz), but in zps (zero-crossings per second).




If the signal is only 2 samples long, 2 Walsh functions suffice to perfectly reconstruct any such signal. If the signal is 4 samples long, 4 Walsh functions suffice to perfectly reconstruct any such signal. In general, for a signal of 2^L long, you need to combine up to 2^L Walsh functions to perfectly reconstruct any such signal. This is similar to the discrete Fourier transform where the original signal and the transformed signal both have the same length.

Walsh functions always start with +1 as their first component. For completeness I should probably mention that the even sequencies are sometimes called CAL functions, whereas the odd sequencies are called SAL functions.

WAL(2n, t) = CAL(n, t)    n = 1, 2, ...
WAL(2n-l, t) = SAL(n, t)  n = 1, 2, ...

As with sines and cosines, CAL and SAL are in essence time-shifted versions of each other.

Here's an example of decomposing a 4 sample signal into a linear combination of Walsh functions:


The signal [-1, 1, 0, -2] can be decomposed using level 2 sequencies:

[-1,1,0,-2] = -0.5*[1,1,1,1] + 0.5*[1,1,-1,-1] -1*[1,-1,-1,1] + 0*[1,-1,1,-1]

or in words:

[-1,1,0,-2]  = -0.5*sequency0 + 0.5*sequency1 - 1*sequency2 + 0*sequency3

Note that this formula shows how to convert from Walsh spectrum back to time domain using the (known) sequencies.

Ok, so how did you find that combination? Can you always do this? Is there always only one possible combination?

I'll skip the mathematics, but yes, it can always be done, and there's always exactly one possible decomposition. Some very smart people invented an efficient way to find this decomposition, and the efficient way is known as the "Fast Walsh Transform". Explaining the transform in detail, however, is way out of bounds for this article. Please refer to some external reference for the juicy details.

Looking at the example above, the fast Walsh transform of [ -1, 1, 0, -2] should give [ -0.5, 0.5, -1, 0].

And we can go back from [ -0.5, 0.5, -1, 0] to [ -1, 1, 0, -2] by using the sequencies as we did in the example above.While we did the calculations by hand in the example above, the fast Walsh transform actually has a wonderful property: it is its own inverse (except for some constant factor).

This means that if you apply the Walsh transform to a signal, you get the Walsh spectrum, and if you then apply the Walsh transform to that Walsh spectrum again, you get the original signal again (apart from some constant factor). 

What does any of this have to do with audio or supercollider?

Well, in itself nothing really.  But we can propose some experiments with this transform.

Remember that what we are really doing here is decomposing signals (think: sounds) into weighted sums of Walsh functions (think: square waves). Square waves happen to be a basic waveform used in subtractive synthesis (think analog synths!) so now it starts to sound kind of interesting doesn't it?

We already have a way to decompose any sound into a sum of square waves, and to go back from these square waves to the original signal (time domain). What if, just before we go back to the time domain, we modify the Walsh spectrum first?

What is the effect on a sound of removing all the fast square waves (= high sequencies)? (For lack of a better word, you could call it a Walsh-Low-Pass-Filter). What is the effect on a sound of removing the slow square waves (= low sequencies)? (kind of Walsh-High-Pass-Filter). What is the effect on a sound of setting all Welsh spectrum values to 0 if they happen to be smaller than some threshold? (This could be the core algorithm of some lossy data compression scheme). What is the effect on a sound of shifting the Welsh spectrum values to the left/right (a kind of Walsh-Pitch-Shifting). Can we synthesize interesting sounds by making up a new Walsh spectrum (kind of additive synthesis with square waves)? What does a walsh filter sweep sound like? What do you get if you reinterpret the Walsh spectrum as a Fourier spectrum or vice versa? Can useful/beautiful visualizations be derived from the Walsh spectrum?

It is to be expected that the auditory results will be wildly different from what we are used to hearing in transformations based on the Fourier transform (classical high-pass and low-pass filters e.g.), but that should be all the more reason to try it out, shouldn't it? Maybe you can think of really cool new applications made possible by using the Walsh transform in audio context? If so, be sure to comment :)

Walsh transform in supercollider

Before we can play with audio, we need a way to calculate the fast Walsh transform in supercollider. Since I don't know how to write UGEN's yet, I will do some calculations in the language for now.

Here's a pretty straightforward translation of this c implementation:
This code is also available on https://sccode.org/1-5bD.


If we evaluate the following code in scide:
~walsh_transform.(values:[-1,1,0,2]);
we get back the expected result:
[0.5,-0.5,0,-1]
And to check that the inverse transform works as expected:
~walsh_transform.(values:~walsh_transform.(values:[-1,1,0,2]), rescale:false);
gives the original signal:
[-1,1,0,2].

Bring on the sounds!

This walsh, this walsh, this walsh, this walsh...

My first interest is in hearing the timbres of the Walsh functions. by themselves. So let's listen to some of those. I'll take the 256 Walsh functions from level 8 (calculate them by applying the inverse walsh transform on a spectrum containing a single "1"), and concatenate 100 copies of each into a (stereo) buffer and then play the buffer.



The resulting tones pretty much sound like pulsewidth modulated pulse waves because, obviously, they *are* pulse waves.

This article has been more than long enough for now. If there's any interest in the subject I may prepare a follow-up article in which we experiment with Walsh-transform based filters.


Saturday, July 13, 2013

Solving the towers of Hanoi using Lindenmayer systems

Introduction

What I'm about to describe is a funny side result of a technique to transform recursion to iteration, derived mathematically in my PhD thesis "Platform-independent source code transformations for task concurrency management", written 10 years ago. Time flies!

Funny how after so many years I consider exactly this little side-result to be the most intriguing result of my thesis. The mathematical proof of how it works is best left to the thesis itself, but I will try to demonstrate you how you could calculate the j-th move in an N-disk Hanoi problem using a Lindenmayer system.

Solving what with what?!

towers of Hanoi

The towers of Hanoi are a puzzle in which one is asked to move disks from the rod on the left to the rod on the right according to the following rules:
  1. move only one disk at a time
  2. Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod.
  3. No disk may be placed on top of a smaller disk.
If you start with only three disks, it takes at least 7 moves to solve the problem. The wikipedia page linked above shows many ways to solve the Hanoi problem. The method I'm about to demonstrate is related to their "Binary solution" but it's different from what is described there.

Lindenmayer systems

A Lindenmayer system (or L-system as it's often called) consists of a series of symbols, called an alphabet (think: A,B,C,...) and a set of rules that transform sequences of symbols into new sequences of symbols. Here's an example:

  • alphabet A, B, C.
  • rule 1: ACB -> AB
  • rule 2: AB -> 

Rule 1 can be read as: whenever you encounter the sequence ACB, replace it with AB.
Rule 2 can be read as: given sequence AB, remove it (or: replace it with an empty string, if you like fancy words :) )

We could apply the rules to the following start sequence: ABAABCBA
  • first, use rule 2: ABAABCBA becomes  AABCBA
  • then, use rule 2: AABCBA becomes ACBA
  • then, use rule 1: ACBA becomes ABA
  • then, use rule 2: ABA becomes A
I hope by now you get the idea... L-systems have various applications, but they are best known for generating computer graphics representations of plants and trees, and various fractals (the wikipedia entry linked above has a list of interesting demonstrations). To go from a sequence of letters to a drawing, each symbol is interpreted as a drawing operation (e.g. a symbol A could mean: turn 45deg, symbol B could mean: step 1 unit forward, etc.)

How are both things related?

The following drawings show the solution of the Hanoi problem with three disks:


  • Move 1 goes from rod A to rod C
  • Move 2 goes from rod A to rod B
  • Move 3 goes from rod C to rod B
  • Move 4 goes from rod A to rod C
  • Move 5 goes from rod B to rod A
  • Move 6 goes from rod B to rod C
  • Move 7 goes from rod A to rod C
The question is: if we use 3 disks, and we want to calculate from which rod to which rod we have to move the upper disk, how can we do that? Or more general: if we use N disks, and we want to know the j-th move, can we calculate this, without actually doing all the previous moves first?

And the answer, of course, is yes we can!

Recipe

For the mathematical details of why the following recipe works, you will need to read chapter 6 in the thesis, but if you just want to apply a recipe and marvel at the wonders of science (ahem!), keep reading...

First the recipe itself, then I will illustrate using an application. If you want to calculate the j-th move (where j can go from 1 to 2^N-1) in an N-disk problem, do the following:
  1. take j and write in binary representation
  2. reverse the binary representation
  3. pad with 0's on the right side until you've used N bits
  4. remove everything coming before and including the first 1-bit
  5. Now we have a sequence of 0 and 1 which we consider the alphabet of a Lindenmayer system and we apply the following rules (in any order you like) until no more rules can be applied:
    00 -> <empty>
    11 -> <empty>
    1010 -> 01
    0101 -> 10
  6. eventually you end up with one of the following sequences. The  sequence we end up with directly tells us the j-th move in the N-disk towers of Hanoi problem:
  7. SequenceMove from to
    0A->B
    1B->C
    01B->A
    10C->B
    101C->A
    010C->A
    <empty>A->C

Example

Recall our N=3-disk Hanoi problem. Let's see if we can calculate the j=2nd move using the method shown above.
  1. take number 2 and write it in binary representation:  10
  2. reverse the binary representation: 01
  3. pad with 0's to the right until we have used N=3 bits: 010
  4. chop off everything before and including the first 1-bit:  0
  5. apply the rules until you hit one of the cases in the table: no rules can be applied here, since we are already in the case 0. According to the table, ending up with 0 indicates a move from A->B
  6. Double-checking in the solutions that were displayed before, we indeed see that the 2nd move goes from A->B.
Was this an accident, or does it really work? Lets quickly try it for all 7 moves. Each column represents a step in the recipe.
Move no.BinaryReversePadChop L-system simplifcationMove according to the table
1 1 1 10000<empty>A->C
2 10 01 01000A->B
3 11 11 1101010C->B
4 100 001 001<empty><empty>A->C
5 101 101 1010101B->A
6 110 011 01111B->C
7 111 111 11111<empty>A->C
If we take the successive moves listed in the right-most column of this table, and compare it to the solution shown before it becomes clear that the system indeed results in the correct solution (at least for the case N=3). In this case, our L-system didn't really have a lot of work to do: only twice it reduced a bit sequence. This is to be expected for low values of N (N = the number of disks used, here 3).

Example for N=5

Now let's apply the recipe to the 5-disk problem. The 5-disk problem takes (2^5-1) = 31 moves to solve.
Move no.BinaryReversePadChop L-system simplifcationMove according to the table
111100000000<empty>A->C
21001010000000A->B
3111111000100010C->B
41000010010000<empty>A->C
510110110100010001B->A
6110011011001001B->C
7111111111001100<empty>A->C
8100000010001000A->B
91001100110010001010C->B
101010010101010010010C->A
111011110111010101001B->A
1211000011001101010C->B
1311011011101100110<empty>A->C
1411100111011101100A->B
151111111111110111010C->B
16100000000100001<empty>A->C
17100011000110001000101B->A
181001001001010010011B->C
191001111001110011001<empty>A->C
201010000101001010101B->A
21101011010110101010110C->B
22101100110101101101101C->A
23101111110111101110101B->A
2411000000110001111B->C
251100110011100110011<empty>A->C
261101001011010110110A->B
27110111101111011101110C->B
2811100001110011111<empty>A->C
29111011011110111011101B->A
301111001111011111111B->C
311111111111111111111<empty>A->C

So now you can calculate the optimal move in a N-disk Hanoi puzzle with 3 poles at any moment in time without having to know any of the previous or future moves.

Order of rule application in the L-system simplifications

From the N=5 case, it becomes clear that the L-system has more work to do, and the work to be done by our L-system increases as N increases.

One thing I'd like to show you in more detail is how the order of application of the rules doesn't influence the end result. This is not as obvious as it might seem.

First consider a different L-system with the following rules:
  • alphabet: 0,1
  • rule 1: 001 -> 0
  • rule 2: 00 -> <empty>
Now start from the sequence: 001
  • If you first apply rule 1, you get 001 ->  0
  • On the other hand, if you first apply rule 2, you get 001-> 1
We ended up with 2 different results that can no longer be further simplified. Clearly, in general the order of rule application matters!

But interestingly enough, this is not the case in our towers of Hanoi move simplification L-system. While the proof is in the thesis, I will just demonstrate on a few values.

Consider the 45th move in an 9-disk problem.
  • 45 binary:  101101
  • reversed: 101101
  • padded to 9 bits: 101101000
  • chopped after first 1 bit: 01101000
The L-system is asked to simplify 01101000. It's clear that the rules can be applied in different order, but the end result is always the same move. This is not a coincidence, but a direct result of how the method works. Here are some different orders that yield the same result. No matter how hard you try, you won't find a counter-example (unless you made a calculation error ;) ).
  • 01101000 -> 001000 -> 1000 -> 10 -> move C->B
  • 01101000 -> 010100 -> 1000 -> 10 -> move C->B
  • 01101000 -> 011010 -> 0101 -> 10 -> move C->B
  • 01101000 -> 011010 -> 0010 ->  10 -> move C->B
  • ...
There you have it. L-systems solving the towers of Hanoi problem. Who would have thought ;)