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, '.');



写作助手,把中式英语变成专业英文


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

第五十一期fNIRS Journal Club通知2024/05/11, 10am 雷心博士

机器人通常以合作者的身份出现,以礼貌、鼓励、友好的方式与人类互动,但如果有一个竞争导向的机器人出现会如何?人们更喜欢与合作导向的机器人互动,还是更愿意通过竞争来激发自身动力呢?当我们与机器人合作或竞争
Wanling Zhu
13 sec read

第五十期fNIRS Journal Club视频 王一晖

Youtube: https://youtu.be/a2QlCFZUytA优酷:https://v.youku.com/v_show/id_XNjM3MjMyNjUxMg==.html 早期的 STE
Wanling Zhu
13 sec read

第五十期fNIRS Journal Club通知2024/03/30, 10am 王一晖

早期的 STEM 教育对于以后的学习至关重要。现有研究尚未就STEM教学法达成共识,学生先验知识对基于故事的STEM教学法的影响还有待探讨。来自澳门大学张娟教授团队的王一晖将会分享基于fNIRS超扫描
Wanling Zhu
9 sec read

One Reply to “Calculate phase difference between two general signals (e.g. HbO…”

Leave a Reply

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