Example single peak

Import the nmrlineshapeanalyser library

import sys
import matplotlib.pyplot as plt
sys.path.append("../../src")
from nmrlineshapeanalyser.core import NMRProcessor
nmrglue: 0.11
numpy: 2.4.6
scipy: 1.17.1
matplotlib: 3.11.1
pandas: 3.0.5

Create NMRProcessor object

processor = NMRProcessor()

Load filepath

nmrlineshapeanalyser supports an already processed Bruker data. It does not support data from any other brand at the moment. You might want to check NMRglue on how to go about that.

filepath has the following format: bruker's processed data path (10\pdata\1) + "\\". It is pertinent to leave the "\\" at the end of the filepath.

filepath = "../../data/single_peak/10/pdata/1/"

Load the data

processor.load_data(filepath)

Select the region of interest: (lower_value, higher_value)

x_data, y_data = processor.select_region(512, 650)

Normalize the data and return normalised y_axis and the corresponding x_axis

x_data, y_normalized, y_amp, y_ground = processor.normalize_data(x_data, y_data)

define initial parameters for the fitting

this example is for a single peak

format of the parameters is [x0, amplitude, width, eta, offset]

x0 has to be close to the peak position

initial_params = [
581, 0.12, 40.51, 0.89, -1 
  ]

fixed_x0 controls whether peak positions should be fixed during fitting

False means position can vary, True means position is fixed

fixed_eta controls whether each peak's Gaussian/Lorentzian mixing parameter (eta) is fixed.

False means eta is fitted freely (default), True fixes it at its initial_params value.

number_of_peaks = 1

fixed_x0 = [False] * number_of_peaks

fixed_amp = [False]*number_of_peaks

fixed_width = [False]*number_of_peaks

fixed_eta = [False]*number_of_peaks

Fit the data

popt, metrics, fitted = processor.fit_peaks(x_data, y_normalized, initial_params, fixed_x0, fixed_amp, fixed_width, fixed_eta)

popt is the optimized parameters

metrics is the metrics of the fitting

fitted is the fitted curve data

Plot and examine the results of the fitting

fig, axes, components = processor.plot_results(x_data, y_normalized, fitted, popt)
No description has been provided for this image

Save the figure as png file and the results as a csv file

processor.save_results(filepath, x_data, y_normalized, fitted, metrics, popt, components)

Peak Fitting Results:
===================

Peak 1 (Position: 582.01 ± 0.01):
  Amplitude: 0.993 ± 0.002
  Width (FWHM): 12.33 ± 0.03 ppm
  Width (FWHM): 835.74 ± 2.36 Hz
  Carrier Frequency: 67.804154 MHz
  Eta: 1.00 ± 0.01
  Offset: -0.0039 ± 0.0004
  Gaussian Area: 0.00 ± 0.10
  Lorentzian Area: 19.23 ± 0.16
  Total Area: 19.23 ± 0.19
Peak 1 Percentage: 100.00% ± 1.39%
Total: 100.00%