Analyze NIRS data with NIRS-SPM

4 min read

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:


Want to receive new post notification? 有新文章通知我

第五十三期fNIRS Journal Club通知2024/06/22, 10am 李洪

Wanling Zhu
8 sec read

第五十二期fNIRS Journal Club视频 周欣博士

Youtube:优酷: 自闭症特质(A
Wanling Zhu
12 sec read

第五十二期fNIRS Journal Club通知2024/06/01, 10am 周欣博士

自闭症特质(Autistic traits)影响人与人之间的社交互动,但该影响背后的神经机制仍然有待研究。来自香港中文大学的周欣博士将分享团队利用近红外超扫描技术研究不同互动场景下脑同步与自闭特质之间
Wanling Zhu
8 sec read

16 Replies to “Analyze NIRS data with NIRS-SPM”

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

  3. 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

  4. 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?

  5. @Eidan
    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.

  6. 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.

  7. @neuron
    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.

  8. Hello Xu,

    Thanks for detailed steps.
    I was working on the nirs_spm temporal processing. And my time_series views doesn’t display the Load button. I tried to debug and look like the panel covers all the below button. Could you please share your nirs_timeseries viewer.m file .

    Thank you

    1. Binal,

      I belive NIRS_TimeSeries_Viewer was provided by NIRS-SPM. Please feel free to ask the develop for the code.


      1. I downloaded entire folder from spm website. Looks like there is bug with this file during update.So was asking if you could provide the file.


          1. Thank you so much Xu for providing the matlab file and quick response.
            I would need the timeseries_viewer.fig file also for the code to work.

            Thanks again. I really appreciate your time and help.

Leave a Reply

Your email address will not be published. Required fields are marked *