In [72]:
import math
import os, sys
lib_path = os.path.abspath('../')
sys.path.append(lib_path)
import quietnet
import capture_audio # helper methods to get audio buffers
import options
import psk
CHUNK = options.chunk
RATE = options.rate
FRAME_LENGTH = options.frame_length
search_freq = options.freq
# Graphing helper function
def setup_graph(title='', x_label='', y_label='', fig_size=[16, 8], x_grid=None):
fig = plt.figure()
if fig_size != None:
fig.set_size_inches(fig_size[0], fig_size[1])
ax = fig.add_subplot(111)
ax.set_title(title)
if x_grid != None:
ax.set_xticks(x_grid)
plt.grid(True)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
In [73]:
buffers_packed = capture_audio.capture_seconds(5, CHUNK, RATE, 2)
buffers = [quietnet.unpack(b) for b in buffers_packed]
In [74]:
long_buffer = reduce(lambda x, y: x + y, buffers)
In [75]:
ffts = [quietnet.fft(b) for b in buffers]
In [76]:
setup_graph(title='FFTs', x_label='Frequency', y_label='Amplitude')
for fft in ffts:
plot(fft)
# each color is a different FFT
In [77]:
setup_graph(title='FFTs at Frequency of Interest', x_label='Frequency', y_label='Amplitude')
peak = quietnet.get_peak(search_freq, RATE, CHUNK)
print peak
for fft in ffts:
plot(fft[peak-3:peak+4])
In [78]:
freq_samples = quietnet.get_freq_over_time(ffts, search_freq, CHUNK, RATE)
threshold = np.median(freq_samples)
bottom_threshold = 20000#threshold / FRAME_LENGTH
print bottom_threshold
# try and find the beginning of a frame
for i in range(len(freq_samples) - FRAME_LENGTH):
if np.average(freq_samples[i:i + FRAME_LENGTH]) < bottom_threshold:
freq_samples = freq_samples[i:]
print "starting frame at %s" % i
break
setup_graph(title='Frequency of Interest over Time', x_label='Time', y_label='Frequency')#,x_grid=numpy.arange(-0.5,len(freq_samples),FRAME_LENGTH))
plot(freq_samples, 'grey')
plot(freq_samples, '.')
pylab.ylim([-500,math.ceil(np.max(freq_samples))*1.1])
Out[78]:
In [79]:
setup_graph(title='Classification of Frequency', x_label='Time', y_label='Bit Value',
x_grid=numpy.arange(0,len(freq_samples),FRAME_LENGTH))
points = quietnet.get_points(freq_samples, FRAME_LENGTH, bottom_threshold)
plot(points, 'grey')
plot(points, '.')
pylab.ylim([-.5,1.5])
Out[79]:
In [80]:
bits = quietnet.get_bits(points, FRAME_LENGTH)
print ''.join([str(bit) for bit in bits])
In [81]:
psk_letters = [a for a in ''.join([str(bit) for bit in bits]).split('00') if len(a) > 0]
print psk_letters
In [82]:
print ''.join([psk.decode(letter) for letter in psk_letters])
In [84]:
import numpy as np
import cmath
l = CHUNK * 5
tiny_buffers = [b for b in list(quietnet.chunks(long_buffer, l)) if len(b) == l]
print len(tiny_buffers)
peak = quietnet.get_peak(search_freq, RATE, l)
fft_raw = [np.fft.rfft(b)[peak] for b in tiny_buffers]
fft_phase = [cmath.phase(a) for a in fft_raw]
fft_v = [np.abs(a) for a in fft_raw]
setup_graph()
plot(fft_phase)
Out[84]:
In [83]: