Archive for the ‘nirs’ Category

Communications between two MatLabs (1) over file

October 3rd, 2016

Ref to: Communications between two MatLabs (2): over socket

It’s common that two MatLab programs needs to communicate. For instance, one program is collecting the brain imaging data but not display them, and the other program is to display the data. (Another case is at Sometimes it is not practical to merge the two program together (e.g. to keep the code clean). In this case we can run two MatLabs simultaneously. One keeps saving the data to a file, and the other keep reading the file.

Here I played with such a setup, and find they communicate well with small delay (small enough for hemodynamic responses). Check out the video below:


for ii=1:100
    disp(['write ' num2str(ii)])

last_ii = 0;
        load data
        if(ii ~= last_ii)
            disp(['get data. i=' num2str(ii)])
        last_ii = ii;

Caveat: writing/reading to/from disc is slow. So if your experiment requires real time communication without any delay (say <1ms), this method may not work. Also, the amount of data to write/read each time should be very small, and the frequency of write should be small too. The file needs to locate in your local hard drive instead of a network drive.

———- Comments ———–
Paul Mazaika from Stanford:
Cool piece of code! There may be a way to do this with one umbrella Matlab program that calls both components as subroutines. The potential advantage is that one program will keep memory in cache, not at disk, which can support rapidly updating information. For high speeds, it may be better to only occasionally update the graphical display, which otherwise may be a processing bottleneck.

Aaron Piccirilli from Stanford:
There is, sort’ve! I think Xu’s little nugget is probably best choice for many applications, but if speed is an especially big concern then there are a couple of options that I’ve come across that will maintain some sort of shared memory.

Perhaps the easiest is to use sockets to communicate data, via UDP or TCP/IP, just like you use over the internet, but locally. You write some data to a socket in one program, and read it from that same socket in another program. This keeps all of your data in memory as opposed to writing it to disk, but there is definitely some overhead for housekeeping and to move the data from one program’s memory into the operating system’s memory then back into the other program’s memory. An added bonus here: you can communicate between different languages. If you have a logging function written in Python and a visualization program in MATLAB, they can pretty easily communicate with each other via sockets.

MATLAB doesn’t have explicit parallel computing built-in like many other languages, sadly, but we all have access here to the Parallel Computing Toolbox, which is another option for some more heavy-duty parallel processing where you have a problem you can easily distribute to multiple workers.

Finally, true shared memory might be more trouble than it’s worth for most applications, as you then have to deal with potential race conditions of accessing the same resource at the same time.


More on this topic: Please continue to read Communications between two MatLabs (2): over socket

Author: Xu Cui Categories: brain, matlab, nirs, programming Tags:

A mistake in my False discovery rate (FDR) correction script

August 8th, 2016

I have posted an FDR script at I noticed that there is a small bug. In rare cases, this bug will cause the most significant voxel to be classified as ‘non-significant’ while other voxels are ’significant’.

Consider the following example:

p = [0.8147 0.9058 0.0030 0.9134 0.6324 0.0029 0.2785 0.5469 0.9575 0.9649 0.1576 0.9706 0.9572 0.4854 0.8003 0.1419 0.4218 0.9157];

The previous script will classify p(3) as significant but p(6) as non-significant.

Here is the updated version of the script:

function y = fdr0(p, q)
% y = fdr0(p, q)
% to calculate whether a pvalue survive FDR corrected q
% p: an array of p values. (e.g. p values for each channel)
% q: desired FDR threshold (typically 0.05)
% y: an array of the same size with p with only two possible values. 0
% means this position (channel) does not survive the threshold, 1 mean it
% survives
% Ref:
% Genovese et al. (2002). Thresholding statistical maps in functional
% neuroimaging using the false discovery rate. Neuroimage, 15:722-786.
% Example:
%   y = fdr0(rand(10,1),0.5);
% Xu Cui
% 2016/3/14

pvalue = p;
y = 0 * p;

[sortedpvalue, sortedposition] = sort(pvalue);
v = length(sortedposition);
for ii=1:v
    if q*ii/v >= sortedpvalue(ii)
        y(sortedposition(1:ii)) = 1;

Author: Xu Cui Categories: brain, matlab, nirs, programming Tags:

Some tips to use wavelet toolbox

June 13th, 2016

Wavelet toolbox is a useful tool to study hyperscanning data. Many recent publications on NIRS hyperscanning use wavelet coherence to quantify the relationship between two interacting brains (e.g. Baker et al 2016, Nozawa et al 2016). You can see more information about wavelet coherence at



Here are some tips to use the toolbox:

1. It often takes a long time to run Monte Carlo simulation. You may use ‘mcc’=0 to disable it.


2. If you need to get the values of the result (instead of the graphic), you may specify the return value

[Rsq,period,scale,coi,sig95] = wtc(signal1,signal2,'mcc',0); %Rsq is a complex matrix

3. If you are only interested in a certain band, you can specify the MaxScale (i.e. ms) parameter. More information at

[Rsq,period,scale,coi,sig95] = wtc(signal1,signal2,'mcc',0, 'ms', 128);

4. If you are interested in finding the “phase” information (visualized by the arrows), you may use xwt function. The returned value is a complex matrix and you can calculate the phase.

[Rsq,period,scale,coi,sig95] = xwt(signal(:,jj),signal(:,jj+22));

5. To visualize the power of a single signal, you may use wt, which I personally feel much better than FFT.


6. To change the density of the arrows, you may specify the ArrowDensity parameter

figure;wtc(signal(:,jj),signal(:,jj+22),'mcc',0,'ArrowDensity',[30 30]);

Do you have any tips? Please let me know.

Author: Xu Cui Categories: brain, matlab, nirs, programming Tags:

Just published a paper: Men vs Women, are they different in cooperation?

June 13th, 2016

We just published a big and long study. It is a NIRS hyperscanning study aiming to investigate the brain difference between men and women during cooperation. We have scanned 222 people! And it is one of the largest NIRS study I have seen. And it is a long study. The project started in 2012 (today is 2016/6/13). You can imagine how long it took to collect and analyze the data, and to get the paper published!

After a series of rejections (Nature -> Journal of Neuroscience -> PNAS -> NeuroImage), this paper was finally published in Scientific Reports this month (2016/06). The full-text of this paper is accessible via PubMed Central.

A cooperation game

Two lab members are playing the cooperation game

After the paper was published, it has attracted a lot of media attention. For example, Stanford news center has reported “Study finds differences in male, female brain activity when it comes to cooperation” (below).

Stanford News Center

Stanford News Center

Two major contributors of this study are Ning Liu and Joe Baker.

Ning Liu

Ning Liu

Joe Baker

Joe Baker

Author: Xu Cui Categories: brain, nirs Tags:

Hitachi ETG4000 on ebay, for less than $10,000

June 6th, 2016

In our lab meeting today we accidentally discovered that you can actually purchase a used ETG4000 on ebay! The seller asked for $9,995. When we purchased ETG4000 back in 2007, it costed us about half million!

Check it out:

Author: Xu Cui Categories: brain, nirs Tags:

fNIRS 2016

June 6th, 2016

fNIRS 2016 conference will be held in Paris, October 13 – 16, Université Paris Descartes, 12 rue de l’Ecole de Médecine, Paris 75006, FRANCE

Check out the society’s home page:

The 2016 conference will take place in central Paris, October 13 – 16, 2016.

As with past meetings, there will be a day-long course prior to the start of the conference, on Thursday, October 13.

The conference is co-chaired by Judit Gervain and Joseph Culver.

Maria Angela Franceschini (MGH, Harvard Medical School, Boston, USA) – Keynote
Sol Diamond (Dartmouth College, USA)
Frédéric Dehais (ISAE, Toulouse, France)
Ursula Wolf (Bern University, Switzerland)
Frédéric Lesage (McGill University, Canada)
Gorm Greisen (University of Copenhagen, Danemark)
Yasuyo Minagawa (Keio University, Japan)
Ippeita Dan (Jichi Med. University, Japan)
Chuck Nelson (BCH, Harvard Medical School, Boston, USA)

Abstract submission opens: May 1st, 2016
Abstract submission ends: June 10th, 2016 (midnight GMT)
Notification to authors: Late July 2016
Early registration ends: Sept 10th, 2016

Author: Xu Cui Categories: nirs Tags:

Who cited my paper?

May 9th, 2016

Back in 2010 we published a paper titled “Functional Near Infrared Spectroscopy (NIRS) signal improvement based on negative correlation between oxygenated and deoxygenated hemoglobin dynamics“. It is about a really simple method which surprisingly works well in reducing motion artifact (noise) in NIRS data. As of today (2016-05-09), the paper has been cited by more than 130 times.

Who cited this paper? Well, we created this map to show where the authors are:

If you clicks the red dots you will find the detailed information about the paper, such as the author name and journal.

Then who are the top researchers who cited this paper? We count the number of citation for each author, and rank them by the number of papers. Here are the top authors who cited our 2010 paper:

Author Number of Papers
Fallgatter, AJ 9
Ehlis, AC 8
Hong, KS 8
Dresler, T 8
Herrmann, MJ 7
Scheutz, M 6
Strait, M 5
Boas, DA 5
Sato, H 4
Scholkmann, F 4
Herff, C 4
Seghouane, AK 4
Ge, SS 4
Shah, A 4
Katura, T 4
Molavi, B 4
Wolf, M 4
Author: Xu Cui Categories: brain, nirs, programming Tags:

NIRS manufactures locations

May 4th, 2016

Where are the major NIRS device manufactures? You can see from the map below.

There are NIRS manufactures in US, Japan, Europe and Korea.

If you are a NIRS manufacture and would like to add to this list, please let me know (leave a comment in this post).
If you are interested in creating a map like this, please let me know (leave a comment in this post).

Related: NIRS manufactures products

Author: Xu Cui Categories: brain, nirs Tags:

Projection of a NIRS channel on a brain surface

April 11th, 2016

When analyzing the data in our concurrent NIRS-fMRI study, we are particularly interested in how the NIRS signals were correlated to the fMRI signal. To answer the question we need to create an ROI (region of interest) in brain which is directly underneath the NIRS channel (which is on the skull). So a projection of the NIRS channel on the brain surface is necessary.

It might be easy if the brain were a perfect sphere, or at least doable if it is smooth. But brain surface is anything but smooth. What we did is:

  1. Create a brain surface mask. This is easy using BET (
  2. Loop over all voxels on the surface, calculate the distance between the NIRS channel (point A) and the voxel
  3. Find the voxel which is closest to the NIRS channel (It is point B in the figure below)

Projection of NIRS channel on brain surface

Projection of NIRS channel on brain surface

Author: Xu Cui Categories: brain, matlab, nirs, programming Tags:

Is measuring both oxy and deoxy-Hb useful?

March 28th, 2016

One of the advantages of NIRS is that it measure two values at a time - both oxy- and deoxy-Hb concentration, while fMRI measure only one value, called BOLD. (See a comparison between NIRS and fMRI.) However, what is the use of measuring two values if they are highly correlated (actually anti-correlated)?

This is a valid question. In our 2010 paper titled “Functional Near Infrared Spectroscopy (NIRS) signal improvement based on negative correlation between oxygenated and deoxygenated hemoglobin dynamics“, we have shown that (1) theoretically (using Balloon model) oxy and deoxy Hb should be highly correlated, close to -1; (2) with real data, they are highly correlated. See the figure below with real data (I was the subject).

oxy and deoxy-Hb are highly (anti)correlated

oxy and deoxy-Hb are highly (anti)correlated

Now go back to our question: if they are highly correlated, maybe measuring both does not bring additional advantage. If we look at NIRS publications, we see most publications only use either oxy-Hb or deoxy-Hb in their analysis, or at least use them separately. It is not very clear why we should use both.

However, it turns out it is an advantage to measure both. In a study we have tried to “predict” if the subject is doing finger tapping or not by looking at the NIRS signal only (see the paper). This is useful for brain-computer-interface purpose. In the figure below, you will see that if we use only oxy-Hb (blue) or deoxy-Hb (green), the prediction accuracy is lower than if we combine them (red).

Combined oxy- and deoxy-Hb predict better

Combined oxy- and deoxy-Hb predict better

This means while oxy- and deoxy-Hb are highly correlated, we still get more if we measure both. I think part of the reason is that there are always noise in real data. With two measurement the signals are more robust again the corruption of noise.

Author: Xu Cui Categories: brain, nirs Tags: