TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
correctBPM.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 correctBPM.m
17 %> @b DEPENDANCIES :
18 %> PICtoBPM : compute BPM from of vector of peaks
19 %> Guillaume Chanel (thanks to Joep)
20 %> 4/09/2008
21 %
22 %> @param listePic_in [1*N] : vector of peaks with position of peak in sample for each
23 %> peak ([1 8 20] 3 peaks at samples 1, 8 and 20)
24 %> @param fe ampling frequency
25 %> @param thresh limit between two peaks for detection of miss or bad peak.
26 %> decreasing this limit will not always augment the number of
27 %> corrections (see First step, Decide which problems are true ones in the code)
28 %
29 %> @retval bpm new computed bpm from the corrected list of peaks
30 %> @retval listPic corrected list of peaks
31 
32 function [bpm, delta_t, t, listePic] = correctBPM(listePic_in, fe, thresh)
33 %WARN I am not sure but this might be an old version check that this
34 %corresponds to correctBPM3 in my codes
35 
36 
37 if(nargin < 3)
38  thresh = 0.2;
39 end
40 
41 
42 listePic = listePic_in;
43 
44 nbTP = 5; %number of time peak to look ahead
45 
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 %First step : remove the bad peaks
48 
49 %Find time between all two consecutive peaks
50 timePic = diff(listePic)./fe;
51 
52 %Find potential problem by checking the change in duration between two
53 %peaks compared to the threshold
54 listePb = [];
55 medTP = zeros(1, length(timePic));
56 for(iTP = [nbTP+1:length(timePic)])
57  medTP(iTP) = median(timePic([iTP-nbTP:iTP-1]));
58  if(medTP(iTP) - timePic(iTP)> thresh) %Pb detected %%%%%%%%%%%%%
59  listePb = [listePb iTP];
60  end
61 end
62 
63 
64 
65 %Decide wich problems are true ones (if the peak is removed does the remaining
66 %interval corresponds to the median of the precedent)
67 realPb = [];
68 for(iPb = [1:length(listePb)])
69  if(timePic(listePb(iPb))+timePic(listePb(iPb)-1) < medTP(listePb(iPb)) + thresh)
70  realPb = [realPb listePb(iPb)];
71  end
72 end
73 listePic(realPb) = [];
74 
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 %Second step : add peak if needed
77 
78 %Find time between all two consecutive peaks
79 timePic = diff(listePic)./fe;
80 
81 %Find potential problem by checking the change in duration between two
82 %peaks compared to the threshold
83 listePb = [];
84 medTP = zeros(1, length(timePic));
85 for(iTP = [nbTP+1:length(timePic)])
86  medTP(iTP) = median(timePic(iTP-nbTP:iTP-1));
87  if(medTP(iTP) - timePic(iTP) < -thresh) %Pb detected
88  listePb = [listePb iTP];
89  end
90 end
91 
92 %for each problem add the necessary number of peaks
93 
94 nbAddTotal = 0;
95 for(iPb = [1:length(listePb)])
96  nbAdd = round(timePic(listePb(iPb))/medTP(listePb(iPb)))-1;
97  splAdd = (timePic(listePb(iPb)) / (nbAdd + 1))*fe;
98  relativePos = round(cumsum(splAdd*ones(1, nbAdd))); %relative position of each new peak to the peak before missing ones
99  listePic = [listePic(1:listePb(iPb)+nbAddTotal) (relativePos + listePic(listePb(iPb)+nbAddTotal)) listePic(listePb(iPb)+nbAddTotal+1:end)];
100  nbAddTotal = nbAddTotal + nbAdd;
101 end
102 
103 
104 
105 
106 
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 %Finally recompute BPM from new pics
109 [bpm delta_t t] = PICtoBPM(listePic, fe);
110 
PICtoBPM
function PICtoBPM(in listePic, in fe)
correctBPM
function correctBPM(in listePic_in, in fe, in thresh)