app beat_detect: initilalize state on every init call

Signed-off-by: Thomas Schmid <tom@lfence.de>
This commit is contained in:
Thomas 2022-06-19 15:15:24 +02:00
parent 3d8e9deddd
commit 872946fa9a
3 changed files with 28 additions and 5 deletions

View File

@ -36,4 +36,11 @@ struct Biquad
return y; return y;
} }
void reset() {
this->xn1 = 0.0;
this->xn2 = 0.0;
this->yn1 = 0.0;
this->yn1 = 0.0;
}
}; };

View File

@ -16,4 +16,8 @@ struct Pt1
this->y_n1 = y; this->y_n1 = y;
return y; return y;
} }
void reset() {
this->y_n1 = 0.f;
}
}; };

View File

@ -16,9 +16,9 @@
#define PI 3.1415926535897932384626433832795 #define PI 3.1415926535897932384626433832795
#define n_BP 30 // number of band pass filters #define n_BP 30 // number of band pass filters
static unsigned long sampling_period_bp = 1000000L / SAMPLING_FREQUENCY_BP; static const unsigned long sampling_period_bp = 1000000L / SAMPLING_FREQUENCY_BP;
static unsigned long sampling_period_control = 1000000L / SAMPLING_FREQUENCY_CONTROL; static const unsigned long sampling_period_control = 1000000L / SAMPLING_FREQUENCY_CONTROL;
static double energy = 0; static float energy = 0;
static unsigned long last_us_bp = 0L; static unsigned long last_us_bp = 0L;
static unsigned long last_us_control = 0L; static unsigned long last_us_control = 0L;
@ -85,6 +85,18 @@ void BeatDetectApp::init()
{ {
set_filter(); set_filter();
initial_time = micros(); initial_time = micros();
pos_target = NUM_LEDS / 2;
pos_target_filtered = NUM_LEDS / 2;
active = 15;
candidate = 15;
rounds = 0;
n_samples = 0;
pos_filter.reset();
for (int i = 0; i<n_BP; i++){
bp_filters[i].reset();
}
} }
void BeatDetectApp::deinit() void BeatDetectApp::deinit()
@ -101,7 +113,7 @@ void BeatDetectApp::loop()
if (micros() - last_us_bp > sampling_period_bp) if (micros() - last_us_bp > sampling_period_bp)
{ {
n_samples = 0; n_samples = 0;
last_us_bp += sampling_period_bp; last_us_bp = micros();
// energy_fil += (energy - energy_fil) * 0.01; // energy_fil += (energy - energy_fil) * 0.01;
for (int i = 0; i < n_BP; i++) for (int i = 0; i < n_BP; i++)
@ -171,7 +183,7 @@ void BeatDetectApp::loop()
if (micros() - last_us_control > sampling_period_control) if (micros() - last_us_control > sampling_period_control)
{ {
last_us_control += sampling_period_control; last_us_control = micros();
int argmax = -1; int argmax = -1;
float valuemax = 0; float valuemax = 0;
for (int i = 0; i < n_BP; i++) for (int i = 0; i < n_BP; i++)