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])


152

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])


20000
starting frame at 0
Out[78]:
(-500, 28172.100000000002)

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]:
(-0.5, 1.5)

In [80]:
bits = quietnet.get_bits(points, FRAME_LENGTH)
print ''.join([str(bit) for bit in bits])


000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010010000110011011000000000001110011100110001011100001000111001

In [81]:
psk_letters = [a for a in ''.join([str(bit) for bit in bits]).split('00') if len(a) > 0]
print psk_letters


['1', '1', '11', '11011', '0111', '111', '11', '010111', '1', '0111', '1']

In [82]:
print ''.join([psk.decode(letter) for letter in psk_letters])


  eloe  

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)


125
Out[84]:
[<matplotlib.lines.Line2D at 0x109262d10>]

In [83]: