smooth: Use integers rather than floats to store tme
parent
baad0ecd68
commit
c794ca3822
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is an exponential smoothing filter with a time constant.
|
* This is an exponential smoothing filter with a time constant.
|
||||||
*
|
*
|
||||||
|
@ -27,7 +29,7 @@
|
||||||
struct smooth {
|
struct smooth {
|
||||||
double time_constant;
|
double time_constant;
|
||||||
|
|
||||||
double last_time;
|
uint64_t last_time;
|
||||||
double last_result;
|
double last_result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
static inline double gettime_s(void)
|
static inline uint64_t gettime_us(void)
|
||||||
{
|
{
|
||||||
struct timespec ts = { 0 };
|
struct timespec ts = { 0 };
|
||||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||||
return (double)ts.tv_sec + (double)ts.tv_nsec / 1.0e-9;
|
return ts.tv_sec * 1000000ULL + (double)ts.tv_nsec / 1000ULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint32_t gettime_ms(void)
|
static inline uint32_t gettime_ms(void)
|
||||||
|
|
|
@ -21,8 +21,8 @@
|
||||||
|
|
||||||
double smooth(struct smooth* self, double input)
|
double smooth(struct smooth* self, double input)
|
||||||
{
|
{
|
||||||
double now = gettime_s();
|
uint64_t now = gettime_us();
|
||||||
double dt = now - self->last_time;
|
double dt = (now - self->last_time) * 1.0e-6;
|
||||||
self->last_time = now;
|
self->last_time = now;
|
||||||
|
|
||||||
double factor = 1.0 - exp(-dt / self->time_constant);
|
double factor = 1.0 - exp(-dt / self->time_constant);
|
||||||
|
|
Loading…
Reference in New Issue