Skip to content

Commit

Permalink
Update AranovskiyFilter.h
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrouch authored Sep 10, 2024
1 parent 0194ce5 commit acf707a
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions bbn_wave_freq_m5atomS3/AranovskiyFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
Usage example:
float omega_up = 1.0 * (2 * PI); // upper frequency Hz * 2 * PI
float k_gain = 2.0;
float t_0 = 0.0;
float x1_0 = 0.0;
float theta_0 = - (omega_up * omega_up / 4.0);
float sigma_0 = theta_0;
float delta_t; // time step sec
double omega_up = 1.0 * (2 * PI); // upper frequency Hz * 2 * PI
double k_gain = 2.0;
double t_0 = 0.0;
double x1_0 = 0.0;
double theta_0 = - (omega_up * omega_up / 4.0);
double sigma_0 = theta_0;
double delta_t; // time step sec
AranovskiyParams params;
AranovskiyState state;
Expand All @@ -37,7 +37,7 @@
delay(4);
// measure
float y = getAccelZfrom(imu) - 1.0; // remove G acceration
double y = getAccelZfrom(imu) - 1.0; // remove G acceration
now = millis();
delta_t = ((now - last_update) / 1000.0);
Expand All @@ -48,46 +48,47 @@
// state.f contains estimated frequency
}
Use double instead of float to avoid float overflows with higher Aranovskiy gain values
*/

#define PI 3.1415926535897932384626433832795

typedef struct aranovskiy_params {
float a = 1.0;
float b = a;
float k = 1.0; // gain
double a = 1.0;
double b = a;
double k = 1.0; // gain
} AranovskiyParams;

typedef struct aranovskiy_state {
float t = 0.0; // time
float y; // signal measurement
float x1 = 0.0;
float theta = -0.25;
float sigma = -0.25;
float x1_dot;
float sigma_dot;
float omega; // frequency * 2 * pi
float f; // frequency
double t = 0.0; // time
double y; // signal measurement
double x1 = 0.0;
double theta = -0.25;
double sigma = -0.25;
double x1_dot;
double sigma_dot;
double omega; // frequency * 2 * pi
double f; // frequency
} AranovskiyState;

void aranovskiy_default_params(AranovskiyParams* p, float omega_up, float k_gain);
void aranovskiy_init_state(AranovskiyState* s, float t_0, float x1_0, float theta_0, float sigma_0);
void aranovskiy_update(AranovskiyParams* p, AranovskiyState* s, float y, float delta_t);
void aranovskiy_default_params(AranovskiyParams* p, double omega_up, double k_gain);
void aranovskiy_init_state(AranovskiyState* s, double t_0, double x1_0, double theta_0, double sigma_0);
void aranovskiy_update(AranovskiyParams* p, AranovskiyState* s, double y, double delta_t);

void aranovskiy_default_params(AranovskiyParams* p, float omega_up, float k_gain) {
void aranovskiy_default_params(AranovskiyParams* p, double omega_up, double k_gain) {
p->a = omega_up;
p->b = p->a;
p->k = k_gain;
}

void aranovskiy_init_state(AranovskiyState* s, float t_0, float x1_0, float theta_0, float sigma_0) {
void aranovskiy_init_state(AranovskiyState* s, double t_0, double x1_0, double theta_0, double sigma_0) {
s->t = t_0;
s->x1 = x1_0;
s->theta = theta_0;
s->sigma = sigma_0;
}

void aranovskiy_update(AranovskiyParams* p, AranovskiyState* s, float y, float delta_t) {
void aranovskiy_update(AranovskiyParams* p, AranovskiyState* s, double y, double delta_t) {
s->x1_dot = - p->a * s->x1 + p->b * y;
s->sigma_dot = - p->k * s->x1 * s->x1 * s->theta - p->k * p->a * s->x1 * s->x1_dot - p->k * p->b * s->x1_dot * y;
s->theta = s->sigma + p->k * p->b * s->x1 * y;
Expand Down

0 comments on commit acf707a

Please sign in to comment.