.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/design_examples/polyphase_decimator.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_design_examples_polyphase_decimator.py: ================================== Deriving a polyphase FIR decimator ================================== Unfold a six-tap FIR filter by three, discard two output phases using Sink nodes, and propagate the unused results backwards. The resulting polyphase decimator consumes three consecutive input samples per iteration and produces one output sample at a third of the input rate. .. GENERATED FROM PYTHON SOURCE LINES 11-18 .. code-block:: Python import numpy as np from b_asic.sfg_generators import fir from b_asic.simulation import Simulation from b_asic.utility_operations import Sink .. GENERATED FROM PYTHON SOURCE LINES 19-21 Start with a small FIR filter. With three phases, these six coefficients give two taps per phase. .. GENERATED FROM PYTHON SOURCE LINES 21-27 .. code-block:: Python coefficients = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6]) fir_sfg = fir(coefficients) fir_sfg.input_operations[0].name = "x" fir_sfg.output_operations[0].name = "y" fir_sfg .. image-sg:: /examples/design_examples/images/sphx_glr_polyphase_decimator_001.png :alt: polyphase decimator :srcset: /examples/design_examples/images/sphx_glr_polyphase_decimator_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 28-30 Unfold by three. The input/output ports are in chronological order, so each iteration now processes three consecutive samples and computes three outputs. .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: Python factor = 3 unfolded = fir_sfg.unfold(factor) unfolded .. image-sg:: /examples/design_examples/images/sphx_glr_polyphase_decimator_002.png :alt: polyphase decimator :srcset: /examples/design_examples/images/sphx_glr_polyphase_decimator_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 35-38 Retain output phase zero: y[0], y[3], y[6], ... . Replacing the other two Output nodes by Sinks removes their ports from the graph interface and marks the corresponding values as unused, while keeping the branches visible. .. GENERATED FROM PYTHON SOURCE LINES 38-43 .. code-block:: Python sink_connected = unfolded for output in unfolded.output_operations[1:]: sink_connected = sink_connected.replace(output.graph_id, Sink()) sink_connected .. image-sg:: /examples/design_examples/images/sphx_glr_polyphase_decimator_003.png :alt: polyphase decimator :srcset: /examples/design_examples/images/sphx_glr_polyphase_decimator_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/b-asic/b-asic/galleries/examples/design_examples/polyphase_decimator.py:40: UserWarning: Output port out0_1 has been removed sink_connected = sink_connected.replace(output.graph_id, Sink()) /home/runner/work/b-asic/b-asic/galleries/examples/design_examples/polyphase_decimator.py:40: UserWarning: Output port out0_2 has been removed sink_connected = sink_connected.replace(output.graph_id, Sink()) .. GENERATED FROM PYTHON SOURCE LINES 44-47 Propagate Sinks backwards. Computations serving only discarded phases are removed; shared input history needed by the retained phase is preserved. All three input phases contribute to the remaining decimated output. .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. code-block:: Python polyphase = sink_connected.sink_propagation() polyphase.name = "Three-phase FIR decimator" polyphase .. image-sg:: /examples/design_examples/images/sphx_glr_polyphase_decimator_004.png :alt: polyphase decimator :srcset: /examples/design_examples/images/sphx_glr_polyphase_decimator_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 52-55 Verify against FIR filtering followed by retaining every third sample. Input port r receives x[3k + r] at iteration k. Each graph delay now stores history across three input samples, i.e. one low-rate iteration. .. GENERATED FROM PYTHON SOURCE LINES 55-63 .. code-block:: Python samples = np.array([1.0, -2.0, 3.0, 0.5, 2.0, -1.0, 0.0, 0.0, 0.0]) inputs = [samples[phase::factor] for phase in range(factor)] simulation = Simulation(polyphase, inputs) simulation.run() decimated = simulation.results[polyphase.output_operations[0].graph_id] reference = np.convolve(samples, coefficients)[: len(samples) : factor] np.testing.assert_allclose(decimated, reference, atol=1e-14) print("Polyphase outputs match FIR filtering followed by decimation by three.") .. rst-class:: sphx-glr-script-out .. code-block:: none Polyphase outputs match FIR filtering followed by decimation by three. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.229 seconds) .. _sphx_glr_download_examples_design_examples_polyphase_decimator.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: polyphase_decimator.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: polyphase_decimator.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: polyphase_decimator.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_