-
Notifications
You must be signed in to change notification settings - Fork 34
/
GREEN_VERKRUYSSE.m
156 lines (128 loc) · 5.59 KB
/
GREEN_VERKRUYSSE.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function [BVP, PR, HR_ECG, PR_PPG, SNR] = GREEN_VERKRUYSSE(VideoFile, FS, StartTime, Duration, ECGFile, PPGFile, PlotTF)
% GREEN_VERKRUYSSE The Green-Channel Method from: Verkruysse, W., Svaasand, L. O., & Nelson, J. S. (2008). Remote plethysmographic imaging using ambient light. Optics express, 16(26), 21434-21445. DOI: 10.1364/OE.16.021434
%
% Inputs:
% VideoFile = Video file path.
% FS = Video framerate (fps).
% StartTime = Timepoint at which to start process (default = 0 seconds).
% Duration = Duration of the time window to process (default = 60 seconds).
% ECGFile = File path to corresponding ECG data file (.mat) containing: 1) The waveform - ECGData.data, 2) The ECG sampling rate - ECGData.fs, 3) The ECG peak locations (in samples) - ECGData.peaks.
% PPGFile = File path to corresponding PPG data file (.mat) containing: 1) The waveform - PPGData.data, 2) The PPG sampling rate - PPGData.fs, 3) The PPG peak locations (in samples) - PPGData.peaks.
% PlotTF = Boolean to turn plotting results on or off.
%
% Outputs:
% BVP = Processed Blood Volume Pulse (BVP).
% PR = Estimated Pulse Rate (PR) from processed BVP timeseries using peak in periodogram.
% HR_ECG = Gold standard Heart Rate (HR) measured from the ECG timeseries R-waves for the window.
% PR_PPG = Pulse Rate (PR) measured from the PPG timeseries systolic onsets for the window.
% SNR = Blood Volume Pulse Signal-to-Noise Ratio (SNR) calculated based on the ECG HR frequency using a method adapted from the method by G. de Haan, TBME, 2013.
%
% Requires - Signal Processing Toolbox
%
% Daniel McDuff, Ethan Blackford, January 2019
% Copyright (c)
% Licensed under the MIT License and the RAIL AI License.
addpath(genpath('tools'))
%% Parameters
LPF = 0.7; %low cutoff frequency (Hz) - 0.8 Hz in reference
HPF = 2.5; %high cutoff frequency (Hz) - both 6.0 Hz and 2.0 Hz used in reference
%% Plot Control
if(PlotTF)
PlotPRPSD = true;
PlotSNR = true;
else
PlotPRPSD = false;
PlotSNR = false;
end
%% Load Video:
VidObj = VideoReader(VideoFile);
VidObj.CurrentTime = StartTime;
FramesToRead=ceil(Duration*VidObj.FrameRate); %video may be encoded at slightly different frame rate
%% Read Video and Spatially Average:
T = zeros(FramesToRead,1);%initialize time vector
RGB = zeros(FramesToRead,3);%initialize color signal
FN = 0;
while hasFrame(VidObj) && (VidObj.CurrentTime <= StartTime+Duration)
FN = FN+1;
T(FN) = VidObj.CurrentTime;
VidFrame = readFrame(VidObj);
%position for optional face detection/tracking - originally specified in reference as a manual segmentation.
VidROI = VidFrame;
%position for optional skin segmentation
RGB(FN,:) = sum(sum(VidROI));%if different size regions are used for different frames, the signals should be normalized by the region size, but not necessary for whole frame processing or constant region size
end
%% Select BVP Source:
% Green channel
BVP = RGB(:,2);
%% Filter, Normalize
NyquistF = 1/2*FS;
[B,A] = butter(3,[LPF/NyquistF HPF/NyquistF]);%Butterworth 3rd order filter - originally specified in reference with a 4th order butterworth using filtfilt function
BVP_F = filtfilt(B,A,(double(BVP)-mean(BVP)));
BVP = BVP_F;
% Estimate Pulse Rate from periodogram
PR = prpsd(BVP,FS,40,240,PlotPRPSD);
%% Ground Truth HR:
load(ECGFile);
ECG.time = (1:length(ECG.data))/ECG.fs;
ECGMask = (ECG.time>=StartTime) & (ECG.time<=StartTime+Duration);
ECGPeakMask = ((ECG.peaks./ECG.fs)>=StartTime) & ((ECG.peaks./ECG.fs)<=StartTime+Duration);
HR_ECG = (1/mean(diff(ECG.peaks(ECGPeakMask)./ECG.fs)))*60;
if ~isempty(PPGFile)
load(PPGFile);
PPG.time = (1:length(PPG.data))/PPG.fs;
PPGMask = (PPG.time>=StartTime) & (PPG.time<=StartTime+Duration);
if isfield(PPG,'peaks')
PPGPeakMask = ((PPG.peaks./PPG.fs)>=StartTime) & ((PPG.peaks./PPG.fs)<=StartTime+Duration);
PR_PPG = (1/mean(diff(PPG.peaks(PPGPeakMask)./PPG.fs)))*60;
else
PR_PPG = NaN;
end
else
PR_PPG = NaN;
end
%% SNR
SNR = bvpsnr(BVP,FS,HR_ECG,PlotSNR);
%% Optionally Plot Timeseries
if(PlotTF)
%Plot ECG, PPG, iPPG timeseries
figure
if ~isempty(PPGFile)
%Plot ECG
Ax1=subplot(3,1,1);
plot(ECG.time(ECGMask),ECG.data(ECGMask))
hold on
plot(ECG.peaks(ECGPeakMask)/ECG.fs,ECG.data(ECG.peaks(ECGPeakMask)),'*')
ylabel('ECG (a.u.)')
title('GREEN Method - ECG, PPG, iPPG Timeseries')
%Plot PPG
Ax2=subplot(3,1,2);
plot(PPG.time(PPGMask),PPG.data(PPGMask))
hold on
plot(PPG.peaks(PPGPeakMask)/PPG.fs,PPG.data(PPG.peaks(PPGPeakMask)),'*')
ylabel('PPG (a.u.)')
%Plot iPPG
Ax3=subplot(3,1,3);
plot(T,BVP)
hold on
ylabel('iPPG (a.u.)')
xlabel('Time (s)')
linkaxes([Ax1,Ax2,Ax3],'x')
else
%Plot ECG
Ax1=subplot(2,1,1);
plot(ECG.time(ECGMask),ECG.data(ECGMask))
hold on
plot(ECG.peaks(ECGPeakMask)/ECG.fs,ECG.data(ECG.peaks(ECGPeakMask)),'*')
ylabel('ECG (a.u.)')
title('GREEN Method - ECG, iPPG Timeseries')
%Plot iPPG
Ax2=subplot(2,1,2);
plot(T,BVP)
hold on
ylabel('iPPG (a.u.)')
xlabel('Time (s)')
linkaxes([Ax1,Ax2],'x')
end
xlim([StartTime StartTime+Duration])
end%endif plot
end%end function