'''Convert an arbitrary frequency in the AM broadcast band to the frequency of the nearest radio station frequency. Mike Markowski, mike.ab3ap@gmail.com Feb 2021 ''' import numpy as np from gnuradio import gr import pmt class snapBlock(gr.basic_block): def __init__(self): gr.basic_block.__init__( self, name="Snap to Station", in_sig=None, out_sig=None) # User input that is only somewhere near a station frequency. self.message_port_register_in(pmt.intern('raw_Hz_in')) self.set_msg_handler(pmt.intern('raw_Hz_in'), self.stationSnap) # Output in Hz and kHz that is exactly a station frequency. self.message_port_register_out(pmt.intern('snapped_Hz_out')) self.message_port_register_out(pmt.intern('snapped_kHz_out')) def stationSnap(self, msg): '''Accept a pmt pair with user selected frequency information and adjust it to the nearest broadcast AM frequency. Inputs: msg (dictionary) : assumed to contain only: (freq . ) Outputs: freq_Hz_out (pmt.dict) : (freq . snappedFreq) where snappedFreq is input frequency in Hz moved to nearest broadcast frequency. freq_kHz_out (pmt.dict) : (freq . snappedFreq) where snappedFreq is input frequency in kHz moved to nearest broadcast frequency. ''' # Disassemble pmt pair. cmd = pmt.car(msg) freq = pmt.cdr(msg) freqRaw_kHz = pmt.to_python(freq) / 1e3 # Easier to work in kHz. # Snap user-clicked freq to nearest AM station. spacing = 10 # N. American broadcast AM spacing in kHz. freq_kHz = float(round(freqRaw_kHz / spacing) * spacing) freq_Hz = freq_kHz * 1e3 # Generate output message, (freq . freq_Hz). pair = pmt.cons(cmd, pmt.to_pmt(freq_Hz)) self.message_port_pub(pmt.intern('snapped_Hz_out'), pair) # Generate output message, (freq . freq_kHz). pair2 = pmt.cons(cmd, pmt.to_pmt(freq_kHz)) self.message_port_pub(pmt.intern('snapped_kHz_out'), pair2)