# The sound Of Hydrogen¶

Inspired by minutephysics, and the explanation do do it n mathematica: The sound of hydrogen.

The goal of this notebook is show how one can write a sound file to the disk, and use Html5 <audio> tag to play it dirrectly inside te notebook.

To do this we use the spectrum spectrum of hydrogen that we shift the into the audible range.

Plese be aware that the html5 player might not work in nbviewer.

In [1]:
%pylab inline

Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.kernel.zmq.pylab.backend_inline].

In [2]:
import scipy.constants as const
import scipy
from scipy.io import wavfile
from IPython.core.display import HTML
from __future__ import division

In [3]:
## for this notebook, we need to be in the folder where the ipynb are stored.
## which for me is here :

In [4]:
# this is a wrapper that take a filename and publish an html <audio> tag to listen to it

def wavPlayer(filepath):
""" will display html 5 player for compatible browser

Parameters :
------------
filepath : relative filepath with respect to the notebook directory ( where the .ipynb are not cwd)
of the file to play

The browser need to know how to play wav through html5.

there is no autoplay to prevent file playing when the browser opens
"""

src = """
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Simple Test</title>

<body>
<audio controls="controls" style="width:600px" >
<source src="files/%s" type="audio/wav" />
Your browser does not support the audio element.
</audio>
</body>
"""%(filepath)
display(HTML(src))

In [5]:
## some consstant for our audio file

rate = 44100 #44.1 khz
duration =5 # in sec

# this will give us sin with the righ amplitude to use with wav files
normedsin = lambda f,t : 2**13*sin(2*pi*f*t)

time = np.linspace(0,duration, num=rate*duration)


# Test the wav Player¶

let's try to first just play an A (440 Hz).

In [13]:
pwd

Out[13]:
u'/Users/bussonniermatthias/ipynb'
In [14]:
# define A as a 440 Hz sin function
la    = lambda t : normedsin(440,t)

# look at it on the first 25 ms
plot(time[0:1000], la(time)[0:1000])

# write the file on disk, and show in in a Html 5 audio player
wavfile.write('440.wav', rate, la(time).astype(np.int16))
wavPlayer("440.wav")

Simple Test

The differents frequencies emmited by an hydrogen atom is given by the rydberg formulae :

$${1 \over \lambda} = R \left({1\over n_1}-{1\over n_2}\right)$$

Which gives a similar relation on the emitted frequencies of the Hydrogen :

$$f_{n,m}={c \over \lambda} = {R_h\over h} \left({1\over n}-{1\over m}\right)$$

for $n=1$ we've got the Lyman series, and for $n=2$ we have the Balmer series

In [7]:
# fondamental frequency of hydrogen
f0 = const.Rydberg*const.c
print "The highest frequency of hydrogen is ",f0,"Hz. and correspond to n = 1, m = ∞"
fshift = 440
print "we can shift the spectrum for it to be at 440 Hz (A)"

The highest frequency of hydrogen is  3.28984196036e+15 Hz. and correspond to n = 1, m = ∞
we can shift the spectrum for it to be at 440 Hz (A)

In [8]:
ryd = lambda n,m : fshift*(1/(n**2) -1/(m**2))
flyman = lambda x : ryd(1,x)
fbalmer = lambda x : ryd(2,x)

In [9]:
## define the sum,
ser = lambda t : sum( [normedsin(flyman(i),t)+normedsin(fbalmer(i+1),t) for i in range(2,8)])

# and a verorialized function to work on a by element basis with matlab
serv = scipy.vectorize(ser)

In [10]:
ss = serv(time)

In [11]:
plot(time,ss)
ss = 2**15*ss/ ss.max()

In [15]:
wavfile.write('hydrogen.wav', rate, ss.astype(np.int16))
wavPlayer('hydrogen.wav')

Simple Test
In [14]: