Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

196 righe
4.8 KiB

  1. /*
  2. * Atpdec
  3. * Copyright (c) 2003 by Thierry Leconte (F4DWV)
  4. *
  5. * $Id$
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Library General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <math.h>
  23. #ifndef M_PI
  24. #define M_PI 3.14159265358979323846 /* for OS that don't know it */
  25. #endif /* */
  26. #include "filter.h"
  27. #include "filtercoeff.h"
  28. #define Fe 11025.0
  29. #define Fc 2400.0
  30. #define DFc 50.0
  31. #define PixelLine 2080
  32. #define Fp (2*PixelLine)
  33. #define RSMULT 10
  34. #define Fi (Fp*RSMULT)
  35. static double FreqOsc = Fc / Fe;
  36. static double FreqLine = 1.0;
  37. extern int getsample (float *inbuff, int nb);
  38. static float
  39. pll (float In)
  40. {
  41. /* pll coeff */
  42. #define K1 5e-3
  43. #define K2 3e-6
  44. static double PhaseOsc = 0.0;
  45. static iirbuff_t Ifilterbuff, Qfilterbuff;
  46. double Io, Qo;
  47. double Ip, Qp;
  48. double DPhi;
  49. double DF;
  50. /* quadrature oscillator */
  51. Io = cos (PhaseOsc);
  52. Qo = sin (PhaseOsc);
  53. /* phase detector */
  54. Ip = iir (In * Io, &Ifilterbuff, &PhaseFilterCf);
  55. Qp = iir (In * Qo, &Qfilterbuff, &PhaseFilterCf);
  56. DPhi = -atan2 (Qp, Ip) / M_PI;
  57. /* loop filter */
  58. DF = K1 * DPhi + FreqOsc;
  59. FreqOsc += K2 * DPhi;
  60. if (FreqOsc > ((Fc + DFc) / Fe))
  61. FreqOsc = (Fc + DFc) / Fe;
  62. if (FreqOsc < ((Fc - DFc) / Fe))
  63. FreqOsc = (Fc - DFc) / Fe;
  64. PhaseOsc += 2.0 * M_PI * DF;
  65. if (PhaseOsc > M_PI)
  66. PhaseOsc -= 2.0 * M_PI;
  67. if (PhaseOsc <= -M_PI)
  68. PhaseOsc += 2.0 * M_PI;
  69. return (float) (In * Io);
  70. } static double fr = Fc / Fe;
  71. static double offset = 0.0;
  72. getamp (float *ambuff, int nb)
  73. {
  74. #define BLKIN 1024
  75. float inbuff[BLKIN];
  76. int n;
  77. int res;
  78. res = getsample (inbuff, nb > BLKIN ? BLKIN : nb);
  79. for (n = 0; n < res; n++) {
  80. ambuff[n] = pll (inbuff[n]);
  81. fr = 0.25 * FreqOsc + 0.75 * fr;
  82. }
  83. return (res);
  84. }
  85. int
  86. getpixelv (float *pvbuff, int nb)
  87. {
  88. #define BLKAMP 256
  89. static float ambuff[BLKAMP];
  90. static int nam = 0;
  91. static int idxam = 0;
  92. int n;
  93. for (n = 0; n < nb; n++) {
  94. double mult;
  95. int shift;
  96. if (nam < BLKAMP) {
  97. int res;
  98. memmove (ambuff, &(ambuff[idxam]), nam * sizeof (float));
  99. idxam = 0;
  100. res = getamp (&(ambuff[nam]), BLKAMP - nam);
  101. nam += res;
  102. if (nam < BLKAMP)
  103. return (n);
  104. }
  105. mult = (double) Fi *fr / Fc * FreqLine;
  106. pvbuff[n] =
  107. rsfir (&(ambuff[idxam]), rsfilter, RSFilterLen, offset,
  108. mult) * mult * 2 * 256.0;
  109. shift = (int) ((RSMULT - offset + mult - 1) / mult);
  110. offset = shift * mult + offset - RSMULT;
  111. idxam += shift;
  112. nam -= shift;
  113. } return (nb);
  114. }
  115. int
  116. getpixelrow (float *pixelv)
  117. {
  118. static float pixels[PixelLine + SyncFilterLen];
  119. static int npv = 0;
  120. static int synced = 0;
  121. static double max = 0.0;
  122. double corr, ecorr, lcorr;
  123. double ph;
  124. int n, res;
  125. if (npv > 0)
  126. memmove (pixelv, pixels, npv * sizeof (float));
  127. if (npv < SyncFilterLen + 2) {
  128. res = getpixelv (&(pixelv[npv]), SyncFilterLen + 2 - npv);
  129. npv += res;
  130. if (npv < SyncFilterLen + 2)
  131. return (0);
  132. }
  133. /* test sync */
  134. corr = fir (&(pixelv[1]), Sync, SyncFilterLen);
  135. ecorr = fir (pixelv, Sync, SyncFilterLen);
  136. lcorr = fir (&(pixelv[2]), Sync, SyncFilterLen);
  137. FreqLine = 1.0 + (ecorr - lcorr) / corr / PixelLine / 4.0;
  138. if (corr < 0.75 * max) {
  139. synced = 0;
  140. FreqLine = 1.0;
  141. }
  142. max = corr;
  143. if (synced < 8) {
  144. int shift, mshift;
  145. if (npv < PixelLine + SyncFilterLen) {
  146. res = getpixelv (&(pixelv[npv]), PixelLine + SyncFilterLen - npv);
  147. npv += res;
  148. if (npv < PixelLine + SyncFilterLen)
  149. return (0);
  150. }
  151. /* lookup sync start */
  152. mshift = 0;
  153. for (shift = 1; shift < PixelLine; shift++) {
  154. double corr;
  155. corr = fir (&(pixelv[shift + 1]), Sync, SyncFilterLen);
  156. if (corr > max) {
  157. mshift = shift;
  158. max = corr;
  159. }
  160. }
  161. if (mshift != 0) {
  162. memmove (pixelv, &(pixelv[mshift]), (npv - mshift) * sizeof (float));
  163. npv -= mshift;
  164. synced = 0;
  165. FreqLine = 1.0;
  166. }
  167. else
  168. synced += 1;
  169. }
  170. if (npv < PixelLine) {
  171. res = getpixelv (&(pixelv[npv]), PixelLine - npv);
  172. npv += res;
  173. if (npv < PixelLine)
  174. return (0);
  175. }
  176. if (npv == PixelLine) {
  177. npv = 0;
  178. }
  179. else {
  180. memmove (pixels, &(pixelv[PixelLine]),
  181. (npv - PixelLine) * sizeof (float));
  182. npv -= PixelLine;
  183. } return (1);
  184. }