[PATCH] RTL-SDR: convert _lut to float[] to reduce size by a factor of 256

Krzysztof Halasa khc at pm.waw.pl
Wed Aug 24 21:53:39 UTC 2016


The _lut is being indexed by I + Q (16 bits = 65536 entries), however
both samples can be processed independently, resulting in 8-bit LUT.
Saves a bit of RAM and CPU cache.

diff --git a/lib/rtl/rtl_source_c.cc b/lib/rtl/rtl_source_c.cc
index 93328b8..999ff9e 100644
--- a/lib/rtl/rtl_source_c.cc
+++ b/lib/rtl/rtl_source_c.cc
@@ -172,15 +172,8 @@ rtl_source_c::rtl_source_c (const std::string &args)
   _samp_avail = _buf_len / BYTES_PER_SAMPLE;
 
   // create a lookup table for gr_complex values
-  for (unsigned int i = 0; i <= 0xffff; i++) {
-#ifdef BOOST_LITTLE_ENDIAN
-    _lut.push_back( gr_complex( (float(i & 0xff) - 127.4f) * (1.0f/128.0f),
-                                (float(i >> 8) - 127.4f) * (1.0f/128.0f) ) );
-#else // BOOST_BIG_ENDIAN
-    _lut.push_back( gr_complex( (float(i >> 8) - 127.4f) * (1.0f/128.0f),
-                                (float(i & 0xff) - 127.4f) * (1.0f/128.0f) ) );
-#endif
-  }
+  for (unsigned int i = 0; i < 0x100; i++)
+    _lut.push_back((i - 127.4f) / 128.0f);
 
   _dev = NULL;
   ret = rtlsdr_open( &_dev, dev_index );
@@ -230,11 +223,11 @@ rtl_source_c::rtl_source_c (const std::string &args)
 
   set_if_gain( 24 ); /* preset to a reasonable default (non-GRC use case) */
 
-  _buf = (unsigned short **) malloc(_buf_num * sizeof(unsigned short *));
+  _buf = (unsigned char **)malloc(_buf_num * sizeof(unsigned char *));
 
   if (_buf) {
     for(unsigned int i = 0; i < _buf_num; ++i)
-      _buf[i] = (unsigned short *) malloc(_buf_len);
+      _buf[i] = (unsigned char *)malloc(_buf_len);
   }
 }
 
@@ -348,10 +341,10 @@ int rtl_source_c::work( int noutput_items,
 
   while (noutput_items && _buf_used) {
     const int nout = std::min(noutput_items, _samp_avail);
-    const unsigned short *buf = _buf[_buf_head] + _buf_offset;
+    const unsigned char *buf = _buf[_buf_head] + _buf_offset * 2;
 
     for (int i = 0; i < nout; ++i)
-      *out++ = _lut[ *(buf + i) ];
+      *out++ = gr_complex(_lut[buf[i * 2]], _lut[buf[i * 2 + 1]]);
 
     noutput_items -= nout;
     _samp_avail -= nout;
diff --git a/lib/rtl/rtl_source_c.h b/lib/rtl/rtl_source_c.h
index 76de400..902b386 100644
--- a/lib/rtl/rtl_source_c.h
+++ b/lib/rtl/rtl_source_c.h
@@ -122,11 +122,11 @@ private:
   static void _rtlsdr_wait(rtl_source_c *obj);
   void rtlsdr_wait();
 
-  std::vector<gr_complex> _lut;
+  std::vector<float> _lut;
 
   rtlsdr_dev_t *_dev;
   gr::thread::thread _thread;
-  unsigned short **_buf;
+  unsigned char **_buf;
   unsigned int _buf_num;
   unsigned int _buf_len;
   unsigned int _buf_head;


More information about the osmocom-sdr mailing list