#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('matplotlib', 'notebook') from matplotlib.pyplot import * from numpy import * from audio.coders import * from bitstream import * # In[2]: N = 16000 # ### Random Exponential (Independent) Values # [exponential distributions](https://docs.scipy.org/doc/numpy-1.12.0/reference/generated/numpy.random.exponential.html) # In[3]: data = random.exponential(scale=30.0, size=N).astype(uint16) # In[4]: figure() plot(data) # In[5]: figure() hist(data, bins=r_[0:256], density=True) grid() # In[6]: BitStream(0, unary) # In[7]: BitStream(100, unary) # In[8]: N * 16 # In[9]: stream = BitStream(data, unary) # In[10]: len(stream) / (N * 16.0) * 100.0 # In[11]: BitStream(0, rice(b = 8, signed=False)) # In[12]: BitStream(1, rice(b = 8, signed=False)) # In[13]: BitStream(255, rice(b = 8, signed=False)) # In[14]: BitStream([0,1,255], rice(b = 8, signed=False)) # In[15]: n = 1024+512+256 + 42 display(BitStream(n, uint16)) display(BitStream(n, rice(b = 8, signed=False))) # In[16]: r = rice.from_frame(data, signed=False) # In[17]: r = rice(b=6, signed=False) stream = BitStream(data, r) len(stream) / (16.0 * N) * 100.0 # ### Pure Tone # In[18]: df = 44100 dt = 1.0 / df t = r_[0:N] * dt f= 440 data = sin(2*pi*f*t) # In[19]: import audio.wave as wave from IPython.display import * wave.write(data, "A4.wav", df=df) Audio("A4.wav") # In[20]: figure() data = wave.read("A4.wav", scale=False)[0] plot(t, data) # In[21]: figure() hist(data, density=True) # In[22]: r = rice.from_frame(data, signed=True) r # In[23]: stream = BitStream(data, r) len(stream) / (16.0 * N) * 100 # In[24]: len(data), len(diff(data)) # In[25]: ddata = r_[data[0], diff(data)] # In[26]: ddata2 = diff(r_[0, data]) all(ddata2 == ddata) # In[27]: figure() hist(ddata, density=True) # In[28]: stream = BitStream(ddata, rice.from_frame(ddata, signed=True)) len(stream)/ (16.0 * N) * 100 # In[29]: all(cumsum(ddata) == data) # In[30]: dddata = diff(r_[0, ddata]) r = rice.from_frame(dddata, signed=True) #display(r) #r = rice(b=12, signed=True) stream = BitStream(dddata, r) len(stream)/ (16.0 * N) * 100 # In[31]: from audio.index import * import nltk nltk.download("timit") # In[32]: wave.write(search("circle")[0].parent.audio, "circle.wav", df=16000) # In[33]: Audio("circle.wav")