Files
GnuRadio/gr-jay/python/peak_manager.py
T
jens fa9e33cc37 - peak detect: update of peaks without smooting
- send message of existent peak changes (frequency, pos)

- peak manager: added freq message output for channelizer

git-svn-id: http://moon:8086/svn/software/trunk/projects/GnuRadio@493 b431acfa-c32f-4a4a-93f1-934dc6c82436
2019-05-28 19:11:33 +00:00

133 lines
3.6 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2019 Jay Arrowfield.
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
import numpy
import pmt
from gnuradio import gr
class peak_manager(gr.basic_block):
"""
docstring for block peak_manager
"""
def __init__(self, test):
gr.basic_block.__init__(self,
name="peak_manager",
in_sig=None,
out_sig=None)
self.test = test
self.numFrequencies = 1
self.peak_dict = {}
print ("test : ", test)
# Register in / out message ports
self.message_port_register_in(pmt.intern("control"))
self.message_port_register_out(pmt.intern("status"))
self.message_port_register_out(pmt.intern("freq"))
# Register in message handler
self.set_msg_handler(pmt.intern("control"), self.msg_handler)
def peak_add(self, key, peak):
if len(self.peak_dict) < self.numFrequencies:
self.peak_dict[key] = peak
return True
return False
def peak_del(self, key):
if key in self.peak_dict:
self.peak_dict.pop(key, None)
return True
return False
def peak_get(self, key):
result = None
if key in self.peak_dict:
result = self.peak_dict[key]
return result
def msg_handler(self, msg):
if pmt.is_dict(msg):
items = pmt.dict_items(msg)
peak = {}
for n in range(0, pmt.length(items)):
pair = pmt.nth(n, items)
key = pmt.car(pair)
val = pmt.cdr(pair)
key_str = pmt.symbol_to_string(key)
if pmt.is_integer(val):
value = pmt.to_long(val)
peak[key_str] = value
# print("{}={}".format(key_str, value))
elif pmt.is_real(val):
value = pmt.to_float(val)
peak[key_str] = value
# print("{}={}".format(key_str, value))
elif pmt.is_bool(val):
value = pmt.to_bool(val)
peak[key_str] = value
# print("{}={}".format(key_str, value))
id = peak['id']
freq_MHz = peak['freq_Hz']*1e-6
if peak['state'] == 1:
print ("New peak #{} at {:0.2f} MHz".format(id, freq_MHz))
peak_key = 'peak' + str(id)
peak0 = self.peak_get(peak_key)
if peak0 is not None:
pair = pmt.cons(pmt.intern("freq"), pmt.from_float(peak['freq_Hz']))
print("Update " + peak_key)
self.message_port_pub(pmt.intern("freq"), pair)
elif self.peak_add(peak_key, peak):
pair = pmt.cons(pmt.intern("freq"), pmt.from_float(peak['freq_Hz']))
print("Added " + peak_key)
self.message_port_pub(pmt.intern("freq"), pair)
else:
print ("Lost peak #{} at {:0.2f} MHz".format(id, freq_MHz))
peak_key = 'peak' + str(id)
if self.peak_del(peak_key):
print("Deleted " + peak_key)
else:
print(pmt.symbol_to_string(msg))
def forecast(self, noutput_items, ninput_items_required):
#setup size of input_items[i] for work call
for i in range(len(ninput_items_required)):
ninput_items_required[i] = noutput_items
def general_work(self, input_items, output_items):
output_items[0][:] = input_items[0]
consume(0, len(input_items[0]))
#self.consume_each(len(input_items[0]))
return len(output_items[0])