I've got some basic filter code setup (but not yet tested).
Dependencies: BLE_API Queue mbed nRF51822
Fork of BLE_HeartRate by
sampler.cpp@62:8e2fbe131b53, 2015-06-28 (annotated)
- Committer:
- roysandberg
- Date:
- Sun Jun 28 03:06:00 2015 +0000
- Revision:
- 62:8e2fbe131b53
- Parent:
- 61:1de72bdab0ef
Working Beat Detection and Analysis
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roysandberg | 61:1de72bdab0ef | 1 | #include "mbed.h" |
roysandberg | 62:8e2fbe131b53 | 2 | #include <qrsdet.h> |
roysandberg | 61:1de72bdab0ef | 3 | // Use a ticker (interrupt routine) to sample the ADC at a fast rate (is 200Khz the max?), and generate an average value |
roysandberg | 61:1de72bdab0ef | 4 | // Use another ticker to grab the average value at 1Khz, which is the maximum useful sampling rate for HRV data collection |
roysandberg | 61:1de72bdab0ef | 5 | |
roysandberg | 61:1de72bdab0ef | 6 | |
roysandberg | 61:1de72bdab0ef | 7 | // These need to divide evenly into each other |
roysandberg | 61:1de72bdab0ef | 8 | |
roysandberg | 62:8e2fbe131b53 | 9 | // 3 means sample at 8x read-out rate - SAMPLE_RATE is defined in qrsdet.h |
roysandberg | 62:8e2fbe131b53 | 10 | #define SHIFT_FOR_READ_OUT 1 |
roysandberg | 62:8e2fbe131b53 | 11 | #define ADC_RATE (SAMPLE_RATE<<SHIFT_FOR_READ_OUT) |
roysandberg | 62:8e2fbe131b53 | 12 | #define READ_OUT_RATE_HZ SAMPLE_RATE |
roysandberg | 62:8e2fbe131b53 | 13 | |
roysandberg | 62:8e2fbe131b53 | 14 | #define SAMPLES_PER_READOUT (1<<SHIFT_FOR_READ_OUT) |
roysandberg | 61:1de72bdab0ef | 15 | |
roysandberg | 61:1de72bdab0ef | 16 | AnalogIn ECG(P0_1); |
roysandberg | 61:1de72bdab0ef | 17 | Ticker sampling_rate; |
roysandberg | 61:1de72bdab0ef | 18 | int NumReadings=0; |
roysandberg | 62:8e2fbe131b53 | 19 | int32_t ReadingTotal=0; |
roysandberg | 61:1de72bdab0ef | 20 | void (*SamplingFunction)(uint32_t); |
roysandberg | 61:1de72bdab0ef | 21 | |
roysandberg | 61:1de72bdab0ef | 22 | |
roysandberg | 61:1de72bdab0ef | 23 | void ADC_read() |
roysandberg | 61:1de72bdab0ef | 24 | { |
roysandberg | 61:1de72bdab0ef | 25 | NumReadings++; |
roysandberg | 61:1de72bdab0ef | 26 | ReadingTotal += ECG.read_u16(); |
roysandberg | 61:1de72bdab0ef | 27 | if (NumReadings == SAMPLES_PER_READOUT) |
roysandberg | 61:1de72bdab0ef | 28 | { |
roysandberg | 62:8e2fbe131b53 | 29 | SamplingFunction(ReadingTotal>>SHIFT_FOR_READ_OUT); |
roysandberg | 61:1de72bdab0ef | 30 | ReadingTotal = 0; |
roysandberg | 61:1de72bdab0ef | 31 | NumReadings = 0; |
roysandberg | 61:1de72bdab0ef | 32 | } |
roysandberg | 61:1de72bdab0ef | 33 | } |
roysandberg | 61:1de72bdab0ef | 34 | |
roysandberg | 61:1de72bdab0ef | 35 | void setup_sampler(void (*function)(uint32_t)) |
roysandberg | 61:1de72bdab0ef | 36 | { |
roysandberg | 61:1de72bdab0ef | 37 | SamplingFunction = function; |
roysandberg | 62:8e2fbe131b53 | 38 | sampling_rate.attach(&ADC_read, 1.0/ADC_RATE); |
roysandberg | 61:1de72bdab0ef | 39 | } |