NIRS data analysis (GLM and visualization)

3 min read

[last updated: 2019/11/20]

Also check out NIRS data analysis (time series)

Environment requirement

  1. MatLab
  2. SPM 5 or 8
  3. xjView 8
    xjview can be downloaded for free from
    (If you are inside CIBSR, xjview  is located in /fs/fmrihome/fMRItools/Xjview)
    Add xjview to path by addpath(genpath('/fs/fmrihome/fMRItools/Xjview'))
  4. nirs2img function in this article can be obtained in the page [Link updated on 2019/10/02] or It is located in the nirs folder.
  5. NFRI toolbox [Link updated on 2013/08/13] (for standard brain registration)
    Download from and save it in a directory whose name contains no space (e.g. not in something like c:\program files\…).


  1. convert NIRS data file to csv format using ETG4000 program.
  2. copy the 3D positioning data (00X.pos). If you didn’t measure 3D positioning data, jump to step 5
  3. use NFRI toolbox (Pepe Dan, Japan. to get the MNI coordinates of each probe.  Detailed information on how to use this toolbox can be found in its manual. [Update 2015-07-13. A video tutorial:]
    1. Convert 00?.pos file to csv file using
    2. Convert the 3D positioning data into MNI space coordinate using
    3. You will get a xls file containing the positions. There are several sheets in that file and you should use the sheet called “WShatC”, which contains the positions of cortical surface.
  4. Find channel positions based on probe positions using probe2channel.m
    probe2channel(probe, config)
    Download probe2channel.m here
  5. If you don’t have 3D positioning data, you may use the template channel positions located in
    load xjview/nirs_data_sample/templateMNI.mat
    . (Please note, this file was created by Xu based on a single subject’s data. It’s useful for quick data review but it will be inaccurate to your own subjects. So you should create your own file in formal analysis. [update 2019-02-01])
    You will find 6 variables in MatLab workspace. They are channelMNI3x11  channelMNI3x5   channelMNI4x4   probeMNI3x11    probeMNI3x5     probeMNI4x4. They are all Nx3 matrix.

Read data and do GLM

  1. use readHitachData.m to read the data file (csv format). Type help readHitachData to see how to use it. Note if your input is two files (for 4×4 and 3×5 configurations), this script will automatically concatenate the data.
    [hbo,hbr,mark] = readHitachData({'XC_tap_MES_Probe1.csv','XC_tap_MES_Probe2.csv'});
  2. Prepare event onset timing, duration etc from the mark data, or external data you have, for later GLM analysis (step 3). The format is:
    • onset: onset timing of every event. a cell array. Each element is a numeric vector for one event type. Unit: second
    • duration: duration of every event. same with onset, except the meaning of numbers are duration. If the event is punctuated event, use 0 as duration. Unit: second
    • modulation (optional): modulation of event. For the same type of event you may have different intensities. For example, your event is flash of 5 levels of intensities. You can use modulation to modulate the intensity.  Format is exactly same with onset.
  3. GLM analysis using glm. Type help glm for more info.
    [beta, T, pvalue] = glm(hbdata, onset, duration, modulation);
  4. You may want to save the data for future use.
  5. (if you want to view the result in a standard brain) Convert the values (T or beta or contrasts) to an image file by nirs2img. Try help nirs2img to get more information.
    nirs2img(imgFileName, mni, value, doInterp, doXjview)


  1. plotTopoMap will plot data on a plane. The data can be T or beta or other values. Type help plotTopoMap for more info. Here is an example (note the data is smoothed by spline):
    plotTopoMap(randn(24,1), '4x4');
  2. nirs2img will convert your data to an image file which can be visuzlied by many fMRI functional image programs (such as xjview). Here is an example of visualizing the image by xjview. Note, after xjview window launches, you need to check “render view”, and then you may choose between new or old style.
    nirs2img('nirs_test.img', mni, value, 1, 1);
  3. You can also visualize the result with NFRI’s nfri_mni_plot (in NFRI toolbox). You need to prepare the the plot data in excel format beforehand. More information can be found in Readme.doc in NFRI toolbox.

Group analysis

  1. For each individual subject, perform GLM and save the beta values for each condition and subject.
  2. Do contrast  on each subject. Contrasts are simply difference of beta values. For example, contrast between 1st condition and 2nd condition is simply c = beta(:,1) - beta(:,2); Then save the contrast in an image file using nirs2img for each subject. You get a bunch of contrast images (one for each subject)
  3. Perform T test on the contrast images using onesampleT.m, or you can use SPM to do one sample T test if you prefer. You will get a T test image file.
  4. Visualize the T test image with xjview (or SPM)

第十九期 fNIRS Journal Club 通知 2021/05/29,9:30am

美国普渡大学童云杰助理教授,将为大家讲解他们组最近被接受的一篇使用近红外相位信息研究脑血流变化的文章。热烈欢迎大家参与讨论。 时间: 北京时间2021年5月29日上午9:30地点: https://zoom.com房间号: 846 8391 7517密码: 805190 童云杰教授简介:普渡大学 生物医学工程助理教授、博士生导师。主攻方向是多模态脑成像, 包括核磁,fNIRS, EEG。关注脑功能及生理信号的提取与研究。发表论文九十余篇,引用上千次(H-index = 20)。 童教授要讲解的文章如下: Liang Z, Tian H, Yang HC, Arimitsu T, Takahashi...
Xu Cui
12 sec read

第十八期 fNIRS Journal Club 视频

北京时间2021年4月25日10点,北京师范大学的朱朝喆教授为大家讲解了他们最近几年在经颅脑图谱(Transcranial brain Atlas) 方面做的工作。视频如下: Youtube: Youku: 该视频在优酷上传后被优酷屏蔽,不清楚什么原因。申诉无效。
Xu Cui
3 sec read


会议日期:2021年5月22日-24日会议地点:天津师范大学 一、 会议简介       近红外光谱脑功能成像(fNIRS)具有设备购买与使用成本低、可在自然环境条件下使用、具有较高的时间分辨率和空间定位能力等特点,受到了脑科学研究的高度重视。“近红外光谱脑功能成像学术会议”是由北京师范大学认知神经科学与学习国家重点实验室朱朝喆教授发起并组织的全国性学术会议。已连续成功举办六届,共吸引全国近百家高校、科研院所及医院的六百余名学者参加。该会议已成为国内规模和影响力最大的fNIRS脑成像学术活动。       本届会议由北京师范大学与天津师范大学联合主办。会议将延用往届会议将学术报告与研究方法工作坊相结合的模式。学术报告模块(5月22日周六)将邀请心理学与认知神经科学领域、基础与临床医学领域以及工程技术领域知名学者汇报其fNIRS最新研究成果;工作坊模块(5月23-24日)由fNIRS领域一线研究者系统讲授fNIRS成像原理、fNIRS实验设计、fNIRS数据分析与统计、fNIRS论文写作以及fNIRS前沿技术等。除理论讲授外,还设置了fNIRS空间定位与数据分析操作(NIRS-KIT软件)环节,此外还安排充足的研讨答疑时间以便与会人员交流互动。       具体日程与详细内容等最新消息请关注后续通知,可通过天津师范大学心理部网站或北京师范大学国家重点实验室网站,或者扫描下方二维码关注微信公众号-“fNIRS脑成像实验室”查阅更新信息,期盼在天津师范大学与您相聚! 二、会议组织机构 主办单位:教育部人文社会科学重点研究基地天津师范大学心理与行为研究院、天津师范大学心理学部、北京师范大学认知神经科学与学习国家重点实验室会议主席:白学军、朱朝喆组织委员会:赵春健、杨邵峰、侯鑫、曹正操 三、说明1.        学术报告模块注册费:人民币500元/人;工作坊模块注册费:人民币2500元/人。发票为电子发票,内容均为:“会议费”。两个模块各自独立收费,参会者可根据自己需要进行选择。2.        注册费包括各自模块的资料费、午餐费;其他费用自理。3.        会议报告人免除会议模块注册费,其他费用请自理。4.       ...
Xu Cui
18 sec read

29 Replies to “NIRS data analysis (GLM and visualization)”

  1. Dear Xu,

    I have been reading your writing on NIRS_SPM for a while. Thank you for contributing 🙂

    Just a short qns: do you know if there is any Help Forum on NIRS-SPM? I have been trying to use the program but I encountered some annoying errors which I couldnt solve.

    Hope you may point me to the right direction. Thanks in advance!

    – Lee from Singapore

  2. Hi Xu,
    I have been following your writings on NIRS_SPM. Your contribution have been a great help for me.
    I have been trying to learn this NIRS_SPM, but it keeps on giving me error. Is there any other material that I can use to learn NIRS_SPM apart from its user manual OR is there any help forum for NIRS_SPM.
    Hope to get help from you.
    Thanks in advance.

  3. I haven’t been using NIRS_SPM for a while. I don’t know if there is any other resources but I do know the authors are very helpful. You might want to contact them directly.

  4. Why can’t find this three function code (glm.m, nirs2img.m, onesampleT.m) in environment requirement your suggestion?

  5. Hi Xu,
    I just downloaded the code described in this section and want to play around with an ETG dataset that I did not create position data for. I’m trying to find the template position files that are referenced in:

    “If you don’t have 3D positioning data, you may use the template channel positions located in
    load xjview/nirs_data_sample/templateMNI.mat”

    But I’m having trouble. I don’t have a folder called xjview, just the xjview.m. I can’t seem to find templateMNI.mat. This may be just an issue I’m having w/ matlab, as I haven’t worked with .mat files before. I’ve only worked with .m files. Can you be more specific about how to find these template datafiles? Thanks–I’m really excited to play around with the code you have posted here:)!!

  6. Hi Xu,
    Could you please also send me the copy of those three files? glm.m, nirs2img.m, and onesampleT.m. Thank you a lot!

  7. We used “readHitachData”to read files from two probes, but an error appeared:
    “Undefined function or variable “timeindex”.
    Error in loadHitachiText (line 42)
    data(:, timeindex) = timedata;
    Error in readHitachData (line 48)
    [data, variablename] = loadHitachiText(filenames{fileindex});

    Our data have a “Time” column. Could you tell me what are the possibe reasons?

  8. Dear Prof Xu

    could you help me?
    i have some datas for nirs. i want to read these datas and use wavelet for processing. i want to know do you have any book or paper that help me how to read data in matlab and use wavelet.

    Thank you a lot!

  9. Dear Prof Xu,
    Thank you sooo much for sharing this Visualization tool of fNIRS.
    But I have a problem about the input parameter of nirs2img.
    In the 5th step of “Read data and do GLM” process,”(if you want to view the result in a standard brain) Convert the values (T or beta or contrasts) to an image file by nirs2img(imgFileName, mni, value, doInterp, doXjview)”, I don’t know why can beta or contrasts can work and get the P value in xjview. Because NIRS_SPM just use the T to get the P value. Could you please help me understand why beta and contrasts can work?

    Thank you so much!!

  10. @Yanchun Zheng
    Yanchun, nirs2img is to convert your data (whatever it is) to an image file which can be opened by xjview. It’s does not convert to p-value. In xjview, you can view the image (beta, T, contrast etc). If it’s beta or contrast, then p-value in xjview is meaningless.

  11. Hi I am at the GLM stage and cannot continue because of this erro that I am getting:

    [beta, T, pvalue, covb] = glm(hbo, {onset}, {duration});
    Undefined function ‘max’ for input arguments of type ‘cell’.

    Can you please help me?


  12. @Avi
    See source code of glm in line 84~ 86, this is where the function ‘max’ appears. You may set a stop point there to see what’s going on there.

  13. Dear Prof. Cui
    I have a question concerning following step:
    “GLM analysis using glm. Type help glm for more info.[beta, T, pvalue] = glm(hbdata, onset, duration, modulation)”. Shouldn’t we correct the serial correlation for Hb data before we estimate the beta coefficient? Hb data usually shows strong serial correlation after low pass filtering,also considering its relative high sampling rate compared to fMRI.

  14. Hi,Cuixu,
    For the nirs2img.funciton. I have a question about “value” in the function.[nirs2img(imgFileName, mni, value, doInterp, doXjview, bilateral) ]
    The Note said “value: Nx1 matrix, each row is the value corresponding to mni” , But it sill confunse me a lot.

    The value is related with girddata function ? I have try it several time, but still fail…

    How should I define this parameter?

  15. Thanks, I got it !! I could also define these value using the t-value or F-value, right? Thanks again for your help.

  16. Hi There Prof. Cui,

    Can you please direct me to the probe2channel.m script? Is it in xjView? I cannot seem to find it.


Leave a Reply

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