"""
Embedded Python Blocks:

Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. The arguments to __init__  will
be the parameters. All of them are required to have default values!
"""


import numpy as np
from gnuradio import gr
import pmt
import random 
import math
from itertools import islice 

class ener_det(gr.basic_block):
    """
    docstring for block set_gain
    """
    def __init__(self, num_values=1024, Vec_Length=1024):
        gr.basic_block.__init__(self,
            name="ED_block",
            in_sig=[np.float32],
            out_sig=[])
     #self.Vec_Length = Vec_Length                 
        self.num_values = num_values
        self.seen = 0       #keeps a count of how many samples have been seen
        
    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):
        in0 = input_items[0]
        self.seen+=in0.shape[0]
        print "Number of input items:", self.seen
        if self.seen>self.num_values:	
            in1=islice(in0, 1024)    #1024 values are taken from the variable in0 and stored in in1
## Each item in the list of 1024 elements in in1 is converted to dB and then compared to a threshold 
	    for i in in1:
               # b = i/1024
                c = math.log10(i/1024)  #converts 1024 values stored in in1 to dB and stores them in c      
		if i > 70:	   #checks if any of the values stored in c are greater than a threshold value            
              
                     print "A signal is present"
                           
                else:
                     print "No signal is present"	      
            
            self.seen = 0
        self.consume_each(in0.shape[0]) #consume everything you've account for
                                    # tell system to move on to next samples

        return 0        ##return 0 samples generated
                        ## as the block doesn't have an output stream


