Exemple #1
0
def test_TopHat():
    test_dataset = cp.CSDM(
        dependent_variables=[cp.as_dependent_variable(np.ones(500))],
        dimensions=[cp.LinearDimension(500, "1 s")],
    )

    processor = sp.SignalProcessor()
    processor.operations = [
        sp.apodization.TopHat(rising_edge="100 s", falling_edge="400 s")
    ]

    rise_and_fall_dataset = processor.apply_operations(test_dataset.copy())
    rise_and_fall_should_be = np.zeros(500)
    rise_and_fall_should_be[100:400] = 1

    assert np.allclose(rise_and_fall_dataset.y[0].components,
                       rise_and_fall_should_be)

    processor.operations = [sp.apodization.TopHat(rising_edge="100 s")]

    rise_only_dataset = processor.apply_operations(test_dataset.copy())
    rise_only_should_be = np.zeros(500)
    rise_only_should_be[100:] = 1

    assert np.allclose(rise_only_dataset.y[0].components, rise_only_should_be)

    processor.operations = [sp.apodization.TopHat(falling_edge="400 s")]

    fall_only_dataset = processor.apply_operations(test_dataset.copy())
    fall_only_should_be = np.zeros(500)
    fall_only_should_be[:400] = 1

    assert np.allclose(fall_only_dataset.y[0].components, fall_only_should_be)
Exemple #2
0
    def to_csdm(self):
        """
        Return a csdm object containing data.

        Returns
        -------
        data : csdm object
            CSDM object containing parameters and data

        """

        try:
            import csdmpy as cp
            # self._oproc = {}
            # self._odtype = str(self._data.dtype)

            # create a list of dimension objects
            dimensions = [
                cp.LinearDimension(
                    count=value["size"],
                    increment=f'{1 / value["sw"]} s',
                    reciprocal={
                        "coordinates_offset": f'{value["car"]} Hz',
                        "origin_offset": f'{value["obs"]} MHz',
                    },
                    label=value["label"],
                ) for key, value in list(self._udic.items())
                if type(key) == int and value["size"] != 1
            ]

            return cp.CSDM(
                dimensions=dimensions[::-1],
                dependent_variables=[
                    cp.as_dependent_variable(self._data.copy())
                ],
            )
        except:
            raise ImportError(
                "csdmpy must be installed to use this function. Please install by typing 'pip install csdmpy' in the terminal."
            )
Exemple #3
0
])

# %%
# Here the applied offset will be the following function
#
# .. math::
#
#   f(x) = 0.00001 \cdot x^2 + 0.2
#
# Next we create a CSDM object with a test dataset which our signal processor will
# operate on. Here, the dataset spans 500 Hz with a delta function centered at
# 100 Hz.
test_data = np.zeros(500)
test_data[350] = 1
csdm_object = cp.CSDM(
    dependent_variables=[cp.as_dependent_variable(test_data)],
    dimensions=[
        cp.LinearDimension(count=500, increment="1 Hz", complex_fft=True)
    ],
)

# %%
# Now to apply the processor to the CSDM object, use the
# :py:meth:`~mrsimulator.signal_processor.SignalProcessor.apply_operations` method as
# follows
processed_dataset = processor.apply_operations(dataset=csdm_object.copy()).real

# %%
# To see the results of the exponential apodization, we create a simple plot using the
# ``matplotlib`` library.
import matplotlib.pyplot as plt
processor = sp.SignalProcessor(
    operations=[
        sp.IFFT(),
        sp.apodization.TopHat(rising_edge="1 s", falling_edge="9 s"),
        sp.FFT(),
    ]
)

# %%
# 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.
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)],
)

# %%
# To apply the previously defined signal processor, we use the
# :py:meth:`~mrsimulator.signal_processor.SignalProcessor.apply_operations` method as
# as follows
processed_dataset = processor.apply_operations(dataset=csdm_object).real

# %%
# To see the results of the top hat apodization, we create a simple plot using the
# ``matplotlib`` library.
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")
def generate_data():
    dv1 = cp.as_dependent_variable(np.random.rand(20))
    dv2 = cp.as_dependent_variable(np.random.rand(20))
    dv3 = cp.as_dependent_variable(np.random.rand(20))
    dim = cp.as_dimension(np.arange(20))
    return cp.CSDM(dependent_variables=[dv1, dv2, dv3], dimensions=[dim])