summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Adam <_@yuv.al>2018-04-05 16:45:36 +0300
committerYuval Adam <_@yuv.al>2018-04-05 16:45:36 +0300
commit476dd7446c84d39c47f10773571babc70916dd79 (patch)
treee6ceb33430c44f9c62f45f7ec2234d535e673590
parent5211b5d543bbdad6e5de54e4a46302c3d4f2d01f (diff)
Add dump/load functionality
-rw-r--r--README.md10
-rw-r--r--numpy_fm_demod.py41
2 files changed, 41 insertions, 10 deletions
diff --git a/README.md b/README.md
index 15c1b33..e9e43a8 100644
--- a/README.md
+++ b/README.md
@@ -5,8 +5,16 @@ Just some tests with binding RTL-SDR to Python programs
## NumPy Demod
```bash
-$ python numpy_fm_demod.py
+$ python numpy_fm_demod.py capture
$ aplay wbfm-mono-50000.0.raw -r 50000 -f S16_LE -t raw -c 1
```
Note that the final sample rate can never really be 44.1kHz.
+
+Alternatively dump and load samples:
+
+```bash
+$ python numpy_fm_demod.py dump out.npy
+$ python numpy_fm_demod.py load out.npy
+$ aplay wbfm-mono-50000.0.raw -r 50000 -f S16_LE -t raw -c 1
+```
diff --git a/numpy_fm_demod.py b/numpy_fm_demod.py
index 9d4be21..f478105 100644
--- a/numpy_fm_demod.py
+++ b/numpy_fm_demod.py
@@ -1,3 +1,4 @@
+import sys
import numpy as np
import scipy.signal as signal
@@ -22,13 +23,19 @@ class NumpyFmDemod():
sdr.center_freq = self.freq - self.dc_offset
sdr.gain = 'auto'
self.samples = sdr.read_samples(self.sample_count)
+ self.samples_to_np()
sdr.close()
def load_samples(self, filename):
- with open(filename, 'rb') as f:
- self.samples = f.read()
+ self.samples = np.load(filename)
+
+ def dump_samples(self, filename):
+ np.save(filename, self.samples)
def decimate(self, rate):
+ '''
+ Utility function to decimate signal by a given rate
+ '''
self.samples = signal.decimate(self.samples, rate)
self.sample_rate /= rate
@@ -83,11 +90,10 @@ class NumpyFmDemod():
'''
self.samples *= 10000 / np.max(np.abs(self.samples))
- def output_file(self, filename):
- self.samples.astype('int16').tofile(filename)
+ def output_file(self, filename, astype='int16'):
+ self.samples.astype(astype).tofile(filename)
def demod(self):
- self.samples_to_np()
self.mix_down_dc_offset()
self.lowpass_filter()
self.polar_discriminator()
@@ -96,8 +102,25 @@ class NumpyFmDemod():
self.scale_volume()
if __name__ == '__main__':
+ if len(sys.argv) < 2:
+ print('Usage: numpy_fm_demod.py <command> [arg]\n')
+ exit()
+ else:
+ cmd = sys.argv[1]
+ try:
+ arg = sys.argv[2]
+ except:
+ arg = None
+
nfd = NumpyFmDemod(frequency=91.8e6)
- nfd.capture_samples()
- #nfd.load_samples('963fm.out')
- nfd.demod()
- nfd.output_file(f'wbfm-mono-{nfd.sample_rate}.raw')
+ if cmd == 'load':
+ nfd.load_samples(arg)
+ nfd.demod()
+ nfd.output_file(f'wbfm-mono-{nfd.sample_rate}.raw', astype='int16')
+ elif cmd == 'capture':
+ nfd.capture_samples()
+ nfd.demod()
+ nfd.output_file(f'wbfm-mono-{nfd.sample_rate}.raw', astype='int16')
+ elif cmd == 'dump':
+ nfd.capture_samples()
+ nfd.dump_samples(arg)