0
$\begingroup$

I am giving my first steps in data analysis, gathering/cleaning.

To learn, I am trying to create a simple code that can detect heartbeats from color variations from the image coming from the camera on the iPhone.

This is what I have done so far:

  1. I have created a code that detects faces and cuts them from the image.
  2. then I get the average color from that face image.
  3. Because the values are grabbed at irregular time intervals, I interpolate the data. Now I have a series of values "taken" at regular intervals.

Plotting the data I have this:

enter image description here

I think I am seeing something that looks like heartbeats there.

To confirm that I am not seeing artifacts from the camera, I sample a rectangle from the background and I get the same color with very little random variation. The background color is stable and shows barely any variation.

  1. Now, I apply a Direct Cosine Transform to the data and get this:

enter image description here

The DCT has negative vales.

The two first items of the DCT are peaks, at t=0 and t=0.10482311 seconds.

I read a paper where the researcher says heartbeats occur between 0.4 and 4Hz. I suppose this has a relation why the first two DCT items are peaks...

What do I do now?

I have tried to apply a threshold to zero every term from the DCT below 150 and do an inverse DCT to reconstruct the signal and got this

enter image description here

I have reduced the threshold to 85 and got this

enter image description here

I am not sure if I see a heartbeat there. I mean, the peaks where the beats start.

This is the data at regular time intervals I have (256 samples):

17.0
14.151599
11.303198
8.454798
5.74406
7.5946655
12.173319
18.0
18.0
7.448537
18.0
16.36569
12.0000515
13.0
13.0
13.0
13.0
12.442261
9.134232
11.052017
12.0
7.605861
10.42025
11.911684
3.4419365
2.0
2.0
14.082303
16.227417
9.639368
7.0
3.4693644
5.0578203
7.0
14.859163
8.363442
7.876806
16.721855
16.367476
13.054573
17.416235
6.6276655
11.617707
12.0
6.3224463
11.111255
14.0
14.0
14.0
14.0
8.705125
17.478954
18.0
17.259813
14.371727
12.0
12.0
17.304781
18.0
18.0
2.6055841
9.789778
8.290463
6.3248563
2.3246787
12.944895
2.999961
8.443108
16.994041
7.714527
11.132567
2.2226758
5.6030903
12.528458
16.932209
16.087227
7.9563236
16.43404
8.239223
17.576452
12.102208
15.194642
1.0
1.0
1.0
1.0
1.0
1.0
1.7313459
17.479614
11.889918
11.0
11.0
14.0
14.0
13.802156
12.975004
6.840872
14.405505
7.928815
1.4836411
7.193712
15.371663
6.2666273
6.1682196
17.532732
16.0
15.034918
9.363262
1.0
4.654352
17.0
17.0
17.0
17.0
10.1624565
2.696946
13.607519
10.2923975
1.0
1.49984
9.996923
7.0500054
17.0
17.0
17.343903
13.191299
1.6610342
5.2127213
7.0
5.6532416
3.0295517
12.250117
12.687657
2.9297333
7.463814
9.5274105
13.640531
18.061134
16.376305
5.9621263
4.2103305
7.751727
7.023478
2.4116693
14.6888685
11.718729
4.0103664
12.0
8.606849
1.0
1.0
7.0
7.0
8.867684
12.0
12.0
9.598275
4.9731455
2.0
2.0
2.0
2.4965348
4.843401
5.4067893
3.655766
7.0
11.899609
11.855668
10.36767
16.59431
7.843425
1.9749169
8.829407
12.43066
11.205647
16.514818
17.0
13.328903
11.462145
12.0
12.0
12.0
12.502206
15.606055
18.0
18.0
13.699917
2.0
2.0
8.122738
12.0342
12.311193
9.434023
8.419968
8.64551
2.1631317
7.9250226
13.173435
3.0252013
1.0
1.0
1.0
1.0
1.0
3.7013192
6.0
6.0
6.3827076
7.0
6.570015
6.0
6.0
6.0
6.0
2.8011405
7.071625
15.575444
17.0
17.0
6.811758
1.0
4.5509486
5.3095756
10.288496
13.577595
2.493825
10.179988
12.0
6.3059773
7.9304085
14.911688
8.452748
2.8948724
10.117218
12.0
12.0
15.008322
16.0
7.473282
4.4403195
12.0
12.0
8.330021
6.0
6.0
6.0
11.515245
0.946867
0.83356774
0.95877224
0.05619842
15.221931
16.469358
5.231963
1.7867849

and these are the regular time intervals

0.0
0.03317536
0.06635072
0.09952608
0.13270144
0.1658768
0.19905217
0.23222753
0.26540288
0.29857823
0.33175358
0.36492893
0.39810428
0.43127963
0.46445498
0.49763033
0.5308057
0.56398106
0.5971564
0.63033175
0.6635071
0.69668245
0.7298578
0.76303315
0.7962085
0.82938385
0.8625592
0.89573455
0.9289099
0.96208525
0.9952606
1.028436
1.0616113
1.0947866
1.127962
1.1611373
1.1943127
1.227488
1.2606634
1.2938387
1.3270141
1.3601894
1.3933648
1.4265401
1.4597155
1.4928908
1.5260662
1.5592415
1.5924169
1.6255922
1.6587676
1.6919429
1.7251183
1.7582936
1.791469
1.8246443
1.8578197
1.890995
1.9241704
1.9573457
1.9905211
2.0236964
2.056872
2.0900474
2.1232228
2.1563983
2.1895738
2.2227492
2.2559247
2.2891002
2.3222756
2.355451
2.3886266
2.421802
2.4549775
2.488153
2.5213284
2.554504
2.5876794
2.6208549
2.6540303
2.6872058
2.7203813
2.7535567
2.7867322
2.8199077
2.8530831
2.8862586
2.919434
2.9526095
2.985785
3.0189605
3.052136
3.0853114
3.118487
3.1516623
3.1848378
3.2180133
3.2511888
3.2843642
3.3175397
3.3507152
3.3838906
3.417066
3.4502416
3.483417
3.5165925
3.549768
3.5829434
3.616119
3.6492944
3.6824698
3.7156453
3.7488208
3.7819963
3.8151717
3.8483472
3.8815227
3.9146981
3.9478736
3.981049
4.0142245
4.0474
4.0805755
4.113751
4.1469264
4.180102
4.2132773
4.246453
4.2796283
4.3128037
4.345979
4.3791547
4.41233
4.4455056
4.478681
4.5118566
4.545032
4.5782075
4.611383
4.6445584
4.677734
4.7109094
4.744085
4.7772603
4.810436
4.8436112
4.8767867
4.909962
4.9431376
4.976313
5.0094886
5.042664
5.0758395
5.109015
5.1421905
5.175366
5.2085414
5.241717
5.2748923
5.308068
5.3412433
5.3744187
5.407594
5.4407697
5.473945
5.5071206
5.540296
5.5734715
5.606647
5.6398225
5.672998
5.7061734
5.739349
5.7725244
5.8057
5.8388753
5.872051
5.905226
5.9384017
5.971577
6.0047526
6.037928
6.0711036
6.104279
6.1374545
6.17063
6.2038054
6.236981
6.2701564
6.303332
6.3365073
6.369683
6.4028583
6.4360337
6.469209
6.5023847
6.53556
6.5687356
6.601911
6.6350865
6.668262
6.7014375
6.734613
6.7677884
6.800964
6.8341393
6.867315
6.9004903
6.9336658
6.966841
7.0000167
7.033192
7.0663676
7.099543
7.1327186
7.165894
7.1990695
7.232245
7.2654204
7.298596
7.3317714
7.364947
7.3981223
7.431298
7.4644732
7.4976487
7.530824
7.5639997
7.597175
7.6303506
7.663526
7.6967015
7.729877
7.7630525
7.796228
7.8294034
7.862579
7.8957543
7.92893
7.9621053
7.9952807
8.028456
8.061631
8.094807
8.127982
8.161158
8.194333
8.227509
8.260684
8.2938595
8.327035
8.36021
8.393386
8.426561
8.459737

As requested, this is my original sampling values

17.0
8.0
13.0
2.0
2.0
12.0
12.0
18.0
18.0
2.0
18.0
18.0
12.0
13.0
13.0
13.0
13.0
13.0
12.0
8.0
12.0
12.0
7.0
11.0
12.0
2.0
2.0
2.0
19.0
13.0
7.0
7.0
2.0
7.0
7.0
18.0
7.0
8.0
19.0
13.0
18.0
6.0
12.0
12.0
2.0
14.0
14.0
14.0
14.0
14.0
8.0
18.0
18.0
17.0
12.0
12.0
12.0
18.0
18.0
18.0
2.0
12.0
6.0
7.0
2.0
13.0
2.0
7.0
18.0
7.0
12.0
2.0
6.0
12.0
17.0
18.0
7.0
17.0
7.0
18.0
12.0
19.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
18.0
12.0
11.0
11.0
14.0
14.0
14.0
13.0
5.0
16.0
11.0
1.0
5.0
17.0
7.0
1.0
18.0
16.0
16.0
11.0
1.0
1.0
17.0
17.0
17.0
17.0
12.0
1.0
17.0
12.0
1.0
1.0
11.0
6.0
17.0
17.0
17.0
18.0
1.0
7.0
7.0
2.0
12.0
13.0
2.0
7.0
8.0
12.0
18.0
19.0
7.0
2.0
8.0
7.0
2.0
12.0
18.0
1.0
12.0
12.0
1.0
1.0
7.0
7.0
7.0
12.0
12.0
12.0
7.0
2.0
2.0
2.0
3.0
2.0
8.0
2.0
7.0
7.0
18.0
7.0
17.0
11.0
2.0
1.0
17.0
7.0
16.0
17.0
17.0
11.0
12.0
12.0
12.0
12.0
13.0
18.0
18.0
18.0
2.0
2.0
2.0
16.0
7.0
17.0
1.0
16.0
6.0
1.0
16.0
6.0
1.0
1.0
1.0
1.0
1.0
1.0
6.0
6.0
6.0
7.0
7.0
6.0
6.0
6.0
6.0
6.0
1.0
17.0
17.0
17.0
1.0
1.0
6.0
5.0
16.0
12.0
0.0
12.0
12.0
0.0
12.0
16.0
0.0
5.0
12.0
12.0
12.0
16.0
16.0
0.0
6.0
12.0
12.0
6.0
6.0
6.0
6.0
12.0
0.0
1.0
1.0
0.0
15.0
17.0
6.0
1.0
17.0

and the original irregular times for each sample

0.0
0.10482311
0.119249344
0.13964272
0.15387249
0.17532921
0.19857883
0.21496487
0.27026558
0.2828083
0.32911777
0.3525095
0.39810467
0.41083145
0.44736195
0.47371197
0.5073452
0.54704
0.5774145
0.60497
0.63820934
0.6747322
0.69970894
0.7349682
0.76575184
0.8013401
0.8341179
0.86776924
0.9071169
0.954278
0.9682169
1.0041189
1.0385561
1.0762548
1.1014042
1.1385756
1.1643295
1.1985254
1.2350531
1.2934237
1.3314543
1.3617754
1.3955145
1.4337587
1.4794769
1.4971437
1.5272751
1.5614061
1.5997353
1.6470509
1.6603279
1.6936808
1.7275419
1.7690878
1.8116655
1.824687
1.8617201
1.8948317
1.9302769
1.9663658
1.9914713
2.0328398
2.0717106
2.0850267
2.1222086
2.1565704
2.192874
2.2181673
2.2530928
2.2840595
2.3193321
2.3532658
2.3925219
2.417821
2.4554882
2.4818573
2.5180626
2.5522127
2.5926962
2.618288
2.6546497
2.679552
2.7157555
2.7500048
2.7874317
2.8332338
2.8457584
2.8827896
2.9178934
2.9537058
2.9820414
3.0160494
3.0571985
3.0840607
3.1196194
3.1434994
3.1847591
3.20994
3.2581816
3.271309
3.313818
3.3445988
3.3780565
3.413885
3.43991
3.4738941
3.5146542
3.5432758
3.576911
3.6137676
3.6410694
3.677081
3.7015162
3.740508
3.7845335
3.8095531
3.8431873
3.891838
3.909649
3.942175
3.9797077
4.006543
4.044832
4.0717325
4.108653
4.1351194
4.1694517
4.207103
4.2631474
4.301524
4.343401
4.3684435
4.411268
4.4486666
4.4613724
4.4986906
4.5331697
4.5765543
4.6035967
4.639209
4.664981
4.6995993
4.7451544
4.780141
4.793849
4.830861
4.865369
4.90709
4.9325666
4.966836
5.0153437
5.0297213
5.0639524
5.0963545
5.130248
5.177142
5.1903505
5.228221
5.2615137
5.296006
5.342946
5.35703
5.391568
5.4253855
5.4609804
5.5001307
5.529132
5.5541964
5.588704
5.641984
5.655326
5.7057123
5.7240953
5.755267
5.7930346
5.820138
5.856534
5.8841944
5.9185686
5.961484
5.9868727
6.0231733
6.05124
6.0907927
6.1166677
6.151658
6.1964464
6.2238283
6.2588625
6.2868814
6.3244963
6.351754
6.3855104
6.4221964
6.4501696
6.476058
6.5103655
6.5649385
6.578372
6.617936
6.643077
6.6851377
6.7118216
6.7488527
6.786395
6.813361
6.847041
6.881585
6.9309835
6.944688
6.996209
7.0101023
7.045926
7.077056
7.1158667
7.142207
7.204627
7.243209
7.290002
7.3034983
7.3409586
7.374736
7.4086084
7.455802
7.4701204
7.5048704
7.5354643
7.575053
7.6216745
7.634803
7.674262
7.721833
7.7357264
7.7731066
7.8044004
7.84241
7.8692274
7.905286
7.9496527
7.9664793
7.9915056
8.030282
8.081534
8.104392
8.138106
8.169377
8.196527
8.230163
8.266778
8.292344
8.329101
8.354308
8.407498
8.420852
8.458022
8.492892
$\endgroup$
  • $\begingroup$ Have you tried an FFT or an autocorrelation? $\endgroup$ – Ben Feb 11 at 17:27
  • $\begingroup$ not yet. Do you think a FFT may help? $\endgroup$ – SpaceDog Feb 11 at 17:30
  • $\begingroup$ I would try an autocorrelation first.. $\endgroup$ – Ben Feb 11 at 18:15
  • $\begingroup$ there is some obvious clipping in your interpolated data that happens at different levels that makes me suspicious. could you post your irregular spaced data with the true sample times. there is a technique called Lomb-Scargle that would be interesting to try. $\endgroup$ – Stanley Pawlukiewicz Feb 11 at 19:01
  • $\begingroup$ @StanleyPawlukiewicz - I have posted the original data and times, without any manipulation, as coming from the sensor. $\endgroup$ – SpaceDog Feb 11 at 20:09
2
$\begingroup$

I have to argue a little bit with your rate expectations.

4 Hz = 240 bpm (beats per minute)

I think at that point you are in the emergency room. I believe 50-70 bpm is considered ideal. Which is right around 1 Hz.

Since your frame is 8.5 seconds long, we are looking for a frequency around 8 to 9 cycles per frame, which corresponds to bins 8 and 9 on a DFT. (Note: That would be 9 an 10 in MATLAB).

I smoothed your signal repeatedly using the average of a forward exponential smoothing and a backward exponential smoothing. (See my article Exponential Smoothing with a Wrinkle). This the result:

Smoothed Signal The crest to crest distance on some of the smaller waves in the center is about 20 samples, corresponding to ~2 Hz, or 120 bpm, which is a bit fast.

Before I took the DFT of the smoothed signal, I subtracted away the average to zero out the DC bin. Otherwise, it would dominate the chart and make the rest less discernible. So, here is the 1/N normalized DFT:

enter image description here

There are strong peaks at 4 and 6, but those are too low. The peak at 20 is too fast. Without knowing the actual rate you are looking for, it is difficult for me to say this represents a clean pulse signal.

$\endgroup$
  • 1
    $\begingroup$ you can get 240 beats a minute with babies. Olympic sprinters can hit 220 beats a minute so they can happen but are not common. $\endgroup$ – Stanley Pawlukiewicz Feb 11 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.