.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/arbitrarymagnitude.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_arbitrarymagnitude.py: ======================================= FIR filter with magnitude from function. ======================================= .. GENERATED FROM PYTHON SOURCE LINES 6-15 .. code-block:: Python import math import matplotlib.pyplot as plt from mplsignal import freqz_fir from fird.designer import FIRDesigner from fird.objective import FunctionLinearObjective .. GENERATED FROM PYTHON SOURCE LINES 16-17 First, design a filter minimizing the approximation error when approximating cos in the range 0 to 0.3π. .. GENERATED FROM PYTHON SOURCE LINES 17-26 .. code-block:: Python o = FunctionLinearObjective([0, 0.3], lambda f: (1 - math.sin(f * math.pi))) d = FIRDesigner(15, o) d.solve() fig, ax = plt.subplots() freqz_fir(d.get_impulse_response(), ax=ax, magnitude_scale="linear", style="magnitude") o.plot(ax, label="Desired") ax.legend() fig.show() .. image-sg:: /examples/images/sphx_glr_arbitrarymagnitude_001.png :alt: arbitrarymagnitude :srcset: /examples/images/sphx_glr_arbitrarymagnitude_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 27-29 To avoid that the magnitude becomes large in the band that we do not care about, add a constraint. .. GENERATED FROM PYTHON SOURCE LINES 29-40 .. code-block:: Python from fird.constraint import PiecewiseLinearConstraint # noqa: E402 c = PiecewiseLinearConstraint([0.3, 1], [0, 0], [1]) d = FIRDesigner(15, o, c) d.solve() fig, ax = plt.subplots() freqz_fir(d.get_impulse_response(), ax=ax, magnitude_scale="linear", style="magnitude") o.plot(ax, label="Desired") ax.legend() fig.show() .. image-sg:: /examples/images/sphx_glr_arbitrarymagnitude_002.png :alt: arbitrarymagnitude :srcset: /examples/images/sphx_glr_arbitrarymagnitude_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.287 seconds) .. _sphx_glr_download_examples_arbitrarymagnitude.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: arbitrarymagnitude.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: arbitrarymagnitude.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: arbitrarymagnitude.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_