/**
* @file ALGO_Algorithm_ATY.h
*
* @param Project ALGO_Algorithm_ATY_LIB
*
* @author ATY
*
* @copyright
* - Copyright 2017 - 2025 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
*
* https://mengze.top/MZ-ATY_VCJS
* - CC 4.0 BY-NC-SA -
*
* https://creativecommons.org/licenses/by-nc-sa/4.0/
* - Your use will be deemed to have accepted the terms of this statement.
*
* @brief Familiar functions of algorithm
*
* @version
* - 1_01_220601 > ATY
* -# Preliminary version, first Release
********************************************************************************
*/
#ifndef __ALGO_Algorithm_ATY_H
#define __ALGO_Algorithm_ATY_H
#include "INCLUDE_ATY.h"
/******************************* For user *************************************/
// #define __DEBUG_ALGO_Algorithm_ATY
// #define __DEBUG_ALGO_Kalman_ATY
// #define __DEBUG_WAVE_ATY
/******************************************************************************/
/* Kalman filter **************************************************************/
typedef struct
{
float L_P; // estimate uncertainty (covariance) matrix of last state, not zero !
float P; // predicted estimate uncertainty (covariance) matrix for this state
float Out; // kalman filter out
float G; // kalman gain
float Q; // process noise (covariance) matrix
float R; // measurement noise covariance matrix (Measurement Uncertainty)
}ALGO_Kalman1D_S;
float ALGO_KalmanFilter1D(ALGO_Kalman1D_S* kfp, float input);
#ifdef __DEBUG_ALGO_Kalman_ATY
extern ALGO_Kalman1D_S ALGO_kfp;
extern float testSignalSigned[120];
extern float testSignalUnsigned[120];
extern uint16_t testSignalUint16[5000];
void ALGO_Kalman_Test(void);
#endif /* __DEBUG_ALGO_Kalman_ATY */
/* Wave parameters ************************************************************/
extern uint16_t wave_lastWaveAve_Kalman;
extern uint16_t wave_firstWavePeakPoint, wave_firstWaveZeroPoint;
extern uint16_t wave_quarterSpace;
extern uint16_t wave_maxVpp;
#define WAVE_DISPLAY_NUM 100
#define WAVE_DISPLAY_PEAK_HIGH wave_lastWaveAve_Kalman + (WAVE_DISPLAY_NUM * 2)
#define WAVE_DISPLAY_PEAK_LOW wave_lastWaveAve_Kalman - (WAVE_DISPLAY_NUM * 2)
#define WAVE_DISPLAY_ZERO_RAISE wave_lastWaveAve_Kalman + WAVE_DISPLAY_NUM
#define WAVE_DISPLAY_ZERO_FALL wave_lastWaveAve_Kalman - WAVE_DISPLAY_NUM
#define WAVE_BASE_AVE wave_lastWaveAve_Kalman
#define WAVE_KALMAN_ATTENUATION 2
void WavePeakZeroPointFind(
uint16_t* inputWave,
uint16_t* kalmanWave,
uint16_t* deepKalmanWave,
uint16_t* peakZeroPoints,
uint16_t size);
#define WAVE_FIRST_COUNT 10
#define WAVE_FIRST_THRESHOLD_V 25
#define FIRST_THRESHOLD_HIGH (wave_lastWaveAve_Kalman + WAVE_FIRST_THRESHOLD_V)
#define FIRST_THRESHOLD_LOW (wave_lastWaveAve_Kalman - WAVE_FIRST_THRESHOLD_V)
void WaveFirstPulseFind(uint16_t* inputWave, uint16_t* peakZeroPoints, uint16_t size);
// trans time(12bit@36MHz ADCCLK) = point to point time
// 15 x 1_000_000us / 36_000_000Hz = 5/12us = 0.4167us
#define ADC_TIME_UNIT (15.0 / 36.0)
void WaveParamCalc(uint16_t* peakZeroPoints, uint16_t firstWavePoint, uint16_t size);
void WaveVppCalc(uint16_t* peakZeroPoints, uint16_t* kalmanWave, uint16_t size);
void WaveWholeProcess(
uint16_t* inputWave,
uint16_t* kalmanWave,
uint16_t* deepKalmanWave,
uint16_t* peakZeroPoints,
uint16_t size);
#ifdef __DEBUG_WAVE_ATY
#define TEST_WAVE_SIZE 5000
extern uint16_t kalmanWave_Test[TEST_WAVE_SIZE];
extern uint16_t deepKalmanWave_Test[TEST_WAVE_SIZE];
extern uint16_t peakZeroPoints_Test[TEST_WAVE_SIZE];
void WaveAnalyse_Test(uint16_t* testDataIn, uint8_t WAVE_DISPLAY_FLAG);
#endif /* __DEBUG_WAVE_ATY */
#define ALGO_TEMP_Kelvin_T0 0 // K
#define ALGO_TEMP_Celsius_C0 0 // C
#define ALGO_TEMP_Fahrenheit_F0 0 // F
#define ALGO_TEMP_TtoC(T) ((T) - 273.15)
#define ALGO_TEMP_CtoT(C) ((C) + 273.15)
#define ALGO_TEMP_CtoF(C) ((C) * 1.8 + 32)
#define ALGO_TEMP_FtoC(F) (((F) - 32 ) / 1.8)
#define ALGO_TEMP_FtoT(F) ALGO_TEMP_CtoT(ALGO_TEMP_FtoC((F)))
#define ALGO_TEMP_TtoF(T) ALGO_TEMP_CtoF(ALGO_TEMP_TtoC((T)))
double ALGO_MATH_Pow(double num, uint8_t n);
double ALGO_MATH_LogLn(double num);
float ALGO_ResToKelvinTemp(float Rntc, float R25, float B);
float ALGO_WaterDensityFromTemp(float temperature);
float ALGO_EConductanceFromRes(float Res);
float ALGO_ResistivityFromRes(float Res, float A, float L);
float ALGO_ConductivityFromRes(float Res, float A, float L);
float ALGO_MolarityFromConductivity(float Conductivity, float MC);
float ALGO_MassConcentrationFromMolarity(float Molarity, float MM);
#endif /* __ALGO_Algorithm_ATY_H */
/******************************** End Of File *********************************/