QRS detection with MathLab script of Hooman Sedghamiz


In [1]:
import wfdb
import numpy as np
import matplotlib.pyplot as plt
import sys

Downloading an ECG from mimic III


In [2]:
folder = "p05/p050140"
waveform = "p050140-2188-07-26-05-51"
sig, fields = wfdb.srdsamp(waveform,pbdir='mimic3wdb/matched/'+folder, sampto=150000)

In [3]:
print("signame: " + str(fields['signame']))
print("units: " + str(fields['units']))
print("fs: " + str(fields['fs']))
print("comments: " + str(fields['comments']))
print("fields: " + str(fields))


signame: ['aVR', 'II', 'I', 'III', 'ABP', 'CVP', 'PLETH']
units: ['mV', 'mV', 'mV', 'mV', 'mmHg', 'mmHg', 'NU']
fs: 125
comments: ['Location: micu']
fields: {'signame': ['aVR', 'II', 'I', 'III', 'ABP', 'CVP', 'PLETH'], 'comments': ['Location: micu'], 'units': ['mV', 'mV', 'mV', 'mV', 'mmHg', 'mmHg', 'NU'], 'fs': 125}

Lets check if the signal contains the signal II


In [4]:
signalII = None
try:
    signalII = fields['signame'].index("II")
except ValueError:
    print("List does not contain value")
if(signalII!=None):
    print("List contain value")


List contain value

Obtain only the signal II from the ECG


In [5]:
array = sig[:, signalII]
array = np.nan_to_num(array)

Save the Frame Secuence


In [6]:
fs = fields['fs']
fs


Out[6]:
125

Create Numpy Array


In [7]:
npArray = np.array(array)
npArray


Out[7]:
array([ 0.        ,  0.        ,  0.        , ...,  0.47244094,
        0.47244094,  0.47244094])

In [8]:
from oct2py import octave
octave.addpath('/home/scidb/HeartRatePatterns/Matlab/')
octave.eval('pkg load signal')

In [9]:
%load_ext oct2py.ipython

In [10]:
%%octave -i array,fs -o R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot
[R_i,R_amp,S_i,S_amp,T_i,T_amp,Q_i,Q_amp,heart_rate,buffer_plot] = peakdetect(array,fs,false);


warning: peakdetect: some elements in list of return values are undefined

In [11]:
print("R_i",R_i)
print("S_i",S_i)
print("R_i",T_i)
print("Q_i",Q_i)
print(heart_rate)
print(buffer_plot)


R_i [[  3350   3609   3727   3833   4044   4255   4532   4947   5148   5576
    5787   5929   6139   6345   6560   6757   7329   7816   8452   8589
    9079   9285   9640  10057  10541  10749  10959  11171  11386  11599
   11811  12088  12295  12910  13059  13271  13486  13762  14075  14256
   14401  14612  14817  15088  15177  15384  15524  16296  16504  16713
   17199  17480  17619  17921  18174  18374  18923  20983  21550  21900
   22800  23157  23362  24343  24837  25042  25247  25452  25655  25861
   26063  26261  26402  26674  27006  27152  27356  27701  28253  28526
   28787  29464  29808  29992  30189  30594  30793  31699  31895  32222
   33145  34074  35028  35133  35239  35387  35461  35842  36889  37108
   37925  38252  38652  39115  39444  39903  40035  40154  40370  40566
   40718  40820  40892  40967  41297  41395  41586  42267  42399  42583
   42714  43275  43399  43522  43595  43663  43782  43969  44332  44421
   44517  44610  44820  44944  45179  45261  45447  45572  45752  45876
   46037  46168  46312  46405  46552  46923  47211  47418  47667  47793
   47981  48102  48179  48606  48867  49311  49504  49817  51213  51468
   51938  52193  54405  54721  55038  55199  55623  56284  56897  59177
   59866  60182  60744  61058  62546  62710  64748  66783  67376  67567
   68785  69348  70224  70494  70996  74494  75110  76526  82995  86841
   89790  91361  91691  93719  93864  98698 106076 113057 113226 113354
  113476 117935 122779 127436 128322 130819 132969 135756 137924 143270
  144623 146611 147851]]
S_i [[  3501   3638   3737   3907   4114   4322   4600   5014   5223   5644
    5846   5995   6207   6420   6631   7124   7612   8309   8520   8732
    9151   9495   9847  10399  10608  10815  11029  11241  11455  11667
   11878  12156  12635  12916  13127  13339  13554  13908  14118  14336
   14468  14679  14963  15101  15246  15456  15599  16369  16579  16927
   17272  17545  17756  17960  18241  18449  20778  21196  21688  21968
   22876  23226  23997  24694  24903  25108  25313  25517  25721  25925
   26130  26332  26468  26740  27012  27218  27423  28044  28320  28592
   28927  29661  29861  30061  30523  30723  30921  31766  32154  32488
   33806  34930  35061  35166  35259  35399  35520  36821  37024  37794
   38186  38519  38913  39179  39773  39926  40080  40169  40434  40636
   40747  40830  40898  41213  41320  41519  42084  42292  42460  42636
   43022  43333  43460  43530  43600  43716  43905  44020  44346  44453
   44526  44750  44826  45009  45194  45320  45506  45631  45812  45887
   46070  46186  46322  46432  46800  46987  47231  47540  47726  47851
   48039  48116  48231  48738  49180  49372  49692  51022  51399  51848
   52037  52460  54439  54840  55124  55550  55897  56542  58788  59438
   60059  60497  60936  61315  62580  63279  64879  67314  67437  68078
   69037  70162  70250  70622  74420  74922  75563  80757  84035  89662
   90898  91559  93539  93800  98106 105037 108276 113092 113285 113414
  115268 122709 125995 127709 130056 132835 134253 137789 140466 143744
  145235 146955 148128]]
R_i [[  3524   3662   3799   3982   4190   4490   4907   5114   5536   5720
    5890   6070   6284   6521   6718   7294   7781   8451   8528   9039
    9253   9599  10017  10476  10707  10891  11130  11341  11531  11745
   12022  12256  12876  12993  13204  13418  13726  14025  14218  14361
   14545  14754  15064  15134  15346  15488  16234  16471  16679  17180
   17459  17577  18105  18315  18888  20951  21483  21860  22765  23135
   23327  24283  24769  24977  25206  25387  25591  25795  25999  26202
   26340  26636  26977  27112  27316  27660  28212  28489  28757  29425
   29762  29940  30132  30531  30731  31641  31860  32173  33112  34033
   34978  35069  35180  35331  35443  35783  36833  37083  37867  38218
   38615  39076  39405  39888  39977  40150  40308  40530  40703  40760
   40854  40919  41227  41362  41527  42214  42341  42525  42692  43216
   43341  43483  43555  43630  43753  43943  44276  44367  44461  44548
   44765  44916  45133  45205  45389  45518  45726  45825  46008  46128
   46255  46352  46500  46868  47198  47385  47608  47739  47921  48073
   48149  48578  48829  49247  49467  49783  51179  51431  51910  52150
   54358  54679  54995  55138  55565  56246  56842  59116  59854  60152
   60715  61030  62639  64715  66749  67374  67516  68748  69320  70216
   70286  70943  74466  75082  76495  82962  86806  89754  91324  91632
   93718  93808  98663 106016 113186 113294 113422 117902 122742 127399
  128287 130800 132936 135720 137873 143266 144617 146575 147816]]
Q_i [[  3345   3604   3722   3828   4039   4253   4529   4944   5143   5574
    5785   5925   6134   6340   6555   6757   7324   7811   8452   8584
    9074   9280   9638  10055  10539  10746  10957  11169  11383  11596
   11808  12083  12293  12905  13057  13269  13483  13762  14070  14251
   14398  14607  14812  15088  15174  15379  15519  16291  16499  16708
   17194  17477  17617  17916  18172  18369  18918  20978  21548  21898
   22795  23155  23357  24343  24834  25037  25244  25447  25652  25858
   26059  26261  26399  26672  27001  27150  27354  27698  28250  28524
   28782  29462  29808  29987  30184  30589  30791  31694  31890  32217
   33140  34069  35023  35128  35234  35382  35456  35837  36884  37103
   37920  38247  38650  39110  39442  39898  40030  40154  40368  40563
   40713  40815  40892  40962  41292  41395  41583  42262  42394  42578
   42709  43271  43394  43517  43592  43658  43778  43965  44327  44416
   44515  44605  44815  44939  45174  45257  45442  45567  45747  45871
   46037  46163  46312  46400  46547  46918  47206  47416  47663  47789
   47976  48097  48176  48601  48865  49309  49500  49812  51212  51466
   51933  52188  54400  54716  55033  55194  55618  56282  56892  59175
   59861  60177  60739  61053  62541  62708  64743  66778  67372  67564
   68783  69343  70224  70489  70991  74489  75105  76521  82990  86836
   89785  91359  91686  93714  93859  98693 106071 113052 113221 113349
  113471 117930 122774 127431 128317 130814 132964 135751 137919 143269
  144622 146606 147846]]
[]
[[ 0.          0.          0.         ...,  0.47244094  0.47244094
   0.47244094]]

In [12]:
To= 5
subarray = buffer_plot[0][Q_i[0][0]:T_i[0][To]]
print(len(subarray))
subQ_i = Q_i[0][:To]
subQ_amp = Q_amp[0][:To]
subR_i = R_i[0][:To]
subR_amp = R_amp[0][:To]
subS_i = S_i[0][:To]
subS_amp = S_amp[0][:To]
subT_i = T_i[0][:To]
subT_amp = T_amp[0][:To]


1145

In [13]:
fig_size = [12,9]
plt.rcParams["figure.figsize"] = fig_size

In [14]:
plt.plot(np.arange(Q_i[0][0],T_i[0][To]),np.array(subarray),c='lightgreen')
plt.scatter(subQ_i, subQ_amp,c='yellow')
plt.scatter(subR_i, subR_amp,c='blue')
plt.scatter(subS_i, subS_amp,c='red')
plt.scatter(subT_i, subT_amp,c='black')
plt.show()



In [16]:
qt = Q_i[0][:209]-T_i
qt


Out[16]:
array([[  -179,    -58,    -77,   -154,   -151,   -237,   -378,   -170,
          -393,   -146,   -105,   -145,   -150,   -181,   -163,   -537,
          -457,   -640,    -76,   -455,   -179,   -319,   -379,   -421,
          -168,   -145,   -173,   -172,   -148,   -149,   -214,   -173,
          -583,    -88,   -147,   -149,   -243,   -263,   -148,   -110,
          -147,   -147,   -252,    -46,   -172,   -109,   -715,   -180,
          -180,   -472,   -265,   -100,   -488,   -399,   -716,  -2582,
         -2565,   -882,  -1217,  -1237,   -532,  -1128,  -1412,   -634,
          -372,   -350,   -347,   -348,   -347,   -344,   -281,   -375,
          -578,   -440,   -315,   -510,   -858,   -791,   -507,   -901,
          -980,   -478,   -324,   -544,   -547,  -1052,  -1069,   -479,
         -1222,  -1816,  -1838,  -1000,   -157,   -203,   -209,   -401,
         -1377,  -1246,   -983,  -1115,   -695,   -829,   -755,   -778,
          -535,   -252,   -278,   -376,   -335,   -197,   -141,   -104,
          -335,   -400,   -235,   -819,   -758,   -263,   -298,   -638,
          -632,   -212,   -161,   -113,   -161,   -285,   -498,   -402,
          -134,   -132,   -250,   -311,   -318,   -266,   -215,   -261,
          -284,   -258,   -261,   -257,   -218,   -189,   -188,   -468,
          -651,   -467,   -402,   -323,   -258,   -284,   -173,   -481,
          -653,   -646,   -602,   -474,  -1679,  -1619,   -698,   -684,
         -2425,  -2491,   -595,   -422,   -532,  -1052,  -1224,  -2834,
         -2962,   -977,   -854,   -853,  -1900,  -3662,  -4208,  -4666,
         -2773,  -1970,  -1948,  -2652,  -1503,  -1600,  -4242,  -4593,
         -5504,  -8473, -11701, -13233,  -8334,  -4796,  -3933,  -2449,
         -6977, -12302, -19327, -14601,  -7351,  -4850,  -9521, -14050,
        -14816, -12870, -10162,  -8289,  -9556, -12452, -11653, -10824,
         -9897]])

In [18]:
ts = T_i-S_i[0][:209]
ts


Out[18]:
array([[   23,    24,    62,    75,    76,   168,   307,   100,   313,
           76,    44,    75,    77,   101,    87,   170,   169,   142,
            8,   307,   102,   104,   170,    77,    99,    76,   101,
          100,    76,    78,   144,   100,   241,    77,    77,    79,
          172,   117,   100,    25,    77,    75,   101,    33,   100,
           32,   635,   102,   100,   253,   187,    32,   349,   355,
          647,  2502,   705,   664,  1077,  1167,   451,  1057,   772,
          283,   303,   279,   278,   278,   278,   277,   210,   304,
          509,   372,   304,   442,   789,   445,   437,   833,   835,
          279,   271,   470,   208,   918,   939,   407,   958,  1545,
         1172,   139,   119,   165,   184,   384,  1313,   262,   843,
          424,   429,   557,   492,   709,   204,   224,   228,   361,
          269,   124,   107,    89,   329,   149,   207,   695,   257,
          233,   232,   580,   319,   150,    95,   100,   153,   227,
          371,   347,   115,    95,   239,   166,   307,   196,   195,
          198,   220,   194,   196,   241,   185,   166,   178,   436,
          398,   398,   377,   199,   195,   222,   110,   462,   598,
          509,   287,   411,  1487,   409,   511,   302,  2321,  2219,
          556,   298,   441,   696,   945,  2574,  1066,   714,   656,
          533,  1703,  3400,  4169,  4095,  2637,  1434,  1883,  2138,
         1249,   781,  4216,  4460,  2075,  8040, 11243,  8997,  7289,
         1970,  2820,  2249,  5124, 12216, 15080,  8257,  5146,  4810,
         9457, 13985, 13019,  8091,  6941,  8011,  7817, 10431, 10364,
         8786,  7350]])

In [19]:
sr = S_i-R_i
sr


Out[19]:
array([[ 151,   29,   10,   74,   70,   67,   68,   67,   75,   68,   59,
          66,   68,   75,   71,  367,  283,  493,   68,  143,   72,  210,
         207,  342,   67,   66,   70,   70,   69,   68,   67,   68,  340,
           6,   68,   68,   68,  146,   43,   80,   67,   67,  146,   13,
          69,   72,   75,   73,   75,  214,   73,   65,  137,   39,   67,
          75, 1855,  213,  138,   68,   76,   69,  635,  351,   66,   66,
          66,   65,   66,   64,   67,   71,   66,   66,    6,   66,   67,
         343,   67,   66,  140,  197,   53,   69,  334,  129,  128,   67,
         259,  266,  661,  856,   33,   33,   20,   12,   59,  979,  135,
         686,  261,  267,  261,   64,  329,   23,   45,   15,   64,   70,
          29,   10,    6,  246,   23,  124,  498,   25,   61,   53,  308,
          58,   61,    8,    5,   53,  123,   51,   14,   32,    9,  140,
           6,   65,   15,   59,   59,   59,   60,   11,   33,   18,   10,
          27,  248,   64,   20,  122,   59,   58,   58,   14,   52,  132,
         313,   61,  188, 1205,  186,  380,   99,  267,   34,  119,   86,
         351,  274,  258, 1891,  261,  193,  315,  192,  257,   34,  569,
         131,  531,   61,  511,  252,  814,   26,  128, 3424,  428,  453,
        4231, 1040, 2821, 1108,  198, 1848,   81, 4242, 6339, 2200,   35,
          59,   60, 1792, 4774, 3216,  273, 1734, 2016, 1284, 2033, 2542,
         474,  612,  344,  277]])

In [ ]: