.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/raisedcosine.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_raisedcosine.py: =========================== Raised cosine filter design. =========================== Design a raised cosine filter with a rolloff of 0.3. .. GENERATED FROM PYTHON SOURCE LINES 8-26 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mplsignal import freqz_fir from fird.designer import FIRDesigner from fird.objective import RaisedCosineLinearObjective o = RaisedCosineLinearObjective(0.3) d = FIRDesigner(22, o) d.solve() h = d.get_impulse_response() fig, ax = plt.subplots() o.plot(ax, label="Desired") freqz_fir(h, ax=ax, magnitude_scale="linear", style="magnitude") ax.legend() fig.show() .. image-sg:: /examples/images/sphx_glr_raisedcosine_001.png :alt: raisedcosine :srcset: /examples/images/sphx_glr_raisedcosine_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 27-28 Display the impulse response. As expected every other coefficient is close to zero and the middle coefficient is close to 0.5. .. GENERATED FROM PYTHON SOURCE LINES 28-33 .. code-block:: Python print(h) fig, ax = plt.subplots() ax.stem(h) fig.show() .. image-sg:: /examples/images/sphx_glr_raisedcosine_002.png :alt: raisedcosine :srcset: /examples/images/sphx_glr_raisedcosine_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [-4.7770938e-08 1.1818022e-03 7.7605963e-09 -1.4592795e-02 2.6864261e-08 3.6033765e-02 -3.1785043e-09 -8.7356219e-02 2.4387249e-08 3.1164230e-01 4.9999998e-01 3.1164230e-01 2.4387249e-08 -8.7356219e-02 -3.1785043e-09 3.6033765e-02 2.6864261e-08 -1.4592795e-02 7.7605963e-09 1.1818022e-03 -4.7770938e-08] .. GENERATED FROM PYTHON SOURCE LINES 34-35 To make the coefficients exactly as expected, add a :class:`NyquistConstraint` .. GENERATED FROM PYTHON SOURCE LINES 35-48 .. code-block:: Python from fird.constraint import NyquistConstraint # noqa: E402 c = NyquistConstraint(2) d = FIRDesigner(22, o, c) d.solve() h = d.get_impulse_response() fig, ax = plt.subplots() o.plot(ax, label="Desired") freqz_fir(h, ax=ax, magnitude_scale="linear", style="magnitude") ax.legend() fig.show() .. image-sg:: /examples/images/sphx_glr_raisedcosine_003.png :alt: raisedcosine :srcset: /examples/images/sphx_glr_raisedcosine_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 49-50 Now, the every other coefficient is exactly zero. .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: Python print(h) fig, ax = plt.subplots() ax.stem(h) fig.show() .. image-sg:: /examples/images/sphx_glr_raisedcosine_004.png :alt: raisedcosine :srcset: /examples/images/sphx_glr_raisedcosine_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ 0. 0.0011818 0. -0.01459281 0. 0.03603375 0. -0.08735623 0. 0.3116423 0.5 0.3116423 0. -0.08735623 0. 0.03603375 0. -0.01459281 0. 0.0011818 0. ] .. GENERATED FROM PYTHON SOURCE LINES 56-57 It is also possible to design root raised cosine filters .. GENERATED FROM PYTHON SOURCE LINES 57-70 .. code-block:: Python from fird.objective import RootRaisedCosineLinearObjective # noqa: E402 o = RootRaisedCosineLinearObjective(0.3) d = FIRDesigner(12, o) d.solve() h = d.get_impulse_response() fig, ax = plt.subplots() o.plot(ax, label="Desired") freqz_fir(h, ax=ax, magnitude_scale="linear", style="magnitude") ax.legend() fig.show() .. image-sg:: /examples/images/sphx_glr_raisedcosine_005.png :alt: raisedcosine :srcset: /examples/images/sphx_glr_raisedcosine_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 71-72 Here, the filter is not a Nyquist filter, but the convolution with itself should be. .. GENERATED FROM PYTHON SOURCE LINES 72-79 .. code-block:: Python fig, ax = plt.subplots(2, 1) ax[0].stem(h, label="Root-raised cosine") ax[0].legend() ax[1].stem(np.convolve(h, h), label="Convolved with itself") ax[1].legend() fig.show() .. image-sg:: /examples/images/sphx_glr_raisedcosine_006.png :alt: raisedcosine :srcset: /examples/images/sphx_glr_raisedcosine_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.663 seconds) .. _sphx_glr_download_examples_raisedcosine.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: raisedcosine.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: raisedcosine.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: raisedcosine.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_