.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/simulation_and_analysis/multirate.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_simulation_and_analysis_multirate.py: ============================= Simulating a multi-rate filter ============================= Build a rational sample-rate converter using Expansion and Compression around an FIR filter. Simulation handles the rate changes without modifying this SFG. .. GENERATED FROM PYTHON SOURCE LINES 9-15 .. code-block:: Python import numpy as np from b_asic import SFG, Compression, Expansion, Input, Output, Simulation from b_asic.sfg_generators import fir .. GENERATED FROM PYTHON SOURCE LINES 16-17 Expand by three, filter at the higher rate, and compress by two. .. GENERATED FROM PYTHON SOURCE LINES 17-23 .. code-block:: Python source = Input("x") coefficients = np.array([1.0, 0.5, 0.25, 0.125]) filtered = fir(coefficients)(Expansion(3, source)) graph = SFG(source, Output(Compression(2, filtered)), name="Rate conversion by 3/2") graph # noqa: B018 .. image-sg:: /examples/simulation_and_analysis/images/sphx_glr_multirate_001.png :alt: multirate :srcset: /examples/simulation_and_analysis/images/sphx_glr_multirate_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 24-25 Each period consumes two input samples and produces three output samples. .. GENERATED FROM PYTHON SOURCE LINES 25-38 .. code-block:: Python samples = np.array([1.0, -2.0, 3.0, 0.5, 2.0, -1.0]) simulation = Simulation(graph, [samples]) assert simulation.input_samples_per_iteration == (2,) assert simulation.output_samples_per_iteration == (3,) simulation.run() converted = simulation.results[graph.output_operations[0].graph_id] expanded = np.zeros(3 * len(samples)) expanded[::3] = samples reference = np.convolve(expanded, coefficients)[: len(expanded) : 2] np.testing.assert_allclose(converted, reference) print("Multi-rate simulation matches zero insertion, FIR filtering, and compression.") .. rst-class:: sphx-glr-script-out .. code-block:: none Multi-rate simulation matches zero insertion, FIR filtering, and compression. .. GENERATED FROM PYTHON SOURCE LINES 39-40 Obtain a single-rate phase graph explicitly for later architecture work. .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: Python normalization = graph.to_single_rate() normalization.sfg # noqa: B018 .. image-sg:: /examples/simulation_and_analysis/images/sphx_glr_multirate_002.png :alt: multirate :srcset: /examples/simulation_and_analysis/images/sphx_glr_multirate_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 44-45 Simplification removes arithmetic on inserted zeros and unused output phases. .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: Python single_rate = normalization.sfg.constant_propagation().sink_propagation() single_rate # noqa: B018 .. image-sg:: /examples/simulation_and_analysis/images/sphx_glr_multirate_003.png :alt: multirate :srcset: /examples/simulation_and_analysis/images/sphx_glr_multirate_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.178 seconds) .. _sphx_glr_download_examples_simulation_and_analysis_multirate.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: multirate.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: multirate.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: multirate.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_