#!/usr/bin/env python # -*- coding: utf-8 -*- # # Copyright 2018 <+YOU OR YOUR COMPANY+>. # # 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 copy from gnuradio import gr class vave(gr.decim_block): """ Vector Average with Decimation. Only one vector is returned for N input. This block is intended to reduce the downstream CPU load. """ def __init__(self, vlen, vdecimate): gr.decim_block.__init__(self, name="vave", in_sig=[(numpy.float32, int(vlen))], out_sig=[(numpy.float32, int(vlen))], decim=int(vdecimate)) self.vlen = int(vlen) self.vdecimate = int(vdecimate) self.sum = numpy.zeros(self.vlen) self.count = 0 self.oneovern = 1./float(self.vdecimate) def forecast( self, noutput_items, ninput_items): if noutput_items == None: return self.vdecimate # print 'Forecast: ', noutput_items for i in range(len(nout_items)): ninput_items[i] = noutput_items[i]*self.vdecimate # print 'Forecast: ', ninput_items return ninput_items # return self.vdecimate def work(self, input_items, output_items): """ Work averages all input vectors and outputs one vector for all inputs """ inn = input_items[0] # get the number of input vectors n = len( input_items) # number of input PORTS (only 1) nv = len(inn) # number of vectors in this port nout = len( output_items) #number of putput ports ini = inn[0] # first input vector li = len(ini) # length of first input vector # print 'Number work vectors: ', nv, ' Length: ',li ncp = min( li, self.vlen) noutports = len( output_items) if noutports != 1: print '!!!!!!! Unexpected number of output ports: ', noutports out = output_items[0] # vectors in PORT 0 nout = len(out) # number of output vectors out0 = out[0] # get the first output vector lo = len(out0) # length of 1st output vector # print 'Number work outputs: ', nout,' Length: ',lo iout = 0 # count the number of output vectors for i in range(nv): # get the lenght of one input ini = inn[i] ncp = min( li, self.vlen) # now save this vector until all are received self.sum[0:ncp] = self.sum[0:ncp] + ini[0:ncp] self.count = self.count + 1 # indicate consumption of a vector from input if self.count >= self.vdecimate: # normalize output average self.sum = self.oneovern * self.sum # out0[:] = copy.deepcopy(self.sum) outi = out[iout] outi = self.sum out[iout] = outi iout = iout+1 # now reset the count and restart the sum self.count = 0 self.sum = numpy.zeros( self.vlen) # self.produce(0,len(output_items[0])) # self.consume_each(nv) # return no # end for all input vectors # if here, then not enough vectors input to produce an output # self.consume(0,nv) output_items[0] = out # print 'N outputs: ', len(output_items[0]), iout return len(output_items[0]) # end vave()