2

I am trying to pass the sampled/capture IQ data from my SDR through my Python FM demodulator.

Right now, the results I am getting is a continuous stuttering loops of short audio samples. I which to have it play continuously just like a normal FM radio.

Any recommendation on how I can make this work?

#!/usr/bin/env python

import numpy as np
import scipy.signal as signal
from rtlsdr import RtlSdr
import sounddevice as sd

device_index = RtlSdr.get_device_index_by_serial('00000001')

class fmDemodulator(object):
    def __init__(self, sdr=None):
        self.sdr = sdr if sdr else RtlSdr(device_index)

    def demod(self, *args):
        Fs = self.sdr.sample_rate

        # Read IQ samples
        samples = self.sdr.read_samples(256*1024)
        print(samples)
        # Convert sampled data into numpy array
        x1 = np.array(samples).astype("complex64")

        # Downmixed Baseband Signal (Adjust offset to be centered)
        offsetFreq = 0    # already centered
        fc1 = np.exp(-1.0j*2.0*np.pi* offsetFreq/Fs*np.arange(len(x1)))  
        x2 = x1 * fc1  
        # Filter and downsample the FM Radio Signal
        bwFM = 200000   # approx. 170 kHz for a single channel
        decRate = int(Fs/bwFM)
        x3 = signal.decimate(x2, decRate)
        newFs = Fs/decRate
        ### Demodulate 200kHz FM Signal
        # Polar discriminator
        y4 = x3[1:] * np.conj(x3[:-1])  
        x4 = np.angle(y4)  
        # The de-emphasis filter
        # Given a signal 'x4' (in a numpy array) with sampling rate newFS
        d = newFs * 75e-6   # Calculate the # of samples to hit the -3dB point  
        x = np.exp(-1/d)   # Calculate the decay between each sample  
        b = [1-x]          # Create the filter coefficients  
        a = [1,-x]  
        x5 = signal.lfilter(b,a,x4)  

        # Find a decimation rate to achieve audio sampling rate between 44-48 kHz
        audioFreq = 44100.0  
        dec_audio = int(newFs/audioFreq)  
        audioFs = newFs/dec_audio
        print(audioFreq)
        x6 = signal.decimate(x5, dec_audio) 

        # Scale audio to adjust volume
        x6 *= 10000 / np.max(np.abs(x6))  

        return x6, audioFs

    def start(self):
        while True:
            x6, audioFs = self.demod()
            sd.play(x6, audioFs)

        return


def main():
    sdr = RtlSdr(device_index)
    fm = fmDemodulator(sdr)

    # SDR Configurations
    sdr.sample_rate = int(2.4e6)        # Hz
    sdr.center_freq = int(102e6)     # Hz
    sdr.freq_correction = 77            # PPM +- 20
    sdr.gain = 'auto'

    fm.start()

    # Clean up
    sdr.close()
    del(sdr)

if __name__ == '__main__':
    main()

Thanks in advance!

5
  • 3
    sd.play() is not suitable for gapless playback. You should instead use the write() method of an sd.OutputStream or, probably even better, you should write your own callback function and use it with an OutputStream.
    – Matthias
    Dec 10, 2018 at 19:35
  • 2
    I seem to not be able to understand sounddevice and switching to PyAudio. Maybe I should also try to figure out how to stream the IQ sample first. I will post an update when I have any progress.
    – Seaarmy
    Dec 11, 2018 at 3:12
  • 2
    The sounddevice module can do the same as PyAudio can. It just has some more functions that seem to have distracted you. But you are of course free to choose whichever you want!
    – Matthias
    Dec 11, 2018 at 9:28
  • 1
    @Matthias Oh I have no idea on audio or even Python itself. Currently I can't seem to make it work. I guess I will keep trying and once settled will have a look at the sounddevice module!
    – Seaarmy
    Dec 12, 2018 at 5:11
  • 1
    any luck with this?
    – aheigins
    Mar 24, 2019 at 22:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.