Permalink
Browse files

Make graphing_receiver and sender work again (support AGC in Receiver)

  • Loading branch information...
1 parent 84cdc14 commit 4dc48a72be913185f526895566802e295fd0c6d9 @keithw committed Aug 10, 2012
Showing with 38 additions and 11 deletions.
  1. +15 −4 library/au_receive.py
  2. +3 −3 library/au_send.py
  3. +11 −2 library/graphing_receiver
  4. +9 −2 library/sender
View
@@ -51,15 +51,17 @@ class Receiver:
def receive( self, num_samples, stream, samples_per_chunk ):
return self.demodulate( raw_receive( num_samples, stream, samples_per_chunk ) )
- def __init__( self, center_frequency, bandwidth ):
+ def __init__( self, center_frequency, bandwidth, auto_gain_control_window=0 ):
self.carrier_freq = center_frequency
self.tuner = Filter( center_frequency - bandwidth, center_frequency + bandwidth )
self.lowpass = Filter( 0, bandwidth )
- def demodulate( self, samples ):
- print "Running demodulate()"
+ self.auto_gain_control_window = auto_gain_control_window
+ self.auto_gain = 1
+ self.auto_gain_saved_samples = numpy.array([])
+ def demodulate( self, samples ):
# Tune in just a band around the carrier frequency
samples = self.tuner( samples )
@@ -75,7 +77,16 @@ def demodulate( self, samples ):
# We assume the transmitted data had equal zeros and ones, and therefore
# that the average value is the (complex) amplitude of the carrier
- estimated_carrier = sum( demodulated_samples ) / len( samples )
+
+ # use a rolling AGC window if caller (like graphing_receiver) wants it
+ self.auto_gain_saved_samples = numpy.append( self.auto_gain_saved_samples, demodulated_samples )
+ # use at least this round of samples for auto gain control
+ operational_window = max( self.auto_gain_control_window, len( samples ) )
+ excess = len( self.auto_gain_saved_samples ) - operational_window
+ if excess > 0:
+ self.auto_gain_saved_samples = numpy.delete( self.auto_gain_saved_samples, slice( excess ) )
+
+ estimated_carrier = sum( self.auto_gain_saved_samples ) / len( self.auto_gain_saved_samples )
# Rotate and shift samples in time back to original phase and amplitude,
# including subtracting off the DC offset
View
@@ -14,10 +14,10 @@
total_sample_count = 0
-lowpass = Filter( 0, 500 )
+lowpass = Filter( 0, 500 ) # hardcoded bandwidth
-def send( samples, stream, samples_per_chunk ):
- return raw_send( modulate_float( samples,
+def send( samples, stream, samples_per_chunk, carrier_freq ):
+ return raw_send( modulate_float( samples, carrier_freq,
samples_per_chunk ),
stream )
@@ -2,6 +2,13 @@
from au_defs import *
+# these definitions are for the graphing receiver (outside the library)
+# so it tunes in a particular frequency band
+# they must match the same parameters in library/sender
+
+CARRIER_CYCLES_PER_SECOND = 1500
+BASEBAND_WIDTH = 500
+
import au_receive
import pygtk
import gtk
@@ -12,7 +19,8 @@ import pyaudio
WIDTH=512
HEIGHT=512
window_size = 4096
-slice_size = 256
+slice_size = 512
+AUTO_GAIN_CONTROL_WINDOW = 4096 # average amplitude over this number of samples
# Open soundcard
p = pyaudio.PyAudio()
@@ -42,7 +50,8 @@ cursor = colormap.alloc_color( "green" )
sample_num = 0
-receiver = au_receive.Receiver()
+receiver = au_receive.Receiver( CARRIER_CYCLES_PER_SECOND, BASEBAND_WIDTH,
+ AUTO_GAIN_CONTROL_WINDOW )
while True:
# Receive audio
View
@@ -2,6 +2,13 @@
from au_defs import *
+# these definitions are for the sender (outside the library)
+# so it tunes in a particular frequency band
+# they must match the same parameters in library/graphing_receiver
+
+CARRIER_CYCLES_PER_SECOND = 1500
+# BASEBAND_WIDTH = 500 # this is hardcoded in au_send.py
+
import au_send
import pyaudio
@@ -16,7 +23,7 @@ soundcard_out = p.open(format = FORMAT,
VOLTAGES = [ 1, 0.5, 0, -0.5, -1, 0, 0, 0 ]
#VOLTAGES = [ -1, -1, -1, -1, 1, 1, 1, 1]
-VOLTAGE_LENGTH = 128
+VOLTAGE_LENGTH = 256
samples = []
@@ -25,4 +32,4 @@ for voltage in VOLTAGES:
samples.append( voltage )
while True:
- au_send.send( samples, soundcard_out, 4096 )
+ au_send.send( samples, soundcard_out, 4096, CARRIER_CYCLES_PER_SECOND )

0 comments on commit 4dc48a7

Please sign in to comment.