Home > brain, matlab, nirs > Analyze NIRS data with NIRS-SPM

Analyze NIRS data with NIRS-SPM

November 18th, 2008

being updated for v01_r14 …

NIRS-SPM is a SPM5 and MATLAB-based software package for statistical analysis of near-infrared spectroscopy (NIRS) signals, developed at the Bio Imaging Signal Processing (BISP) lab. at KAIST in Korea.

prepare files needed by NIRS-SPM

  1. NIRS data (in csv format or nii format. For Hitachi ETG4000, you need “File out” the measurement data, not the Hb data, into csv format)
  2. (optional) Structural image (e.g. xu.img). I use SPM2 to do normalization and segmentation (below), but you can choose whatever tool you prefer.
    1. normalize the image (use template T1.mnc) and get two files, wxu.img and xu_sn.img. This step takes 2 min.
    2. segment the image (not the normalized image) into gray and white volumes and get 3 files (xu_seg1.img, xu_seg2.img and xu_seg3.img). They are gray, white, and CSF volumes. The first two images will be used in NIRS-SPM. This step takes ~6 min.
  3. (optional) Coordinate file (a plain text file, e.g. RealCoordinate.txt), from probe positioning system
    This file contains 3 columns, with each row a 3-D coordinate of a point measured with 3D digitizer. The total number of rows is the sum of landmarker points (nasion, inion, etc)  plus the number of optodes. You will have to specify the two numbers in NIRS-SPM.

    • If you don’t have 3d digitizer at all but you know the positions of each optodes in your structural image (e.g. you used vitamin E marker), you can simply find the positions using SPM’s display functionality and put into the file.
    • If you don’t have MRI image at all, you can use a standard brain image. You need to check how well the registration is visually.
    • If you don’t have MRI image, and you didn’t measure optode positions with 3D digitizer, but you take a picture of the subject, you can use a standard brain, and “guess” the optode coordinate with the picture.
    • If you don’t have MRI image, or 3D digitizer measurement, or a picture, then you can use a standard brain, and guess, and cross your finger.
  4. The event onset timing and duration (in seconds). For example, [12 34 55 67] etc.

NIRS-SPM system requirement:

  1. MatLab (with graphic display)
  2. SPM5 (note: not SPM2)


  1. (optional) Login scuttlebutt (even you are on scuttlebutt computer)
    ssh fmri@scuttlebutt -Y
  2. Start matlab with graphic support and spm5 in path
    ml7spm5 -jvm &
  3. add NIRS-SPM folder into MatLab path
    (You need to replace NIRS-SPM path to your own path)
  4. Enter sample_data directory (optional)
    cd /net/cibsrdata/Volumes/SPNLData05/quarry/cuixu/NIRS/sample_data
  5. run NIRS-SPM and the main window pops up
  6. Click “Convert” button, data conversion window pops up
  7. Select Hitachi ETG-4000
    Select the csv file, conversion begins automatically. It will take ~20s
    Click “Save .mat file” button, and save the file as “converted_NIRS.mat”
    Close data conversion window.
  8. (optional) Display NIRS data.
    Click “Display NIRS Time Series” button in the main window. NIRS_TimeSeries_Viewer window pops up.
    Click “Specify NIRS(.mat) file” button and select “converted_NIRS.mat”. Time series of channel 1 is displayed.
    Click “Specify model parameter” button and input the vector of onsets and duration
    Close NIRS_TimeSeries_Viewer window
  9. Select “With MRI” and Click “Spatial registration” button, two windows pops up.
    Select “T1_MRimage/uniform.img” as T1 image
    Select “wuniform.img” as normalized T1 image
    Then a SPM big window pops up and the subject’s T1 is shown.
    Find point (-70 34 36) and click + (you will see the point is added into “Indicator Locations” window)
    Repeat for other points (-82 -31 36), (-1 85 -15 nasion), (-8 -86 -58 inion)
    In “Indicator Locations” window, enter 4 and 16 for “Indicator #” and “Optode #”
    Click “Select Real Coordinate File” button and select file “RealCoordinate_txt_format.txt”. The locations will be displayed.
    Click “Get Optode Pos. in MRI” button and select “T1_MRimage/uniform_sn.mat” file. Also select the gray and white file (”T1_MRimage/c1uniform.img” and “T1_MRimage/c2uniform.img”)
    After ~30s the coordinates in MRI is calculated.
    Click “View Channel Pos” button you will see the positions of channels
    Click Save button and save the position as “channel_position.mat”
  10. Click “Specify 1st Level” button and “NIRS_Specification” window pops up
    Select nirs data file “converted_NIRS.mat”
    Create directory “spm” and Select it
    Select “Oxy-Hb”.
    Click “Specification” and specify the following parameters
    “hrf (with time and dispersion derivatives”
    number of conditions: 1
    name for condition 1: right finger tapping
    vector of onsets: [42:51:501]*9.75
    duration: 21*9.75
    high-pass filter: 60
    low-pass filter: Gaussian
    Gaussian FWHM: 4
    Correct for serial correlations? none
    Then a big SPM window pops up with design matrix
    Close the windows except for the main window
  11. Click “Estimate” button in the main window, NIRS_Estimation window pops up
    Select “Individual Analysis”
    Select “SPM_indiv_HbO.mat” as SPM.mat
    Click “Estimation” button. It will take 7 min. File “V_indiv_HbO.mat” is saved.
  12. Click “Result NIRS” and NIRS_Results_Viewer window pops up
    Select SPM_indiv_HbO.mat
    Select channel_positoin.mat
    Click “Contrast”, SPM contrast manager window pops up
    Click “Define new contrast”. name: finger, type: t, contrast 1 0 0 0, click OK, click “Done”
    After ~20s T map is shown. Click “View the thresholded T-statistic” and try different p-values.
  13. Click “Result fMRI” and select SPM.mat in fMRI_result folder
    (can’t get this to work now)

files required:

  1. NIRS_data_finger_tapping.nir
  2. RealCoordinates_txt_format.txt
  3. uniform.img : subject’s structural image
  4. wuniform.img : subject’s structural image after normalization
  5. uniform_sn.mat : the mat file produced by SPM during structural image normalization
  6. c1uniform.img : gray volume of segmentation
  7. c2uniform.img : white volume of segmentation
  8. SPM.mat (for fMRI and NIRS comparison)
  9. spmT_0001.img (for fMRI and NIRS comparison)

inside the files:

  1. .nir file is simply a text file. Row is time and column is channel.
  2. .csv file is also text file exported from Hitachi ETG4000
  3. RealCoordinates.txt is a text file with each row a point coordinate.
  4. converted_NIRS, or nirs data after conversion is a mat file with a single variable nirs_data. nirs_data is a struct. An example is:
              oxyData: [5384x24 double]
              dxyData: [5384x24 double]
                   fs: 9.7500
           timeLength: 14
                  nch: 24
             lightNum: 2
        rawdataLength: 5384
  5. channel_position.mat, or the channel’s position after calculation is a mat file with a single variable, preproc_info. An example is:
           wT1_info: [1x1 struct]
        rend_ch_pos: {1x6 cell}

    wT1_info is the volume information of the normalized brain (you can get this info by spm_vol). rend_ch_pos is a cellarray. Each element contains a different view of the brain. Try
    hold on;plot(preproc_info.rend_ch_pos{4}.rchn,preproc_info.rend_ch_pos{4}.cchn,'o')
    You will see how the channel positions are displayed.

  6. SPM_indiv_HbO.mat, containing estimation parameters and info
  7. V_indiv_HbO.mat, a matlab mat file containing variable V_nirs, which is a sparse matrix.

related links:

Author: Xu Cui Categories: brain, matlab, nirs Tags:
Try Stork, a research tool we developed

Stork is a publication alert app developed by us at Stanford. As a researcher we often forget to follow up important publications - and it's practically impossible to search many keywords or researchers' names everyday. Stork can help us to search everyday and notifies us when there are new publications/grants. How Stork helped me?

About the author:

Xu Cui is a human brain research scientist in Stanford University. He lives in the Bay Area in the United States. He is also the founder of Stork (smart publication alert app), PaperBox and BizGenius.


He was born in He'nan province, China. He received education in Beijing University(BS), University of Tennessee (Knoxville) (MS), Baylor College of Medicine (PhD) and Stanford University (PostDoc). Read more ...
  1. Megan
    May 22nd, 2012 at 12:08 | #1

    Hi Xu,

    This looks really great!

    Is the NIRS-SPM script for converting raw data to hemoglobin specific to continuous wave nirs devices, or can it be adapted for frequency domain?


  2. May 22nd, 2012 at 13:53 | #2

    I believe it can do both - but NIRSPM developer should have a definite answer.

  3. ingo
    August 31st, 2012 at 02:17 | #3

    Hi Cui,

    thanks for these detailed instructions.
    I have another question:
    how about the baseline correction. i have more than one time point that i measure a baseline to use it as a contrast to a motor condition. in the dialog in NIRS-SPM “enter baseline”: can I enter there a vector for many baselines and does NIRS-SPM calculate a mean baseline then?

    thank you, ingo

  4. August 31st, 2012 at 09:42 | #4


    Unfortunately I don’t know the answer. I suggest you contact NIRS SPM developer for an accurate answer.

  5. Irene
    November 25th, 2014 at 07:36 | #5

    Thank you a lot for this work, is very useful, specially when you are a beginner. I have a question about the group analysis: I am trying to do the group analysis and there is an option that I have not clear what is the minimum number of overlapped individual subjects. If I have already 9 subjects, how should I determinate ?
    Thank you

  6. Eidan
    December 25th, 2016 at 06:42 | #6

    Hello Xu,
    I’m using SPM5/8 and NIRS_SPM software and ETG-4000, I’m having trouble to find the SPM_indiv_HbO.mat for SPM_nirs.mat function in the Results NIRS button. can you tell me where to find it?

  7. December 30th, 2016 at 13:25 | #7

    Eidan, I suggest you contact NIRS SPM’s developer. I have not used the software for a long time and not sure if they have updated it.

  8. neuron
    November 3rd, 2017 at 21:02 | #8

    Could you send the whole NIRS-SPM files to me?Cause I can’t access the download pages.My e-mail address is 2248290284@qq.com.Thank you very much.

  9. neuron
    November 4th, 2017 at 20:24 | #10

    Can I start NIRS-SPM without the data files?Thank you.Hello Xu.
    When I input the NIRS-SPM in the command window of MATLAB,it shows Undefined function or variable ‘nirs’.But I’ve already set the path in a correct way.I mean, Is there need any connection between my computer and NIRS machine?And how can I work it out?
    Thank you.

  10. November 4th, 2017 at 21:08 | #11

    I suggest you contact NIRS SPM’s developers directly. I have not used the software for a long time.

    I don’t think you need to connect to the nirs machine to use NIRS-SPM.