.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/design_examples/polyphase_interpolator.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_interpolator.py: ===================================== Deriving a polyphase FIR interpolator ===================================== Unfold a six-tap FIR filter by three, set two input phases to zero, and propagate those zeros to obtain a three-phase interpolator. The three outputs are consecutive samples of the interpolated signal, produced for each sample at the remaining low-rate input. .. GENERATED FROM PYTHON SOURCE LINES 11-19 .. code-block:: Python import numpy as np from b_asic.core_operations import Constant from b_asic.sfg import SFG from b_asic.sfg_generators import fir from b_asic.simulation import Simulation .. GENERATED FROM PYTHON SOURCE LINES 20-22 Start with a small direct-form FIR filter. Six taps give two coefficients per phase when the interpolation factor is three. .. GENERATED FROM PYTHON SOURCE LINES 22-28 .. 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_interpolator_001.png :alt: polyphase interpolator :srcset: /examples/design_examples/images/sphx_glr_polyphase_interpolator_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 29-31 Unfold by three. One iteration of the unfolded graph processes three consecutive input samples and produces three consecutive output samples. .. GENERATED FROM PYTHON SOURCE LINES 31-35 .. code-block:: Python factor = 3 unfolded = fir_sfg.unfold(factor) unfolded .. image-sg:: /examples/design_examples/images/sphx_glr_polyphase_interpolator_002.png :alt: polyphase interpolator :srcset: /examples/design_examples/images/sphx_glr_polyphase_interpolator_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 36-39 Zero insertion for interpolation keeps phase zero and sets phases one and two to constant zero. Remove those two unused input ports from the interface so the graph takes just one new sample per iteration. .. GENERATED FROM PYTHON SOURCE LINES 39-49 .. code-block:: Python zero_stuffed = unfolded for input_operation in unfolded.input_operations[1:]: zero_stuffed = zero_stuffed.replace(input_operation.graph_id, Constant(0)) zero_stuffed = SFG( zero_stuffed.input_operations[0], zero_stuffed.output_operations, name="Zero-stuffed unfolded FIR", ) zero_stuffed .. image-sg:: /examples/design_examples/images/sphx_glr_polyphase_interpolator_003.png :alt: polyphase interpolator :srcset: /examples/design_examples/images/sphx_glr_polyphase_interpolator_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 50-53 Propagate zeros through multipliers, additions, and zero-initialized delays. The result is a polyphase interpolator: output r uses coefficients h[r], h[r + 3], ... and all three phases share the same low-rate input history. .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: Python polyphase = zero_stuffed.constant_propagation() polyphase.name = "Three-phase FIR interpolator" polyphase .. image-sg:: /examples/design_examples/images/sphx_glr_polyphase_interpolator_004.png :alt: polyphase interpolator :srcset: /examples/design_examples/images/sphx_glr_polyphase_interpolator_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 58-60 Verify the result against explicit zero insertion followed by FIR filtering. Append one low-rate zero to flush the two-tap polyphase branches. .. GENERATED FROM PYTHON SOURCE LINES 60-73 .. code-block:: Python samples = np.array([1.0, -2.0, 3.0, 0.5, 0.0]) simulation = Simulation(polyphase, [samples]) simulation.run() phase_outputs = np.column_stack( [simulation.results[output.graph_id] for output in polyphase.output_operations] ) interpolated = phase_outputs.ravel() upsampled = np.zeros(factor * len(samples)) upsampled[::factor] = samples reference = np.convolve(upsampled, coefficients)[: len(upsampled)] np.testing.assert_allclose(interpolated, reference, atol=1e-14) print("Polyphase outputs match zero insertion followed by FIR filtering.") .. rst-class:: sphx-glr-script-out .. code-block:: none Polyphase outputs match zero insertion followed by FIR filtering. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.223 seconds) .. _sphx_glr_download_examples_design_examples_polyphase_interpolator.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: polyphase_interpolator.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: polyphase_interpolator.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: polyphase_interpolator.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_