Sound of Pi

by Gammawave in Design > Digital Graphics

551 Views, 2 Favorites, 0 Comments

Sound of Pi

pidaymain.png

An annual celebration of the mathematical constant Pi (π).

Pi Day celebrated on March 14 (3/14 in the mm/dd date format), although alternative celebrations are held on dates were the date has a relationship with Pi.

These dates are:

Pi Approximation Day observed on July 22 (22/7 in the dd/mm date format), based on the fraction 22⁄7 a common approximation of π,

Tau Day, also known as Two-Pi Day is observed on June 28 (6/28 in the mm/dd format). The number 𝜏, denoted by the Greek letter tau, is the ratio of a circle's circumference to its radius; it equals 2π and approximately equals 6.28.

Others also celebrate π on November 10, since it is the 314th day of the year (November 9 on a leap year).

Pi day is also celebrated in many other ways:

Pi in the Sky; skywriting the digits, Pi Pie; baking, Pi Chain; human chain each holding a digit, Pi Ku; with syllable count matching the digits, Pi Art; Artistic depictions.

This is my interpretation of Pi being artistic in nature based on the final result although its generated using computational mathematics (Spigot algorithm), Excel and VBA.

Supplies

MS Excel with VBA

Media Player or alternative application

Optional Supplies

Document printer

Cura 5.8.1 (used in this project), or alternative application

3D Printer

Background

The final output of this project relies on the ability to generate sequential individual digits of Pi.

These are generated using a Spigot algorithm although other methods do exist.

What is s Spigot algorithm.

Named Spigot (tap or valve), due to the digits flowing out one by one like a dripping or leaky tap.

It computes the digits of Pi producing digits incrementally and does not reuse them once they have been calculated. Uses only ordinary integer arithmetic and does not require high-precision operations on floating-point numbers. This algorithm was first proposed by A. Sale in 1968 for computing the digits of e (the base of natural logarithms), and later adapted for pi by D. Saada in 1988 and Stanley Rabinowitz in 1991.

The process in summary:

The algorithm starts with a series of 2s, in columns headed by a series fractions (1/3, 2/5, 3,7 etc). Each entry is multiplied by 10. Then, starting from the right, the entries are reduced modulo denominator (den), where the head of the column is num/den, producing a quotient q and remainder r. The remainder is left in place and q × num is carried one column left. This reduce-and-carry is continued all the way left. The tens digit of the leftmost result is the next digit of π. The process continues with the multiplication of the remainders by 10, the reductions modulo the denominators, and the augmented carrying handling.

Further details can be found at the following source: spigot.tex (A Spigot algorithm for the digits of Pi by Stanley Rabinowitz and Stan wagon).

Charting

Pi72kchartscl.JPG
pi2kwave.png

The coding uses VBA in Excel which encapsulates all the required functions to display Pi in a number of different ways.

The charts and images within this project use 2000 digits of Pi more digits could be used but the downside is increased time, reduced speed and impact on system resources,

As per the Spigot algorithm a series of digits in Pi are generated sequentially.

This is contained within a Function Spigot which is called iteratively for each digit.

For each digit generated a variety of calculations are carried out.

Referencing each digit (0 to 9 for a total of 10 digits), to the angular position on the circumference of a circle over 360 degrees. Equals 360/10 = 36 degrees per digit.

Thus for digits of Pi; (314), 3 = (3*36) = 108 degrees, 1 = 36 degrees and 4 = 144 degrees etc.

The value is the direction angle and is used to calculate the X, Y co-ordinates on an X/Y scatter plot.

Utilising the following:

X = Sine(direction angle) * radius, Y = Cos(direction angle) * radius

Example: sin(108)*4 = 3.707, cos(108)*4 = 1.502 giving the following (x,y) co-ordinates (3.707, 1.502)

Values for the radius other than 4 could be used as suits chart scaling.

The size or eccentricity of the circle can be varied or scaled by changing the radii or applying a scaling factor to the calculation.

X = (Sine(direction angle) * radius) * scaling factor, Y = (Cos(direction angle) * radius) * scaling factor

The result would either be a circle of dots or a circle of serially connected lines with or without dots difficult to interpret and certainly not the intended output.

Rather than a circular chart the intention was to create a horizontally aligned chart.

Using the following:

X =( (Sine(direction angle) * radius) * scaling factor) + Xprev

Y = ((Cos(direction angle) * radius) * scaling factor)) + (Yprev * -1)

The amplitude of the Y axis is normalised to the range -1 to +1 using min-max normalisation with the following:

Call normalization(data_Arr, 6) where 6 is the column number for the Y axis data


Min-max normalisation taking the form:

new_value = 2 * (v -min(v))/(max(v)-min(v)) -1 where v is the existing value and min(v),max(v) are the extreme values in the data.

The purpose for the normalisation will become clearer in the next step.


Xprev is the previous x value that is add to the next x value.

This increments the X value (negatively in this case), whilst the Y value is varying either side of the zero referenced horizontal axis.

The data for each digit is output to the sheet in the following columns up to rows = digitsofpi

Column 1 = Row count, Column 2 = sample rate, Column 3 = Pi Digit, Column 4 = direction angle.

The X/Y co-ordinates occupy columns 5 & 6.

Some for interest and information rather than being used directly.


To speed up the process all the data is generated and stored in an array data_Arr() first.

The array data_Arr() is then dumped to the sheet without iteration using:

.Range("A1").Resize(numDigits, numcols).value = data_Arr


After the data is output to the sheet the data in column 6 of data_Arr() is output to a file named piwaveform.csv


The chart is a manually generated X/Y scatter with smooth lines chart formatted to remove all un-necessary legends.

The resulting chart of the X/Y co-ordinates is a digital representation of a waveform derived from the digits of Pi.

This is interesting as a chart but I was intrigued as to what it would sound like.

Making a Wave

planetcalc2.jpg
wavecity2.jpg

The next part of the process was to convert the data into a WAV file.

Again this was coded in VBA for Excel.

Contained within subroutine CSV_WAV

This uses the RIFF (Resource Interchange File Format), file container to format the file.

This takes the data on the Y axis which is output to a text file named piwaveform.csv

Some of the parameters for this process are set based on the number of digits for Pi during the initialization process.

Sample Rate = Digits for Pi = Equivalent value in Hertz. (Sample rate for CD quality is 44.1kHz)

The sound duration derived from the sample rate = Digits for Pi/(Digits for Pi/n) where n is an integer number.

Example:

Sound duration = 6000/(6000/3) = 3 seconds

Bits per sample = 16

Channels = 1 (Mono)

Once the Y axis data is processed it can be saved with a suitable name.

The data was pre-processed to meet the requirements for floating points numbers between -1.0 to +1.0 although signed or unsigned integers could also be used.

To hear the "Sound of PI", the file can be opened with Media Player or a similar application that will play wav files.

I wanted to check the waveform visually matched the chart output and ensure the conversion process had not introduced any issues.

I tried a variety of applications some of which reported errors but identified two free online applications* which successfully displayed the wav file, these being Planetcalc.com and Wavecity.com.

*No affiliation to these applications, feel free to use whichever application you find suitable.

These two applications displayed comparable profiles although compared to the Excel chart flipped left to right.

Printing

PiCurawav2k.JPG
DSCF6463_2.jpg
pi2kwave.png

Rather than a virtual item I wanted to preserve the waveform in a physical form and a 3D print seemed fitting.

The 3D printing process could be carried out directly in one colour but to improve contrast I adopted for a multi coloured print.

On a single head machine this entails printing the supporting background in one colour and performing a colour change.

There is no need to generate an STL file as the image file (pi2kwave.png), can be imported into the slicer (Cura), directly.

On the image import the base height is 1mm and overall height is 2.5 mm the footprint dimensions are 120(L) x 66.7(W) mm.

Print details

Filament: PLA White (background) & Orange silk (waveform)

Layer Height: 0.15mm

Infill: 75%

Infill pattern: Cubic Subdivision

Base Adhesion: Skirt

Layer Change: 9 (changing parameters that affect the thickness will influence the layer change value).

No supports.

The model is rotated such that the waveform has the longest dimension running top to bottom, this allows the extrusion to more closely follow the line used to create the chart.

Setting Layer change

Using Cura and in preview mode use the layer slider to identify the layer to apply the colour change.

From the menu select Extensions > Post Processing > Modify G-Code.

Select Add a script > Filament Change

Set Layer = the appropriate layer number.

Close

A small red circle will be displayed next to the slice button showing the number of active post processing scripts.

Once the assigned layer has been reached, printing will stop pending the filament change.

Summary

In summary the main part of the project was to process the digits of Pi to create a sound.

In doing so I also produced the visual representation of this sound which can be viewed on a screen, printed to paper or even 3D printed.

In addition other visual representations of Pi were produced.

Full code attached.

Downloads

Other Outputs

PiCells.png
piradar.png

Within the same application other outputs are also available.

1: Each digit of Pi output to a grid of cells and each cell colour coded based on the numerical value of the digit.

2: Each digit of Pi plotted on a Radar chart, made an interesting pattern of colours.

Finally

Hope you found this of interest.