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.

1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 = calloc(1, sizeof(fir_t));
  63. fir->ntaps = ntaps;
  64. fir->ring_size = max_size + ntaps;
  65. fir->taps = calloc(ntaps, sizeof(float));
  66. fir->ring_buffer = calloc(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 = calloc(1, 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 = calloc(1, sizeof(aptdec_t));
  89. apt->sample_rate = sample_rate;
  90. apt->sync_frequency = 1.0f;
  91. apt->interpolator_n = APTDEC_BUFFER_SIZE;
  92. apt->interpolator_offset = 0.0f;
  93. // PLL configuration
  94. // https://www.trondeau.com/blog/2011/8/13/control-loop-gain-values.html
  95. float damp = 0.7f;
  96. float bw = 0.005f;
  97. float alpha = (4.0f * damp * bw) / (1.0f + 2.0f * damp * bw + bw * bw);
  98. float beta = (4.0f * bw * bw) / (1.0f + 2.0f * damp * bw + bw * bw);
  99. apt->pll = pll_init(alpha, beta, CARRIER_FREQ-MAX_CARRIER_OFFSET, CARRIER_FREQ+MAX_CARRIER_OFFSET, sample_rate);
  100. if (apt->pll == NULL) {
  101. free(apt);
  102. return NULL;
  103. }
  104. // Hilbert transform
  105. apt->hilbert = fir_init(APTDEC_BUFFER_SIZE, 31);
  106. if (apt->hilbert == NULL) {
  107. free(apt->pll);
  108. free(apt);
  109. return NULL;
  110. }
  111. design_hilbert(apt->hilbert->taps, apt->hilbert->ntaps);
  112. design_low_pass(apt->low_pass, apt->sample_rate, (2080.0f + CARRIER_FREQ) / 2.0f, LOW_PASS_SIZE);
  113. return apt;
  114. }
  115. void aptdec_free(aptdec_t *apt) {
  116. fir_free(apt->hilbert);
  117. free(apt->pll);
  118. free(apt);
  119. }
  120. static complexf_t pll_work(pll_t *pll, complexf_t in) {
  121. // Internal oscillator (90deg offset)
  122. complexf_t osc = complex_build(cosf(pll->phase), -sinf(pll->phase));
  123. in = complex_multiply(in, osc);
  124. // Error detector
  125. float error = cargf(in);
  126. // Loop filter (single pole IIR)
  127. pll->freq += pll->beta * error;
  128. pll->freq = clamp(pll->freq, pll->min_freq, pll->max_freq);
  129. pll->phase += pll->freq + (pll->alpha * error);
  130. pll->phase = remainderf(pll->phase, M_TAUf);
  131. return in;
  132. }
  133. static int am_demod(aptdec_t *apt, float *out, size_t count, aptdec_callback_t callback, void *context) {
  134. size_t read = callback(&apt->hilbert->ring_buffer[apt->hilbert->ntaps], count, context);
  135. for (size_t i = 0; i < read; i++) {
  136. complexf_t sample = hilbert_transform(&apt->hilbert->ring_buffer[i], apt->hilbert->taps, apt->hilbert->ntaps);
  137. out[i] = crealf(pll_work(apt->pll, sample));
  138. }
  139. memcpy(apt->hilbert->ring_buffer, &apt->hilbert->ring_buffer[read], apt->hilbert->ntaps*sizeof(float));
  140. return read;
  141. }
  142. static int get_pixels(aptdec_t *apt, float *out, size_t count, aptdec_callback_t callback, void *context) {
  143. float ratio = apt->sample_rate / (4160.0f * apt->sync_frequency);
  144. for (size_t i = 0; i < count; i++) {
  145. // Get more samples if there are less than `LOW_PASS_SIZE` available
  146. if (apt->interpolator_n + LOW_PASS_SIZE > APTDEC_BUFFER_SIZE) {
  147. memcpy(apt->interpolator_buffer, &apt->interpolator_buffer[apt->interpolator_n], (APTDEC_BUFFER_SIZE-apt->interpolator_n) * sizeof(float));
  148. size_t read = am_demod(apt, &apt->interpolator_buffer[APTDEC_BUFFER_SIZE-apt->interpolator_n], apt->interpolator_n, callback, context);
  149. if (read != apt->interpolator_n) {
  150. return i;
  151. }
  152. apt->interpolator_n = 0;
  153. }
  154. out[i] = interpolating_convolve(&apt->interpolator_buffer[apt->interpolator_n], apt->low_pass, LOW_PASS_SIZE, apt->interpolator_offset);
  155. // Do not question the sacred code
  156. int shift = ceilf(ratio - apt->interpolator_offset);
  157. apt->interpolator_offset = shift + apt->interpolator_offset - ratio;
  158. apt->interpolator_n += shift;
  159. }
  160. return count;
  161. }
  162. // Get an entire row of pixels, aligned with sync markers
  163. int aptdec_getrow(aptdec_t *apt, float *row, aptdec_callback_t callback, void *context) {
  164. // Wrap the circular buffer
  165. memcpy(apt->row_buffer, &apt->row_buffer[APT_IMG_WIDTH], (SYNC_SIZE + 2) * sizeof(float));
  166. // Get a lines worth (APT_IMG_WIDTH) of samples
  167. if (get_pixels(apt, &apt->row_buffer[SYNC_SIZE + 2], APT_IMG_WIDTH, callback, context) != APT_IMG_WIDTH) {
  168. return 0;
  169. }
  170. // Error detector
  171. float left = FLT_MIN;
  172. float middle = FLT_MIN;
  173. float right = FLT_MIN;
  174. size_t phase = 0;
  175. for (size_t i = 0; i < APT_IMG_WIDTH; i++) {
  176. float _left = convolve(&apt->row_buffer[i + 0], sync_pattern, SYNC_SIZE);
  177. float _middle = convolve(&apt->row_buffer[i + 1], sync_pattern, SYNC_SIZE);
  178. float _right = convolve(&apt->row_buffer[i + 2], sync_pattern, SYNC_SIZE);
  179. if (_middle > middle) {
  180. left = _left;
  181. middle = _middle;
  182. right = _right;
  183. phase = i + 1;
  184. }
  185. }
  186. // Frequency
  187. float bias = (left / middle) - (right / middle);
  188. apt->sync_frequency = 1.0f + bias / APT_IMG_WIDTH / 4.0f;
  189. // Phase
  190. memcpy(&row[APT_IMG_WIDTH], &apt->row_buffer[phase], (APT_IMG_WIDTH - phase) * sizeof(float));
  191. memcpy(&row[APT_IMG_WIDTH - phase], apt->row_buffer, phase * sizeof(float));
  192. return 1;
  193. }