Roy Sandberg / Mbed 2 deprecated BLE_ECG

Dependencies:   BLE_API Queue mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers postclas.cpp Source File

postclas.cpp

00001 /*****************************************************************************
00002 FILE:  postclas.cpp
00003 AUTHOR: Patrick S. Hamilton
00004 REVISED:    5/13/2002
00005   ___________________________________________________________________________
00006 
00007 postclas.cpp: Post classifier
00008 Copywrite (C) 2002 Patrick S. Hamilton
00009 
00010 This file is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Library General Public License as published by the Free
00012 Software Foundation; either version 2 of the License, or (at your option) any
00013 later version.
00014 
00015 This software is distributed in the hope that it will be useful, but WITHOUT ANY
00016 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
00017 PARTICULAR PURPOSE.  See the GNU Library General Public License for more
00018 details.
00019 
00020 You should have received a copy of the GNU Library General Public License along
00021 with this library; if not, write to the Free Software Foundation, Inc., 59
00022 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023 
00024 You may contact the author by e-mail (pat@eplimited.edu) or postal mail
00025 (Patrick Hamilton, E.P. Limited, 35 Medford St., Suite 204 Somerville,
00026 MA 02143 USA).  For updates to this software, please visit our website
00027 (http://www.eplimited.com).
00028   __________________________________________________________________________
00029 
00030 This file contains functions for classifying beats based after the
00031 following beat is detected.
00032 
00033     ResetPostClassify() -- Resets static variables used by
00034         PostClassify()
00035     PostClassify() -- classifies each beat based on six preceding
00036         beats and the following beat.
00037     CheckPostClass() --  classifys beat type based on the last
00038         eight post classifications of that beat.
00039     CheckPCRhythm() -- returns the classification of the RR interval
00040         for this type of beat based its previous eight RR intervals.
00041 
00042 ****************************************************************/
00043 
00044 #include <mbed.h>
00045 #include "bdac.h"
00046 #include "ecgcodes.h"
00047 
00048 // External Prototypes.
00049 
00050 double DomCompare(int newType, int domType) ;
00051 int GetBeatTypeCount(int type) ;
00052 
00053 // Records of post classifications.
00054 
00055 int PostClass[MAXTYPES][8], PCInitCount = 0 ;
00056 int PCRhythm[MAXTYPES][8] ;
00057 
00058 /**********************************************************************
00059  Resets post classifications for beats.
00060 **********************************************************************/
00061 
00062 void ResetPostClassify()
00063     {
00064     int i, j ;
00065     for(i = 0; i < MAXTYPES; ++i)
00066         for(j = 0; j < 8; ++j)
00067             {
00068             PostClass[i][j] = 0 ;
00069             PCRhythm[i][j] = 0 ;
00070             }
00071     PCInitCount = 0 ;
00072     }
00073 
00074 /***********************************************************************
00075     Classify the previous beat type and rhythm type based on this beat
00076     and the preceding beat.  This classifier is more sensitive
00077     to detecting premature beats followed by compensitory pauses.
00078 ************************************************************************/
00079 
00080 void PostClassify(int *recentTypes, int domType, int *recentRRs, int width, double mi2,
00081     int rhythmClass)
00082     {
00083     static int lastRC, lastWidth ;
00084     static double lastMI2 ;
00085     int i, regCount, pvcCount, normRR ;
00086     double mi3 ;
00087 
00088     // If the preceeding and following beats are the same type,
00089     // they are generally regular, and reasonably close in shape
00090     // to the dominant type, consider them to be dominant.
00091 
00092     if((recentTypes[0] == recentTypes[2]) && (recentTypes[0] != domType)
00093         && (recentTypes[0] != recentTypes[1]))
00094         {
00095         mi3 = DomCompare(recentTypes[0],domType) ;
00096         for(i = regCount = 0; i < 8; ++i)
00097             if(PCRhythm[recentTypes[0]][i] == NORMAL)
00098                 ++regCount ;
00099         if((mi3 < 2.0) && (regCount > 6))
00100             domType = recentTypes[0] ;
00101         }
00102 
00103     // Don't do anything until four beats have gone by.
00104 
00105     if(PCInitCount < 3)
00106         {
00107         ++PCInitCount ;
00108         lastWidth = width ;
00109         lastMI2 = 0 ;
00110         lastRC = 0 ;
00111         return ;
00112         }
00113 
00114     if(recentTypes[1] < MAXTYPES)
00115         {
00116 
00117         // Find first NN interval.
00118         for(i = 2; (i < 7) && (recentTypes[i] != recentTypes[i+1]); ++i) ;
00119         if(i == 7) normRR = 0 ;
00120         else normRR = recentRRs[i] ;
00121 
00122         // Shift the previous beat classifications to make room for the
00123         // new classification.
00124         for(i = pvcCount = 0; i < 8; ++i)
00125             if(PostClass[recentTypes[1]][i] == PVC)
00126                 ++pvcCount ;
00127 
00128         for(i = 7; i > 0; --i)
00129             {
00130             PostClass[recentTypes[1]][i] = PostClass[recentTypes[1]][i-1] ;
00131             PCRhythm[recentTypes[1]][i] = PCRhythm[recentTypes[1]][i-1] ;
00132             }
00133 
00134         // If the beat is premature followed by a compensitory pause and the
00135         // previous and following beats are normal, post classify as
00136         // a PVC.
00137 
00138         if(((normRR-(normRR>>3)) >= recentRRs[1]) && ((recentRRs[0]-(recentRRs[0]>>3)) >= normRR)// && (lastMI2 > 3)
00139             && (recentTypes[0] == domType) && (recentTypes[2] == domType)
00140                 && (recentTypes[1] != domType))
00141             PostClass[recentTypes[1]][0] = PVC ;
00142 
00143         // If previous two were classified as PVCs, and this is at least slightly
00144         // premature, classify as a PVC.
00145 
00146         else if(((normRR-(normRR>>4)) > recentRRs[1]) && ((normRR+(normRR>>4)) < recentRRs[0]) &&
00147             (((PostClass[recentTypes[1]][1] == PVC) && (PostClass[recentTypes[1]][2] == PVC)) ||
00148                 (pvcCount >= 6) ) &&
00149             (recentTypes[0] == domType) && (recentTypes[2] == domType) && (recentTypes[1] != domType))
00150             PostClass[recentTypes[1]][0] = PVC ;
00151 
00152         // If the previous and following beats are the dominant beat type,
00153         // and this beat is significantly different from the dominant,
00154         // call it a PVC.
00155 
00156         else if((recentTypes[0] == domType) && (recentTypes[2] == domType) && (lastMI2 > 2.5))
00157             PostClass[recentTypes[1]][0] = PVC ;
00158 
00159         // Otherwise post classify this beat as UNKNOWN.
00160 
00161         else PostClass[recentTypes[1]][0] = UNKNOWN ;
00162 
00163         // If the beat is premature followed by a compensitory pause, post
00164         // classify the rhythm as PVC.
00165 
00166         if(((normRR-(normRR>>3)) > recentRRs[1]) && ((recentRRs[0]-(recentRRs[0]>>3)) > normRR))
00167             PCRhythm[recentTypes[1]][0] = PVC ;
00168 
00169         // Otherwise, post classify the rhythm as the same as the
00170         // regular rhythm classification.
00171 
00172         else PCRhythm[recentTypes[1]][0] = lastRC ;
00173         }
00174 
00175     lastWidth = width ;
00176     lastMI2 = mi2 ;
00177     lastRC = rhythmClass ;
00178     }
00179 
00180 
00181 /*************************************************************************
00182     CheckPostClass checks to see if three of the last four or six of the
00183     last eight of a given beat type have been post classified as PVC.
00184 *************************************************************************/
00185 
00186 int CheckPostClass(int type)
00187     {
00188     int i, pvcs4 = 0, pvcs8 ;
00189 
00190     if(type == MAXTYPES)
00191         return(UNKNOWN) ;
00192 
00193     for(i = 0; i < 4; ++i)
00194         if(PostClass[type][i] == PVC)
00195             ++pvcs4 ;
00196     for(pvcs8=pvcs4; i < 8; ++i)
00197         if(PostClass[type][i] == PVC)
00198             ++pvcs8 ;
00199 
00200     if((pvcs4 >= 3) || (pvcs8 >= 6))
00201         return(PVC) ;
00202     else return(UNKNOWN) ;
00203     }
00204 
00205 /****************************************************************************
00206     Check classification of previous beats' rhythms based on post beat
00207     classification.  If 7 of 8 previous beats were classified as NORMAL
00208     (regular) classify the beat type as NORMAL (regular).
00209     Call it a PVC if 2 of the last 8 were regular.
00210 ****************************************************************************/
00211 
00212 int CheckPCRhythm(int type)
00213     {
00214     int i, normCount, n ;
00215 
00216 
00217     if(type == MAXTYPES)
00218         return(UNKNOWN) ;
00219 
00220     if(GetBeatTypeCount(type) < 9)
00221         n = GetBeatTypeCount(type)-1 ;
00222     else n = 8 ;
00223 
00224     for(i = normCount = 0; i < n; ++i)
00225         if(PCRhythm[type][i] == NORMAL)
00226             ++normCount;
00227     if(normCount >= 7)
00228         return(NORMAL) ;
00229     if(((normCount == 0) && (n < 4)) ||
00230         ((normCount <= 1) && (n >= 4) && (n < 7)) ||
00231         ((normCount <= 2) && (n >= 7)))
00232         return(PVC) ;
00233     return(UNKNOWN) ;
00234     }