.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_denoise/06_03_3d_volume_smoothing.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. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_06_denoise_06_03_3d_volume_smoothing.py: 3D Volume Smoothing =================== We use the smooth module to smooth N-dimensional data. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from splineops.denoise.smoothing_spline import smoothing_spline_nd .. GENERATED FROM PYTHON SOURCE LINES 21-23 Sinusoid 3D Data ---------------- .. GENERATED FROM PYTHON SOURCE LINES 23-101 .. code-block:: Python def demo_3d_sinusoid(): # Desired cutoff frequency cutoff_freq = 0.1 # Adjusted cutoff frequency gamma = 2.0 # Order of the spline operator # Compute lambda_ based on cutoff frequency lambda_ = (1 / (2 * np.pi * cutoff_freq)) ** (2 * gamma) snr_db = 10.0 # Desired SNR in dB # Create a 3D clean volume (sinusoid) x = np.linspace(0, 1, 64) y = np.linspace(0, 1, 64) z = np.linspace(0, 1, 64) X, Y, Z = np.meshgrid(x, y, z, indexing='ij') clean_volume = np.sin(8 * np.pi * X) + np.sin(8 * np.pi * Y) + np.sin(8 * np.pi * Z) clean_volume = (clean_volume - clean_volume.min()) / (clean_volume.max() - clean_volume.min()) # Normalize to [0, 1] # Add noise signal_power = np.mean(clean_volume ** 2) sigma = np.sqrt(signal_power / (10 ** (snr_db / 10))) noise = np.random.randn(*clean_volume.shape) * sigma noisy_volume = clean_volume + noise # Apply smoothing spline smoothed_volume = smoothing_spline_nd(noisy_volume, lambda_, gamma) # Compute SNRs snr_noisy = compute_snr(clean_volume, noisy_volume) snr_smoothed = compute_snr(clean_volume, smoothed_volume) snr_improvement = snr_smoothed - snr_noisy print("3D Sinusoid Volume:") print(f"SNR of noisy volume: {snr_noisy:.2f} dB") print(f"SNR after smoothing: {snr_smoothed:.2f} dB") print(f"SNR improvement: {snr_improvement:.2f} dB\n") # Visualize one slice of the volume (middle slice) slice_index = clean_volume.shape[2] // 2 plt.figure(figsize=(12, 4)) plt.subplot(1, 3, 1) plt.imshow(clean_volume[:, :, slice_index], cmap='gray') plt.title('Clean Volume Slice') plt.axis('off') plt.subplot(1, 3, 2) plt.imshow(noisy_volume[:, :, slice_index], cmap='gray') plt.title(f'Noisy Slice (SNR={snr_noisy:.2f} dB)') plt.axis('off') plt.subplot(1, 3, 3) plt.imshow(smoothed_volume[:, :, slice_index], cmap='gray') plt.title(f'Smoothed Slice (SNR={snr_smoothed:.2f} dB)') plt.axis('off') plt.tight_layout() plt.show() def compute_snr(clean_signal, noisy_signal): """ Compute the Signal-to-Noise Ratio (SNR). Parameters: clean_signal (np.ndarray): Original clean signal. noisy_signal (np.ndarray): Noisy signal. Returns: float: SNR value in decibels (dB). """ signal_power = np.mean(clean_signal ** 2) noise_power = np.mean((noisy_signal - clean_signal) ** 2) snr = 10 * np.log10(signal_power / noise_power) return snr # Run the 3D sinusoid demo demo_3d_sinusoid() .. image-sg:: /auto_examples/06_denoise/images/sphx_glr_06_03_3d_volume_smoothing_001.png :alt: Clean Volume Slice, Noisy Slice (SNR=10.01 dB), Smoothed Slice (SNR=24.98 dB) :srcset: /auto_examples/06_denoise/images/sphx_glr_06_03_3d_volume_smoothing_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 3D Sinusoid Volume: SNR of noisy volume: 10.01 dB SNR after smoothing: 24.98 dB SNR improvement: 14.97 dB .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.159 seconds) .. _sphx_glr_download_auto_examples_06_denoise_06_03_3d_volume_smoothing.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/splineops/splineops.github.io/main?urlpath=lab/tree/notebooks_binder/auto_examples/06_denoise/06_03_3d_volume_smoothing.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/06_denoise/06_03_3d_volume_smoothing.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 06_03_3d_volume_smoothing.ipynb <06_03_3d_volume_smoothing.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 06_03_3d_volume_smoothing.py <06_03_3d_volume_smoothing.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 06_03_3d_volume_smoothing.zip <06_03_3d_volume_smoothing.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_