Skip to content

Commit

Permalink
Filter: Testes: notch: interpolate crossing points for acurate phase lag
Browse files Browse the repository at this point in the history
  • Loading branch information
IamPete1 authored and tridge committed Jun 1, 2024
1 parent c908636 commit 51c77fe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions libraries/Filter/tests/plot_harmonictest5.gnu
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ set key autotitle columnhead
set xlabel "Attenuation(dB)"
set ylabel "PhaseLag(deg)"
set key left bottom
set yrange [0:60]
plot "harmonicnotch_test5.csv" using 1:2, "harmonicnotch_test5.csv" using 1:3, "harmonicnotch_test5.csv" using 1:4, "harmonicnotch_test5.csv" using 1:5, "harmonicnotch_test5.csv" using 1:6

16 changes: 11 additions & 5 deletions libraries/Filter/tests/test_notchfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ static void test_one_filter(float base_freq, float attenuation_dB,
double last_in;
double last_out;
double v_max;
uint32_t last_crossing;
uint32_t total_lag_samples;
double last_crossing;
double total_lag_samples;
uint32_t lag_count;
float get_lag_degrees(const float freq) const {
const float lag_avg = total_lag_samples/float(lag_count);
Expand Down Expand Up @@ -218,10 +218,16 @@ static void test_one_filter(float base_freq, float attenuation_dB,
integral.v_max = MAX(integral.v_max, v);
}
if (sample >= 0 && integral.last_in < 0) {
integral.last_crossing = s;
// Always interpolating the value at 0.0
// crossing happened some fraction before the current sample
// result in the range -1.0 to 0.0
// linear interpolation: ((0.0 - last_in) / (sample - last_in)) - 1.0 is the same as:
// sample / (last_in - sample)
integral.last_crossing = (double)s + (sample / (integral.last_in - sample));
}
if (v >= 0 && integral.last_out < 0 && integral.last_crossing != 0) {
integral.total_lag_samples += s - integral.last_crossing;
if (v >= 0 && integral.last_out < 0 && integral.last_crossing > 0) {
const double crossing = (double)s + (v / (integral.last_out - v));
integral.total_lag_samples += crossing - integral.last_crossing;
integral.lag_count++;
}
integral.last_in = sample;
Expand Down

0 comments on commit 51c77fe

Please sign in to comment.