TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
interpIBI.m
Go to the documentation of this file.
1 %This file is part of TEAP.
2 %
3 %TEAP is free software: you can redistribute it and/or modify
4 %it under the terms of the GNU General Public License as published by
5 %the Free Software Foundation, either version 3 of the License, or
6 %(at your option) any later version.
7 %
8 %TEAP is distributed in the hope that it will be useful,
9 %but WITHOUT ANY WARRANTY; without even the implied warranty of
10 %MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 %GNU General Public License for more details.
12 %
13 %You should have received a copy of the GNU General Public License
14 %along with TEAP. If not, see <http://www.gnu.org/licenses/>.
15 %
16 %> @file interpIBI.m
17 %> @brief This function interpolate an HR/IBI signal (from a list of peaks) with the
18 %> method proposed in:
19 %> Berger et al., "An Efficient Algorithm for Spectral Analysis of
20 %> Heart Rate Variability", IEEE Trans. on Biomedical Engineering,
21 %> Vol. 33, No. 9, sept. 1986
22 %> @param peaks: a list of R peaks expressed in seconds (e.g. [0.3 1.4 2.3 3.2])
23 %> @param fs: sampling frequency desired for the interpolated HR/IBI signal
24 %> @param duration: duration desired. Concerning time aspects, the first sample
25 %> is assumed to lie at time 1/fs (which can be before the first peak) and
26 %> the last at time 'duration'. If point in time that are not computable
27 %> because of lack of peaks (beginning and end of the output signals), they are
28 %> simply padded with the closest computed value.
29 %
30 %> @retval IBI [1*N]: N interbeat intervals (in seconds)
31 %> @retval HR [1*N]: N heart rate values
32 %> @retval t [1*N]: N time stamps corresponding to the IBI and HR values
33 %
34 % @author: Guillaume Chanel
35 function [IBI,HR,t] = interpIBI(peaks,fs,duration, silent)
36 % function [IBI,HR,t] = interpIBI(peaks,fs,duration)
37 %
38 % This function interpolate an HR/IBI signal (from a list of peaks) with the
39 % method proposed in:
40 % Berger et al., "An Efficient Algorithm for Spectral Analysis of
41 % Heart Rate Variability", IEEE Trans. on Biomedical Engineering,
42 % Vol. 33, No. 9, sept. 1986
43 %
44 % IN:
45 %
46 % peaks: a list of R peaks expressed in seconds (e.g. [0.3 1.4 2.3 3.2])
47 %
48 % fs : sampling frequency desired for the interpolated HR/IBI signal
49 %
50 % duration : duration desired. Concerning time aspects, the first sample
51 % is assumed to lie at time 1/fs (which can be before the first peak) and
52 % the last at time 'duration'. If point in time that are not computable
53 % because of lack of peaks (beginning and end of the output signals), they are
54 % simply padded with the closest computed value.
55 %
56 % OUT:
57 %
58 % IBI [1*N]: N interbeat intervals (in seconds)
59 %
60 % HR [1*N]: N heart rate values
61 %
62 % t [1*N]: N time stamps corresponding to the IBI and HR values
63 %
64 % Author: Guillaume Chanel
65 % Date: 7/12/2009
66 
67 if(nargin < 4)
68  silent = 0;
69 end
70 
71 %construction of time vector (starting at t=1/fs up to duration, with fs
72 %sampling rate) and corresponding HR vector
73 t = [1/fs:1/fs:duration]';
74 nbSpl = length(t);
75 HR = zeros(nbSpl,1);
76 
77 %Construction of the uneven sampled IBI vector (to be replaced in the end)
78 IBI = diff(peaks);
79 
80 % Define the first and last sample for HR computation : it starts 2 samples
81 % after the first peak and stop 2 samples before the last peak. This is
82 % necessary to have a full window for HR computation
83 firstSpl = find(t > peaks(1));
84 firstSpl = firstSpl(2);
85 lastSpl = find(t < peaks(end));
86 lastSpl = lastSpl(end-2);
87 if(isempty(lastSpl))
88  lastSpl = nbSpl;
89 end
90 
91 % First the first computable to the last computable samples:
92 % compute the HR values !
93 for(i=firstSpl:lastSpl)
94  %Definition of boundaries of interval used for HR computation
95  minT = t(i-1);
96  maxT = t(i+1);
97 
98  %Identify the peaks that are present in the current interval
99  pInt = (peaks > minT) & (peaks < maxT);
100  nbP = sum(pInt);
101 
102  %Depending on the differents cases:
103  if(nbP < 1) % no peaks in the current interval
104  %Find the unique IBI that correspond to the current time
105  idxIBI = find(peaks > t(i));
106  idxIBI = max(idxIBI(1)-1,1);
107 
108  %Update HR
109  HR(i) = 1/IBI(idxIBI);
110 
111  else %At least one peak in the current interval
112  %Send waring if more than one peak in an interval
113  if((nbP > 1) && ~silent) %more than one peak in an interval
114  warning([num2str(nbP) ' peaks were found in an interval -> consider increasing frequency']);
115  end
116 
117  %Compute the number of peaks (sum of IBI before peaks + IBI after
118  %the last peak)
119  idxPeaks = find(pInt);
120  nbPeaks = 0;
121  for(iP = idxPeaks)
122  nbPeaks = nbPeaks + (peaks(iP)-max(minT,peaks(iP-1)))/IBI(iP-1);
123  end
124  nbPeaks = nbPeaks + (maxT-peaks(idxPeaks(end)))/IBI(idxPeaks(end)); %add IBI after the last peak
125 
126  %Update HR
127  HR(i) = fs*nbPeaks/2;
128 
129 
130  end
131 end
132 
133 %Fill begining and ending values with the first and last computed HR
134 HR(1:firstSpl) = HR(firstSpl);
135 HR(lastSpl:end) = HR(lastSpl);
136 
137 %Compute IBI
138 IBI = 1./HR;
interpIBI
function interpIBI(in peaks, in fs, in duration, in silent)