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

**Background**

Relaxation is one of the most important information in magnetic resonance imaging (MRI) and nuclear magnetic resonance (NMR) spectroscopy for medical applications, material characterizations, and reaction mechanism studies. Relaxation can be measured by using two separate processes which generate longitudinal relaxation time, T1, and the transverse relaxation time, T2, respectively. Both T1 and T2 measurements involve carrying out 2D NMR/MRI experiments during which a series of spectra are measured using different time delays (tau) for each spectrum.

**Problem**

The procedures to get T1 and T2 values are not so straightforward. Even worse, these procedures are often extremely time consuming, tedius, and requiring use of multiple software. Conventional data processing for T1 and T2 includes following steps.

1. Baseline correction for each spectrum with changing time delay;

2. Phase adjustment for each spectrum;

3. For T2, find peak position for each spectrum (fixed point for T1);

4. Read peak height for real signal and write down the values;

5. Input tau & peak height into scientific software such as Igor or Origin to plot figures;

6. Need to manually choose initial values for fitting to get T1/T2.

Doing all these steps could take as long as 30-40 min for a single T1/T2 experiment depending on how experienced the person is with this process.

**Programming in MATLAB to Automatically Process T1/T2 Data**

A typical experiment contains ten to twenty spectra. The first thing in processing is exporting raw data as a single text file. Then copy the table of tau values with data file name into a .txt file. The raw data and this text file will be loaded by MATLAB using “*textread*” function. Remember to look into data file to find how many header lines need to be skipped.

The next step is baseline correction. As discussed in a previous article, data acquisition software that come with the instrument could have incorrect baseline correction results in some cases. So we can use our own algorithm as the first part of T1/T2 data processing. We can plot the spectra before and after correction for a brief review. Then adjust phase or alternatively use magnitude values by calculating square root of the sum of real square and imaginary square. Using the tau values from the text file to read the signal at tau position for T2 or at fixed point for T1. When we got the values, generate Excel file containing tau and (echo max) values we just read using “*writetable*” function which can assign file name and the starting cell to write. In order to get T1/T2, we need to plot Echo vs tau graph and fit the curve by following equations.

**T1**

y=a-b*exp((-1)*x/T1)

**Scripts for fitting**

eq=fittype(‘a-b*exp((-1)*x/T1)’);

T1f=fit(tau,Signal,eq);

Please note that for saturation recovery, the fitting function for T1 is y = a*(1-exp(-x/T1)). However, if you do inversion recovery where the initial magnetization is inverted, then you will have a coefficient 2 in front of the “exp”. In order to combine these cases, I used a “b” in a generalized function as shown above.

**T2**

y=a*exp(b*x), coefficient b=-1/T2

**Scripts for fitting**

fit(tau,EchoMax,’exp1′);

Then use “*coeffvalues*” to get the fitted parameters from “*T1f*“. One can also give some initial values to the fitting fuction for better results. Lastly, save the T1/T2 graphs with fitting curves as .png or other type of image files and write the T1/T2 values in the same Excel file that generated previously.

**A summary of the processing steps in my script**

1. Read .txt parameter file and data file;

2. Baseline correction for real and imag data in each experiment;

3. Draw graphs for spectra before and after correction for review;

4. Phase adjustment or calculate magnitude values from baseline corrected real and imaginary data;

5. Read tau values from parameter file and read Mag signal at fixed point (for T1) or Echo Max (for T2);

6. Generate a table of tau and Echo and save as a Excel file;

7. Draw Echo vs tau graph and fit the curve to get T1/T2 values;

8. Save the T1/T2 graph with fitting curve as a .png file;

9. Write the T1/T2 values in the same Excel file.

By programming in MATLAB, we can minimize steps that need manual operations. The new procedure only contains 3 steps.

1. Export 2D experiments as a text file as usual;

2. Copy tau table into tau.txt;

3. Open MATLAB and hit Run.

Export data from TNMR software and copy tau table and file name into text file (I use TNMR software as an example since it’s the one that works with my NMR spectrometer. There are a bunch of other choices.)

Besides, as a fun part, one can add a dialog using “*msgbox*” to show summary of performed tasks and calculate time for processing. As we can see, the whole procedure only takes 7 seconds to finish. T1/T2 data processing becomes a breeze.

Baseline corrected spectra and summary dialog

Generated Excel files containing T1/T2 fitting results

Generated T1 graph with fitting curve (inset figure: manually done whole process for comparison)

Generated T2 graph with fitting curve (inset figure: manually done whole process for comparison)

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**

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

## Trackbacks & Pingbacks