## Tuesday, 27 November 2018

### Tower of Hanoi

The Tower of Hanoi is a classic mathematical puzzle consisting of three pegs and disks of different sizes. The aim of the puzzle is to move the stack of increasing size disks from one peg to the other, by moving only the upper disk of any peg at a time. On each movement, no larger disk may be placed above another smaller disk.

The problem was first described by the French mathematician Édouard Lucas in 1883 and it is a  problem where recursion can be easily used to solve the game. Along with the Fibonacci series, it is the classic exercise of a 101 programming course where students are asked to apply recursion.

Recently I built three wooden play sets of Tower of Hanoi and thought it would be interesting to increase my knowledge on the problem.  As a first approach of the problem, I wrote the classical recursive solution, which I post here. I will tackle the problem from a different perspective, probably graph theory, in the coming posts.

The Python code can be found on my github repository. I have splitted the solution in three different Python classes, Rectangle, which deals with the graphic representation of the objects using Pyglet library, Disk, which represents a disk in the Tower of Hanoi problem, keeping track of the position of itself at any moment, and finally the Hanoi class, which solves the problem using the recursive algorithm and show a graphical representation of the step by step solution.

Following the instructions of the README.md file at the git hub repository, you can get the following solution video.

Stay tuned for upcoming post on the subject with different solution approaches.

## Sunday, 28 October 2018

### Human Hearing Frequency Range Test

The other day I came across an article on human hearing which sparked my curiosity on what my audible frequency range was.

Human ear can usually detect sounds ranging from frequencies from 20Hz to 20KHz. However, as we grow up, the auditory organ loses capabilities and the dynamic range gets narrower. I wondered what my current dynamic range was, and I wrote this simple Python script to test it.

The script makes use of PyAudio, a Python Package which gives us access to the sound card. The code saves a number of num_samples of sound samples into a NumPy matrix. Each sample is generated from a sinusoidal signal of a different frequency, specified in the frequencies list.

Once the different waves have been saved, each frequency in a row of the matrix waves, we play the sounds by writing into the stream object created from PyAudio module.

The script starts by playing a sinusoidal sound of 5Hz, and increases step by step the frequency of the samples. Each frequency is played for 5secs. In my case, I start hearing sounds after the 20Hz tone. As the frequency increases beyond the 20Hz, even if the intensity is kept constant in the script, my ear response to the different frequencies is not the same, giving us the illusion of a different intensity for each tone. After the 14KHz barrier, I was unable to hear any signal.

If you have pets around, try to test this scripts by increasing the frequency beyond the 22KHz. Observe however that if you increase the frequency beyond 22KHz, you should increase as well the sample rate, to satisfy the requirements imposed by Nyquist sampling theorem.