summaryrefslogtreecommitdiff
path: root/gr-analog/python/analog/nbfm_rx.py
blob: 129a4e9f01fb5d0b02d237219d157c37243af363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#
# Copyright 2005,2012 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
#


import math

from gnuradio import gr
from gnuradio import filter

from . import analog_python as analog
from .fm_emph import fm_deemph


class nbfm_rx(gr.hier_block2):
    """
    Narrow Band FM Receiver.

    Takes a single complex baseband input stream and produces a single
    float output stream of audio sample in the range [-1, +1].

    Args:
        audio_rate: sample rate of audio stream, >= 16k (integer)
        quad_rate: sample rate of output stream (integer)
        tau: preemphasis time constant (default 75e-6) (float)
        max_dev: maximum deviation in Hz (default 5e3) (float)

    quad_rate must be an integer multiple of audio_rate.

    Exported sub-blocks (attributes):
      squelch
      quad_demod
      deemph
      audio_filter
    """
    def __init__(self, audio_rate, quad_rate, tau=75e-6, max_dev=5e3):
        gr.hier_block2.__init__(self, "nbfm_rx",
                                gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature
                                gr.io_signature(1, 1, gr.sizeof_float))      # Output signature

        # FIXME audio_rate and quad_rate ought to be exact rationals
        self._audio_rate = audio_rate = int(audio_rate)
        self._quad_rate = quad_rate = int(quad_rate)

        if quad_rate % audio_rate != 0:
            raise ValueError("quad_rate is not an integer multiple of audio_rate")

        squelch_threshold = 20        # dB
        #self.squelch = analog.simple_squelch_cc(squelch_threshold, 0.001)

        # FM Demodulator  input: complex; output: float
        k = quad_rate / (2*math.pi*max_dev)
        self.quad_demod = analog.quadrature_demod_cf(k)

        # FM Deemphasis IIR filter
        self.deemph = fm_deemph(quad_rate, tau=tau)

        # compute FIR taps for audio filter
        audio_decim = quad_rate // audio_rate
        audio_taps = filter.firdes.low_pass(1.0,            # gain
                                            quad_rate,      # sampling rate
                                            2.7e3,          # Audio LPF cutoff
                                            0.5e3,          # Transition band
                                            fft.window.WIN_HAMMING)  # filter type

        print("len(audio_taps) =", len(audio_taps))

        # Decimating audio filter
        # input: float; output: float; taps: float
        self.audio_filter = filter.fir_filter_fff(audio_decim, audio_taps)

        self.connect(self, self.quad_demod, self.deemph, self.audio_filter, self)

    def set_max_deviation(self, max_dev):
        k = self._quad_rate / (2*math.pi*max_dev)
        self.quad_demod.set_gain(k)