.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/simulation_and_analysis/lti_system_analysis.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_lti_system_analysis.py: =================== LTI System Analysis =================== In this example the state-space, transfer function, zero-pole-gain, and magnitude response for an LTI system are derived and visualized. .. GENERATED FROM PYTHON SOURCE LINES 13-16 Signal-flow graph ----------------- 6th-order elliptic low-pass filter .. GENERATED FROM PYTHON SOURCE LINES 16-31 .. code-block:: Python from b_asic.sfg_generators.digital_filters import direct_form_2_iir b = [ 0.00688044, 0.00271194, 0.01334486, 0.00694836, 0.01334486,0.00271194, 0.00688044 ] a = [ 1, -3.82875342, 7.15331101, -7.94817034, 5.49500754, -2.23261844, 0.42049185 ] sfg = direct_form_2_iir(b, a) sfg .. image-sg:: /examples/simulation_and_analysis/images/sphx_glr_lti_system_analysis_001.png :alt: lti system analysis :srcset: /examples/simulation_and_analysis/images/sphx_glr_lti_system_analysis_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 32-34 State-space representation -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 34-38 .. code-block:: Python ss = sfg.to_ss() print(ss) .. rst-class:: sphx-glr-script-out .. code-block:: none StateSpace (6 states, 1 inputs, 1 outputs) A = [[ 3.82875342, -7.15331101, 7.94817034, -5.49500754, 2.23261844, -0.42049185], [ 1. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 1. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 1. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 1. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 1. , 0. ]], B = [[1.], [0.], [0.], [0.], [0.], [0.]], C = [[ 0.02905545, -0.03587307, 0.06163527, -0.02446321, 0.01807334, 0.00398727]], D = [[0.00688044]] .. GENERATED FROM PYTHON SOURCE LINES 39-41 Transfer function ----------------- .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. code-block:: Python tf = sfg.to_tf() print(tf) .. rst-class:: sphx-glr-script-out .. code-block:: none TransferFunction (1 inputs, 1 outputs) Denominator: [ 1. -3.82875342 7.15331101 -7.94817034 5.49500754 -2.23261844 0.42049185] in0 Numerator: [0.00688044 0.00271194 0.01334486 0.00694836 0.01334486 0.00271194 0.00688044] .. GENERATED FROM PYTHON SOURCE LINES 46-48 Zero-pole-gain -------------- .. GENERATED FROM PYTHON SOURCE LINES 48-55 .. code-block:: Python zpk = sfg.to_zpk() zeros, poles, gain = zpk["in0"] print("Zeros:", zeros) print("Poles:", poles) print("Gain:", gain) .. rst-class:: sphx-glr-script-out .. code-block:: none Zeros: [-0.66130483+0.75011727j -0.66130483-0.75011727j 0.34164245+0.93983j 0.34164245-0.93983j 0.12258632+0.99245786j 0.12258632-0.99245786j] Poles: [0.57014161+0.78145237j 0.57014161-0.78145237j 0.62440867+0.61818949j 0.62440867-0.61818949j 0.71982644+0.25279698j 0.71982644-0.25279698j] Gain: 0.006880439999999988 .. GENERATED FROM PYTHON SOURCE LINES 56-58 Pole-zero plot -------------- .. GENERATED FROM PYTHON SOURCE LINES 58-76 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt theta = np.linspace(0, 2 * np.pi, 1024) fig, ax = plt.subplots() ax.plot(np.cos(theta), np.sin(theta), "k--") ax.scatter(zeros.real, zeros.imag, marker="o") ax.scatter(poles.real, poles.imag, marker="x") ax.axhline(0, color="black") ax.axvline(0, color="black") ax.set_aspect("equal") ax.set_xlabel("Real") ax.set_ylabel("Imaginary") plt.tight_layout() plt.show() .. image-sg:: /examples/simulation_and_analysis/images/sphx_glr_lti_system_analysis_002.png :alt: lti system analysis :srcset: /examples/simulation_and_analysis/images/sphx_glr_lti_system_analysis_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-79 Magnitude response ------------------ .. GENERATED FROM PYTHON SOURCE LINES 79-99 .. code-block:: Python from b_asic.signal_generator import Impulse from b_asic.simulation import Simulation sim = Simulation(sfg, [Impulse()]) sim.run_for(1024) h = np.array(sim.results["out0"]) H = np.fft.rfft(h) freqs = np.fft.rfftfreq(len(h)) fig, ax = plt.subplots() ax.plot(freqs, 20 * np.log10(np.abs(H))) ax.set_xlabel("Normalized frequency") ax.set_ylabel("Magnitude, dB") ax.grid(True) plt.tight_layout() plt.show() .. image-sg:: /examples/simulation_and_analysis/images/sphx_glr_lti_system_analysis_003.png :alt: lti system analysis :srcset: /examples/simulation_and_analysis/images/sphx_glr_lti_system_analysis_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.423 seconds) .. _sphx_glr_download_examples_simulation_and_analysis_lti_system_analysis.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: lti_system_analysis.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: lti_system_analysis.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: lti_system_analysis.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_