Skip to content

Programming in MATLAB for Data Analysis – Baseline Correction for NMR Spectra

December 7, 2015

MATLAB for NMR Baseline Data Analysis title

In my research, doing Nuclear Magnetic Resonance (NMR) for material characterization and mechanism study is an important part of my work. Although every scientific instrument has its own data processing software, there are some clear disadvantages which may lead to incorrect results or low efficiency. Unfortunately, the processing tools that come with instruments are normally proprietary software, meaning you would never know what the software is actually doing when you click each button, even for simplest processing such as baseline correction, curve fitting, integration, and Fourier Transform. This situation is particularly inconvenient when there is an unexpected output since one cannot determine whether it’s due to a flaw in the algorithm or the experiment itself.

In this blog article, I will share how I take initiative to solve the problem by writing scripts in MATLAB and use its tools to perform automatic NMR data processing which would save countless hours for people who intensively work with NMR. Due to the length, I am planning to write another article talking about how to programming in MATLAB to turn time-consuming and tedious T1/T2 data analysis process into a breeze.

 

Problem that needs to be solved

Baseline correction is a basic yet extremely important step of NMR data analysis. The raw data that is acquired by the spectrometer should (but in most cases does not) have a baseline at zero. When the spectrum has an offset from zero, it will give rise to many problems such as incorrect integral values, additional peaks in Fourier space, and being unable to do parallel comparison between experiments etc. Thus, it is usually the first thing one needs to do before further processing. Every NMR software has that function.

However, not all of the commercial software does that as we expect every time. Following screenshot shows a problem that I encountered when using a very popular NMR software to do baseline correction. The red line indicates zero where the base should be. It is obvious that the processed spectrum (lower figure) has a shape that differs from the original one (upper figure).

NMR Baseline Correction Problem from Commercial Software
Screenshot showing baseline correction error from using a NMR software (time domain)

 

Principle

I don’t know how does that strange shape come from since it is a proprietary software. But if we look into the principle of baseline correction, we may have a few ideas. As in the following schematic, raw data normally has an offset from zero and sometimes a slope, too. Zero order baseline correction only subtracts raw data with its mean value and moves the line to x axis. First order correction fits with a straight line (green dashed line) and subtracts the raw data by that to get rid of the slope and intercept. If the baseline has a curvature, a second or higher order treatment can also be used, but that is rarely the case in NMR.

The ideal correction should only consider the “baseline” part while ignoring the actual signal. But if the actual signal is taken into the correction, as shown in the last figure of the schematic, the corrected baseline as well as the signal will be distorted.

Principle of Baseline Correction and Problem
Principle of baseline correction and possible reason for the problem

By looking at the spectrum, one can see that the signal is always at the beginning of the time domain and decays to zero gradually. Thus, we can deal with this problem by carrying out linear fitting using only the last part of the data. I use a text file to store file names that will be processed, and use “for” loops to process as many files as necessary one after the other. When dealing with lots of raw data, this may greatly increase the efficiency of data processing. For the linear fitting, one can write one’s own algorithm according to the following linear equation.

f(x) = a*x + b

A second and more convenient way is to use MATLAB’s “polyfit” function, which generates a two-value array as its result. The first value is parameter a and the second is b in the above function.

 

A summary of the processing steps in my script
1. Read baseline.txt to get all file names of raw data, then load data into matrices;
2. Use last 1/5 or 1/4 of the data points, depending on experiment, for fitting;
3. Subtract raw data by fitted line;
4. Baseline correction for the next file til all are done;
5. Plot all experiments comparing before and after baseline correction for quick review;
6. Save baseline corrected .txt files & use original file names adding “_baseline” to the end.

The figure below is the output for the same data that we just see in the error screenshot. It’s done perfectly and the signals are well preserved.

Baseline correction results by programming in MATLAB

Improved baseline correction results by using MATLAB

A friendly reminder: pay attention to the end of the raw data because some experimental software may add additional zeros or other values after acquisition. So you may want to modify algorithm accordingly.

If my experience can be of any help to your research or inspire your great idea, I will be very happy. Please don’t hesitate to share your thoughts if you have criticisms or suggestions.

 

Related articles

Automatic Data Analysis in MATLAB for MRI and NMR T1/T2 Measurements

Advertisements
2 Comments
  1. Dear Yangwei Liu

    I am viswanathan, excited about your article and interested to try myself. But I am new to matlab. Can you give more details about the script and how to use it to my email. My email id is vsuscient@gmail.com.

    thanks

    viswanathan

    Like

Trackbacks & Pingbacks

  1. Automatic Data Analysis in MATLAB for MRI and NMR T1/T2 Measurements | Yangwei Liu's Research Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: