I Made a Piano with Maths
- Eloise L
- Apr 13, 2021
- 3 min read
Cool, right?!

For Year 4 of my degree I had to choose a project that I would work on throughout the year, writing up a written report and giving an oral presentation about it at the end. Most of these are group projects but no one else was allocated to my group (this was probably for the best after the stories I've heard about some of the other groups...).
My project was entitled "Finite Difference Schemes and Sound Synthesis". I didn't know exactly what I was in for, but I like coding, maths and music so this seemed like a pretty cool project. Basically, it involved making sounds from scratch using a computer. You can construct equations that model the instrument over time (these are usually Partial Differential Equations - PDEs). The problem is that these equations are usually far too difficult or even impossible to solve in theory. Instead, we change the equations into a numerical form that can be solved using computer code. One method for doing this is the "Finite Difference Method", in which all the derivatives in the model are replaced by discrete approximations (finite differences). So the original equations are not solved exactly, but we try to make fairly accurate approximations that don't change the solution too much.
I spent most of Semester 1 working my way through Stefan Bilbao's excellent textbook on the subject: "Numerical Sound Synthesis: Finite Difference Schemes and Simulation in Musical Acoustics" [1]. I had weekly supervision meetings with my supervisor Dr Charlotte Desvages. It was a new experience having an academic all to myself 1-1 for around an hour (even if these meeting were never in-person). From explaining really tricky parts of the maths to helping me fix my silly coding errors, these meetings were extremely valuable.
At some point during the semester, I decided to concentrate on modelling a piano string, since I actually play the piano, and I thought the papers about piano modelling were the most interesting.
In January, the time finally came to start writing up my report. At first, this was a daunting task and I had to keep forcing myself to sit down and write. However, at some point around early February, things seemed to be coming together very nicely, I finally had a fully working final model, and I became very engrossed (perhaps slightly obsessively) in my report.

The report is structured around building up a more and more complex model of a hammer striking a piano string.
Starting off with a very simple model (the 1D wave equation) I then added:
Inharmonicity - a weird phenomenon that means the different frequency components of a note are slightly out of tune with each other and gives the piano its characteristic sound
Damping - where the energy of wave oscillations gradually decrease over time so the sound gradually fades over time
Hammer - this is what 'excites' the piano string vibrations, and usually requires adding a nonlinear component to the mode
The first sound I made was from the 1D wave equation:
This might not sound very impressive or piano-like but it was pretty exciting to make a sound from scratch with some code!
After adding more and more complexity to the model and finally ironing out all my coding errors, this is my final piano sound:
And here's an animation of the string over the first 3ms. The oscillations in real life happen much faster than this, and also look much smaller (the maximum string displacement on the y-axis is around 1mm).
Of course, these animations and sounds couldn't be put in my pdf report, but here's a nice figure of snapshots of the string and hammer over time.

The sounds so far were all of 'middle-C', but varying the physical parameters in the model allowed me to make piano notes of different pitches. And one day I had a little fun making this familiar tune with my digital piano sounds :)
Subjectively, I think the final piano sounds are pretty good, albeit still rather synthetic sounding. Unfortunately, most of my code would take around 5-10 seconds to generate just 1 second of simulated sound, which would not be great for real time synthesis. This is why most digital pianos nowadays still use large databases of recorded sounds to generate their sounds.
Overall, I was very pleased with my project. The report goes much more into detail of the background maths, with reference to stability, accuracy and efficiency of numerical models. Maybe I'll post it here once the report has been marked! In the mean time you can access all of my code, sounds and animations here: https://github.com/eslardet/y4project-soundsynthesis.
References
[1] S. Bilbao, Numerical sound synthesis. John Wiley & Son, 2009. DOI: 10.
1002/9780470749012.
[2] D. Russel. (1997). The piano hammer as a nonlinear spring, Penn State
College of Engineering, [Online]. Available: https://www.acs.psu.edu/drussell/Piano/NonlinearHammer.html
Comments