You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

237 line
7.4 KiB

  1. /*
  2. * aptdec - A lightweight FOSS (NOAA) APT decoder
  3. * Copyright (C) 2004-2009 Thierry Leconte (F4DWV) 2019-2023 Xerbo (xerbo@protonmail.com)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include <float.h>
  19. #include <math.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <aptdec.h>
  24. #include "filter.h"
  25. #include "util.h"
  26. #include "algebra.h"
  27. #define LOW_PASS_SIZE 101
  28. #define CARRIER_FREQ 2400.0f
  29. #define MAX_CARRIER_OFFSET 10.0f
  30. const float sync_pattern[] = {-1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1,
  31. 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 0};
  32. #define SYNC_SIZE (sizeof(sync_pattern)/sizeof(sync_pattern[0]))
  33. typedef struct {
  34. float alpha;
  35. float beta;
  36. float min_freq;
  37. float max_freq;
  38. float freq;
  39. float phase;
  40. } pll_t;
  41. typedef struct {
  42. float *ring_buffer;
  43. size_t ring_size;
  44. float *taps;
  45. size_t ntaps;
  46. } fir_t;
  47. struct aptdec_t {
  48. float sample_rate;
  49. float sync_frequency;
  50. pll_t *pll;
  51. fir_t *hilbert;
  52. float low_pass[LOW_PASS_SIZE];
  53. float row_buffer[APT_IMG_WIDTH + SYNC_SIZE + 2];
  54. float interpolator_buffer[APTDEC_BUFFER_SIZE];
  55. size_t interpolator_n;
  56. float interpolator_offset;
  57. };
  58. char *aptdec_get_version(void) {
  59. return VERSION;
  60. }
  61. fir_t *fir_init(size_t max_size, size_t ntaps) {
  62. fir_t *fir = (fir_t *)malloc(sizeof(fir_t));
  63. fir->ntaps = ntaps;
  64. fir->ring_size = max_size + ntaps;
  65. fir->taps = (float *)malloc(ntaps * sizeof(float));
  66. fir->ring_buffer = (float *)malloc((max_size + ntaps) * sizeof(float));
  67. return fir;
  68. }
  69. void fir_free(fir_t *fir) {
  70. free(fir->ring_buffer);
  71. free(fir->taps);
  72. free(fir);
  73. }
  74. pll_t *pll_init(float alpha, float beta, float min_freq, float max_freq, float sample_rate) {
  75. pll_t *pll = (pll_t *)malloc(sizeof(pll_t));
  76. pll->alpha = alpha;
  77. pll->beta = beta;
  78. pll->min_freq = M_TAUf * min_freq / sample_rate;
  79. pll->max_freq = M_TAUf * max_freq / sample_rate;
  80. pll->phase = 0.0f;
  81. pll->freq = 0.0f;
  82. return pll;
  83. }
  84. aptdec_t *aptdec_init(float sample_rate) {
  85. if (sample_rate > 96000 || sample_rate < (CARRIER_FREQ + APT_IMG_WIDTH) * 2.0f) {
  86. return NULL;
  87. }
  88. aptdec_t *apt = (aptdec_t *)malloc(sizeof(aptdec_t));
  89. apt->sample_rate = sample_rate;
  90. apt->sync_frequency = 1.0f;
  91. // PLL configuration
  92. // https://www.trondeau.com/blog/2011/8/13/control-loop-gain-values.html
  93. float damp = 0.7f;
  94. float bw = 0.005f;
  95. float alpha = (4.0f * damp * bw) / (1.0f + 2.0f * damp * bw + bw * bw);
  96. float beta = (4.0f * bw * bw) / (1.0f + 2.0f * damp * bw + bw * bw);
  97. apt->pll = pll_init(alpha, beta, CARRIER_FREQ-MAX_CARRIER_OFFSET, CARRIER_FREQ+MAX_CARRIER_OFFSET, sample_rate);
  98. if (apt->pll == NULL) {
  99. free(apt);
  100. return NULL;
  101. }
  102. // Hilbert transform
  103. apt->hilbert = fir_init(APTDEC_BUFFER_SIZE, 31);
  104. if (apt->hilbert == NULL) {
  105. free(apt->pll);
  106. free(apt);
  107. return NULL;
  108. }
  109. design_hilbert(apt->hilbert->taps, apt->hilbert->ntaps);
  110. design_low_pass(apt->low_pass, apt->sample_rate, (2080.0f + CARRIER_FREQ) / 2.0f, LOW_PASS_SIZE);
  111. return apt;
  112. }
  113. void aptdec_free(aptdec_t *apt) {
  114. fir_free(apt->hilbert);
  115. free(apt->pll);
  116. free(apt);
  117. }
  118. static complexf_t pll_work(pll_t *pll, complexf_t in) {
  119. // Internal oscillator (90deg offset)
  120. complexf_t osc = complex_build(cosf(pll->phase), -sinf(pll->phase));
  121. in = complex_multiply(in, osc);
  122. // Error detector
  123. float error = cargf(in);
  124. // Loop filter (single pole IIR)
  125. pll->freq += pll->beta * error;
  126. pll->freq = clamp(pll->freq, pll->min_freq, pll->max_freq);
  127. pll->phase += pll->freq + (pll->alpha * error);
  128. pll->phase = remainderf(pll->phase, M_TAUf);
  129. return in;
  130. }
  131. static int am_demod(aptdec_t *apt, float *out, size_t count, aptdec_callback_t callback, void *context) {
  132. size_t read = callback(&apt->hilbert->ring_buffer[apt->hilbert->ntaps], count, context);
  133. for (size_t i = 0; i < read; i++) {
  134. complexf_t sample = hilbert_transform(&apt->hilbert->ring_buffer[i], apt->hilbert->taps, apt->hilbert->ntaps);
  135. out[i] = crealf(pll_work(apt->pll, sample));
  136. }
  137. memcpy(apt->hilbert->ring_buffer, &apt->hilbert->ring_buffer[read], apt->hilbert->ntaps*sizeof(float));
  138. return read;
  139. }
  140. static int get_pixels(aptdec_t *apt, float *out, size_t count, aptdec_callback_t callback, void *context) {
  141. float ratio = apt->sample_rate / (4160.0f * apt->sync_frequency);
  142. for (size_t i = 0; i < count; i++) {
  143. // Get more samples if there are less than `LOW_PASS_SIZE` available
  144. if (apt->interpolator_n + LOW_PASS_SIZE > APTDEC_BUFFER_SIZE) {
  145. memcpy(apt->interpolator_buffer, &apt->interpolator_buffer[apt->interpolator_n], (APTDEC_BUFFER_SIZE-apt->interpolator_n) * sizeof(float));
  146. size_t read = am_demod(apt, &apt->interpolator_buffer[APTDEC_BUFFER_SIZE-apt->interpolator_n], apt->interpolator_n, callback, context);
  147. if (read != apt->interpolator_n) {
  148. return i;
  149. }
  150. apt->interpolator_n = 0;
  151. }
  152. out[i] = interpolating_convolve(&apt->interpolator_buffer[apt->interpolator_n], apt->low_pass, LOW_PASS_SIZE, apt->interpolator_offset);
  153. // Do not question the sacred code
  154. int shift = ceilf(ratio - apt->interpolator_offset);
  155. apt->interpolator_offset = shift + apt->interpolator_offset - ratio;
  156. apt->interpolator_n += shift;
  157. }
  158. return count;
  159. }
  160. // Get an entire row of pixels, aligned with sync markers
  161. int aptdec_getrow(aptdec_t *apt, float *row, aptdec_callback_t callback, void *context) {
  162. // Wrap the circular buffer
  163. memcpy(apt->row_buffer, &apt->row_buffer[APT_IMG_WIDTH], (SYNC_SIZE + 2) * sizeof(float));
  164. // Get a lines worth (APT_IMG_WIDTH) of samples
  165. if (get_pixels(apt, &apt->row_buffer[SYNC_SIZE + 2], APT_IMG_WIDTH, callback, context) != APT_IMG_WIDTH) {
  166. return 0;
  167. }
  168. // Error detector
  169. float left = FLT_MIN;
  170. float middle = FLT_MIN;
  171. float right = FLT_MIN;
  172. size_t phase = 0;
  173. for (size_t i = 0; i < APT_IMG_WIDTH; i++) {
  174. float _left = convolve(&apt->row_buffer[i + 0], sync_pattern, SYNC_SIZE);
  175. float _middle = convolve(&apt->row_buffer[i + 1], sync_pattern, SYNC_SIZE);
  176. float _right = convolve(&apt->row_buffer[i + 2], sync_pattern, SYNC_SIZE);
  177. if (_middle > middle) {
  178. left = _left;
  179. middle = _middle;
  180. right = _right;
  181. phase = i + 1;
  182. }
  183. }
  184. // Frequency
  185. float bias = (left / middle) - (right / middle);
  186. apt->sync_frequency = 1.0f + bias / APT_IMG_WIDTH / 4.0f;
  187. // Phase
  188. memcpy(&row[APT_IMG_WIDTH], &apt->row_buffer[phase], (APT_IMG_WIDTH - phase) * sizeof(float));
  189. memcpy(&row[APT_IMG_WIDTH - phase], apt->row_buffer, phase * sizeof(float));
  190. return 1;
  191. }