GNU Radio World

Python Block - Soft Limiter

Arbitrary Python DSP in the browser -- a soft limiter written as an Embedded Python Block, with its gain driven live by a slider

Press Load to open this flowgraph in an editor embedded above, then ▶ to run it. Nothing is downloaded until you do.

Open in the full editor ▶ Download the .grc

Blocks used

BlockModuleUsed
Python BlockGNU Radio1
QT GUI RangeGNU Radio1
QT GUI Time SinkGNU Radio1
Signal SourceGNU Radio1
ThrottleGNU Radio1
VariableGNU Radio1

Flowgraph source

python/epy_soft_limiter.grc
options:
    parameters:
        author: Claude
        copyright: ''
        description: Arbitrary Python DSP in the browser -- a soft limiter written as an Embedded Python Block, with its gain driven live by a slider
        generate_options: qt_gui
        id: Python_Block___Soft_Limiter
        title: Python Block - Soft Limiter
    states:
        coordinate: [10, 10]
        rotation: 0
        state: enabled
blocks:
-   name: gain
    id: variable_qtgui_range
    parameters:
        comment: ''
        label: Limiter gain
        min_len: '240'
        orient: QtCore.Qt.Horizontal
        rangeType: float
        start: '0.1'
        step: '0.1'
        stop: '20'
        value: '1'
        widget: counter_slider
    states:
        coordinate: [580, 10]
        rotation: 0
        state: enabled
-   name: samp_rate
    id: variable
    parameters:
        comment: ''
        value: '48000'
    states:
        coordinate: [830, 10]
        rotation: 0
        state: enabled
-   name: scope
    id: qtgui_time_sink_x
    parameters:
        affinity: ''
        alias: ''
        alpha1: '1'
        alpha10: '1'
        alpha2: '1'
        alpha3: '1'
        alpha4: '1'
        alpha5: '1'
        alpha6: '1'
        alpha7: '1'
        alpha8: '1'
        alpha9: '1'
        autoscale: 'False'
        axislabels: 'True'
        color1: blue
        color10: dark blue
        color2: red
        color3: green
        color4: black
        color5: cyan
        color6: magenta
        color7: yellow
        color8: dark red
        color9: dark green
        comment: ''
        ctrlpanel: 'False'
        grid: 'True'
        label1: limited
        label10: Signal 10
        label2: Signal 2
        label3: Signal 3
        label4: Signal 4
        label5: Signal 5
        label6: Signal 6
        label7: Signal 7
        label8: Signal 8
        label9: Signal 9
        legend: 'True'
        marker1: '-1'
        marker10: '-1'
        marker2: '-1'
        marker3: '-1'
        marker4: '-1'
        marker5: '-1'
        marker6: '-1'
        marker7: '-1'
        marker8: '-1'
        marker9: '-1'
        name: Soft limiter output
        nconnections: '1'
        samp_rate: '32000'
        size: '1024'
        stemplot: 'False'
        style1: '1'
        style10: '1'
        style2: '1'
        style3: '1'
        style4: '1'
        style5: '1'
        style6: '1'
        style7: '1'
        style8: '1'
        style9: '1'
        tr_chan: '0'
        tr_delay: '0'
        tr_level: '0'
        tr_mode: qtgui.TRIG_MODE_FREE
        tr_slope: qtgui.TRIG_SLOPE_POS
        tr_tag: ''
        type: float
        update_time: '0.1'
        width1: '1'
        width10: '1'
        width2: '1'
        width3: '1'
        width4: '1'
        width5: '1'
        width6: '1'
        width7: '1'
        width8: '1'
        width9: '1'
        ylabel: Amplitude
        ymax: '1.5'
        ymin: '-1.5'
        yunit: ''
    states:
        coordinate: [860, 210]
        rotation: 0
        state: enabled
-   name: soft_limiter
    id: epy_block
    parameters:
        _io_cache: '{"block_type":"sync","callbacks":["gain"],"cls":"blk","decim":1,"doc":"Soft limiter: y = tanh(gain * x)","history":1,"interp":1,"itemsizes_in":[4],"itemsizes_out":[4],"label":"Soft Limiter","max_noutput_items":0,"min_output_buffer":0,"msg_ports_in":[],"msg_ports_out":[],"output_multiple":0,"overrides_forecast":false,"params":[["gain","1.0"]],"relative_rate":1.0,"sinks":[["0","float",1]],"sources":[["0","float",1]],"tag_propagation_policy":1}'
        _source_code: "\"\"\"\nEmbedded Python Block: a soft limiter.\n\nThe gain below is driven live by the QT GUI Range control in this flowgraph, so\nraising the slider past 1.0 turns the sine into a flat-topped, clipped wave --\na shape no stock GNU Radio block produces.\n\nEdit this code in the block's Properties dialog, press the button under it to\nre-read it, and the block's parameters and ports follow whatever you wrote.\n\"\"\"\n\nimport numpy as np\nfrom gnuradio import gr\n\n\nclass blk(gr.sync_block):\n    \"\"\"Soft limiter: y = tanh(gain * x)\"\"\"\n\n    def __init__(self, gain=1.0):\n        gr.sync_block.__init__(\n            self,\n            name='Soft Limiter',\n            in_sig=[np.float32],\n            out_sig=[np.float32])\n        self.gain = gain\n\n    def work(self, input_items, output_items):\n        np.tanh(self.gain * input_items[0], out=output_items[0])\n        return len(output_items[0])\n"
        affinity: ''
        alias: ''
        comment: ''
        gain: gain
    states:
        coordinate: [620, 230]
        rotation: 0
        state: enabled
-   name: throttle
    id: blocks_throttle2
    parameters:
        affinity: ''
        alias: ''
        comment: ''
        ignoretag: 'True'
        limit: time
        maximum: '0.1'
        maxoutbuf: '0'
        minoutbuf: '0'
        samples_per_second: samp_rate
        type: float
        vlen: '1'
    states:
        coordinate: [290, 210]
        rotation: 0
        state: enabled
-   name: tone
    id: analog_sig_source_x
    parameters:
        affinity: ''
        alias: ''
        amplitude: '1'
        comment: ''
        frequency: '1000'
        maxoutbuf: '0'
        minoutbuf: '0'
        samp_rate: samp_rate
        type: float
        waveform: analog.GR_COS_WAVE
    states:
        coordinate: [10, 210]
        rotation: 0
        state: enabled
connections:
- [soft_limiter, '0', scope, '0']
- [throttle, '0', soft_limiter, '0']
- [tone, '0', throttle, '0']
metadata:
    file_format: 1
    grc_version: 3.11.0.0