class SimpleAudioDemodulator(Demodulator, SquelchMixin): def __init__(self, demod_rate=0, audio_rate=0, band_filter=None, band_filter_transition=None, stereo=False, **kwargs): assert audio_rate > 0 self.__signal_type = SignalType( kind='STEREO' if stereo else 'MONO', sample_rate=audio_rate) Demodulator.__init__(self, **kwargs) SquelchMixin.__init__(self, demod_rate) self.demod_rate = demod_rate self.audio_rate = audio_rate input_rate = self.input_rate self.band_filter_block = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=band_filter, transition_width=band_filter_transition) @exported_value(type=BandShape, changes='never') # TODO not sure if this is the right change policy def get_band_shape(self): """Implements IDemodulator.""" return self.band_filter_block.get_shape() def get_output_type(self): """Implements IDemodulator.""" return self.__signal_type def set_rec_freq(self, freq): """Implements ITunableDemodulator.""" self.band_filter_block.set_center_freq(freq)
class ChannelFilterMixin(object): """Provides a MultistageChannelFilter block and matching implementations of get_band_shape and ITunableDemodulator. Does not make any connection automatically. """ def __init__(self, input_rate=0, demod_rate=0, cutoff_freq=0, transition_width=0): # mandatory keyword arguments assert input_rate > 0 assert demod_rate > 0 assert cutoff_freq > 0 assert transition_width > 0 self.channel_filter_block = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=cutoff_freq, transition_width=transition_width) @exported_value(type=BandShape, changes='never') def get_band_shape(self): """Implements IDemodulator.""" return self.channel_filter_block.get_shape() def set_rec_freq(self, freq): """Implements ITunableDemodulator.""" self.channel_filter_block.set_center_freq(freq)
def __init__(self, demod_rate=0, audio_rate=0, band_filter=None, band_filter_transition=None, stereo=False, **kwargs): assert audio_rate > 0 self.__signal_type = SignalType(kind='STEREO' if stereo else 'MONO', sample_rate=audio_rate) Demodulator.__init__(self, **kwargs) SquelchMixin.__init__(self, demod_rate) self.demod_rate = demod_rate self.audio_rate = audio_rate input_rate = self.input_rate self.band_filter_block = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=band_filter, transition_width=band_filter_transition)
def test_explain(self): # this test was written before __run checke everything; kept around for just another example f = MultistageChannelFilter(input_rate=10000, output_rate=1000, cutoff_freq=500, transition_width=100) self.assertEqual(f.explain(), textwrap.dedent("""\ 2 stages from 10000 to 1000 freq xlate and decimate by 5 using 43 taps (86000) in freq_xlating_fir_filter_ccc_sptr final filter and decimate by 2 using 49 taps (49000) in fft_filter_ccc_sptr No final resampler stage."""))
def __init__(self, mode='MODE-S', input_rate=0, context=None): assert input_rate > 0 gr.hier_block2.__init__( self, type(self).__name__, gr.io_signature(1, 1, gr.sizeof_gr_complex * 1), gr.io_signature(0, 0, 0)) demod_rate = 2000000 transition_width = 500000 hex_msg_queue = gr.msg_queue(100) self.__band_filter = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=demod_rate / 2, transition_width=transition_width) # TODO optimize filter band self.__demod = air_modes.rx_path( rate=demod_rate, threshold=7.0, # default used in air-modes code but not exposed queue=hex_msg_queue, use_pmf=False, use_dcblock=True) self.connect(self, self.__band_filter, self.__demod) self.__messages_seen = 0 self.__message_rate_calc = LazyRateCalculator( lambda: self.__messages_seen, min_interval=2) # Parsing # TODO: These bits are mimicking gr-air-modes toplevel code. Figure out if we can have less glue. # Note: gr pubsub is synchronous -- subscribers are called on the publisher's thread parser_output = gr.pubsub.pubsub() parser = air_modes.make_parser(parser_output) cpr_decoder = air_modes.cpr_decoder( my_location=None) # TODO: get position info from device air_modes.output_print(cpr_decoder, parser_output) def msq_runner_callback(msg): # called on msgq_runner's thread # pylint: disable=broad-except try: reactor.callFromThread(parser, msg.to_string()) except Exception: print(traceback.format_exc()) self.__msgq_runner = gru.msgq_runner(hex_msg_queue, msq_runner_callback) def parsed_callback(msg): timestamp = time.time() self.__messages_seen += 1 context.output_message( ModeSMessageWrapper(msg, cpr_decoder, timestamp)) for i in six.moves.range(0, 2**5): parser_output.subscribe('type%i_dl' % i, parsed_callback)
def __init__(self, input_rate=0, demod_rate=0, cutoff_freq=0, transition_width=0): # mandatory keyword arguments assert input_rate > 0 assert demod_rate > 0 assert cutoff_freq > 0 assert transition_width > 0 self.channel_filter_block = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=cutoff_freq, transition_width=transition_width)
def __init__(self, input_rate, output_rate=12000, output_frequency=1500, transition_width=100, width=800): """Make a new WSPRFilter. input_rate: the incomming sample rate output_rate: output sample rate output_frequency: 0Hz in the complex input will be centered on this frequency in the real output width, transition_width: passband and transition band widths. """ gr.hier_block2.__init__(self, type(self).__name__, gr.io_signature(1, 1, gr.sizeof_gr_complex), gr.io_signature(1, 1, gr.sizeof_float)) self.connect( self, MultistageChannelFilter(input_rate=input_rate, output_rate=output_rate, cutoff_freq=width / 2, transition_width=transition_width), blocks.rotator_cc(2 * pi * output_frequency / output_rate), blocks.complex_to_real(vlen=1), analog.agc2_ff(reference=dB(-10), attack_rate=8e-1, decay_rate=8e-1), self)
def test_interpolating(self): """Output rate higher than input rate""" # TODO: Test filter functionality more f = MultistageChannelFilter(input_rate=8000, output_rate=20000, cutoff_freq=8000, transition_width=5000) self.__run(f, 4000, 20000 / 8000, """\ 2 stages from 8000 to 20000 freq xlation only using 1 taps (8000) in freq_xlating_fir_filter_ccc_sptr rational_resampler by 5/2 (stage rates 20000/8000) using 165 taps (3300000) in rational_resampler_base_ccf_sptr""")
def test_odd_interpolating(self): """Output rate higher than input rate and not a multiple""" # TODO: Test filter functionality more f = MultistageChannelFilter(input_rate=8000, output_rate=21234, cutoff_freq=8000, transition_width=5000) self.__run(f, 4000, 21234 / 8000, """\ 2 stages from 8000 to 21234 freq xlation only using 1 taps (8000) in freq_xlating_fir_filter_ccc_sptr rational_resampler by 10617/4000 (stage rates 21234/8000) using 350361 taps (7439565474) in rational_resampler_base_ccf_sptr""")
class SimpleAudioDemodulator(Demodulator, SquelchMixin): implements(ITunableDemodulator) def __init__(self, demod_rate=0, audio_rate=0, band_filter=None, band_filter_transition=None, stereo=False, **kwargs): assert audio_rate > 0 self.__signal_type = SignalType( kind='STEREO' if stereo else 'MONO', sample_rate=audio_rate) Demodulator.__init__(self, **kwargs) SquelchMixin.__init__(self, demod_rate) self.band_filter = band_filter self.band_filter_transition = band_filter_transition self.demod_rate = demod_rate self.audio_rate = audio_rate input_rate = self.input_rate self.band_filter_block = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=band_filter, transition_width=band_filter_transition) def get_half_bandwidth(self): return self.band_filter def get_output_type(self): return self.__signal_type def set_rec_freq(self, freq): '''for ITunableDemodulator''' self.band_filter_block.set_center_freq(freq) @exported_value() def get_band_filter_shape(self): return { 'low': -self.band_filter, 'high': self.band_filter, 'width': self.band_filter_transition }
def test_one_filter(**kwargs): print '------ %s -------' % (kwargs, ) f = MultistageChannelFilter(**kwargs) size = 10000000 top = gr.top_block() top.connect(blocks.vector_source_c([5] * size), f, blocks.null_sink(gr.sizeof_gr_complex)) print f.explain() t0 = time.clock() top.start() top.wait() top.stop() t1 = time.clock() print size, 'samples processed in', t1 - t0, 'CPU-seconds'
def __make_channel_filter(self): '''Return the channel filter. rtty_demod_cb includes filters, so here we just need a broad, cheap filter to decimate. ''' return MultistageChannelFilter(input_rate=self.__input_rate, output_rate=self.__demod_rate, cutoff_freq=self.__spacing * 5, transition_width=self.__spacing * 5)
def __init__(self, mode='MODE-S', input_rate=0, context=None): assert input_rate > 0 gr.hier_block2.__init__( self, 'Mode S/ADS-B/1090 demodulator', gr.io_signature(1, 1, gr.sizeof_gr_complex * 1), gr.io_signature(0, 0, 0)) demod_rate = 2000000 transition_width = 500000 hex_msg_queue = gr.msg_queue(100) self.__band_filter = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=demod_rate / 2, transition_width=transition_width) # TODO optimize filter band self.__demod = air_modes.rx_path( rate=demod_rate, threshold=7.0, # default used in air-modes code but not exposed queue=hex_msg_queue, use_pmf=False, use_dcblock=True) self.connect( self, self.__band_filter, self.__demod) self.__messages_seen = 0 self.__message_rate_calc = LazyRateCalculator(lambda: self.__messages_seen, min_interval=2) # Parsing # TODO: These bits are mimicking gr-air-modes toplevel code. Figure out if we can have less glue. # Note: gr pubsub is synchronous -- subscribers are called on the publisher's thread parser_output = gr.pubsub.pubsub() parser = air_modes.make_parser(parser_output) cpr_decoder = air_modes.cpr_decoder(my_location=None) # TODO: get position info from device air_modes.output_print(cpr_decoder, parser_output) def msq_runner_callback(msg): # called on msgq_runner's thread # pylint: disable=broad-except try: reactor.callFromThread(parser, msg.to_string()) except Exception: print traceback.format_exc() self.__msgq_runner = gru.msgq_runner(hex_msg_queue, msq_runner_callback) def parsed_callback(msg): timestamp = time.time() self.__messages_seen += 1 context.output_message(ModeSMessageWrapper(msg, cpr_decoder, timestamp)) for i in xrange(0, 2 ** 5): parser_output.subscribe('type%i_dl' % i, parsed_callback)
def __make_channel_filter(self): '''Return the channel filter. psk31_demodulator_cbc includes filters, so this filter will be wide to assure the passband has no group delay and make it easier to listen to. Output has frequencies from -250 to +250. ''' return MultistageChannelFilter(input_rate=self.__input_rate, output_rate=self.__demod_rate, cutoff_freq=250 - 25, transition_width=25)
def test_one_filter(**kwargs): print '------ %s -------' % (kwargs,) f = MultistageChannelFilter(**kwargs) size = 10000000 top = gr.top_block() top.connect( blocks.vector_source_c([5] * size), f, blocks.null_sink(gr.sizeof_gr_complex)) print f.explain() t0 = time.clock() top.start() top.wait() top.stop() t1 = time.clock() print size, 'samples processed in', t1 - t0, 'CPU-seconds'
def test_basic(self): # TODO: Test filter functionality more f = MultistageChannelFilter(input_rate=32000000, output_rate=16000, cutoff_freq=3000, transition_width=1200) self.__run(f, 400000, 16000 / 32000000, """\ 7 stages from 32000000 to 16000 freq xlate and decimate by 5 using 25 taps (160000000) in freq_xlating_fir_filter_ccc_sptr decimate by 5 using 25 taps (32000000) in fft_filter_ccc_sptr decimate by 5 using 25 taps (6400000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (1408000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (704000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (352000) in fft_filter_ccc_sptr final filter and decimate by 2 using 65 taps (1040000) in fft_filter_ccc_sptr No final resampler stage.""")
def test_float_rates(self): # Either float or int rates should be accepted f = MultistageChannelFilter(input_rate=32000000.0, output_rate=16000.0, cutoff_freq=3000, transition_width=1200) self.__run(f, 400000, 16000 / 32000000, """\ 7 stages from 32000000 to 16000 freq xlate and decimate by 5 using 25 taps (160000000) in freq_xlating_fir_filter_ccc_sptr decimate by 5 using 25 taps (32000000) in fft_filter_ccc_sptr decimate by 5 using 25 taps (6400000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (1408000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (704000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (352000) in fft_filter_ccc_sptr final filter and decimate by 2 using 65 taps (1040000) in fft_filter_ccc_sptr No final resampler stage.""")
def test_decimating(self): """Sample problematic decimation case""" # TODO: Test filter functionality more f = MultistageChannelFilter(input_rate=8000000, output_rate=48000, cutoff_freq=10000, transition_width=5000) self.__run(f, 400000, 48000 / 8000000, """\ 8 stages from 8000000 to 48000 freq xlate and decimate by 2 using 9 taps (36000000) in freq_xlating_fir_filter_ccc_sptr decimate by 2 using 9 taps (18000000) in fir_filter_ccc_sptr decimate by 2 using 9 taps (9000000) in fir_filter_ccc_sptr decimate by 2 using 9 taps (4500000) in fir_filter_ccc_sptr decimate by 2 using 11 taps (2750000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (1375000) in fft_filter_ccc_sptr final filter and decimate by 2 using 61 taps (3812500) in fft_filter_ccc_sptr rational_resampler by 96/125 (stage rates 48000/62500) using 4128 taps (198144000) in rational_resampler_base_ccf_sptr""")
def test_center_freq_interpolating(self): f = MultistageChannelFilter(input_rate=1000, output_rate=10000, cutoff_freq=400, transition_width=200, center_freq=1) self.assertEqual(f.get_center_freq(), 1) f.set_center_freq(2) self.assertEqual(f.get_center_freq(), 2)
def __init__(self, mode, input_rate=0, context=None): assert input_rate > 0 gr.hier_block2.__init__( self, 'RTTY demodulator', gr.io_signature(1, 1, gr.sizeof_gr_complex * 1), gr.io_signature(1, 1, gr.sizeof_float * 1), ) self.__text = u'' baud = _DEFAULT_BAUD # TODO param self.baud = baud demod_rate = 6000 # TODO optimize this value self.samp_rate = demod_rate # TODO rename self.__channel_filter = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=self.__filter_high, transition_width=self.__transition) # TODO optimize filter band self.__sharp_filter = grfilter.fir_filter_ccc( 1, firdes.complex_band_pass(1.0, demod_rate, self.__filter_low, self.__filter_high, self.__transition, firdes.WIN_HAMMING)) self.fsk_demod = RTTYFSKDemodulator(input_rate=demod_rate, baud=baud) self.__real = blocks.complex_to_real(vlen=1) self.__char_queue = gr.msg_queue(limit=100) self.char_sink = blocks.message_sink(gr.sizeof_char, self.__char_queue, True) self.connect( self, self.__channel_filter, self.__sharp_filter, self.fsk_demod, rtty.rtty_decode_ff(rate=demod_rate, baud=baud, polarity=False), self.char_sink) self.connect( self.__sharp_filter, self.__real, self)
def __init__(self, demod_rate=0, audio_rate=0, band_filter=None, band_filter_transition=None, stereo=False, **kwargs): assert audio_rate > 0 self.__signal_type = SignalType( kind='STEREO' if stereo else 'MONO', sample_rate=audio_rate) Demodulator.__init__(self, **kwargs) SquelchMixin.__init__(self, demod_rate) self.demod_rate = demod_rate self.audio_rate = audio_rate input_rate = self.input_rate self.band_filter_block = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=band_filter, transition_width=band_filter_transition)
def __init__(self, mode, input_rate, context): channels = 2 audio_rate = 10000 gr.hier_block2.__init__( self, str('%s demodulator' % (mode,)), gr.io_signature(1, 1, gr.sizeof_gr_complex), gr.io_signature(1, 1, gr.sizeof_float * channels)) self.__input_rate = input_rate self.__rec_freq_input = 0.0 self.__signal_type = SignalType(kind='STEREO', sample_rate=audio_rate) # Using agc2 rather than feedforward AGC for efficiency, because this runs at the RF rate rather than the audio rate. agc_block = analog.agc2_cc(reference=dB(-8)) agc_block.set_attack_rate(8e-3) agc_block.set_decay_rate(8e-3) agc_block.set_max_gain(dB(40)) self.connect( self, agc_block) channel_joiner = blocks.streams_to_vector(gr.sizeof_float, channels) self.connect(channel_joiner, self) for channel in six.moves.range(0, channels): self.connect( agc_block, grfilter.fir_filter_ccc(1, design_sawtooth_filter(decreasing=channel == 0)), blocks.complex_to_mag(1), blocks.float_to_complex(), # So we can use the complex-input band filter. TODO eliminate this for efficiency MultistageChannelFilter( input_rate=input_rate, output_rate=audio_rate, cutoff_freq=5000, transition_width=5000), blocks.complex_to_real(), # assuming below 40Hz is not of interest grfilter.dc_blocker_ff(audio_rate // 40, False), (channel_joiner, channel))
class RTL433Demodulator(gr.hier_block2, ExportedState): __log = Logger() # TODO: log to context/client def __init__(self, mode='433', input_rate=0, context=None): assert input_rate > 0 assert context is not None gr.hier_block2.__init__(self, type(self).__name__, gr.io_signature(1, 1, gr.sizeof_gr_complex), gr.io_signature(0, 0, 0)) # The input bandwidth chosen is not primarily determined by the bandwidth of the input signals, but by the frequency error of the transmitters. Therefore it is not too critical, and we can choose the exact rate to make the filtering easy. if input_rate <= upper_preferred_demod_rate: # Skip having a filter at all. self.__band_filter = None demod_rate = input_rate else: # TODO: This gunk is very similar to the stuff that MultistageChannelFilter does. See if we can share some code. lower_rate = input_rate lower_rate_prev = None while lower_rate > upper_preferred_demod_rate and lower_rate != lower_rate_prev: lower_rate_prev = lower_rate if lower_rate % 5 == 0 and lower_rate > upper_preferred_demod_rate * 3: lower_rate /= 5 elif lower_rate % 2 == 0: lower_rate /= 2 else: # non-integer ratio lower_rate = upper_preferred_demod_rate break demod_rate = lower_rate self.__band_filter = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=demod_rate * 0.4, transition_width=demod_rate * 0.2) # Subprocess # using /usr/bin/env because twisted spawnProcess doesn't support path search # pylint: disable=no-member self.__process = the_reactor.spawnProcess( RTL433ProcessProtocol(context.output_message, self.__log), '/usr/bin/env', env=None, # inherit environment args=[ b'env', b'rtl_433', b'-F', b'json', b'-r', b'-', # read from stdin b'-m', b'3', # complex float input b'-s', str(demod_rate), b'-q' # quiet mode, suppress "Registering protocol..." stderr flood ], childFDs={ 0: 'w', 1: 'r', 2: 2 }) sink = make_sink_to_process_stdin(self.__process, itemsize=gr.sizeof_gr_complex) agc = analog.agc2_cc(reference=dB(-4)) agc.set_attack_rate(200 / demod_rate) agc.set_decay_rate(200 / demod_rate) if self.__band_filter: self.connect(self, self.__band_filter, agc) else: self.connect(self, agc) self.connect(agc, sink) def _close(self): # TODO: This never gets called except in tests. Do this better, like by having an explicit life cycle for demodulators. self.__process.loseConnection() @exported_value(type=BandShape, changes='never') def get_band_shape(self): """implements IDemodulator""" if self.__band_filter: return self.__band_filter.get_shape() else: # TODO Reuse UnselectiveAMDemodulator's approach to this return BandShape(stop_low=0, pass_low=0, pass_high=0, stop_high=0, markers={}) def get_output_type(self): """implements IDemodulator""" return no_signal
def __init__(self, mode='433', input_rate=0, context=None): assert input_rate > 0 assert context is not None gr.hier_block2.__init__( self, type(self).__name__, gr.io_signature(1, 1, gr.sizeof_gr_complex), gr.io_signature(0, 0, 0)) # The input bandwidth chosen is not primarily determined by the bandwidth of the input signals, but by the frequency error of the transmitters. Therefore it is not too critical, and we can choose the exact rate to make the filtering easy. if input_rate <= upper_preferred_demod_rate: # Skip having a filter at all. self.__band_filter = None demod_rate = input_rate else: # TODO: This gunk is very similar to the stuff that MultistageChannelFilter does. See if we can share some code. lower_rate = input_rate lower_rate_prev = None while lower_rate > upper_preferred_demod_rate and lower_rate != lower_rate_prev: lower_rate_prev = lower_rate if lower_rate % 5 == 0 and lower_rate > upper_preferred_demod_rate * 3: lower_rate /= 5 elif lower_rate % 2 == 0: lower_rate /= 2 else: # non-integer ratio lower_rate = upper_preferred_demod_rate break demod_rate = lower_rate self.__band_filter = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=demod_rate * 0.4, transition_width=demod_rate * 0.2) # Subprocess # using /usr/bin/env because twisted spawnProcess doesn't support path search # pylint: disable=no-member process = the_reactor.spawnProcess( RTL433ProcessProtocol(context.output_message), '/usr/bin/env', env=None, # inherit environment args=[ 'env', 'rtl_433', '-F', 'json', '-r', '-', # read from stdin '-m', '3', # complex float input '-s', str(demod_rate), ], childFDs={ 0: 'w', 1: 'r', 2: 2 }) sink = make_sink_to_process_stdin(process, itemsize=gr.sizeof_gr_complex) agc = analog.agc2_cc(reference=dB(-4)) agc.set_attack_rate(200 / demod_rate) agc.set_decay_rate(200 / demod_rate) if self.__band_filter: self.connect( self, self.__band_filter, agc) else: self.connect( self, agc) self.connect(agc, sink)
def __init__(self, mode='433', input_rate=0, context=None): assert input_rate > 0 assert context is not None gr.hier_block2.__init__(self, type(self).__name__, gr.io_signature(1, 1, gr.sizeof_gr_complex), gr.io_signature(0, 0, 0)) # The input bandwidth chosen is not primarily determined by the bandwidth of the input signals, but by the frequency error of the transmitters. Therefore it is not too critical, and we can choose the exact rate to make the filtering easy. if input_rate <= upper_preferred_demod_rate: # Skip having a filter at all. self.__band_filter = None demod_rate = input_rate else: # TODO: This gunk is very similar to the stuff that MultistageChannelFilter does. See if we can share some code. lower_rate = input_rate lower_rate_prev = None while lower_rate > upper_preferred_demod_rate and lower_rate != lower_rate_prev: lower_rate_prev = lower_rate if lower_rate % 5 == 0 and lower_rate > upper_preferred_demod_rate * 3: lower_rate /= 5 elif lower_rate % 2 == 0: lower_rate /= 2 else: # non-integer ratio lower_rate = upper_preferred_demod_rate break demod_rate = lower_rate self.__band_filter = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=demod_rate * 0.4, transition_width=demod_rate * 0.2) # Subprocess # using /usr/bin/env because twisted spawnProcess doesn't support path search # pylint: disable=no-member process = the_reactor.spawnProcess( RTL433ProcessProtocol(context.output_message), '/usr/bin/env', env=None, # inherit environment args=[ 'env', 'rtl_433', '-F', 'json', '-r', '-', # read from stdin '-m', '3', # complex float input '-s', str(demod_rate), ], childFDs={ 0: 'w', 1: 'r', 2: 2 }) sink = make_sink_to_process_stdin(process, itemsize=gr.sizeof_gr_complex) agc = analog.agc2_cc(reference=dB(-4)) agc.set_attack_rate(200 / demod_rate) agc.set_decay_rate(200 / demod_rate) if self.__band_filter: self.connect(self, self.__band_filter, agc) else: self.connect(self, agc) self.connect(agc, sink)
class ModeSDemodulator(gr.hier_block2, ExportedState): def __init__(self, mode='MODE-S', input_rate=0, context=None): assert input_rate > 0 gr.hier_block2.__init__( self, type(self).__name__, gr.io_signature(1, 1, gr.sizeof_gr_complex * 1), gr.io_signature(0, 0, 0)) demod_rate = 2000000 transition_width = 500000 hex_msg_queue = gr.msg_queue(100) self.__band_filter = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=demod_rate / 2, transition_width=transition_width) # TODO optimize filter band self.__demod = air_modes.rx_path( rate=demod_rate, threshold=7.0, # default used in air-modes code but not exposed queue=hex_msg_queue, use_pmf=False, use_dcblock=True) self.connect( self, self.__band_filter, self.__demod) self.__messages_seen = 0 self.__message_rate_calc = LazyRateCalculator(lambda: self.__messages_seen, min_interval=2) # Parsing # TODO: These bits are mimicking gr-air-modes toplevel code. Figure out if we can have less glue. # Note: gr pubsub is synchronous -- subscribers are called on the publisher's thread parser_output = gr.pubsub.pubsub() parser = air_modes.make_parser(parser_output) cpr_decoder = air_modes.cpr_decoder(my_location=None) # TODO: get position info from device air_modes.output_print(cpr_decoder, parser_output) def msq_runner_callback(msg): # called on msgq_runner's thread # pylint: disable=broad-except try: reactor.callFromThread(parser, msg.to_string()) except Exception: print traceback.format_exc() self.__msgq_runner = gru.msgq_runner(hex_msg_queue, msq_runner_callback) def parsed_callback(msg): timestamp = time.time() self.__messages_seen += 1 context.output_message(ModeSMessageWrapper(msg, cpr_decoder, timestamp)) for i in xrange(0, 2 ** 5): parser_output.subscribe('type%i_dl' % i, parsed_callback) def __del__(self): self.__msgq_runner.stop() @exported_value(type=RangeT([(0, 30)], unit=units.dB), changes='this_setter', label='Decode threshold') def get_decode_threshold(self): return self.__demod.get_threshold() @setter def set_decode_threshold(self, value): self.__demod.set_threshold(float(value)) @exported_value(float, changes='continuous', label='Messages/sec decoded') def get_message_rate(self): return round(self.__message_rate_calc.get(), 1) def get_output_type(self): return no_signal @exported_value(type=BandShape, changes='never') def get_band_shape(self): return self.__band_filter.get_shape()
class ModeSDemodulator(gr.hier_block2, ExportedState): implements(IDemodulator) def __init__(self, mode='MODE-S', input_rate=0, context=None): assert input_rate > 0 gr.hier_block2.__init__( self, 'Mode S/ADS-B/1090 demodulator', gr.io_signature(1, 1, gr.sizeof_gr_complex * 1), gr.io_signature(0, 0, 0)) demod_rate = 2000000 transition_width = 500000 hex_msg_queue = gr.msg_queue(100) self.__band_filter = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=demod_rate / 2, transition_width=transition_width) # TODO optimize filter band self.__demod = air_modes.rx_path( rate=demod_rate, threshold=7.0, # default used in air-modes code but not exposed queue=hex_msg_queue, use_pmf=False, use_dcblock=True) self.connect(self, self.__band_filter, self.__demod) self.__messages_seen = 0 self.__message_rate_calc = LazyRateCalculator( lambda: self.__messages_seen, min_interval=2) # Parsing # TODO: These bits are mimicking gr-air-modes toplevel code. Figure out if we can have less glue. # Note: gr pubsub is synchronous -- subscribers are called on the publisher's thread parser_output = gr.pubsub.pubsub() parser = air_modes.make_parser(parser_output) cpr_decoder = air_modes.cpr_decoder( my_location=None) # TODO: get position info from device air_modes.output_print(cpr_decoder, parser_output) def callback(msg): # called on msgq_runner's thrad # pylint: disable=broad-except try: reactor.callFromThread(parser, msg.to_string()) except Exception: print traceback.format_exc() self.__msgq_runner = gru.msgq_runner(hex_msg_queue, callback) def parsed_callback(msg): timestamp = time.time() self.__messages_seen += 1 context.output_message( ModeSMessageWrapper(msg, cpr_decoder, timestamp)) for i in xrange(0, 2**5): parser_output.subscribe('type%i_dl' % i, parsed_callback) def __del__(self): self.__msgq_runner.stop() @exported_value(type=Range([(0, 30)]), changes='this_setter', label='Decode threshold') def get_decode_threshold(self): return self.__demod.get_threshold(None) @setter def set_decode_threshold(self, value): self.__demod.set_threshold(float(value)) @exported_value(float, changes='continuous', label='Messages/sec decoded') def get_message_rate(self): return round(self.__message_rate_calc.get(), 1) def can_set_mode(self, mode): return False def get_output_type(self): return no_signal @exported_value(changes='never') def get_band_filter_shape(self): return self.__band_filter.get_shape()
def test_setters(self): # TODO: Test filter functionality; this only tests that the operations work filt = MultistageChannelFilter(input_rate=32000000, output_rate=16000, cutoff_freq=3000, transition_width=1200) filt.set_cutoff_freq(2900) filt.set_transition_width(1000) filt.set_center_freq(10000) self.assertEqual(2900, filt.get_cutoff_freq()) self.assertEqual(1000, filt.get_transition_width()) self.assertEqual(10000, filt.get_center_freq()) self.assertEqual(filt.explain(), textwrap.dedent("""\ 7 stages from 32000000 to 16000 freq xlate and decimate by 5 using 25 taps (160000000) in freq_xlating_fir_filter_ccc_sptr decimate by 5 using 25 taps (32000000) in fft_filter_ccc_sptr decimate by 5 using 25 taps (6400000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (1408000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (704000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (352000) in fft_filter_ccc_sptr final filter and decimate by 2 using 77 taps (1232000) in fft_filter_ccc_sptr No final resampler stage."""))
class RTL433Demodulator(gr.hier_block2, ExportedState): __log = Logger() # TODO: log to context/client def __init__(self, mode='433', input_rate=0, context=None): assert input_rate > 0 assert context is not None gr.hier_block2.__init__( self, type(self).__name__, gr.io_signature(1, 1, gr.sizeof_gr_complex), gr.io_signature(0, 0, 0)) # The input bandwidth chosen is not primarily determined by the bandwidth of the input signals, but by the frequency error of the transmitters. Therefore it is not too critical, and we can choose the exact rate to make the filtering easy. if input_rate <= upper_preferred_demod_rate: # Skip having a filter at all. self.__band_filter = None demod_rate = input_rate else: # TODO: This gunk is very similar to the stuff that MultistageChannelFilter does. See if we can share some code. lower_rate = input_rate lower_rate_prev = None while lower_rate > upper_preferred_demod_rate and lower_rate != lower_rate_prev: lower_rate_prev = lower_rate if lower_rate % 5 == 0 and lower_rate > upper_preferred_demod_rate * 3: lower_rate /= 5 elif lower_rate % 2 == 0: lower_rate /= 2 else: # non-integer ratio lower_rate = upper_preferred_demod_rate break demod_rate = lower_rate self.__band_filter = MultistageChannelFilter( input_rate=input_rate, output_rate=demod_rate, cutoff_freq=demod_rate * 0.4, transition_width=demod_rate * 0.2) # Subprocess # using /usr/bin/env because twisted spawnProcess doesn't support path search # pylint: disable=no-member self.__process = the_reactor.spawnProcess( RTL433ProcessProtocol(context.output_message, self.__log), '/usr/bin/env', env=None, # inherit environment # These arguments were last reviewed for rtl_433 18.12-142-g6c3ca9b args=[ b'env', b'rtl_433', b'-F', b'json', # output format b'-r', str(demod_rate) + b'sps:iq:cf32:-', # specify input format and to use stdin b'-M', 'newmodel', ], childFDs={ 0: 'w', 1: 'r', 2: 2 }) sink = make_sink_to_process_stdin(self.__process, itemsize=gr.sizeof_gr_complex) agc = analog.agc2_cc(reference=dB(-4)) agc.set_attack_rate(200 / demod_rate) agc.set_decay_rate(200 / demod_rate) if self.__band_filter: self.connect( self, self.__band_filter, agc) else: self.connect( self, agc) self.connect(agc, sink) def _close(self): # TODO: This never gets called except in tests. Do this better, like by having an explicit life cycle for demodulators. self.__process.loseConnection() @exported_value(type=BandShape, changes='never') def get_band_shape(self): """implements IDemodulator""" if self.__band_filter: return self.__band_filter.get_shape() else: # TODO Reuse UnselectiveAMDemodulator's approach to this return BandShape(stop_low=0, pass_low=0, pass_high=0, stop_high=0, markers={}) def get_output_type(self): """implements IDemodulator""" return no_signal
def test_setters(self): # TODO: Test filter functionality; this only tests that the operations work filt = MultistageChannelFilter(input_rate=32000000, output_rate=16000, cutoff_freq=3000, transition_width=1200) filt.set_cutoff_freq(2900) filt.set_transition_width(1000) filt.set_center_freq(10000) self.assertEqual(2900, filt.get_cutoff_freq()) self.assertEqual(1000, filt.get_transition_width()) self.assertEqual(10000, filt.get_center_freq()) self.assertEqual( filt.explain(), textwrap.dedent("""\ 7 stages from 32000000 to 16000 freq xlate and decimate by 5 using 25 taps (160000000) in freq_xlating_fir_filter_ccc_sptr decimate by 5 using 25 taps (32000000) in fft_filter_ccc_sptr decimate by 5 using 25 taps (6400000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (1408000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (704000) in fft_filter_ccc_sptr decimate by 2 using 11 taps (352000) in fft_filter_ccc_sptr final filter and decimate by 2 using 77 taps (1232000) in fft_filter_ccc_sptr No final resampler stage."""))