I've got some basic filter code setup (but not yet tested).
Dependencies: BLE_API Queue mbed nRF51822
Fork of BLE_HeartRate by
Diff: sampler.cpp
- Revision:
- 62:8e2fbe131b53
- Parent:
- 61:1de72bdab0ef
--- a/sampler.cpp Sun May 17 00:27:16 2015 +0000 +++ b/sampler.cpp Sun Jun 28 03:06:00 2015 +0000 @@ -1,19 +1,22 @@ #include "mbed.h" - +#include <qrsdet.h> // Use a ticker (interrupt routine) to sample the ADC at a fast rate (is 200Khz the max?), and generate an average value // Use another ticker to grab the average value at 1Khz, which is the maximum useful sampling rate for HRV data collection // These need to divide evenly into each other -#define SAMPLING_RATE_HZ 4000 -#define READ_OUT_RATE_HZ 50 -#define SAMPLES_PER_READOUT (SAMPLING_RATE_HZ/READ_OUT_RATE_HZ) +// 3 means sample at 8x read-out rate - SAMPLE_RATE is defined in qrsdet.h +#define SHIFT_FOR_READ_OUT 1 +#define ADC_RATE (SAMPLE_RATE<<SHIFT_FOR_READ_OUT) +#define READ_OUT_RATE_HZ SAMPLE_RATE + +#define SAMPLES_PER_READOUT (1<<SHIFT_FOR_READ_OUT) AnalogIn ECG(P0_1); Ticker sampling_rate; int NumReadings=0; -uint32_t ReadingTotal=0; +int32_t ReadingTotal=0; void (*SamplingFunction)(uint32_t); @@ -23,7 +26,7 @@ ReadingTotal += ECG.read_u16(); if (NumReadings == SAMPLES_PER_READOUT) { - SamplingFunction(ReadingTotal/NumReadings); + SamplingFunction(ReadingTotal>>SHIFT_FOR_READ_OUT); ReadingTotal = 0; NumReadings = 0; } @@ -32,5 +35,5 @@ void setup_sampler(void (*function)(uint32_t)) { SamplingFunction = function; - sampling_rate.attach(&ADC_read, 1.0/SAMPLING_RATE_HZ); + sampling_rate.attach(&ADC_read, 1.0/ADC_RATE); }