ALGO_Algorithm_ATY.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @file ALGO_Algorithm_ATY.h
  3. *
  4. * @param Project ALGO_Algorithm_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2025 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of algorithm
  20. *
  21. * @version
  22. * - 1_01_220601 > ATY
  23. * -# Preliminary version, first Release
  24. ********************************************************************************
  25. */
  26. #ifndef __ALGO_Algorithm_ATY_H
  27. #define __ALGO_Algorithm_ATY_H
  28. #include "INCLUDE_ATY.h"
  29. /******************************* For user *************************************/
  30. // #define __DEBUG_ALGO_Algorithm_ATY
  31. // #define __DEBUG_ALGO_Kalman_ATY
  32. // #define __DEBUG_WAVE_ATY
  33. /******************************************************************************/
  34. /* Kalman filter **************************************************************/
  35. typedef struct
  36. {
  37. float L_P; // estimate uncertainty (covariance) matrix of last state, not zero !
  38. float P; // predicted estimate uncertainty (covariance) matrix for this state
  39. float Out; // kalman filter out
  40. float G; // kalman gain
  41. float Q; // process noise (covariance) matrix
  42. float R; // measurement noise covariance matrix (Measurement Uncertainty)
  43. }ALGO_Kalman1D_S;
  44. float ALGO_KalmanFilter1D(ALGO_Kalman1D_S* kfp, float input);
  45. #ifdef __DEBUG_ALGO_Kalman_ATY
  46. extern ALGO_Kalman1D_S ALGO_kfp;
  47. extern float testSignalSigned[120];
  48. extern float testSignalUnsigned[120];
  49. extern uint16_t testSignalUint16[5000];
  50. void ALGO_Kalman_Test(void);
  51. #endif /* __DEBUG_ALGO_Kalman_ATY */
  52. /* Wave parameters ************************************************************/
  53. extern uint16_t wave_lastWaveAve_Kalman;
  54. extern uint16_t wave_firstWavePeakPoint, wave_firstWaveZeroPoint;
  55. extern uint16_t wave_quarterSpace;
  56. extern uint16_t wave_maxVpp;
  57. #define WAVE_DISPLAY_NUM 100
  58. #define WAVE_DISPLAY_PEAK_HIGH wave_lastWaveAve_Kalman + (WAVE_DISPLAY_NUM * 2)
  59. #define WAVE_DISPLAY_PEAK_LOW wave_lastWaveAve_Kalman - (WAVE_DISPLAY_NUM * 2)
  60. #define WAVE_DISPLAY_ZERO_RAISE wave_lastWaveAve_Kalman + WAVE_DISPLAY_NUM
  61. #define WAVE_DISPLAY_ZERO_FALL wave_lastWaveAve_Kalman - WAVE_DISPLAY_NUM
  62. #define WAVE_BASE_AVE wave_lastWaveAve_Kalman
  63. #define WAVE_KALMAN_ATTENUATION 2
  64. void WavePeakZeroPointFind(
  65. uint16_t* inputWave,
  66. uint16_t* kalmanWave,
  67. uint16_t* deepKalmanWave,
  68. uint16_t* peakZeroPoints,
  69. uint16_t size);
  70. #define WAVE_FIRST_COUNT 10
  71. #define WAVE_FIRST_THRESHOLD_V 25
  72. #define FIRST_THRESHOLD_HIGH (wave_lastWaveAve_Kalman + WAVE_FIRST_THRESHOLD_V)
  73. #define FIRST_THRESHOLD_LOW (wave_lastWaveAve_Kalman - WAVE_FIRST_THRESHOLD_V)
  74. void WaveFirstPulseFind(uint16_t* inputWave, uint16_t* peakZeroPoints, uint16_t size);
  75. // trans time(12bit@36MHz ADCCLK) = point to point time
  76. // 15 x 1_000_000us / 36_000_000Hz = 5/12us = 0.4167us
  77. #define ADC_TIME_UNIT (15.0 / 36.0)
  78. void WaveParamCalc(uint16_t* peakZeroPoints, uint16_t firstWavePoint, uint16_t size);
  79. void WaveVppCalc(uint16_t* peakZeroPoints, uint16_t* kalmanWave, uint16_t size);
  80. void WaveWholeProcess(
  81. uint16_t* inputWave,
  82. uint16_t* kalmanWave,
  83. uint16_t* deepKalmanWave,
  84. uint16_t* peakZeroPoints,
  85. uint16_t size);
  86. #ifdef __DEBUG_WAVE_ATY
  87. #define TEST_WAVE_SIZE 5000
  88. extern uint16_t kalmanWave_Test[TEST_WAVE_SIZE];
  89. extern uint16_t deepKalmanWave_Test[TEST_WAVE_SIZE];
  90. extern uint16_t peakZeroPoints_Test[TEST_WAVE_SIZE];
  91. void WaveAnalyse_Test(uint16_t* testDataIn, uint8_t WAVE_DISPLAY_FLAG);
  92. #endif /* __DEBUG_WAVE_ATY */
  93. #define ALGO_TEMP_Kelvin_T0 0 // K
  94. #define ALGO_TEMP_Celsius_C0 0 // C
  95. #define ALGO_TEMP_Fahrenheit_F0 0 // F
  96. #define ALGO_TEMP_TtoC(T) ((T) - 273.15)
  97. #define ALGO_TEMP_CtoT(C) ((C) + 273.15)
  98. #define ALGO_TEMP_CtoF(C) ((C) * 1.8 + 32)
  99. #define ALGO_TEMP_FtoC(F) (((F) - 32 ) / 1.8)
  100. #define ALGO_TEMP_FtoT(F) ALGO_TEMP_CtoT(ALGO_TEMP_FtoC((F)))
  101. #define ALGO_TEMP_TtoF(T) ALGO_TEMP_CtoF(ALGO_TEMP_TtoC((T)))
  102. double ALGO_MATH_Pow(double num, uint8_t n);
  103. double ALGO_MATH_LogLn(double num);
  104. float ALGO_ResToKelvinTemp(float Rntc, float R25, float B);
  105. float ALGO_WaterDensityFromTemp(float temperature);
  106. float ALGO_EConductanceFromRes(float Res);
  107. float ALGO_ResistivityFromRes(float Res, float A, float L);
  108. float ALGO_ConductivityFromRes(float Res, float A, float L);
  109. float ALGO_MolarityFromConductivity(float Conductivity, float MC);
  110. float ALGO_MassConcentrationFromMolarity(float Molarity, float MM);
  111. #endif /* __ALGO_Algorithm_ATY_H */
  112. /******************************** End Of File *********************************/