.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/nonnegativecoefficients.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_nonnegativecoefficients.py: ========================= Non-negative coefficients. ========================= To illustrate how bounds can be put on coefficients, a filter without negative coefficients is designed. .. GENERATED FROM PYTHON SOURCE LINES 9-27 .. code-block:: Python import matplotlib.pyplot as plt from mplsignal import freqz_fir from fird.constraint import CoefficientBoundConstraint from fird.designer import FIRDesigner from fird.objective import PiecewiseLinearObjective o = PiecewiseLinearObjective([0, 0.2, 0.5, 1], [1, 1, 0, 0], [1, 10]) c = CoefficientBoundConstraint(lower=0) d = FIRDesigner(21, o, c) d.solve() h = d.get_impulse_response() fig, ax = plt.subplots(2, 1, layout="constrained") freqz_fir(h, ax=ax[0], style="magnitude") ax[1].stem(h) fig.show() .. image-sg:: /examples/images/sphx_glr_nonnegativecoefficients_001.png :alt: nonnegativecoefficients :srcset: /examples/images/sphx_glr_nonnegativecoefficients_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 28-30 This given quite bad results, but for narrow-band filters it can be possible to get decent results. .. GENERATED FROM PYTHON SOURCE LINES 30-40 .. code-block:: Python o = PiecewiseLinearObjective([0, 0.05, 0.3, 1], [1, 1, 0, 0], [1, 10]) c = CoefficientBoundConstraint(lower=0) d = FIRDesigner(21, o, c) d.solve() h = d.get_impulse_response() fig, ax = plt.subplots(2, 1, layout="constrained") freqz_fir(h, ax=ax[0], style="magnitude") ax[1].stem(h) fig.show() .. image-sg:: /examples/images/sphx_glr_nonnegativecoefficients_002.png :alt: nonnegativecoefficients :srcset: /examples/images/sphx_glr_nonnegativecoefficients_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.599 seconds) .. _sphx_glr_download_examples_nonnegativecoefficients.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: nonnegativecoefficients.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: nonnegativecoefficients.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: nonnegativecoefficients.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_