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.1 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. typedef struct {
  31. float alpha;
  32. float beta;
  33. float min_freq;
  34. float max_freq;
  35. float freq;
  36. float phase;
  37. } pll_t;
  38. typedef struct {
  39. float *ring_buffer;
  40. size_t ring_size;
  41. float *taps;
  42. size_t ntaps;
  43. } fir_t;
  44. struct aptdec_t {
  45. float sample_rate;
  46. float sync_frequency;
  47. pll_t *pll;
  48. fir_t *hilbert;
  49. float low_pass[LOW_PASS_SIZE];
  50. };
  51. char *aptdec_get_version(void) {
  52. return VERSION;
  53. }
  54. fir_t *fir_init(size_t max_size, size_t ntaps) {
  55. fir_t *fir = (fir_t *)malloc(sizeof(fir_t));
  56. fir->ntaps = ntaps;
  57. fir->ring_size = max_size + ntaps;
  58. fir->taps = (float *)malloc(ntaps * sizeof(float));
  59. fir->ring_buffer = (float *)malloc((max_size + ntaps) * sizeof(float));
  60. return fir;
  61. }
  62. void fir_free(fir_t *fir) {
  63. free(fir->ring_buffer);
  64. free(fir->taps);
  65. free(fir);
  66. }
  67. pll_t *pll_init(float alpha, float beta, float min_freq, float max_freq, float sample_rate) {
  68. pll_t *pll = (pll_t *)malloc(sizeof(pll_t));
  69. pll->alpha = alpha;
  70. pll->beta = beta;
  71. pll->min_freq = M_TAUf * min_freq / sample_rate;
  72. pll->max_freq = M_TAUf * max_freq / sample_rate;
  73. pll->phase = 0.0f;
  74. pll->freq = 0.0f;
  75. return pll;
  76. }
  77. aptdec_t *aptdec_init(float sample_rate) {
  78. if (sample_rate > 96000 || sample_rate < (CARRIER_FREQ + APT_IMG_WIDTH) * 2.0f) {
  79. return NULL;
  80. }
  81. aptdec_t *apt = (aptdec_t *)malloc(sizeof(aptdec_t));
  82. apt->sample_rate = sample_rate;
  83. apt->sync_frequency = 1.0f;
  84. // PLL configuration
  85. // https://www.trondeau.com/blog/2011/8/13/control-loop-gain-values.html
  86. float damp = 0.7f;
  87. float bw = 0.005f;
  88. float alpha = (4.0f * damp * bw) / (1.0f + 2.0f * damp * bw + bw * bw);
  89. float beta = (4.0f * bw * bw) / (1.0f + 2.0f * damp * bw + bw * bw);
  90. apt->pll = pll_init(alpha, beta, CARRIER_FREQ-MAX_CARRIER_OFFSET, CARRIER_FREQ+MAX_CARRIER_OFFSET, sample_rate);
  91. if (apt->pll == NULL) {
  92. free(apt);
  93. return NULL;
  94. }
  95. // Hilbert transform
  96. apt->hilbert = fir_init(APTDEC_BUFFER_SIZE, 31);
  97. if (apt->hilbert == NULL) {
  98. free(apt->pll);
  99. free(apt);
  100. return NULL;
  101. }
  102. design_hilbert(apt->hilbert->taps, apt->hilbert->ntaps);
  103. design_low_pass(apt->low_pass, apt->sample_rate, (2080.0f + CARRIER_FREQ) / 2.0f, LOW_PASS_SIZE);
  104. return apt;
  105. }
  106. void aptdec_free(aptdec_t *apt) {
  107. fir_free(apt->hilbert);
  108. free(apt->pll);
  109. free(apt);
  110. }
  111. static complexf_t pll_work(pll_t *pll, complexf_t in) {
  112. // Internal oscillator (90deg offset)
  113. complexf_t osc = complex_build(cosf(pll->phase), -sinf(pll->phase));
  114. in = complex_multiply(in, osc);
  115. // Error detector
  116. float error = cargf(in);
  117. // Loop filter (single pole IIR)
  118. pll->freq += pll->beta * error;
  119. pll->freq = clamp(pll->freq, pll->min_freq, pll->max_freq);
  120. pll->phase += pll->freq + (pll->alpha * error);
  121. pll->phase = remainderf(pll->phase, M_TAUf);
  122. return in;
  123. }
  124. static int am_demod(aptdec_t *apt, float *out, size_t count, aptdec_callback_t callback, void *context) {
  125. size_t read = callback(&apt->hilbert->ring_buffer[apt->hilbert->ntaps], count, context);
  126. for (size_t i = 0; i < read; i++) {
  127. complexf_t sample = hilbert_transform(&apt->hilbert->ring_buffer[i], apt->hilbert->taps, apt->hilbert->ntaps);
  128. out[i] = crealf(pll_work(apt->pll, sample));
  129. }
  130. memcpy(apt->hilbert->ring_buffer, &apt->hilbert->ring_buffer[read], apt->hilbert->ntaps*sizeof(float));
  131. return read;
  132. }
  133. static int get_pixels(aptdec_t *apt, float *out, size_t count, aptdec_callback_t callback, void *context) {
  134. static float buffer[APTDEC_BUFFER_SIZE];
  135. static size_t n = APTDEC_BUFFER_SIZE;
  136. static float offset = 0.0;
  137. float ratio = apt->sample_rate / (4160.0f * apt->sync_frequency);
  138. for (size_t i = 0; i < count; i++) {
  139. // Get more samples if there are less than `LOW_PASS_SIZE` available
  140. if (n + LOW_PASS_SIZE > APTDEC_BUFFER_SIZE) {
  141. memcpy(buffer, &buffer[n], (APTDEC_BUFFER_SIZE-n) * sizeof(float));
  142. size_t read = am_demod(apt, &buffer[APTDEC_BUFFER_SIZE-n], n, callback, context);
  143. if (read != n) {
  144. return i;
  145. }
  146. n = 0;
  147. }
  148. out[i] = interpolating_convolve(&buffer[n], apt->low_pass, LOW_PASS_SIZE, offset);
  149. // Do not question the sacred code
  150. int shift = ceilf(ratio - offset);
  151. offset = shift + offset - ratio;
  152. n += shift;
  153. }
  154. return count;
  155. }
  156. const float sync_pattern[] = {-1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1,
  157. 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 0};
  158. #define SYNC_SIZE (sizeof(sync_pattern)/sizeof(sync_pattern[0]))
  159. // Get an entire row of pixels, aligned with sync markers
  160. int aptdec_getrow(aptdec_t *apt, float *row, aptdec_callback_t callback, void *context) {
  161. static float pixels[APT_IMG_WIDTH + SYNC_SIZE + 2];
  162. // Wrap the circular buffer
  163. memcpy(pixels, &pixels[APT_IMG_WIDTH], (SYNC_SIZE + 2) * sizeof(float));
  164. // Get a lines worth (APT_IMG_WIDTH) of samples
  165. if (get_pixels(apt, &pixels[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(&pixels[i + 0], sync_pattern, SYNC_SIZE);
  175. float _middle = convolve(&pixels[i + 1], sync_pattern, SYNC_SIZE);
  176. float _right = convolve(&pixels[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 / 2.0f;
  187. // Phase
  188. memcpy(&row[APT_IMG_WIDTH], &pixels[phase], (APT_IMG_WIDTH - phase) * sizeof(float));
  189. memcpy(&row[APT_IMG_WIDTH - phase], pixels, phase * sizeof(float));
  190. return 1;
  191. }