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.
%pylab inline
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.kernel.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'.
import scipy.constants as const
import scipy
from scipy.io import wavfile
from IPython.core.display import HTML
from __future__ import division
## for this notebook, we need to be in the folder where the ipynb are stored.
## which for me is here :
# 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 = """
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Simple Test</title>
</head>
<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))
## 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)
let's try to first just play an A (440 Hz).
pwd
u'/Users/bussonniermatthias/ipynb'
# 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")
The differents frequencies emmited by an hydrogen atom is given by the rydberg formulae :
1λ=R(1n1−1n2)Which gives a similar relation on the emitted frequencies of the Hydrogen :
fn,m=cλ=Rhh(1n−1m)for n=1 we've got the Lyman series, and for n=2 we have the Balmer series
# 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)
ryd = lambda n,m : fshift*(1/(n**2) -1/(m**2))
flyman = lambda x : ryd(1,x)
fbalmer = lambda x : ryd(2,x)
## 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)
ss = serv(time)
plot(time,ss)
ss = 2**15*ss/ ss.max()
wavfile.write('hydrogen.wav', rate, ss.astype(np.int16))
wavPlayer('hydrogen.wav')