NumPy and Matplotlib are Python Libraries that are widely used in scientific computing and data visualisation.
from numpy import *
from matplotlib.pyplot import *
%matplotlib inline
The audio
package is a collection of Python modules that I have developped for this course:
use audio.wave
to read/write WAVE files,
use audio.io
to play/record sound data,
use audio.bitstream
to read/write binary data.
import audio.io
import audio.wave
from audio.bitstream import BitStream
df = 44100.0
dt = 1.0 / df
f = 440.0
T = 3.0
t = r_[0.0:T:dt] # see also: arange, linspace, etc.
t[-1] # the last value (T) is excluded
plot(t)
A4 = cos(2*pi*f*t)
audio.io.play(A4)
ts = t[t<20.0/1000.0] # 20 ms
A4s = A4[t<20.0/1000.0]
plot(ts, A4s)
def make_tone(symbol):
number = int(symbol[1:])
f = 27.5 * 2 ** number
x = cos(2*pi*f*t)
audio.wave.write(x, symbol + ".wav")
return x
A8 = make_tone("A8")
audio.io.play(A8)
A = []
for i in range(0,11):
symbol = "A" + str(i)
A.append(make_tone(symbol))
for i, sound in enumerate(A):
print "A" + str(i)
audio.io.play(sound)
27.5 * 2**10
audio.io.play(A[10])
for sound in A:
print mean(sound*sound)
def L(x):
return 96.0 + 10.0 * log10(mean(x*x))
L(A[4])
audio.io.play(A[4])
audio.io.play(A[4], df=16000)
Wave format documentation: http://soundfile.sapp.org/doc/WaveFormat/
raw = open("A4.wav").read() # raw is a 'str' (string)
print raw[:4], raw[8:12]
raw = open("A4.wav").read()
stream = BitStream(raw)
print stream.read(str, 4)
_ = stream.read(str, 4)
print stream.read(str, 4)
raw = open("A4.wav").read()
stream = BitStream(raw)
_ = stream.read(str, 22)
print stream.read(uint16).newbyteorder() # 2-byte integer (little end.)
print stream.read(uint32).newbyteorder() # 4-byte integer (little end.)
A4q = audio.wave.read("A4.wav")
e = A4 - A4q
SNR = sqrt(mean(A4*A4) / mean(e * e))
print SNR
print 20.0 * log10(SNR) # SNR in dB