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.
 
 
 
 
 

238 lines
5.1 KiB

  1. /*
  2. * Atpdec
  3. * Copyright (c) 2004 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 <string.h>
  23. #include <math.h>
  24. #ifndef M_PI
  25. #define M_PI 3.14159265358979323846 /* for OS that don't know it */
  26. #endif /* */
  27. #include "filter.h"
  28. #include "filtercoeff.h"
  29. #define Fc 2400.0
  30. #define DFc 50.0
  31. #define PixelLine 2080
  32. #define Fp (2*PixelLine)
  33. #define RSMULT 15
  34. #define Fi (Fp*RSMULT)
  35. static double Fe;
  36. static double FreqOsc;
  37. static double FreqLine = 1.0;
  38. static double fr;
  39. static double offset = 0.0;
  40. extern int getsample(float *inbuff, int nb);
  41. int init_dsp(double F)
  42. {
  43. if(F > Fi) return (1);
  44. if(F < Fp) return (-1);
  45. Fe=F;
  46. fr=FreqOsc=Fc/Fe;
  47. return(0);
  48. }
  49. static float pll(double I, double Q)
  50. {
  51. /* pll coeff */
  52. #define K1 5e-3
  53. #define K2 3e-6
  54. static double PhaseOsc = 0.0;
  55. double Io, Qo;
  56. double Ip, Qp;
  57. double DPhi;
  58. double DF;
  59. /* quadrature oscillator */
  60. Io = cos(PhaseOsc);
  61. Qo = sin(PhaseOsc);
  62. /* phase detector */
  63. Ip = I*Io-Q*Qo;
  64. Qp = I*Qo+Q*Io;
  65. DPhi = -atan2(Qp, Ip) / M_PI;
  66. /* loop filter */
  67. DF = K1 * DPhi + FreqOsc;
  68. FreqOsc += K2 * DPhi;
  69. if (FreqOsc > ((Fc + DFc) / Fe))
  70. FreqOsc = (Fc + DFc) / Fe;
  71. if (FreqOsc < ((Fc - DFc) / Fe))
  72. FreqOsc = (Fc - DFc) / Fe;
  73. PhaseOsc += 2.0 * M_PI * DF;
  74. if (PhaseOsc > M_PI)
  75. PhaseOsc -= 2.0 * M_PI;
  76. if (PhaseOsc <= -M_PI)
  77. PhaseOsc += 2.0 * M_PI;
  78. return ((float)Ip);
  79. }
  80. static int getamp(float *ambuff, int nb)
  81. {
  82. #define BLKIN 1024
  83. static float inbuff[BLKIN];
  84. static int idxin=0;
  85. static int nin=0;
  86. int n;
  87. for (n = 0; n < nb; n++) {
  88. double I,Q;
  89. if (nin < IQFilterLen*2) {
  90. int res;
  91. memmove(inbuff, &(inbuff[idxin]), nin * sizeof(float));
  92. idxin = 0;
  93. res = getsample(&(inbuff[nin]), BLKIN - nin);
  94. nin += res;
  95. if (nin < IQFilterLen*2)
  96. return (n);
  97. }
  98. iqfir(&inbuff[idxin],iqfilter,IQFilterLen,&I,&Q);
  99. ambuff[n] = pll(I,Q);
  100. fr = 0.25 * FreqOsc + 0.75 * fr;
  101. idxin += 1;
  102. nin -= 1;
  103. }
  104. return (n);
  105. }
  106. int getpixelv(float *pvbuff, int nb)
  107. {
  108. #define BLKAMP 1024
  109. static float ambuff[BLKAMP];
  110. static int nam = 0;
  111. static int idxam = 0;
  112. int n,m;
  113. double mult;
  114. mult = (double) Fi *fr / Fc * FreqLine;
  115. m=RSFilterLen/mult+1;
  116. for (n = 0; n < nb; n++) {
  117. int shift;
  118. if (nam < m) {
  119. int res;
  120. memmove(ambuff, &(ambuff[idxam]), nam * sizeof(float));
  121. idxam = 0;
  122. res = getamp(&(ambuff[nam]), BLKAMP - nam);
  123. nam += res;
  124. if (nam < m)
  125. return (n);
  126. }
  127. pvbuff[n] = rsfir(&(ambuff[idxam]), rsfilter, RSFilterLen, offset, mult) * mult * 2 * 256.0;
  128. shift = ((int) floor((RSMULT - offset) / mult))+1;
  129. offset = shift*mult+offset-RSMULT ;
  130. idxam += shift;
  131. nam -= shift;
  132. }
  133. return (nb);
  134. }
  135. int getpixelrow(float *pixelv)
  136. {
  137. static float pixels[PixelLine + SyncFilterLen];
  138. static int npv = 0;
  139. static int synced = 0;
  140. static double max = 0.0;
  141. double corr, ecorr, lcorr;
  142. int res;
  143. if (npv > 0)
  144. memmove(pixelv, pixels, npv * sizeof(float));
  145. if (npv < SyncFilterLen + 2) {
  146. res = getpixelv(&(pixelv[npv]), SyncFilterLen + 2 - npv);
  147. npv += res;
  148. if (npv < SyncFilterLen + 2)
  149. return (0);
  150. }
  151. /* test sync */
  152. corr = fir(&(pixelv[1]), Sync, SyncFilterLen);
  153. ecorr = fir(pixelv, Sync, SyncFilterLen);
  154. lcorr = fir(&(pixelv[2]), Sync, SyncFilterLen);
  155. FreqLine = 1.0 + (ecorr - lcorr) / corr / PixelLine / 4.0;
  156. if (corr < 0.75 * max) {
  157. synced = 0;
  158. FreqLine = 1.0;
  159. }
  160. max = corr;
  161. if (synced < 8) {
  162. int shift, mshift;
  163. if (npv < PixelLine + SyncFilterLen) {
  164. res =
  165. getpixelv(&(pixelv[npv]), PixelLine + SyncFilterLen - npv);
  166. npv += res;
  167. if (npv < PixelLine + SyncFilterLen)
  168. return (0);
  169. }
  170. /* lookup sync start */
  171. mshift = 0;
  172. for (shift = 1; shift < PixelLine; shift++) {
  173. double corr;
  174. corr = fir(&(pixelv[shift + 1]), Sync, SyncFilterLen);
  175. if (corr > max) {
  176. mshift = shift;
  177. max = corr;
  178. }
  179. }
  180. if (mshift != 0) {
  181. memmove(pixelv, &(pixelv[mshift]),
  182. (npv - mshift) * sizeof(float));
  183. npv -= mshift;
  184. synced = 0;
  185. FreqLine = 1.0;
  186. } else
  187. synced += 1;
  188. }
  189. if (npv < PixelLine) {
  190. res = getpixelv(&(pixelv[npv]), PixelLine - npv);
  191. npv += res;
  192. if (npv < PixelLine)
  193. return (0);
  194. }
  195. if (npv == PixelLine) {
  196. npv = 0;
  197. } else {
  198. memmove(pixels, &(pixelv[PixelLine]),
  199. (npv - PixelLine) * sizeof(float));
  200. npv -= PixelLine;
  201. }
  202. return (1);
  203. }