discuss-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Discuss-gnuradio] Float value loses precision


From: zianggaowireless
Subject: [Discuss-gnuradio] Float value loses precision
Date: Mon, 8 Aug 2016 01:37:37 -0400

Hello,
   I'm writing a block doing signal processing and then append the current time 
and output to a file sink, however, I found the output really strange because 
all time values are the same. Then I tried to write another block to test with 
my time value. Here is the block and qa file
#######################
import numpy as np
from gnuradio import gr

class time_test(gr.sync_block):
    """
    docstring for block time_test
    """
    def __init__(self, vlen):
        self.vlen = vlen
        gr.sync_block.__init__(self,
            name="time_test",
            in_sig=[(np.float32,self.vlen)],
            out_sig=[np.float32])


    def work(self, input_items, output_items):
        in0 = input_items[0]
        print 'in0',in0
        time = in0[0]
        print 'time',time
        # <+signal processing here+>
        output_items[0][:] = time
        return len(output_items[0])
#########################
from gnuradio import gr, gr_unittest
from gnuradio import blocks
from time_test import time_test
import timeit

class qa_time_test (gr_unittest.TestCase):

    def setUp (self):
        self.tb = gr.top_block ()

    def tearDown (self):
        self.tb = None

    def test_001_t (self):
        # set up fg
        start = timeit.default_timer()
        src_data = [start]
        print 'src_data', src_data
        expected_result = [start]
        src = blocks.vector_source_f (src_data, False, vlen=1)
        res = time_test(1)
        snk = blocks.vector_sink_f (vlen = 1)
        self.tb.connect (src, res)
        self.tb.connect (res, snk)
        self.tb.run ()
        result_data = snk.data()
        print 'result_data',result_data
        # check data
        self.assertFloatTuplesAlmostEqual (expected_result, result_data, 6)
        
if __name__ == '__main__':
    gr_unittest.run(qa_time_test, "qa_time_test.xml")
############################
And here is the output:

src_data [1470603836.297275]
in0 [  1.47060378e+09]
time 1.4706e+09
result_data (1470603776.0,)

Ran 1 test in 0.002s

FAILED (failures=1)


It's a very strange issue and actually a float32 should have enough precision 
to handle my time value, I tried different float number and here's other float 
values I input:

src_data [0.297275]
in0 [ 0.29727501]
time 0.297275
result_data (0.29727500677108765,)

Ran 1 test in 0.002s

OK

----------------------------------------------------------------
src_data [836.297275]
in0 [ 836.29730225]
time 836.297
result_data (836.2973022460938,)

Ran 1 test in 0.003s

FAILED (failures=1)

Did I missed something when I read the input from gnuradio? Could anyone help 
me with this issue?

Thanks!

Best regards,
Ziang Gao.


Sent from my iPhone


reply via email to

[Prev in Thread] Current Thread [Next in Thread]