.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "signal_processor/plot_5_tophat.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_signal_processor_plot_5_tophat.py: Top-Hat Apodization ^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 7-27 In this example, we will use the :py:class:`~mrsimulator.signal_processor.apodization.TopHat` class to apply a point-wise top hat apodization on the Fourier transform of an example dataset. The function is defined as follows .. math:: f(x) = \begin{cases} 1, \tt{rising\_edge} \leq x \leq \tt{falling\_edge} \\ 0, \text{otherwise} \end{cases} where ``rising_edge`` is the start of the window and ``falling_edge`` is the end of the window. When ``falling_edge`` is undefined, all points after ``rising_edge`` will be 1. Similarly, when ``rising_edge`` is undefined, all points before ``falling_edge`` are 1. Below we import the necessary modules .. GENERATED FROM PYTHON SOURCE LINES 27-33 .. code-block:: default import csdmpy as cp import matplotlib.pyplot as plt import numpy as np from mrsimulator import signal_processor as sp .. GENERATED FROM PYTHON SOURCE LINES 35-41 First we create ``processor``, and instance of the :py:class:`~mrsimulator.signal_processor.SignalProcessor` class. The required attribute of the SignalProcessor class, *operations*, is a list of operations to which we add a :py:class:`~mrsimulator.signal_processor.apodization.TopHat` object sandwiched between two Fourier transformations. Here the window is between 1 and 9 seconds. .. GENERATED FROM PYTHON SOURCE LINES 41-49 .. code-block:: default processor = sp.SignalProcessor( operations=[ sp.IFFT(), sp.apodization.TopHat(rising_edge="1 s", falling_edge="9 s"), sp.FFT(), ] ) .. GENERATED FROM PYTHON SOURCE LINES 50-53 Next we create a CSDM object with a test dataset which our signal processor will operate on. Here, the dataset is a delta function centered at 0 Hz with a some applied Gaussian line broadening. .. GENERATED FROM PYTHON SOURCE LINES 53-60 .. code-block:: default test_data = np.zeros(500) test_data[250] = 1 csdm_object = cp.CSDM( dependent_variables=[cp.as_dependent_variable(test_data)], dimensions=[cp.LinearDimension(count=500, increment="0.1 Hz", complex_fft=True)], ) .. GENERATED FROM PYTHON SOURCE LINES 61-64 To apply the previously defined signal processor, we use the :py:meth:`~mrsimulator.signal_processor.SignalProcessor.apply_operations` method as as follows .. GENERATED FROM PYTHON SOURCE LINES 64-66 .. code-block:: default processed_dataset = processor.apply_operations(dataset=csdm_object).real .. GENERATED FROM PYTHON SOURCE LINES 67-69 To see the results of the top hat apodization, we create a simple plot using the ``matplotlib`` library. .. GENERATED FROM PYTHON SOURCE LINES 69-77 .. code-block:: default fig, ax = plt.subplots(1, 2, figsize=(8, 3.5), subplot_kw={"projection": "csdm"}) ax[0].plot(csdm_object, color="black", linewidth=1) ax[0].set_title("Before") ax[1].plot(processed_dataset.real, color="black", linewidth=1) ax[1].set_title("After") plt.tight_layout() plt.show() .. image-sg:: /signal_processor/images/sphx_glr_plot_5_tophat_001.png :alt: Before, After :srcset: /signal_processor/images/sphx_glr_plot_5_tophat_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 78-80 Below are plots showing how the apodization functions when only ``rising_edge`` or ``falling_edge`` are defined. .. GENERATED FROM PYTHON SOURCE LINES 80-105 .. code-block:: default rising_edge_processor = sp.SignalProcessor( operations=[sp.apodization.TopHat(rising_edge="2 s")] ) falling_edge_processor = sp.SignalProcessor( operations=[sp.apodization.TopHat(falling_edge="8 s")] ) constant_csdm = cp.CSDM( dependent_variables=[cp.as_dependent_variable(np.ones(100))], dimensions=[cp.LinearDimension(100, increment="0.1 s")], ) rising_dataset = rising_edge_processor.apply_operations( dataset=constant_csdm.copy() ).real falling_dataset = falling_edge_processor.apply_operations( dataset=constant_csdm.copy() ).real fig, ax = plt.subplots(1, 2, figsize=(8, 3.5), subplot_kw={"projection": "csdm"}) ax[0].plot(rising_dataset, color="black", linewidth=1) ax[0].set_title("rising_edge") ax[1].plot(falling_dataset, color="black", linewidth=1) ax[1].set_title("falling_edge") plt.tight_layout() plt.show() .. image-sg:: /signal_processor/images/sphx_glr_plot_5_tophat_002.png :alt: rising_edge, falling_edge :srcset: /signal_processor/images/sphx_glr_plot_5_tophat_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.717 seconds) .. _sphx_glr_download_signal_processor_plot_5_tophat.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_5_tophat.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_5_tophat.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_