Raspberry Pi for research labs (2) Connecting an accelerometer

June 29th, 2015

Raspberry Pi for research labs (1)

We recently used a smartphone to measure participants’ head motion during an NIRS experiment and got decent results. Smartphone is easy to use, but its size is relatively bulky on participants’ head. Is it possible to use a much smaller device?

In this summer, Joe Baker and Semir Shafi in our lab at Stanford tackled this problem with Raspberry Pi and a standard alone accelerometer. Raspberry Pi costs ~$40 and the accelerometer costs ~$20. So the total investment is ~$60. Not bad.

Joe and Semir

Joe and Semir

The accelerometer was purchased from adafruit.com. The size of the accelerometer is like a quarter, fairly small and would have no effect on a participant’s head.



Semir connected Raspberry Pi, the accelerometer, a monitor and a keyboard/mouse. He then developed a python program to read the data from the accelerometer and displayed it in real time.

Raspberry pi and accelerometer

Raspberry pi and accelerometer

How did it work? Let’s see a real demonstration by Semir. As you see in the video, while the program is running, Semir took the accelerometer back and forth. The x, y, and z data from the accelerometer is displayed on the screen in the real time.

According to Joe, this accelerometer can capture data at 100Hz, much faster than a smartphone’s accelerometer. Besides, it’s easier to integrate with other devices because Raspberry Pi is highly programmable. For example, it is possible to trigger the measuring with an external program so different data sources can be synchronized.

What is more, it’s possible to integrate Raspberry Pi/Accelerometer system with a tablet (e.g. Microsoft Surface or iPad, or even a smartphone). This will make the system much easier to use.

Author: Xu Cui Categories: brain, nirs Tags:

Movie: blood flow increases in brain motor cortex during finger tapping

June 22nd, 2015

Ever wondered what happened to your brain when you tap your finger?

finger tapping

finger tapping

See this movie:

Link: http://youtu.be/KN3MPtXlOH8

In the above movie I used topo software (by Hitachi) to visualize the blood flow changes in the brain. The data was collected by Hitachi ETG 4000 and the subject was myself. I was doing a finger tapping task. The probe position was measure by 3D digitizer. You can see that during finger tapping (right hand) the blood flow in the left motor cortex increased.

The movie itself is made using http://www.screencast-o-matic.com/

Author: Xu Cui Categories: brain, nirs Tags:

Excel tip: how to unhide the first column

June 8th, 2015

When you hide column A, you may have assumed it’s very easy to unhide it later. It’s not true. Here is how you unhide column A:

1. Type “A1″ in the cell selector box, press Enter
2. click “Format” in the cells tool bar group
3. Click “hide&Unhide” in the menu, and select unhide.

unhide first column in Excel

Author: Xu Cui Categories: life Tags:

An interesting gamble

May 11th, 2015

The other day I was walking on a street, along which there are a lot of booths where people play games to gamble. I stopped in front of one booth. The host was warm and we started to talk.

“How to play?”, I asked.

“Well, simple.” He explained, “You pay $10 to play once. Then you toss a coin until you get a ‘head’ and the game is over. If you get a ‘head’ in the first toss, then I will pay you $1; if in the 2nd toss, I will pay you $2; 3rd I will pay you $4; … and nth I will pay 2^(n-1) dollars.”

“Interesting!” I then started to calculate if this game is fair to me. I needed to calculate the expected return from this game. If it’s larger than $10, then I win; otherwise I will lose. So what is the expected return?

The probability to get ‘head’ in the first toss is 1/2, 2nd time 1/4 and so on. So the expected return is 1×1/2 + 2×1/4 + 4×1/8 + … = 1/2 + 1/2 + 1/2 + 1/2 + … and the sum is infinity!

My return is infinity! And it’s much larger than $10 dollars. For sure I will play. I might become a millionaire today. I count my money in my wallet and I have $100.

“I am in!”, I told the host.

“Good decision.”, He similed.

Then I started to play. The first game I was unlucky. I got the head in the first toss. But it’s the risk I have to take to become a millionaire. So I keep playing.

Before I knew I already spent all my $100! I only won about $50. But at this point nothing could stop me from becoming a millionaire. So I keep playing until I lost all my money.

I was very disappointed, but I was more confused. The expected return is infinity, but why did I lose money? Did I make a mistake in the calculation?

Author: Xu Cui Categories: fun, life Tags:

How to label each point in MatLab plot?

April 27th, 2015

How to label each data point in a MatLab plot, like the following figure?

label data in MatLab plot

label data in MatLab plot

MatLab code:

x = [1:10];
y = x + rand(1,10);

figure('color','w'); plot(x,y,'o');
a = [1:10]'; b = num2str(a); c = cellstr(b);
dx = 0.1; dy = 0.1;
text(x+dx, y+dy, c);

It also works on 3D plot:

label data 3d

label data 3d

Adopted from http://www.mathworks.com/matlabcentral/answers/97277-how-can-i-apply-data-labels-to-each-point-in-a-scatter-plot-in-matlab-7-0-4-r14sp2

Author: Xu Cui Categories: matlab Tags:

SVM regression on time series, is there a lag?

March 23rd, 2015

It would be nice if we can predict the future. For example, give the following time series, can we predict the next point?

Let’s use SVM regression, which is said to be powerful. We use the immediate past data point as the predictor. We train our model with the first 70% of data. Blue and Black are actual data, and Red and Pink are predicted data.

The prediction in general matches the trend. But if you look closely, you see that the predicted data is always lagging the actual data by one time step. See a zoom in below.

Why does this lag come from?

Let’s plot the predictor and the predicted (i.e. the current data point vs the next data point):

It looks normal to me.

It took me a few hours to think about this. Well, the reason turns out to be simple. It’s because our SVM model is too simple (only taking the last data point as predictor): if a data has a increasing trend, then the SVM model, which only consider the immediate history, will give a high predicted value if the current data value is high, a low value if the current data value is low. As a consequence, the predicted value is actually more similar to the current value - and that gives a lag if compared to the actual data.

To reduce the lag, you can build a more powerful SVM model - say use the past 2 data points as the predictor. It will make a more reliable prediction - if the data is not random. See below comparison: you can easily see the lag is much smaller.

Source code can be downloaded here test_svr. Part of the source code is adapted from http://stackoverflow.com/questions/18300270/lag-in-time-series-regression-using-libsvm

Author: Xu Cui Categories: brain, matlab Tags:

NIRS can’t measure deep brain, … maybe it can!

February 27th, 2015

Is NIRS able to measure signal from deep brain structure, such as amygdala? The short answer is no. NIRS is only able to measure the surface of the brain. This is a serious limitation of NIRS compared to fMRI which is able to measure the entire brain.

However, brain is a highly connected network. Deep brain is not isolated from the surface. So maybe we can infer the deep brain activity based on the surface. And this is exactly what we have done.

In a recent publication titled “Inferring deep-brain activity from cortical activity using functional near-infrared spectroscopy”, we used concurrent fMRI-NIRS technology to measure both deep and surface brain activity and explored the possibility to infer deep brain based on surface brain activity measure by NIRS. The result is very encouraging - we are able to infer deep brain from surface activity (correlation ~0.7).

This paper is published in Biomedical Optics Express with Dr Ning Liu, a NIRS expert, as the first author. You can find the paper at http://www.opticsinfobase.org/boe/fulltext.cfm?uri=boe-6-3-1074&id=312512


Functional near-infrared spectroscopy (fNIRS) is an increasingly popular technology for studying brain function because it is non-invasive, non-irradiating and relatively inexpensive. Further, fNIRS potentially allows measurement of hemodynamic activity with high temporal resolution (milliseconds) and in naturalistic settings. However, in comparison with other imaging modalities, namely fMRI, fNIRS has a significant drawback: limited sensitivity to hemodynamic changes in deep-brain regions. To overcome this limitation, we developed a computational method to infer deep-brain activity using fNIRS measurements of cortical activity. Using simultaneous fNIRS and fMRI, we measured brain activity in 17 participants as they completed three cognitive tasks. A support vector regression (SVR) learning algorithm was used to predict activity in twelve deep-brain regions using information from surface fNIRS measurements. We compared these predictions against actual fMRI-measured activity using Pearson’s correlation to quantify prediction performance. To provide a benchmark for comparison, we also used fMRI measurements of cortical activity to infer deep-brain activity. When using fMRI-measured activity from the entire cortex, we were able to predict deep-brain activity in the fusiform cortex with an average correlation coefficient of 0.80 and in all deep-brain regions with an average correlation coefficient of 0.67. The top 15% of predictions using fNIRS signal achieved an accuracy of 0.7. To our knowledge, this study is the first to investigate the feasibility of using cortical activity to infer deep-brain activity. This new method has the potential to extend fNIRS applications in cognitive and clinical neuroscience research.

Author: Xu Cui Categories: brain, nirs Tags:

How to download emails in Gmail?

February 20th, 2015
  1. Log in to your gmail
  2. On the top-right corner, you will find your own icon, click it. A pop up window will show
  3. Click “Account”
  4. You will see a page with a lot of options. Scroll down and find Account tools, then click “Download data”
  5. Select Mail and download your emails in mbox format. The file can be opened with any text editor (e.g. Notepad ++).
Author: Xu Cui Categories: life Tags:

Using a smartphone to measure head motion in a NIRS experiment

February 14th, 2015

Sensitivity of fNIRS measurement to head motion: An applied use of smartphones in the lab

Sensitivity of fNIRS measurement to head motion: An applied use of smartphones in the lab

Is it possible to use a smartphone to measure head motion in a NIRS study? Is it reliable? After all, smartphones are so popular right now and everybody has it. It would make head motion measurement much more convenient than a traditional stand-alone accelerometer if the answers to the above questions are yes.

The good news is, the answers are YES!

In our recently published paper, we demonstrated that a NIRS researcher can easily attach a smartphone to a participant’s head, measure the motion data (3-D), export and analyze the data, and integrate with NIRS measurement.

The title of the paper is “Sensitivity of fNIRS measurement to head motion: An applied use of smartphones in the lab“. The full-text can be found here.



Powerful computing capabilities in small, easy to use hand-held devices have made smart technologies such as smartphones and tablets ubiquitous in today’s society. The capabilities of these devices provide scientists with many tools that can be used to improve the scientific method.


Here, we demonstrate how smartphones may be used to quantify the sensitivity of functional near-infrared spectroscopy (fNIRS) signal to head motion. By attaching a smartphone to participants’ heads during the fNIRS scan, we were able to capture data describing the degree of head motion.


Our results demonstrate that data recorded from an off-the-shelf smartphone accelerometer may be used to identify correlations between head-movement and fNIRS signal change. Furthermore, our results identify correlations between the magnitudes of head-movement and signal artifact, as well as a relationship between the direction of head movement and the location of the resulting signal noise.


These data provide a valuable proof-of-concept for the use of off-the-shelf smart technologies in neuroimaging applications.


  • Near-infrared spectroscopy;
  • fNIRS;
  • smartphone;
  • technology;
  • neuroimaging;
  • accelerometer
Author: Xu Cui Categories: brain, nirs Tags:

How to split a PDF file for free?

February 13th, 2015

I have a PDF file which contains many pages. For some reason I need to send only page 3 and 5 to a friend. How can I create a PDF which contains only page 3 and 5?

It turns out using Google Chrome is very easy (and free)! I simply open the big PDF in Chrome (drag-n-drop the file to Chrome) and then print the file to a new PDF with only page 3 and 5.

Below is a video tutorial.

Author: Xu Cui Categories: life Tags: