squelch.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * squelch.cpp
  3. *
  4. * Copyright (C) 2022-2023 charlie-foxtrot
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "squelch.h"
  20. #ifdef DEBUG_SQUELCH
  21. #include <errno.h> // errno
  22. #include <string.h> // strerror()
  23. #endif /* DEBUG_SQUELCH _*/
  24. #include <stdlib.h> // calloc()
  25. #include <algorithm> // min()
  26. #include <cassert> // assert()
  27. #include <cmath> // pow()
  28. #include "logging.h" // debug_print()
  29. using namespace std;
  30. Squelch::Squelch(void) {
  31. noise_floor_ = 5.0f;
  32. set_squelch_snr_threshold(9.54f); // depends on noise_floor_, sets using_manual_level_, normal_signal_ratio_, flappy_signal_ratio_, and moving_avg_cap_
  33. manual_signal_level_ = -1.0;
  34. pre_filter_ = {0.001f, 0.001f};
  35. post_filter_ = {0.001f, 0.001f};
  36. squelch_level_ = 0.0f;
  37. using_post_filter_ = false;
  38. pre_vs_post_factor_ = 0.9f;
  39. open_delay_ = 197;
  40. close_delay_ = 197;
  41. low_signal_abort_ = 88;
  42. next_state_ = CLOSED;
  43. current_state_ = CLOSED;
  44. delay_ = 0;
  45. open_count_ = 0;
  46. sample_count_ = -1;
  47. flappy_count_ = 0;
  48. low_signal_count_ = 0;
  49. recent_sample_size_ = 1000;
  50. flap_opens_threshold_ = 3;
  51. recent_open_count_ = 0;
  52. closed_sample_count_ = 0;
  53. buffer_size_ = 102; // NOTE: this is specific to the 2nd order lowpass Bessel filter
  54. buffer_head_ = 0;
  55. buffer_tail_ = 1;
  56. buffer_ = (float*)calloc(buffer_size_, sizeof(float));
  57. #ifdef DEBUG_SQUELCH
  58. debug_file_ = NULL;
  59. raw_input_ = 0.0;
  60. filtered_input_ = 0.0;
  61. #endif /* DEBUG_SQUELCH */
  62. assert(open_delay_ > buffer_size_);
  63. debug_print("Created Squelch, open_delay_: %d, close_delay_: %d, low_signal_abort: %d, using_manual_level_: %s\n", open_delay_, close_delay_, low_signal_abort_,
  64. using_manual_level_ ? "true" : "false");
  65. }
  66. void Squelch::set_squelch_level_threshold(const float& level) {
  67. if (level > 0) {
  68. using_manual_level_ = true;
  69. manual_signal_level_ = level;
  70. } else {
  71. using_manual_level_ = false;
  72. }
  73. // Need to update moving_avg_cap_ - depends on using_manual_level_ and manual_signal_level_
  74. calculate_moving_avg_cap();
  75. debug_print("Set level threshold, using_manual_level_: %s, manual_signal_level_: %f, moving_avg_cap_: %f\n", using_manual_level_ ? "true" : "false", manual_signal_level_, moving_avg_cap_);
  76. }
  77. void Squelch::set_squelch_snr_threshold(const float& db) {
  78. using_manual_level_ = false;
  79. normal_signal_ratio_ = pow(10.0, db / 20.0);
  80. flappy_signal_ratio_ = normal_signal_ratio_ * 0.9f;
  81. // Need to update moving_avg_cap_ - depends on using_manual_level_ and normal_signal_ratio_
  82. calculate_moving_avg_cap();
  83. debug_print("SNR threshold updated, using_manual_level_: %s, normal_signal_ratio_: %f, flappy_signal_ratio_: %f, moving_avg_cap_: %f\n", using_manual_level_ ? "true" : "false",
  84. normal_signal_ratio_, flappy_signal_ratio_, moving_avg_cap_);
  85. }
  86. void Squelch::set_ctcss_freq(const float& ctcss_freq, const float& sample_rate) {
  87. // create two CTCSS detectors with different window sizes. 0.4 sec is required to tell between all the "standard"
  88. // tones but 0.05 is enough to tell between tones ~20 Hz appart. Will use ctcss_fast_ until there are enough samples
  89. // for ctcss_slow_
  90. ctcss_fast_ = CTCSS(ctcss_freq, sample_rate, sample_rate * 0.05);
  91. ctcss_slow_ = CTCSS(ctcss_freq, sample_rate, sample_rate * 0.4);
  92. }
  93. bool Squelch::is_open(void) const {
  94. // if current state is OPEN or CLOSING then decide based on CTCSS (if enabled)
  95. if (current_state_ == OPEN || current_state_ == CLOSING) {
  96. // if CTCSS is enabled then use slow (more accurate) if it has enough samples, otherwise
  97. // use fast (will return false if also not enough samples)
  98. if (ctcss_slow_.is_enabled()) {
  99. if (ctcss_slow_.enough_samples()) {
  100. return ctcss_slow_.has_tone();
  101. }
  102. return ctcss_fast_.has_tone();
  103. }
  104. return true;
  105. }
  106. return false;
  107. }
  108. bool Squelch::should_filter_sample(void) {
  109. return ((has_pre_filter_signal() || current_state_ != CLOSED) && current_state_ != LOW_SIGNAL_ABORT);
  110. }
  111. bool Squelch::should_process_audio(void) {
  112. return (current_state_ == OPEN || current_state_ == CLOSING);
  113. }
  114. bool Squelch::first_open_sample(void) const {
  115. return (current_state_ != OPEN && next_state_ == OPEN);
  116. }
  117. bool Squelch::last_open_sample(void) const {
  118. return (current_state_ == CLOSING && next_state_ == CLOSED) || (current_state_ != LOW_SIGNAL_ABORT && next_state_ == LOW_SIGNAL_ABORT);
  119. }
  120. bool Squelch::signal_outside_filter(void) {
  121. return (using_post_filter_ && has_pre_filter_signal() && !has_post_filter_signal());
  122. }
  123. const float& Squelch::noise_level(void) const {
  124. return noise_floor_;
  125. }
  126. const float& Squelch::signal_level(void) const {
  127. return pre_filter_.full_;
  128. }
  129. const float& Squelch::squelch_level(void) {
  130. if (using_manual_level_) {
  131. return manual_signal_level_;
  132. }
  133. if (squelch_level_ == 0.0f) {
  134. if (currently_flapping() && flappy_signal_ratio_ < normal_signal_ratio_) {
  135. squelch_level_ = flappy_signal_ratio_ * noise_floor_;
  136. } else {
  137. squelch_level_ = normal_signal_ratio_ * noise_floor_;
  138. }
  139. }
  140. return squelch_level_;
  141. }
  142. const size_t& Squelch::open_count(void) const {
  143. return open_count_;
  144. }
  145. const size_t& Squelch::flappy_count(void) const {
  146. return flappy_count_;
  147. }
  148. const size_t& Squelch::ctcss_count(void) const {
  149. return ctcss_slow_.found_count();
  150. }
  151. const size_t& Squelch::no_ctcss_count(void) const {
  152. return ctcss_slow_.not_found_count();
  153. }
  154. void Squelch::process_raw_sample(const float& sample) {
  155. // Update current state based on previous state from last iteration
  156. update_current_state();
  157. #ifdef DEBUG_SQUELCH
  158. raw_input_ = sample;
  159. #endif /* DEBUG_SQUELCH */
  160. sample_count_++;
  161. // Auto noise floor
  162. // - Doing this every 16 samples instead of every sample allows a gradual signal increase
  163. // to cross the squelch threshold (that is a function of the noise floor) sooner.
  164. // - Updating even when squelch is open and / or signal is outside filter means the noise
  165. // floor (and squelch threshold) will slowly increasing during a long signal. This can lead
  166. // to flapping, but this keeps a sudden and sustained increase of noise from locking squelch
  167. // OPEN.
  168. if (sample_count_ % 16 == 0) {
  169. calculate_noise_floor();
  170. }
  171. update_moving_avg(pre_filter_, sample);
  172. // Apply the comparison factor before adding to the buffer, will later be used as the threshold
  173. // for the post_filter_
  174. buffer_[buffer_head_] = pre_filter_.capped_ * pre_vs_post_factor_;
  175. // Check signal against thresholds
  176. if (current_state_ == OPEN && !has_signal()) {
  177. debug_print("Closing at %zu: no signal after timeout (%f, %f, %f)\n", sample_count_, pre_filter_.capped_, post_filter_.capped_, squelch_level());
  178. set_state(CLOSING);
  179. }
  180. if (current_state_ == CLOSED && has_signal()) {
  181. debug_print("Opening at %zu: signal (%f, %f, %f)\n", sample_count_, pre_filter_.capped_, post_filter_.capped_, squelch_level());
  182. set_state(OPENING);
  183. }
  184. // Override squelch and close if there are repeated samples under the squelch level
  185. // NOTE: this can cause squelch to close, but it may immediately be re-opened if the signal level still hasn't fallen after the delays
  186. if (current_state_ != CLOSED && current_state_ != LOW_SIGNAL_ABORT) {
  187. if (sample >= squelch_level()) {
  188. low_signal_count_ = 0;
  189. } else {
  190. low_signal_count_++;
  191. if (low_signal_count_ >= low_signal_abort_) {
  192. debug_print("Low signal abort at %zu: low signal count %d\n", sample_count_, low_signal_count_);
  193. set_state(LOW_SIGNAL_ABORT);
  194. }
  195. }
  196. }
  197. }
  198. void Squelch::process_filtered_sample(const float& sample) {
  199. #ifdef DEBUG_SQUELCH
  200. filtered_input_ = sample;
  201. #endif /* DEBUG_SQUELCH */
  202. if (!should_filter_sample()) {
  203. return;
  204. }
  205. if (current_state_ == OPENING) {
  206. // While OPENING, need to wait until the pre-filter value gets through the buffer
  207. if (delay_ < buffer_size_) {
  208. return;
  209. }
  210. // Buffer has been filled, initialize post-filter with the pre-filter value
  211. if (delay_ == buffer_size_) {
  212. post_filter_ = {buffer_[buffer_tail_], buffer_[buffer_tail_]};
  213. }
  214. }
  215. using_post_filter_ = true;
  216. update_moving_avg(post_filter_, sample);
  217. // Always comparing the post-filter average to the buffered pre-filtered value
  218. if (post_filter_.capped_ < buffer_[buffer_tail_]) {
  219. debug_print("Closing at %zu: signal level post filter (%f < %f)\n", sample_count_, post_filter_.capped_, squelch_level());
  220. set_state(CLOSED);
  221. }
  222. }
  223. void Squelch::process_audio_sample(const float& sample) {
  224. #ifdef DEBUG_SQUELCH
  225. audio_input_ = sample;
  226. #endif /* DEBUG_SQUELCH */
  227. if (!ctcss_slow_.is_enabled()) {
  228. return;
  229. }
  230. // ctcss_ is reset on transition to CLOSED and stays "unused" while CLOSED
  231. if (current_state_ != CLOSED) {
  232. // always send the sample to the slow (more accurate) detector, also send to the fast if there havent been enough yet
  233. ctcss_slow_.process_audio_sample(sample);
  234. if (!ctcss_slow_.enough_samples()) {
  235. ctcss_fast_.process_audio_sample(sample);
  236. }
  237. }
  238. }
  239. void Squelch::set_state(State update) {
  240. // Valid transitions (current_state_ -> next_state_) are:
  241. // - CLOSED -> CLOSED
  242. // - CLOSED -> OPENING
  243. // ---------------------------
  244. // - OPENING -> CLOSED
  245. // - OPENING -> OPENING
  246. // - OPENING -> CLOSING
  247. // - OPENING -> OPEN
  248. // ---------------------------
  249. // - CLOSING -> CLOSED
  250. // - CLOSING -> OPENING
  251. // - CLOSING -> CLOSING
  252. // - CLOSING -> LOW_SIGNAL_ABORT
  253. // - CLOSING -> OPEN
  254. // ---------------------------
  255. // - LOW_SIGNAL_ABORT -> CLOSED
  256. // - LOW_SIGNAL_ABORT -> LOW_SIGNAL_ABORT
  257. // ---------------------------
  258. // - OPEN -> CLOSING
  259. // - OPEN -> LOW_SIGNAL_ABORT
  260. // - OPEN -> OPEN
  261. // Invalid transistions (current_state_ -> next_state_) are:
  262. // CLOSED -> CLOSING (if already CLOSED cant go backwards)
  263. if (current_state_ == CLOSED && update == CLOSING) {
  264. update = CLOSED;
  265. }
  266. // CLOSED -> LOW_SIGNAL_ABORT (if already CLOSED cant go backwards)
  267. else if (current_state_ == CLOSED && update == LOW_SIGNAL_ABORT) {
  268. update = CLOSED;
  269. }
  270. // CLOSED -> OPEN (must go through OPENING to get to OPEN)
  271. else if (current_state_ == CLOSED && update == OPEN) {
  272. update = OPENING;
  273. }
  274. // OPENING -> LOW_SIGNAL_ABORT (just go to CLOSED instead)
  275. else if (current_state_ == OPENING && update == LOW_SIGNAL_ABORT) {
  276. update = CLOSED;
  277. }
  278. // LOW_SIGNAL_ABORT -> OPENING (LOW_SIGNAL_ABORT can only go to CLOSED)
  279. // LOW_SIGNAL_ABORT -> OPEN (LOW_SIGNAL_ABORT can only go to CLOSED)
  280. // LOW_SIGNAL_ABORT -> CLOSING (LOW_SIGNAL_ABORT can only go to CLOSED)
  281. else if (current_state_ == LOW_SIGNAL_ABORT && update != LOW_SIGNAL_ABORT && update != CLOSED) {
  282. update = CLOSED;
  283. }
  284. // OPEN -> CLOSED (must go through CLOSING to get to CLOSED)
  285. else if (current_state_ == OPEN && update == CLOSED) {
  286. update = CLOSING;
  287. }
  288. // OPEN -> OPENING (if already OPEN cant go backwards)
  289. else if (current_state_ == OPEN && update == OPENING) {
  290. update = OPEN;
  291. }
  292. next_state_ = update;
  293. }
  294. void Squelch::update_current_state(void) {
  295. if (next_state_ == OPENING) {
  296. if (current_state_ != OPENING) {
  297. debug_print("%zu: transitioning to OPENING\n", sample_count_);
  298. delay_ = 0;
  299. low_signal_count_ = 0;
  300. using_post_filter_ = false;
  301. current_state_ = next_state_;
  302. } else {
  303. // in OPENING delay
  304. delay_++;
  305. if (delay_ >= open_delay_) {
  306. // After getting through OPENING delay, count this as an "open" for flap
  307. // detection even if signal has gone. NOTE - if process_filtered_sample() would
  308. // have already sent state to CLOSED before the delay if post_filter_.capped_ was
  309. // too low, so that wont count towards flapping
  310. if (closed_sample_count_ < recent_sample_size_) {
  311. recent_open_count_++;
  312. if (currently_flapping()) {
  313. flappy_count_++;
  314. }
  315. // Force squelch_level_ recalculation at next call to squelch_level()
  316. squelch_level_ = 0.0f;
  317. }
  318. // Check signal level after delay to either go to OPEN or CLOSED
  319. if (has_signal()) {
  320. next_state_ = OPEN;
  321. } else {
  322. debug_print("%zu: no signal after OPENING delay, going to CLOSED\n", sample_count_);
  323. next_state_ = CLOSED;
  324. }
  325. }
  326. }
  327. } else if (next_state_ == CLOSING) {
  328. if (current_state_ != CLOSING) {
  329. debug_print("%zu: transitioning to CLOSING\n", sample_count_);
  330. delay_ = 0;
  331. current_state_ = next_state_;
  332. } else {
  333. // in CLOSING delay
  334. delay_++;
  335. if (delay_ >= close_delay_) {
  336. if (!has_signal()) {
  337. next_state_ = CLOSED;
  338. } else {
  339. debug_print("%zu: signal after CLOSING delay, reverting to OPEN\n", sample_count_);
  340. current_state_ = OPEN; // set current_state_ to avoid incrementing open_count_
  341. next_state_ = OPEN;
  342. }
  343. }
  344. }
  345. } else if (next_state_ == LOW_SIGNAL_ABORT) {
  346. if (current_state_ != LOW_SIGNAL_ABORT) {
  347. debug_print("%zu: transitioning to LOW_SIGNAL_ABORT\n", sample_count_);
  348. // If coming from CLOSING then keep the delay counter that has already started
  349. if (current_state_ != CLOSING) {
  350. delay_ = 0;
  351. }
  352. current_state_ = next_state_;
  353. } else {
  354. // in LOW_SIGNAL_ABORT delay
  355. delay_++;
  356. if (delay_ >= close_delay_) {
  357. next_state_ = CLOSED;
  358. }
  359. }
  360. } else if (next_state_ == OPEN && current_state_ != OPEN) {
  361. debug_print("%zu: transitioning to OPEN\n", sample_count_);
  362. open_count_++;
  363. current_state_ = next_state_;
  364. } else if (next_state_ == CLOSED && current_state_ != CLOSED) {
  365. debug_print("%zu: transitioning to CLOSED\n", sample_count_);
  366. using_post_filter_ = false;
  367. closed_sample_count_ = 0;
  368. current_state_ = next_state_;
  369. ctcss_fast_.reset();
  370. ctcss_slow_.reset();
  371. } else if (next_state_ == CLOSED && current_state_ == CLOSED) {
  372. // Count this as a closed sample towards flap detection (can stop counting at recent_sample_size_)
  373. if (closed_sample_count_ < recent_sample_size_) {
  374. closed_sample_count_++;
  375. } else if (closed_sample_count_ == recent_sample_size_) {
  376. recent_open_count_ = 0;
  377. squelch_level_ = 0.0f; // Force squelch_level_ recalculation
  378. }
  379. } else {
  380. current_state_ = next_state_;
  381. }
  382. buffer_tail_ = (buffer_tail_ + 1) % buffer_size_;
  383. buffer_head_ = (buffer_head_ + 1) % buffer_size_;
  384. #ifdef DEBUG_SQUELCH
  385. debug_state();
  386. #endif /* DEBUG_SQUELCH */
  387. }
  388. bool Squelch::has_pre_filter_signal(void) {
  389. return pre_filter_.capped_ >= squelch_level();
  390. }
  391. bool Squelch::has_post_filter_signal(void) {
  392. return using_post_filter_ && post_filter_.capped_ >= buffer_[buffer_tail_];
  393. }
  394. bool Squelch::has_signal(void) {
  395. if (using_post_filter_) {
  396. return has_pre_filter_signal() && has_post_filter_signal();
  397. }
  398. return has_pre_filter_signal();
  399. }
  400. void Squelch::calculate_noise_floor(void) {
  401. static const float decay_factor = 0.97f;
  402. static const float new_factor = 1.0 - decay_factor;
  403. noise_floor_ = noise_floor_ * decay_factor + std::min(pre_filter_.capped_, noise_floor_) * new_factor + 1e-6f;
  404. debug_print("%zu: noise floor is now %f\n", sample_count_, noise_floor_);
  405. // Need to update moving_avg_cap_ - depends on noise_floor_
  406. calculate_moving_avg_cap();
  407. // Force squelch_level_ recalculation at next call to squelch_level() - depends on noise_floor_
  408. squelch_level_ = 0.0f;
  409. }
  410. void Squelch::calculate_moving_avg_cap(void) {
  411. // set max value for MovingAverage's capped_ to 1.5 x the normal / manual squelch level.
  412. if (using_manual_level_) {
  413. moving_avg_cap_ = 1.5f * manual_signal_level_;
  414. } else {
  415. moving_avg_cap_ = 1.5f * normal_signal_ratio_ * noise_floor_;
  416. }
  417. }
  418. void Squelch::update_moving_avg(MovingAverage& avg, const float& sample) {
  419. static const float decay_factor = 0.99f;
  420. static const float new_factor = 1.0 - decay_factor;
  421. avg.full_ = avg.full_ * decay_factor + sample * new_factor;
  422. // Cap average level, this lets the average drop after the signal goes away more quickly
  423. // (if current value and update are both at/above the max then can avoid the float multiplications)
  424. if (avg.capped_ >= moving_avg_cap_ && sample >= moving_avg_cap_) {
  425. avg.capped_ = moving_avg_cap_;
  426. } else {
  427. avg.capped_ = min(moving_avg_cap_, avg.capped_ * decay_factor + sample * new_factor);
  428. }
  429. }
  430. bool Squelch::currently_flapping(void) const {
  431. return recent_open_count_ >= flap_opens_threshold_;
  432. }
  433. #ifdef DEBUG_SQUELCH
  434. /*
  435. Debug file methods
  436. ==================
  437. Values written to file are:
  438. - (int16_t) process_raw_sample input
  439. - (int16_t) process_filtered_sample input
  440. - (int16_t) process_audio_sample input
  441. - (int16_t) noise_floor_
  442. - (int16_t) pre_filter_.capped_
  443. - (int16_t) post_filter_.capped_
  444. - (int) current_state_
  445. - (int) delay_
  446. - (int) low_signalcount_
  447. - (int) ctcss_fast_.has_tone()
  448. - (int) ctcss_slow_.has_tone()
  449. The output file can be read / plotted in python as follows:
  450. import matplotlib.pyplot as plt
  451. import numpy as np
  452. def plot_squelch_debug(filepath):
  453. dt = np.dtype([('raw_input', np.single),
  454. ('filtered_input', np.single),
  455. ('audio_input', np.single),
  456. ('noise_floor', np.single),
  457. ('pre_filter_capped', np.single),
  458. ('post_filter_capped', np.single),
  459. ('current_state', np.intc),
  460. ('delay', np.intc),
  461. ('low_signalcount', np.intc),
  462. ('ctcss_fast_has_tone', np.intc),
  463. ('ctcss_slow_has_tone', np.intc)
  464. ])
  465. dat = np.fromfile(filepath, dtype=dt)
  466. plt.figure()
  467. plt.plot(dat['raw_input'], 'b')
  468. plt.plot(dat['pre_filter_capped'], 'g')
  469. plt.plot(dat['noise_floor'], 'r')
  470. plt.show(block=False)
  471. plt.figure()
  472. plt.plot(dat['post_filter_capped'], 'k')
  473. plt.show(block=False)
  474. plt.figure()
  475. axis = plt.subplot2grid((3, 1), (0, 0))
  476. axis.plot(dat['current_state'], 'c')
  477. axis = plt.subplot2grid((3, 1), (1, 0))
  478. axis.plot(dat['delay'], 'm')
  479. axis = plt.subplot2grid((3, 1), (2, 0))
  480. axis.plot(dat['low_signalcount'], 'y')
  481. plt.show(block=False)
  482. return
  483. */
  484. Squelch::~Squelch(void) {
  485. if (debug_file_) {
  486. fclose(debug_file_);
  487. }
  488. }
  489. void Squelch::set_debug_file(const char* filepath) {
  490. debug_file_ = fopen(filepath, "wb");
  491. }
  492. void Squelch::debug_value(const float& value) {
  493. if (!debug_file_) {
  494. return;
  495. }
  496. if (fwrite(&value, sizeof(value), 1, debug_file_) != 1) {
  497. debug_print("Error writing to squelch debug file: %s\n", strerror(errno));
  498. }
  499. }
  500. void Squelch::debug_value(const int& value) {
  501. if (!debug_file_) {
  502. return;
  503. }
  504. if (fwrite(&value, sizeof(value), 1, debug_file_) != 1) {
  505. debug_print("Error writing to squelch debug file: %s\n", strerror(errno));
  506. }
  507. }
  508. void Squelch::debug_state(void) {
  509. if (!debug_file_) {
  510. return;
  511. }
  512. debug_value(raw_input_);
  513. debug_value(filtered_input_);
  514. debug_value(audio_input_);
  515. raw_input_ = 0.0;
  516. filtered_input_ = 0.0;
  517. audio_input_ = 0.0;
  518. debug_value(noise_floor_);
  519. debug_value(pre_filter_.capped_);
  520. debug_value(post_filter_.capped_);
  521. debug_value((int)current_state_);
  522. debug_value(delay_);
  523. debug_value(low_signal_count_);
  524. debug_value((int)ctcss_fast_.has_tone());
  525. debug_value((int)ctcss_slow_.has_tone());
  526. }
  527. #endif /* DEBUG_SQUELCH */