Skip to content

Commit 3464e12

Browse files
handle multiple CTCSS tones with same power
1 parent 5e3cac6 commit 3464e12

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

.devcontainer/devcontainer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"twxs.cmake",
2323
"streetsidesoftware.code-spell-checker",
2424
"ms-azuretools.vscode-docker",
25-
"GitHub.vscode-github-actions"
25+
"GitHub.vscode-github-actions"
2626
]
2727
}
2828
},

src/ctcss.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,25 @@ void CTCSS::process_audio_sample(const float &sample) {
146146

147147
enough_samples_ = true;
148148

149-
// if this is sample fills out the window then check if the strongest tone is
150-
// the CTCSS tone we are looking for
149+
// if this is sample fills out the window then check if one of the "strongest"
150+
// tones is the CTCSS tone we are looking for. NOTE: there can be multiple "strongest"
151+
// tones based on floating point math
151152
vector<ToneDetectorSet::PowerIndex> tone_powers;
152153
float avg_power = powers_.sorted_powers(tone_powers);
153-
if (tone_powers[0].freq == ctcss_freq_ && tone_powers[0].power > avg_power) {
154+
float ctcss_tone_power = 0.0;
155+
for( const auto i:tone_powers) {
156+
if (i.freq == ctcss_freq_) {
157+
ctcss_tone_power = i.power;
158+
break;
159+
}
160+
}
161+
if (ctcss_tone_power == tone_powers[0].power && ctcss_tone_power > avg_power) {
154162
debug_print("CTCSS tone of %f Hz detected\n", ctcss_freq_);
155163
has_tone_ = true;
156164
found_count_++;
157165
} else {
158-
debug_print("CTCSS tone of %f Hz not detected - highest power was %f Hz at %f\n",
159-
ctcss_freq_, tone_powers[0].freq, tone_powers[0].power);
166+
debug_print("CTCSS tone of %f Hz not detected - highest power was %f Hz at %f vs %f\n",
167+
ctcss_freq_, tone_powers[0].freq, tone_powers[0].power, ctcss_tone_power);
160168
has_tone_ = false;
161169
not_found_count_++;
162170
}

src/squelch.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "squelch.h"
2121

2222
#ifdef DEBUG_SQUELCH
23+
#include <errno.h> // errno
2324
#include <string.h> // strerror()
2425
#endif /* DEBUG_SQUELCH _*/
2526

@@ -488,6 +489,8 @@ void Squelch::calculate_noise_floor(void) {
488489

489490
noise_floor_ = noise_floor_ * decay_factor + std::min(pre_filter_.capped_, noise_floor_) * new_factor + 1e-6f;
490491

492+
debug_print("%zu: noise floor is now %f\n", sample_count_, noise_floor_);
493+
491494
// Need to update moving_avg_cap_ - depends on noise_floor_
492495
calculate_moving_avg_cap();
493496

@@ -556,8 +559,8 @@ bool Squelch::currently_flapping(void) const {
556559
('post_filter_capped', np.single),
557560
('current_state', np.intc),
558561
('delay', np.intc),
559-
('low_signalcount', np.intc)
560-
('ctcss_fast_has_tone', np.intc)
562+
('low_signalcount', np.intc),
563+
('ctcss_fast_has_tone', np.intc),
561564
('ctcss_slow_has_tone', np.intc)
562565
])
563566

src/test_generate_signal.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ TEST_F(GenerateSignalTest, get_sample_two_tones) {
236236
Tone tone1(sample_rate, tone1_freq, tone1_ampl);
237237
Tone tone2(sample_rate, tone2_freq, tone2_ampl);
238238
for (int i = 0 ; i < 60 * sample_rate ; ++i) {
239-
ASSERT_FLOAT_EQ(signal.get_sample(), tone1.get_sample() + tone2.get_sample());
239+
ASSERT_NEAR(signal.get_sample(), tone1.get_sample() + tone2.get_sample(), 0.000001);
240240
}
241241
}
242242

0 commit comments

Comments
 (0)