63 lines
1.3 KiB
C
63 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <math.h>
|
|
#include <stdlib.h>
|
|
#define EQUAL_ALPHA 50.0f // 等百分比范围比
|
|
#define LOG_ALPHA log(EQUAL_ALPHA)
|
|
|
|
static inline float32 logarithmic_conversion(float32 setpoint)
|
|
{
|
|
static const float32 b = 13.24f;
|
|
static const float32 linear_coefficient = 0.123f;
|
|
static const float32 base_log = 0.6931471805599453f; // 直接使用log(2)的预计算值
|
|
float32 res = 0.0f;
|
|
if (setpoint <= 0)
|
|
{
|
|
return 0;
|
|
}
|
|
else if (setpoint >= 100)
|
|
{
|
|
return 100;
|
|
}
|
|
else
|
|
{
|
|
res = b * log(setpoint) / base_log + linear_coefficient * setpoint;
|
|
if (res < 0)
|
|
{
|
|
res = 0.0f;
|
|
}
|
|
else if (res > 100)
|
|
{
|
|
res = 100.0f;
|
|
}
|
|
return res;
|
|
}
|
|
}
|
|
|
|
void quick_open(void)
|
|
{
|
|
printf("quick_open:\n");
|
|
for (uint8_t i = 0; i < 20; i++)
|
|
{
|
|
printf("x = %d, y = %f\n", 5 + i * 5, logarithmic_conversion(5 + i * 5));
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
void equal_proportion(void)
|
|
{
|
|
uint8_t x = 0; // 指定的x值
|
|
printf("equal_proportion:\n");
|
|
for (uint8_t i = 0; i <= 20; i++)
|
|
{
|
|
x = i * 5;
|
|
printf("x = %d, y = %f\n", x, (exp(x * 0.01f * LOG_ALPHA) - 1) * 100 / (EQUAL_ALPHA - 1));
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
int main()
|
|
{
|
|
equal_proportion();
|
|
quick_open();
|
|
}
|