import numpy as np, matplotlib.pyplot as plt
from matplotlib.pyplot import *
import queue, pyaudio
from scipy import signal
from numpy import *
%matplotlib inline
def play_audio( Q, p, fs , dev=None):
# play_audio plays audio with sampling rate = fs
# Q - A queue object from which to play
# p - pyAudio object
# fs - sampling rate
# dev - device number
# Example:
# fs = 44100
# p = pyaudio.PyAudio() #instantiate PyAudio
# Q = Queue.queue()
# Q.put(data)
# Q.put("EOT") # when function gets EOT it will quit
# play_audio( Q, p, fs,1 ) # play audio
# p.terminate() # terminate pyAudio
# open output stream
ostream = p.open(format=pyaudio.paFloat32, channels=1, rate=int(fs),output=True,output_device_index=dev)
# play audio
while (1):
data = Q.get()
if data is "EOT" :
break
try:
ostream.write( data.astype(np.float32).tostring() )
except:
break
fs = [72000, 24000, 12000, 8000, 6000]
factor = [1, 3, 6, 10, 12]
# Case 1: 5 kHz sampled with 72 kHz
t1 = np.r_[0:2:(1/fs[0])]
sig1 = np.sin(2*np.pi*5000*t1)
w1, h1 = signal.freqz(sig1,worN=1024)
w1 = w1 / np.pi * fs[0]/2
# Case 2: 5 kHz sampled with 24 kHz
t2 = np.r_[0:2:(1/fs[1])]
sig2 = np.sin(2*np.pi*5000*t2)
w2, h2 = signal.freqz(sig2,worN=1024)
w2 = w2 / np.pi * fs[1]/2
# Case 3: 5 kHz sampled with 12 kHz
t3 = np.r_[0:2:(1/fs[2])]
sig3 = np.sin(2*np.pi*5000*t3)
w3, h3 = signal.freqz(sig3,worN=1024)
w3 = w3 / np.pi * fs[2]/2
# Case 4: 5 kHz sampled with 8 kHz
t4 = np.r_[0:2:(1/fs[3])]
sig4 = np.sin(2*np.pi*5000*t4)
w4, h4 = signal.freqz(sig4,worN=1024)
w4 = w4 / np.pi * fs[3]/2
# Case 5: 5 kHz sampled with 6 kHz
t5 = np.r_[0:2:(1/fs[4])]
sig5 = np.sin(2*np.pi*5000*t5)
w5, h5 = signal.freqz(sig5,worN=1024)
w5 = w5 / np.pi * fs[4]/2
plt.figure()
width, height = plt.figaspect(0.2)
fig = plt.figure(figsize=(width,height))
plt.plot(t1[0:720]*1000,sig1[0:720])
plt.legend(("fs = 72 kHz",""))
plt.xlabel("time (ms)")
plt.ylabel("Amplitude")
plt.figure()
width, height = plt.figaspect(0.2)
fig = plt.figure(figsize=(width,height))
plt.plot(w1/1000,abs(h1)/np.max(abs(h1)))
plt.xlabel("frequency (kHz)")
plt.ylabel("Normalized Amplitude")
plt.legend(("fs = 72 kHz",""))
plt.axis([0,10,0,1])
Qout = queue.Queue()
Qout.put( sig1 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[0])
p.terminate()
plt.figure()
width, height = plt.figaspect(0.2)
fig = plt.figure(figsize=(width,height))
plt.plot(t1[0:720]*1000,sig1[0:720])
plt.plot(t2[0:240]*1000,sig2[0:240])
plt.legend(("fs = 72 kHz","fs = 24 kHz"))
plt.xlabel("time (ms)")
plt.ylabel("Amplitude")
plt.figure()
width, height = plt.figaspect(0.2)
fig = plt.figure(figsize=(width,height))
plt.plot(w1/1000,abs(h1)/np.max(abs(h1)))
plt.plot(w2/1000,abs(h2)/np.max(abs(h2)))
plt.xlabel("frequency (kHz)")
plt.ylabel("Normalized Amplitude")
plt.legend(("fs = 72 kHz","fs = 24 kHz"))
plt.axis([0,10,0,1])
Qout = queue.Queue()
Qout.put( sig1 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[0])
p.terminate()
Qout = queue.Queue()
Qout.put( sig2 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[1])
p.terminate()
plt.figure()
width, height = plt.figaspect(0.2)
fig = plt.figure(figsize=(width,height))
plt.plot(t1[0:720]*1000,sig1[0:720])
plt.plot(t3[0:120]*1000,sig3[0:120])
plt.legend(("fs = 72 kHz","fs = 12 kHz"))
plt.xlabel("time (ms)")
plt.ylabel("Amplitude")
plt.figure()
width, height = plt.figaspect(0.2)
fig = plt.figure(figsize=(width,height))
plt.plot(w1/1000,abs(h1)/np.max(abs(h1)))
plt.plot(w3/1000,abs(h3)/np.max(abs(h3)))
plt.xlabel("frequency (kHz)")
plt.ylabel("Normalized Amplitude")
plt.legend(("fs = 72 kHz","fs = 12 kHz"))
plt.axis([0,10,0,1])
Qout = queue.Queue()
Qout.put( sig1 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[0])
p.terminate()
Qout = queue.Queue()
Qout.put( sig3 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[2])
p.terminate()
Qout = queue.Queue()
Qout.put( sig1 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[0])
p.terminate()
Qout = queue.Queue()
Qout.put( sig4 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[3])
p.terminate()
plt.figure()
width, height = plt.figaspect(0.2)
fig = plt.figure(figsize=(width,height))
plt.plot(t1[0:720]*1000,sig1[0:720])
plt.plot(t4[0:80]*1000,sig4[0:80])
plt.legend(("fs = 72 kHz","fs = 8 kHz"))
plt.xlabel("time (ms)")
plt.ylabel("Amplitude")
plt.figure()
width, height = plt.figaspect(0.2)
fig = plt.figure(figsize=(width,height))
plt.plot(w1/1000,abs(h1)/np.max(abs(h1)))
plt.plot(w4/1000,abs(h4)/np.max(abs(h4)))
plt.xlabel("frequency (kHz)")
plt.ylabel("Normalized Amplitude")
plt.legend(("fs = 72 kHz","fs = 8 kHz"))
plt.axis([0,10,0,1])
Qout = queue.Queue()
Qout.put( sig1 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[0])
p.terminate()
Qout = queue.Queue()
Qout.put( sig4 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[3])
p.terminate()
Qout = queue.Queue()
Qout.put( sig1 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[0])
p.terminate()
Qout = queue.Queue()
Qout.put( sig5 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[4])
p.terminate()
plt.figure()
width, height = plt.figaspect(0.2)
fig = plt.figure(figsize=(width,height))
plt.plot(t1[0:720]*1000,sig1[0:720])
plt.plot(t5[0:60]*1000,sig5[0:60])
plt.legend(("fs = 72 kHz","fs = 6 kHz"))
plt.xlabel("time (ms)")
plt.ylabel("Amplitude")
plt.figure()
width, height = plt.figaspect(0.2)
fig = plt.figure(figsize=(width,height))
plt.plot(w1/1000,abs(h1)/np.max(abs(h1)))
plt.plot(w5/1000,abs(h5)/np.max(abs(h5)))
plt.xlabel("frequency (kHz)")
plt.ylabel("Normalized Amplitude")
plt.legend(("fs = 72 kHz","fs = 6 kHz"))
plt.axis([0,10,0,1])
Qout = queue.Queue()
Qout.put( sig1 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[0])
p.terminate()
Qout = queue.Queue()
Qout.put( sig5 );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs[4])
p.terminate()
fs_a = 4500
t_a = np.r_[0:2:(1/fs_a)]
sig_a = np.sin(2*np.pi*5000*t_a)
Qout = queue.Queue()
Qout.put( sig_a );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs_a)
p.terminate()
fs_a = 12000
t_a = np.r_[0:6:(1/fs_a)]
sig_a = np.zeros_like(t_a)
for i in range(0,6):
sig_a[(i*fs_a):(i*fs_a+fs_a)] = np.sin(2*np.pi*(5000+100*i)*t_a[(i*fs_a):(i*fs_a+fs_a)])
Qout = queue.Queue()
Qout.put( sig_a );
Qout.put( "EOT" );
p = pyaudio.PyAudio()
play_audio(Qout,p,fs_a)
p.terminate()