Calculate phase difference between two general signals (e.g. HbO and Hb) using Hilbert transform

1 min read

In a recent fNIRS journal club (vedio recorded here), Dr. Tong talked about their work on the phase difference between oxy and deoxy Hb, and its relationship with participants’ age. This article is a demo of how to use Hilbert transform to calculate the phase difference between two signals, and whether it’s valid if the signals contain a wide range of frequencies.

Tool: We use MatLab’s hilbert function to calculate the instant phase of a signal. The code can be found in the end of this article.

Case 1: single frequency

Here we tried the simplest case: x and y are single frequency signals with phase difference pi/2, as demonstrated in the figure below. We expect that the calculated phase difference is close to pi/2 or 1.57.

As expected, the phase difference is pi/2! Hilbert transform is doing well in picking up the phase difference! It is noted, however, the calculation is more robust in the middle of the signal.

Case 2: 2 frequencies

What about more complex signals? If x and y both contains two frequencies, each with phase difference pi/2, will Hilbert transform find the correct value?

Yes. It turns out Hilbert transform works for two-frequency signals as well.

Case 3: a lot of frequencies

In this case we generated x and y each with 50 random frequencies. The resulted signals are essentially random:

Again, Hilbert transform found the correct phase difference, pi/2:

People may argue that in such a wide-band case the “phase difference” is not meaningful. However, if the phase difference for each frequency is similar, then it is reasonable to say that the phase difference between the overall signals exists and is that value.

Code (MatLab)

Update: in the original version I shift phase manually (and contains an error!). Now we use unwrap and wrapToPi, suggested by Yafeng Pan, which is easier to use and produce the correct phase shift. Thank Yafeng!

% Demo of Hilbert transform to calculate phase difference between two
% signals
% Xu Cui 2021/6/7

% single frequency
t = [0:0.01:100];
x = sin(t);
y = sin(t+pi/2);
figure; plot(t, x, t, y);
legend('x','y');

xh = hilbert(x);
yh = hilbert(y);

xphase = unwrap(angle(xh));
yphase = unwrap(angle(yh));

figure; plot(t, xphase, t, yphase,'.');

phase_diff = wrapToPi(xphase-yphase);
figure; plot(t, phase_diff, '.');

% 2 frequencies
t = [0:0.01:100];
x = sin(t) + sin(2*t);
y = sin(t+pi/2) + sin(2*t+pi/2);
figure; plot(t, x, t, y);
legend('x','y');

xh = hilbert(x);
yh = hilbert(y);

xphase = unwrap(angle(xh));
yphase = unwrap(angle(yh));

figure; plot(t, xphase, t, yphase,'.');

phase_diff = wrapToPi(xphase-yphase);
figure; plot(t, phase_diff, '.');

% a lot of frequencies
t = [0:0.01:100];
x = zeros(size(t));
y = zeros(size(t));
frequency = rand(1,50)*10;
for ii=1:length(frequency)
    f = frequency(ii);
    x = x + sin(f*t);
    y = y + sin(f*t+pi/2);
end

figure; plot(t, x, t, y);
legend('x','y');

xh = hilbert(x);
yh = hilbert(y);

xphase = unwrap(angle(xh));
yphase = unwrap(angle(yh));

figure; plot(t, xphase, t, yphase,'.');

phase_diff = wrapToPi(xphase-yphase);
figure; plot(t, phase_diff, '.');

第二十三期 fNIRS Journal Club 通知 2021/09/25,10:00am

上海师范大学的张明明博士将为大家介绍团体 (三人或者之上) 超扫描领域的进展,以及他们最近发表的团体超扫描决策行为的文献。热烈欢迎大家参与讨论。 时间: 北京时间2021年9月25日上午10:00地点: https://zoom.com房间号: 894 6339 2399 密码: 264149 张博士要讲解的文章如下: Zhang, Jia, Wang (2021) Interbrain Synchrony of Team Collaborative Decision-Making: An fNIRS...
Xu Cui
9 sec read

第二十二期 fNIRS Journal Club 视频

北京时间2021年8月28日10:00,美国南卡罗来纳大学心理学系的符晓雪博士,为大家讲解她刚刚发表在Neurophotonics上的一篇文章,用NIRS研究发育过程中头皮和大脑关系的变化。https://www.storkapp.me/pubpaper/34322572 Youtube: https://youtu.be/49wmBv1d2TE Youku 优酷:上传后被屏蔽,未知原因。 相关资源: 文献鸟(追踪科学文献)文献大分析(两分钟了解一个领域)
Xu Cui
4 sec read

第二十二期 fNIRS Journal Club 通知 2021/08/28,10:00am

美国南卡罗来纳大学心理学系的符晓雪博士,将为大家讲解她刚刚发表在Neurophotonics上的一篇文章,用NIRS研究发育过程中头皮和大脑关系的变化。热烈欢迎大家参与讨论。 时间: 北京时间2021年8月28日上午10:00地点: https://zoom.com房间号: 845 7541 3964密码: 829803 符晓雪博士是南卡罗来纳大学心理学系的博士后研究员。 她在宾夕法尼亚州立大学获得发育心理学博士学位。 她的博士后工作重点是 fNIRS 和 ERP 的皮层源定位方法。 符博士要讲解的文章如下: Fu, Richards (2021) Investigating developmental changes in scalp-to-cortex...
Xu Cui
6 sec read

Leave a Reply

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