-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
2548 lines (2122 loc) · 85.1 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/* Settings: Do not change below */
#define SIVA_BLE
//#define ADNAN_PT_ALGO
/*********/
/* Setting Region */
//#define SIVA_SERIAL
#define ADNAN_SPIM
#ifdef SIVA_BLE
#define SIVA_BLE_AD
#endif
//#define SIVA_ANN
#define SIVA_RULE
/*********/
#if defined(SIVA_ANN) || defined(SIVA_RULE)
#ifdef SIVA_BLE_AD
#ifndef BLE_ALERT_ECGSAMPLE
//#define BLE_ALERT_1BYTE
#endif
#ifndef BLE_ALERT_1BYTE
#define BLE_ALERT_ECGSAMPLE
#endif
#endif
#endif
#if defined(SIVA_SERIAL) || defined(ADNAN_SPIM)
#if defined(SIVA_BLE) && defined(SIVA_BLE_AD)
#ifndef SIVA_ANN
#ifndef SIVA_RULE
#define BLE_ALL_SAMPLES
#endif
#endif
#endif
#endif
//#include "app_util_platform.h"
#include "nrf_delay.h"
#include "boards.h"
//#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrfx_timer.h"
#include "nrfx_gpiote.h"
#include <stdint.h>
#include "nordic_common.h"
#include "nrf.h"
#include "ble_hci.h"
#include "ble_advdata.h"
#include "ble_advertising.h"
#include "ble_conn_params.h"
#include "nrf_sdh.h"
#include "nrf_sdh_soc.h"
#include "nrf_sdh_ble.h"
#include "nrf_ble_gatt.h"
#include "nrf_ble_qwr.h"
#include "app_timer.h"
#include "ble_nus.h"
#include "app_uart.h"
#include "app_util_platform.h"
#include "bsp_btn_ble.h"
#include "nrf_pwr_mgmt.h"
#include "ble_lbs.h"
#ifdef ADNAN_SPIM
#include "nrfx_spim.h"
#include "nrfx_ppi.h"
#include "ads1292r.h"
#endif
#ifdef SIVA_ANN
#include "FixedPoint.h"
#include "FixedPointANN.h"
#include "HRV.h"
#include "Rules.h"
#include "ANN.h"
#endif
#ifdef SIVA_RULE
#include "FixedPoint.h"
#include "HRV.h"
#include "Rules.h"
#include "PCA.h"
#endif
#ifdef SIVA_SERIAL
#include "nrf_serial.h"
#include "app_util.h"
#include "nrf_drv_power.h"
#include "app_error.h"
#endif
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif
#ifdef SIVA_ANN
#define PCA_SIZE 5
#define NO_PC 6
#define BEAT_LEN 175
#define SHIFT_ANN_NORM_BIT (FIXED_POINT_FRACTIONAL_BITS - FIXED_POINT_FRACTIONAL_ANN_BITS)
fixed_point_ann_t Aout[2] = {0};
struct ANN_SIVA ann;
#endif
#ifdef SIVA_RULE
#define PCA_SIZE 10
#define NO_PC 1
#define BEAT_LEN 175
#endif
bool sendBeat = false;
#ifdef ADNAN_PKD
#define RR_SIZE 8
#define BUFF ARRAY_LIST_SIZE
#define TWO_SEC 500
//#define TWO_SEC 1000
#define WINDOW_WIDTH 38
//#define WINDOW_WIDTH 150
#define REF_PERIOD 50
//#define REF_PERIOD 200
#define T_PERIOD 90
//#define T_PERIOD 360
#define DELAY_F 21
#define DELAY_I 61
#endif
#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
#define DEVICE_NAME "A_SIVA_BLE" /**< Name of device. Will be included in the advertising data. */
#define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */
#define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
#define APP_ADV_INTERVAL 64 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
#define APP_ADV_DURATION 0 //18000 /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
#define MIN_CONN_INTERVAL MSEC_TO_UNITS(400, UNIT_1_25_MS) //400 /**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
#define MAX_CONN_INTERVAL MSEC_TO_UNITS(400, UNIT_1_25_MS) //400 /**< Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */
#define SLAVE_LATENCY 0 /**< Slave latency. */
#define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) //4000 /**< Connection supervisory timeout (4 seconds), Supervision Timeout uses 10 ms units. */
#define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
#define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
#define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */
#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
#define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
#ifdef ADNAN_SPIM
/*
#define MEMORY_SIZE_ROW 1000
#define MEMORY_SIZE_COL 6
static uint8_t memory[MEMORY_SIZE_ROW][MEMORY_SIZE_COL];
static uint16_t memory_index = 0;
static bool volatile memory_full = false;
*/
static volatile bool spi_xfer_done;
static const nrfx_spim_t spim = NRFX_SPIM_INSTANCE(2);
static const nrfx_timer_t timer = NRFX_TIMER_INSTANCE(1);
static nrf_ppi_channel_t ppi_channel;
static bool tx_rdy = true;
static bool resend;
static uint16_t ECG_index;
static uint16_t rr_avg1, rr_avg2 = 120;
static uint16_t global_index = 0;
//filter coefficients for pan algorithm
//int h_l[13] = {1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0, 0};
//int h_h[33] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0};
//float h_d[5] = {-0.125, -0.25, 0, 0.25, 0.125};
void empty_in_pin_handler(nrfx_gpiote_pin_t pin,
nrf_gpiote_polarity_t action)
{
}
#endif
//............Serial........................................//
#if defined(SIVA_SERIAL) || defined(ADNAN_SPIM)
#define BYTE_LEN 8
#define DATA_BUFFER_SIZE 1000
#define R_POS_SIZE 11
//BEAT REGION
//Sample Frequency
#define Fs 250
#define SamplingShift 8 //2^8 is close to the Fs 250 Hz, hence shifting by 8 can be treated as in time
//Sample length of single beat
#define lenSignal round(0.7*Fs)
//R peak location and left and right bands
#define RLoc round(0.25*Fs)-1
#define leftBand RLoc
#define rightBand (lenSignal-RLoc)
#define band round(0.056*Fs)
//DEFINE OUTPUT STRUCTURE
#if defined(SIVA_ANN) || defined(SIVA_RULE)
struct HRVmetric{
fixed_point_t wSDNN; // weighted SDNN
fixed_point_t SDNN; // SDNN
fixed_point_t rrIndex; // RR Index (2 * (Rpre - Rprepre)/ (Rpre - Rprepre))
fixed_point_t SD1; // SD1
fixed_point_t SD2; // SD2
};
#endif
struct BEAT{
#if defined(SIVA_ANN) || defined(SIVA_RULE)
fixed_point_t RR[R_POS_SIZE]; // RR interval in duration
fixed_point_t RRmean; // mean of RR interval
fixed_point_t PCA[PCA_SIZE][NO_PC]; // PCA of previous beats
fixed_point_32t qrsSum; // QRS sum around the band
fixed_point_32t qrsEnergy; // QRS energy around the band
fixed_point_t prevvSTD; // Max - Min V change of previous beat
fixed_point_t vvSTD; // Max - Min V change
//fixed_point_t PCAcoefs[NO_PC]; // PCA coefficient with respect to Normal
fixed_point_t PCAstd; // PCA standard deviation
struct HRVmetric HRV; // HRV structure
#endif
uint8_t Result; // Annotation
} BEAT;
//DEFINE THE DATA BUFFER
static int sampcount = 0;
static int16_t Data_Buf[DATA_BUFFER_SIZE];
static int beatcount = 0;
static int Rpos[R_POS_SIZE];
static char tx_message[] = "\n\rHello...!\r\n";
static char commma = ',';
static char new_line = '\n';
char cr[4];
#endif
#ifdef SIVA_SERIAL
static void sleep_handler(void)
{
__WFE();
__SEV();
__WFE();
}
NRF_SERIAL_DRV_UART_CONFIG_DEF(m_uart0_drv_config,
RX_PIN_NUMBER, TX_PIN_NUMBER,
RTS_PIN_NUMBER, CTS_PIN_NUMBER,
NRF_UART_HWFC_ENABLED, NRF_UART_PARITY_EXCLUDED, //NRF_UART_HWFC_ENABLED, NRF_UART_HWFC_DISABLED
NRF_UART_BAUDRATE_115200,
UART_DEFAULT_CONFIG_IRQ_PRIORITY);
#define SERIAL_FIFO_TX_SIZE 256
#define SERIAL_FIFO_RX_SIZE 256
NRF_SERIAL_QUEUES_DEF(serial_queues, SERIAL_FIFO_TX_SIZE, SERIAL_FIFO_RX_SIZE);
#define SERIAL_BUFF_TX_SIZE 32
#define SERIAL_BUFF_RX_SIZE 32
NRF_SERIAL_BUFFERS_DEF(serial_buffs, SERIAL_BUFF_TX_SIZE, SERIAL_BUFF_RX_SIZE);
NRF_SERIAL_CONFIG_DEF(serial_config, NRF_SERIAL_MODE_IRQ,
&serial_queues, &serial_buffs, NULL, sleep_handler);
NRF_SERIAL_UART_DEF(serial_uart, 0);
//.......end..Serial........................................//
#endif
#ifdef SIVA_BLE
BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT); /**< BLE NUS service instance. */
NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
NRF_BLE_QWR_DEF(m_qwr); /**< Context for the Queued Write module.*/
BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */
static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */
static uint16_t m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - 3; /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
static ble_uuid_t m_adv_uuids[] = /**< Universally unique service identifier. */
{
{BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}
};
#endif
#ifdef SIVA_BLE
#if (defined(SIVA_ANN) || defined(SIVA_RULE))
#ifdef SIVA_ANN
void ble_siva_ann_result_send()
{
uint16_t length;
ret_code_t err_code;
static uint8_t BLE_data[5] = {'~'};
// static uint8_t index = 0;
// NRF_LOG_INFO("Ready to send data over BLE NUS");
if (!resend)
{
BLE_data[1] = (uint8_t)(Aout[0]>> 8);
BLE_data[2] = (uint8_t)Aout[0];
BLE_data[3] = (uint8_t)(Aout[1] >> 8);
BLE_data[4] = (uint8_t)Aout[1];
}
else
resend = false;
length = 5;
err_code = ble_nus_data_send(&m_nus, BLE_data, &length, m_conn_handle);
if (err_code == NRF_ERROR_RESOURCES)
{
tx_rdy = false;
resend = true;
}
else if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_NOT_FOUND))
APP_ERROR_CHECK(err_code);
}
#endif
void ble_siva_ann_alert(char cr)
{
uint16_t length = 1;
ret_code_t err_code;
static uint8_t BLE_data = '~';
// static uint8_t index = 0;
// NRF_LOG_INFO("Ready to send data over BLE NUS");
if (!resend)
{
BLE_data = cr;
}
else
resend = false;
err_code = ble_nus_data_send(&m_nus, &BLE_data, &length, m_conn_handle);
if (err_code == NRF_ERROR_RESOURCES)
{
tx_rdy = false;
resend = true;
}
else if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_NOT_FOUND))
APP_ERROR_CHECK(err_code);
}
#endif
void ble_data_send()
{
uint16_t length;
ret_code_t err_code;
static uint8_t BLE_data[5] = {'~'};
// static uint8_t index = 0;
// NRF_LOG_INFO("Ready to send data over BLE NUS");
if (!resend)
{
BLE_data[1] = (uint8_t)(rr_avg1>> 8); //rr_avg1
BLE_data[2] = (uint8_t)rr_avg1;
BLE_data[3] = (uint8_t)(rr_avg2 >> 8);
BLE_data[4] = (uint8_t)rr_avg2; //rr_avg2
}
else
resend = false;
length = 5;
err_code = ble_nus_data_send(&m_nus, BLE_data, &length, m_conn_handle);
if (err_code == NRF_ERROR_RESOURCES)
{
tx_rdy = false;
resend = true;
}
else if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_NOT_FOUND))
APP_ERROR_CHECK(err_code);
}
void ble_sample_signal_send(int16_t *cr, int li, uint16_t length){
ret_code_t err_code;
static uint8_t BLE_data[240] = {'~'};
// static uint8_t index = 0;
// NRF_LOG_INFO("Ready to send data over BLE NUS");
if (!resend)
{
for(int ii=0;ii<length;){
BLE_data[ii] = (uint8_t)((*(cr+li+(ii/2)))>>8);
BLE_data[ii+1] = (uint8_t)(*(cr+li+(ii/2)));
ii = ii+2;
}
}
else
resend = false;
err_code = ble_nus_data_send(&m_nus, BLE_data, &length, m_conn_handle);
if (err_code == NRF_ERROR_RESOURCES)
{
tx_rdy = false;
resend = true;
}
else if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_NOT_FOUND))
APP_ERROR_CHECK(err_code);
}
void ble_sample_send(char cr[4])
{
uint16_t length = 3;
ret_code_t err_code;
static uint8_t BLE_data[3] = {'~'};
// static uint8_t index = 0;
// NRF_LOG_INFO("Ready to send data over BLE NUS");
if (!resend)
{
BLE_data[1] = (uint8_t)(cr[1]);
BLE_data[2] = (uint8_t)cr[2];
}
else
resend = false;
err_code = ble_nus_data_send(&m_nus, BLE_data, &length, m_conn_handle);
if (err_code == NRF_ERROR_RESOURCES)
{
tx_rdy = false;
resend = true;
}
else if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_NOT_FOUND))
APP_ERROR_CHECK(err_code);
}
#endif
#ifdef ADNAN_PT_ALGO
void Pan_Tompkins_algorithm()
{
bool regular_prev;
uint16_t index, sub_index, qrs_f_index, qrs_index_prev, qrs_i_miss_index, rr_avg1, rr_avg2;
uint32_t peaki;
int32_t peakf, minf, slope, qrs_slope;
static bool phase1_done = false;
static bool phase2_done = false;
static bool asc = false;
static bool rr1_full = false;
static bool rr2_full = false;
static bool regular = true;
static uint16_t qrs_i_index, qrs_index, rr_low_limit, rr_high_limit, rr_missed_limit;
static uint16_t ref_index = 0;
static uint16_t t_index = 0;
static uint16_t t_miss_index = 0;
static uint16_t miss_index = 0;
static uint8_t rr1_index = 0;
static uint8_t rr2_index = 0;
static int32_t ECG_signal[BUFF] = {0};
static int32_t low_pass_filter[BUFF] = {0};
static int32_t high_pass_filter[BUFF] = {0};
static int32_t derivative[BUFF] = {0};
static uint32_t squaring_function[BUFF] = {0};
static uint32_t moving_window_integration[BUFF] = {0};
static uint32_t moving_window_sum = 0;
static uint32_t spki, npki, threshold_i1, threshold_i2;
static int32_t spkf, npkf, minf_curr, threshold_f1, threshold_f2;
static uint16_t rr1[RR_SIZE] = {0};
static uint16_t rr2[RR_SIZE] = {0};
static uint16_t rr_sum1 = 0;
static uint16_t rr_sum2 = 0;
if (ref_index <= BUFF)
ref_index = 1;
else
ref_index -= BUFF;
if (t_index == t_miss_index)
{
if (t_index <= BUFF)
t_index = 0;
else
t_index -= BUFF;
}
else
{
t_index = 0;
t_miss_index = 0;
}
if (miss_index <= BUFF)
miss_index = 0;
else
miss_index -= BUFF;
minf = minf_curr;
minf_curr = INT32_MAX;
for (index = 0; index < BUFF; ++index)
{
ECG_signal[index] = (int32_t)(array_list[index].buffer[6] << 24 | array_list[index].buffer[7] << 16 | array_list[index].buffer[8] << 8) >> 8;
if (index >= WINDOW_WIDTH)
{
low_pass_filter[index] = (low_pass_filter[index-1]<<1) - low_pass_filter[index-2] + ECG_signal[index] - (ECG_signal[index-6]<<1) + ECG_signal[index-12];
high_pass_filter[index] = (low_pass_filter[index-16]<<5) - high_pass_filter[index-1] - low_pass_filter[index] + low_pass_filter[index-32];
if (minf_curr > high_pass_filter[index])
minf_curr = high_pass_filter[index];
derivative[index] = (-high_pass_filter[index-4] - (high_pass_filter[index-3]<<1) + (high_pass_filter[index-1]<<1) + high_pass_filter[index]) >> 10;
squaring_function[index] = derivative[index] * derivative[index];
moving_window_sum -= squaring_function[index-WINDOW_WIDTH];
moving_window_sum += squaring_function[index];
moving_window_integration[index] = moving_window_sum >> 5;
// moving_window_integration[index] = moving_window_sum >> 7;
}
else
{
low_pass_filter[index] = (low_pass_filter[(index-1+BUFF)%BUFF]<<1) - low_pass_filter[(index-2+BUFF)%BUFF] + ECG_signal[index] - (ECG_signal[(index-6+BUFF)%BUFF]<<1) + ECG_signal[(index-12+BUFF)%BUFF];
high_pass_filter[index] = (low_pass_filter[(index-16+BUFF)%BUFF]<<5) - high_pass_filter[(index-1+BUFF)%BUFF] - low_pass_filter[index] + low_pass_filter[(index-32+BUFF)%BUFF];
if (minf_curr > high_pass_filter[index])
minf_curr = high_pass_filter[index];
derivative[index] = (-high_pass_filter[(index-4+BUFF)%BUFF] - (high_pass_filter[(index-3+BUFF)%BUFF]<<1) + (high_pass_filter[(index-1+BUFF)%BUFF]<<1) + high_pass_filter[index]) >> 10;
squaring_function[index] = derivative[index] * derivative[index];
moving_window_sum -= squaring_function[(index-WINDOW_WIDTH+BUFF)%BUFF];
moving_window_sum += squaring_function[index];
moving_window_integration[index] = moving_window_sum >> 5;
// moving_window_integration[index] = moving_window_sum >> 7;
}
// qrs[index] = 0;
if (phase1_done && index >= ref_index)
{
if (moving_window_integration[index] > moving_window_integration[index-1] && asc == false)
asc = true;
else if (moving_window_integration[index] < moving_window_integration[index-1] && asc == true)
{
asc = false;
peaki = moving_window_integration[index-1];
if (peaki <= threshold_i1)
{
npki = (peaki >> 3) + npki - (npki >> 3);
threshold_i1 = npki + ((spki - npki) >> 2);
}
else
{
spki = (peaki >> 3) + spki - (spki >> 3);
threshold_i1 = npki + ((spki - npki) >> 2);
peakf = INT32_MIN;
for (sub_index = 0; sub_index < WINDOW_WIDTH; ++sub_index)
if (peakf < high_pass_filter[(index-sub_index+BUFF)%BUFF])
{
qrs_f_index = (index - sub_index + BUFF) % BUFF;
peakf = high_pass_filter[qrs_f_index];
}
if (peakf <= threshold_f1)
{
npkf = (peakf >> 3) + npkf - (npkf >> 3);
threshold_f1 = npkf + ((spkf - npkf) >> 2);
}
else
{
if (index < t_index)
{
qrs_slope = 0;
slope = 0;
for (sub_index = 0; sub_index < WINDOW_WIDTH; ++sub_index)
{
if (qrs_slope < derivative[(qrs_i_index-sub_index+BUFF)%BUFF])
qrs_slope = derivative[(qrs_i_index-sub_index+BUFF)%BUFF];
if (slope < derivative[(index-sub_index+BUFF)%BUFF])
slope = derivative[(index-sub_index+BUFF)%BUFF];
}
if (slope < (qrs_slope>>1))
{
npki = (peaki >> 3) + npki - (npki >> 3);
threshold_i1 = npki + ((spki - npki) >> 2);
npkf = (peakf >> 3) + npkf - (npkf >> 3);
threshold_f1 = npkf + ((spkf - npkf) >> 2);
// NRF_LOG_RAW_INFO("T Wave\n");
continue;
}
}
spkf = (peakf >> 3) + spkf - (spkf >> 3);
threshold_f1 = npkf + ((spkf - npkf) >> 2);
qrs_i_index = index - 1;
ref_index = qrs_i_index + REF_PERIOD;
t_index = qrs_i_index + T_PERIOD;
t_miss_index = t_index;
miss_index = qrs_i_index + rr_missed_limit;
rr_intervals:
qrs_index_prev = qrs_index;
qrs_index = (qrs_f_index - DELAY_F + BUFF) % BUFF;
// qrs[qrs_index] = 10000;
if (phase2_done)
{
rr_sum1 -= rr1[rr1_index];
rr1[rr1_index] = (qrs_index - qrs_index_prev + BUFF) % BUFF;
rr_sum1 += rr1[rr1_index];
if (rr2_full)
{
if (rr1[rr1_index] >= rr_low_limit && rr1[rr1_index] <= rr_high_limit)
{
rr_sum2 -= rr2[rr2_index];
rr2[rr2_index] = rr1[rr1_index];
rr_sum2 += rr2[rr2_index];
rr_avg2 = rr_sum2 >> 3;
rr2_index = (rr2_index + 1) % RR_SIZE;
rr_low_limit = 0.92 * rr_avg2;
rr_high_limit = 1.16 * rr_avg2;
rr_missed_limit = 1.66 * rr_avg2;
}
rr_avg1 = rr_sum1 >> 3;
rr1_index = (rr1_index + 1) % RR_SIZE;
}
else if (rr1_full)
{
if (rr1[rr1_index] >= rr_low_limit && rr1[rr1_index] <= rr_high_limit)
{
rr_sum2 -= rr2[rr2_index];
rr2[rr2_index] = rr1[rr1_index];
rr_sum2 += rr2[rr2_index];
rr_avg2 = rr_sum2 / ++rr2_index;
rr_low_limit = 0.92 * rr_avg2;
rr_high_limit = 1.16 * rr_avg2;
rr_missed_limit = 1.66 * rr_avg2;
}
rr_avg1 = rr_sum1 >> 3;
rr1_index = (rr1_index + 1) % RR_SIZE;
if (rr2_index == RR_SIZE)
{
rr2_index = 0;
rr2_full = true;
}
}
else
{
if (rr1[rr1_index] >= rr_low_limit && rr1[rr1_index] <= rr_high_limit)
{
rr_sum2 -= rr2[rr2_index];
rr2[rr2_index] = rr1[rr1_index];
rr_sum2 += rr2[rr2_index];
rr_avg2 = rr_sum2 / ++rr2_index;
rr_low_limit = 0.92 * rr_avg2;
rr_high_limit = 1.16 * rr_avg2;
rr_missed_limit = 1.66 * rr_avg2;
}
rr_avg1 = rr_sum1 / ++rr1_index;
if (rr1_index == RR_SIZE)
{
rr1_index = 0;
rr1_full = true;
}
if (rr2_index == RR_SIZE)
{
rr2_index = 0;
rr2_full = true;
}
}
regular_prev = regular;
if (rr_avg1 == rr_avg2)
{
regular = true;
if (regular != regular_prev)
{
threshold_i1 <<= 1;
threshold_f1 = ((threshold_f1 - minf) << 1) + minf;
}
}
else
{
regular = false;
if (regular != regular_prev)
{
threshold_i1 >>= 1;
threshold_f1 = ((threshold_f1 - minf) >> 1) + minf;
}
}
}
else
{
if (rr1_index)
{
rr1[0] = (qrs_index - qrs_index_prev + BUFF) % BUFF;
rr2[0] = rr1[0];
++rr2_index;
rr_sum1 = rr1[0];
rr_sum2 = rr2[0];
rr_avg1 = rr_sum1;
rr_avg2 = rr_sum2;
rr_low_limit = 0.92 * rr_avg2;
rr_high_limit = 1.16 * rr_avg2;
rr_missed_limit = 1.66 * rr_avg2;
phase2_done = true;
// NRF_LOG_RAW_INFO("rr_avg1 = %d, rr_avg2 = %d, rr_low_limit = %d, rr_high_limit = %d, rr_missed_limit = %d\n", rr_avg1, rr_avg2, rr_low_limit, rr_high_limit, rr_missed_limit);
}
else
++rr1_index;
}
// NRF_LOG_RAW_INFO("rr_avg1 = %d, rr_avg2 = %d, rr1 = %d, rr2 = %d\n", rr_avg1, rr_avg2, rr1[(rr1_index-1+RR_SIZE)%RR_SIZE], rr2[(rr2_index-1+RR_SIZE)%RR_SIZE]);
/* NRF_LOG_RAW_INFO("threshold_i1 = %d, peaki = %d, qrs_i_index = %d\n", threshold_i1, peaki, qrs_i_index);
NRF_LOG_RAW_INFO("threshold_f1 = %d, peakf = %d, qrs_f_index = %d\n", threshold_f1, peakf, qrs_f_index);
NRF_LOG_RAW_INFO("qrs_index = %d\n", qrs_index);
*/ }
}
}
if (phase2_done && index >= miss_index)
{
threshold_i2 = threshold_i1 >> 1;
threshold_f2 = ((threshold_f1 - minf) >> 1) + minf;
peaki = 0;
for (sub_index = 0; sub_index < (rr_missed_limit-REF_PERIOD); ++sub_index)
if (peaki < moving_window_integration[(index-sub_index+BUFF)%BUFF])
{
qrs_i_miss_index = (index - sub_index + BUFF) % BUFF;
peaki = moving_window_integration[qrs_i_miss_index];
}
if (peaki > threshold_i2)
{
spki = (peaki >> 2) + spki - (spki >> 2);
threshold_i1 = npki + ((spki - npki) >> 2);
peakf = INT32_MIN;
for (sub_index = 0; sub_index < WINDOW_WIDTH; ++sub_index)
if (peakf < high_pass_filter[(qrs_i_miss_index-sub_index+BUFF)%BUFF])
{
qrs_f_index = (qrs_i_miss_index - sub_index + BUFF) % BUFF;
peakf = high_pass_filter[qrs_f_index];
}
if (peakf > threshold_f2)
{
if (qrs_i_miss_index < ((qrs_i_miss_index>index) ? t_miss_index : t_index))
{
qrs_slope = 0;
slope = 0;
for (sub_index = 0; sub_index < WINDOW_WIDTH; ++sub_index)
{
if (qrs_slope < derivative[(qrs_i_index-sub_index+BUFF)%BUFF])
qrs_slope = derivative[(qrs_i_index-sub_index+BUFF)%BUFF];
if (slope < derivative[(qrs_i_miss_index-sub_index+BUFF)%BUFF])
slope = derivative[(qrs_i_miss_index-sub_index+BUFF)%BUFF];
}
if (slope < (qrs_slope>>1))
{
npki = (peaki >> 3) + npki - (npki >> 3);
threshold_i1 = npki + ((spki - npki) >> 2);
npkf = (peakf >> 3) + npkf - (npkf >> 3);
threshold_f1 = npkf + ((spkf - npkf) >> 2);
// NRF_LOG_RAW_INFO("T Wave\n");
continue;
}
}
spkf = (peakf >> 2) + spkf - (spkf >> 2);
threshold_f1 = npkf + ((spkf - npkf) >> 2);
qrs_i_index = qrs_i_miss_index;
ref_index = qrs_i_index + REF_PERIOD;
t_index = qrs_i_index + T_PERIOD;
t_miss_index = t_index;
miss_index = qrs_i_index + rr_missed_limit;
if (qrs_i_index > index)
{
if (ref_index <= BUFF)
ref_index = 1;
else
ref_index -= BUFF;
if (t_index <= BUFF)
t_index = 0;
else
t_index -= BUFF;
if (miss_index <= BUFF)
miss_index = 0;
else
miss_index -= BUFF;
}
goto rr_intervals;
}
}
}
}
else if (!phase1_done)
{
if (index == DELAY_I+5)
{
spki = moving_window_integration[index];
npki = moving_window_integration[index] >> 9;
// NRF_LOG_RAW_INFO("spki[DELAY_I] = %d, npki[DELAY_I] = %d\n", spki, npki);
}
else if (index > DELAY_I+5)
{
if (spki < moving_window_integration[index])
spki = moving_window_integration[index];
npki += moving_window_integration[index] >> 9;
}
if (index == DELAY_F+5)
{
minf_curr = high_pass_filter[index];
spkf = high_pass_filter[index];
npkf = high_pass_filter[index] >> 9;
// NRF_LOG_RAW_INFO("spkf[DELAY_F] = %d, npkf[DELAY_F] = %d\n", spkf, npkf);
}
else if (index > DELAY_F+5)
{
if (spkf < high_pass_filter[index])
spkf = high_pass_filter[index];
npkf += high_pass_filter[index] >> 9;
}
if (index == TWO_SEC-1)
{
// NRF_LOG_RAW_INFO("spki = %d, npki = %d, spkf = %d, npkf = %d, minf = %d\n", spki, npki, spkf, npkf, minf);
minf = minf_curr;
spki >>= 1;
npki >>= 1;
//spkf = ((spkf - minf) >> 1) + minf;
spkf = ((spkf - minf) >> 1) + ((spkf - minf) >> 2) + minf;
//npkf = ((npkf - minf) >> 1) + minf;
npkf = ((npkf - minf) >> 1) + ((npkf - minf) >> 2) + minf;
threshold_i1 = npki + ((spki - npki) >> 2);
threshold_f1 = npkf + ((spkf - npkf) >> 2);
// NRF_LOG_RAW_INFO("spki = %d, npki = %d, threshold_i1 = %d, threshold_i2 = %d\n", spki, npki, threshold_i1, threshold_i2);
// NRF_LOG_RAW_INFO("spkf = %d, npkf = %d, threshold_f1 = %d, threshold_f2 = %d\n", spkf, npkf, threshold_f1, threshold_f2);
phase1_done = true;
}
}
}
}
//void pan_algo(int32_t x) {
//
// int i;
// static int count = 0;
// static int buf_lp[13] = {0};
// static float buf_hp[33] = {0};
// static float buf_de[5] = {0};
// static float buf_ma[38] = {0}; //window size may change depending on sampling f
// //static float buf_ma[150] = {0}; //window size may change depending on sampling f
// static float buf_fm[5];
// float ecg_temp;