1 %This file is part of TEAP.
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.
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.
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/>.
18 %>
PICtoBPM : compute BPM from of vector of peaks
19 %> Guillaume Chanel (thanks to Joep)
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)
29 %> @retval bpm
new computed bpm from the corrected list of peaks
30 %> @retval listPic corrected list of peaks
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
42 listePic = listePic_in;
44 nbTP = 5; %number of time peak to look ahead
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 %First step : remove the bad peaks
49 %Find time between all two consecutive peaks
50 timePic = diff(listePic)./fe;
52 %Find potential problem by checking the change in duration between two
53 %peaks compared to the threshold
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];
65 %Decide wich problems are
true ones (
if the peak is removed does the remaining
66 %interval corresponds to the median of the precedent)
68 for(iPb = [1:length(listePb)])
69 if(timePic(listePb(iPb))+timePic(listePb(iPb)-1) < medTP(listePb(iPb)) + thresh)
70 realPb = [realPb listePb(iPb)];
73 listePic(realPb) = [];
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 %Second step : add peak
if needed
78 %Find time between all two consecutive peaks
79 timePic = diff(listePic)./fe;
81 %Find potential problem by checking the change in duration between two
82 %peaks compared to the threshold
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];
92 %
for each problem add the necessary number of peaks
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;
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 %Finally recompute BPM from
new pics
109 [bpm delta_t t] =
PICtoBPM(listePic, fe);