Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
hspguisourcev301/HspGuiSourceV301/HSPGui/Presenter/RToRCalculator.cs@3:36de8b9e4b1a, 2021-04-10 (annotated)
- Committer:
- darienf
- Date:
- Sat Apr 10 03:05:42 2021 +0000
- Revision:
- 3:36de8b9e4b1a
ayoooo
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| darienf | 3:36de8b9e4b1a | 1 | /******************************************************************************* |
| darienf | 3:36de8b9e4b1a | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All rights Reserved. |
| darienf | 3:36de8b9e4b1a | 3 | * |
| darienf | 3:36de8b9e4b1a | 4 | * This software is protected by copyright laws of the United States and |
| darienf | 3:36de8b9e4b1a | 5 | * of foreign countries. This material may also be protected by patent laws |
| darienf | 3:36de8b9e4b1a | 6 | * and technology transfer regulations of the United States and of foreign |
| darienf | 3:36de8b9e4b1a | 7 | * countries. This software is furnished under a license agreement and/or a |
| darienf | 3:36de8b9e4b1a | 8 | * nondisclosure agreement and may only be used or reproduced in accordance |
| darienf | 3:36de8b9e4b1a | 9 | * with the terms of those agreements. Dissemination of this information to |
| darienf | 3:36de8b9e4b1a | 10 | * any party or parties not specified in the license agreement and/or |
| darienf | 3:36de8b9e4b1a | 11 | * nondisclosure agreement is expressly prohibited. |
| darienf | 3:36de8b9e4b1a | 12 | * |
| darienf | 3:36de8b9e4b1a | 13 | * The above copyright notice and this permission notice shall be included |
| darienf | 3:36de8b9e4b1a | 14 | * in all copies or substantial portions of the Software. |
| darienf | 3:36de8b9e4b1a | 15 | * |
| darienf | 3:36de8b9e4b1a | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| darienf | 3:36de8b9e4b1a | 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| darienf | 3:36de8b9e4b1a | 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| darienf | 3:36de8b9e4b1a | 19 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
| darienf | 3:36de8b9e4b1a | 20 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| darienf | 3:36de8b9e4b1a | 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| darienf | 3:36de8b9e4b1a | 22 | * OTHER DEALINGS IN THE SOFTWARE. |
| darienf | 3:36de8b9e4b1a | 23 | * |
| darienf | 3:36de8b9e4b1a | 24 | * Except as contained in this notice, the name of Maxim Integrated |
| darienf | 3:36de8b9e4b1a | 25 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
| darienf | 3:36de8b9e4b1a | 26 | * Products, Inc. Branding Policy. |
| darienf | 3:36de8b9e4b1a | 27 | * |
| darienf | 3:36de8b9e4b1a | 28 | * The mere transfer of this software does not imply any licenses |
| darienf | 3:36de8b9e4b1a | 29 | * of trade secrets, proprietary technology, copyrights, patents, |
| darienf | 3:36de8b9e4b1a | 30 | * trademarks, maskwork rights, or any other form of intellectual |
| darienf | 3:36de8b9e4b1a | 31 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
| darienf | 3:36de8b9e4b1a | 32 | * ownership rights. |
| darienf | 3:36de8b9e4b1a | 33 | ******************************************************************************* |
| darienf | 3:36de8b9e4b1a | 34 | */ |
| darienf | 3:36de8b9e4b1a | 35 | |
| darienf | 3:36de8b9e4b1a | 36 | using System; |
| darienf | 3:36de8b9e4b1a | 37 | using System.Collections.Generic; |
| darienf | 3:36de8b9e4b1a | 38 | using System.Linq; |
| darienf | 3:36de8b9e4b1a | 39 | using System.Text; |
| darienf | 3:36de8b9e4b1a | 40 | |
| darienf | 3:36de8b9e4b1a | 41 | namespace HealthSensorPlatform.Presenter |
| darienf | 3:36de8b9e4b1a | 42 | { |
| darienf | 3:36de8b9e4b1a | 43 | /// <summary> |
| darienf | 3:36de8b9e4b1a | 44 | /// R To R Calculator for referring R to R code values to ECG samples |
| darienf | 3:36de8b9e4b1a | 45 | /// </summary> |
| darienf | 3:36de8b9e4b1a | 46 | public class RToRCalculator |
| darienf | 3:36de8b9e4b1a | 47 | { |
| darienf | 3:36de8b9e4b1a | 48 | const int RToROffset = 3; |
| darienf | 3:36de8b9e4b1a | 49 | |
| darienf | 3:36de8b9e4b1a | 50 | double masterClockFrequency; |
| darienf | 3:36de8b9e4b1a | 51 | int rToRDifferentialDelay; |
| darienf | 3:36de8b9e4b1a | 52 | |
| darienf | 3:36de8b9e4b1a | 53 | int ecgDelay; |
| darienf | 3:36de8b9e4b1a | 54 | int rToRDelay; |
| darienf | 3:36de8b9e4b1a | 55 | |
| darienf | 3:36de8b9e4b1a | 56 | double sampleRateRToR; |
| darienf | 3:36de8b9e4b1a | 57 | double sampleRateEcg; |
| darienf | 3:36de8b9e4b1a | 58 | |
| darienf | 3:36de8b9e4b1a | 59 | double rToRTimeMilliSecond; |
| darienf | 3:36de8b9e4b1a | 60 | |
| darienf | 3:36de8b9e4b1a | 61 | /// <summary> |
| darienf | 3:36de8b9e4b1a | 62 | /// Error difference between rounded R To R in ECG samples and actual calculated value. Error used |
| darienf | 3:36de8b9e4b1a | 63 | /// in calculation of next R To R in ECG samples point. |
| darienf | 3:36de8b9e4b1a | 64 | /// </summary> |
| darienf | 3:36de8b9e4b1a | 65 | double ecgSampleError; |
| darienf | 3:36de8b9e4b1a | 66 | |
| darienf | 3:36de8b9e4b1a | 67 | /// <summary> |
| darienf | 3:36de8b9e4b1a | 68 | /// FMSTR clock frequency. Array index by FMSTR[1:0] |
| darienf | 3:36de8b9e4b1a | 69 | /// </summary> |
| darienf | 3:36de8b9e4b1a | 70 | double[] clockFreq = { 32768, 32000, 32000, 32768 * 640 / 656 }; // 31968.7804878 = 32768 * 640 / 65 |
| darienf | 3:36de8b9e4b1a | 71 | /// <summary> |
| darienf | 3:36de8b9e4b1a | 72 | /// ECG decimation delay. Array indexed by FMSTR, ECG_RATE, ECG_DLFF (0 for disabled, 1 for enabled) |
| darienf | 3:36de8b9e4b1a | 73 | /// </summary> |
| darienf | 3:36de8b9e4b1a | 74 | int[, ,] ecgDecimationDelay = new int[,,] { {{650, 1034}, {2922, 3690}, {3370, 4906}, {3370, 4906}}, |
| darienf | 3:36de8b9e4b1a | 75 | {{650, 1034}, {2922, 3690}, {3370, 4906}, {3370, 4906}}, |
| darienf | 3:36de8b9e4b1a | 76 | {{1242, 2202}, {1242, 2202}, {1242, 2202}, {1242, 2202}}, |
| darienf | 3:36de8b9e4b1a | 77 | {{1242, 2202}, {1242, 2202}, {1242, 2202}, {1242, 2202}} |
| darienf | 3:36de8b9e4b1a | 78 | }; |
| darienf | 3:36de8b9e4b1a | 79 | |
| darienf | 3:36de8b9e4b1a | 80 | /// <summary> |
| darienf | 3:36de8b9e4b1a | 81 | /// ECG sample rate look up based on FMSTR and ECG_RATE |
| darienf | 3:36de8b9e4b1a | 82 | /// </summary> |
| darienf | 3:36de8b9e4b1a | 83 | double[][] ecgSampleRates = new double[][] |
| darienf | 3:36de8b9e4b1a | 84 | { |
| darienf | 3:36de8b9e4b1a | 85 | new double[] {512, 256, 128, 128}, |
| darienf | 3:36de8b9e4b1a | 86 | new double[] {500, 250, 125, 125}, |
| darienf | 3:36de8b9e4b1a | 87 | new double[] {200, 200, 200, 200}, |
| darienf | 3:36de8b9e4b1a | 88 | new double[] {199.8, 199.8, 199.8, 199.8} |
| darienf | 3:36de8b9e4b1a | 89 | }; |
| darienf | 3:36de8b9e4b1a | 90 | |
| darienf | 3:36de8b9e4b1a | 91 | /// <summary> |
| darienf | 3:36de8b9e4b1a | 92 | /// ECG processing delay in FMSTR clocks |
| darienf | 3:36de8b9e4b1a | 93 | /// </summary> |
| darienf | 3:36de8b9e4b1a | 94 | public int EcgDecimationDelay |
| darienf | 3:36de8b9e4b1a | 95 | { |
| darienf | 3:36de8b9e4b1a | 96 | get |
| darienf | 3:36de8b9e4b1a | 97 | { |
| darienf | 3:36de8b9e4b1a | 98 | return ecgDelay; |
| darienf | 3:36de8b9e4b1a | 99 | } |
| darienf | 3:36de8b9e4b1a | 100 | } |
| darienf | 3:36de8b9e4b1a | 101 | |
| darienf | 3:36de8b9e4b1a | 102 | /// <summary> |
| darienf | 3:36de8b9e4b1a | 103 | /// R To R path processing delay in FMSTR clocks |
| darienf | 3:36de8b9e4b1a | 104 | /// </summary> |
| darienf | 3:36de8b9e4b1a | 105 | public int RToRDelay |
| darienf | 3:36de8b9e4b1a | 106 | { |
| darienf | 3:36de8b9e4b1a | 107 | get |
| darienf | 3:36de8b9e4b1a | 108 | { |
| darienf | 3:36de8b9e4b1a | 109 | return rToRDelay; |
| darienf | 3:36de8b9e4b1a | 110 | } |
| darienf | 3:36de8b9e4b1a | 111 | } |
| darienf | 3:36de8b9e4b1a | 112 | |
| darienf | 3:36de8b9e4b1a | 113 | /// <summary> |
| darienf | 3:36de8b9e4b1a | 114 | /// Last R To R detected time in millisecond from first ECG point |
| darienf | 3:36de8b9e4b1a | 115 | /// </summary> |
| darienf | 3:36de8b9e4b1a | 116 | public double RToRMilliSecond |
| darienf | 3:36de8b9e4b1a | 117 | { |
| darienf | 3:36de8b9e4b1a | 118 | get |
| darienf | 3:36de8b9e4b1a | 119 | { |
| darienf | 3:36de8b9e4b1a | 120 | return rToRTimeMilliSecond; |
| darienf | 3:36de8b9e4b1a | 121 | } |
| darienf | 3:36de8b9e4b1a | 122 | } |
| darienf | 3:36de8b9e4b1a | 123 | |
| darienf | 3:36de8b9e4b1a | 124 | /// <summary> |
| darienf | 3:36de8b9e4b1a | 125 | /// Constructor for R to R calculator |
| darienf | 3:36de8b9e4b1a | 126 | /// </summary> |
| darienf | 3:36de8b9e4b1a | 127 | /// <param name="masterClockFrequencyField">Value of FMSTR[1:0]</param> |
| darienf | 3:36de8b9e4b1a | 128 | /// <param name="sampleRateEcgField">Value of ECG_RATE[1:0]</param> |
| darienf | 3:36de8b9e4b1a | 129 | /// <param name="ecgDigitalLowPass">Value of ECG_DLF[1:0]</param> |
| darienf | 3:36de8b9e4b1a | 130 | /// <param name="rTorWindowField">Value of RTOR_WNDW[3:0]</param> |
| darienf | 3:36de8b9e4b1a | 131 | public RToRCalculator(int masterClockFrequencyField, int sampleRateEcgField, int ecgDigitalLowPass, int rTorWindowField) |
| darienf | 3:36de8b9e4b1a | 132 | { |
| darienf | 3:36de8b9e4b1a | 133 | masterClockFrequency = clockFreq[masterClockFrequencyField]; |
| darienf | 3:36de8b9e4b1a | 134 | sampleRateRToR = masterClockFrequency / 256; |
| darienf | 3:36de8b9e4b1a | 135 | sampleRateEcg = ecgSampleRates[masterClockFrequencyField][sampleRateEcgField]; |
| darienf | 3:36de8b9e4b1a | 136 | |
| darienf | 3:36de8b9e4b1a | 137 | ecgDelay = ecgDecimationDelay[masterClockFrequencyField, sampleRateEcgField, ecgDigitalLowPass > 0 ? 1 : 0]; |
| darienf | 3:36de8b9e4b1a | 138 | rToRDelay = 5376 + 3370 + 256 * rTorWindowField; |
| darienf | 3:36de8b9e4b1a | 139 | |
| darienf | 3:36de8b9e4b1a | 140 | rToRDifferentialDelay = rToRDelay - ecgDelay; |
| darienf | 3:36de8b9e4b1a | 141 | |
| darienf | 3:36de8b9e4b1a | 142 | rToRTimeMilliSecond = 0; |
| darienf | 3:36de8b9e4b1a | 143 | } |
| darienf | 3:36de8b9e4b1a | 144 | |
| darienf | 3:36de8b9e4b1a | 145 | /// <summary> |
| darienf | 3:36de8b9e4b1a | 146 | /// R To R code to beats per minute |
| darienf | 3:36de8b9e4b1a | 147 | /// </summary> |
| darienf | 3:36de8b9e4b1a | 148 | /// <param name="rToRCode">R To R Code</param> |
| darienf | 3:36de8b9e4b1a | 149 | /// <returns></returns> |
| darienf | 3:36de8b9e4b1a | 150 | public double BeatsPerMinute(int rToRCode) |
| darienf | 3:36de8b9e4b1a | 151 | { |
| darienf | 3:36de8b9e4b1a | 152 | return 60 * 1000 / Millisecond(rToRCode); |
| darienf | 3:36de8b9e4b1a | 153 | } |
| darienf | 3:36de8b9e4b1a | 154 | |
| darienf | 3:36de8b9e4b1a | 155 | /// <summary> |
| darienf | 3:36de8b9e4b1a | 156 | /// R To R code to millisecond interval |
| darienf | 3:36de8b9e4b1a | 157 | /// </summary> |
| darienf | 3:36de8b9e4b1a | 158 | /// <param name="rToRCode">R To R Code</param> |
| darienf | 3:36de8b9e4b1a | 159 | /// <returns></returns> |
| darienf | 3:36de8b9e4b1a | 160 | public double Millisecond(double rToRCode) |
| darienf | 3:36de8b9e4b1a | 161 | { |
| darienf | 3:36de8b9e4b1a | 162 | return rToRCode * 512.0 * 1000 / (2 * masterClockFrequency); |
| darienf | 3:36de8b9e4b1a | 163 | } |
| darienf | 3:36de8b9e4b1a | 164 | |
| darienf | 3:36de8b9e4b1a | 165 | public double Corrected(int rToR, bool first) |
| darienf | 3:36de8b9e4b1a | 166 | { |
| darienf | 3:36de8b9e4b1a | 167 | if (first) |
| darienf | 3:36de8b9e4b1a | 168 | rToRTimeMilliSecond = Millisecond(rToR - rToRDifferentialDelay / 256.0); |
| darienf | 3:36de8b9e4b1a | 169 | else |
| darienf | 3:36de8b9e4b1a | 170 | rToRTimeMilliSecond += Millisecond(rToR); |
| darienf | 3:36de8b9e4b1a | 171 | |
| darienf | 3:36de8b9e4b1a | 172 | return rToRTimeMilliSecond; |
| darienf | 3:36de8b9e4b1a | 173 | } |
| darienf | 3:36de8b9e4b1a | 174 | |
| darienf | 3:36de8b9e4b1a | 175 | /// <summary> |
| darienf | 3:36de8b9e4b1a | 176 | /// R To R code converted to ECG points |
| darienf | 3:36de8b9e4b1a | 177 | /// </summary> |
| darienf | 3:36de8b9e4b1a | 178 | /// <param name="rToR">R To R code</param> |
| darienf | 3:36de8b9e4b1a | 179 | /// <param name="first">first R To R sample include the processing delay between the ECG and R To R processing path</param> |
| darienf | 3:36de8b9e4b1a | 180 | /// <returns></returns> |
| darienf | 3:36de8b9e4b1a | 181 | public int EcgPoints(int rToR, bool first) |
| darienf | 3:36de8b9e4b1a | 182 | { |
| darienf | 3:36de8b9e4b1a | 183 | double rToRInEcgSamples; |
| darienf | 3:36de8b9e4b1a | 184 | int rToRInEcgSamplesInt; |
| darienf | 3:36de8b9e4b1a | 185 | |
| darienf | 3:36de8b9e4b1a | 186 | if (first) |
| darienf | 3:36de8b9e4b1a | 187 | rToRInEcgSamples = (rToR - rToRDifferentialDelay / 256.0) * (sampleRateEcg / sampleRateRToR) + RToROffset; |
| darienf | 3:36de8b9e4b1a | 188 | else |
| darienf | 3:36de8b9e4b1a | 189 | rToRInEcgSamples = rToR * (sampleRateEcg / sampleRateRToR) - ecgSampleError; |
| darienf | 3:36de8b9e4b1a | 190 | |
| darienf | 3:36de8b9e4b1a | 191 | rToRInEcgSamplesInt = (int)(rToRInEcgSamples + 0.5); |
| darienf | 3:36de8b9e4b1a | 192 | ecgSampleError = rToRInEcgSamplesInt - rToRInEcgSamples; |
| darienf | 3:36de8b9e4b1a | 193 | |
| darienf | 3:36de8b9e4b1a | 194 | return rToRInEcgSamplesInt; |
| darienf | 3:36de8b9e4b1a | 195 | } |
| darienf | 3:36de8b9e4b1a | 196 | } |
| darienf | 3:36de8b9e4b1a | 197 | } |