Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

272 lignes
5.5 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. /* fast phase estimator */
  50. static inline double Phase(double I,double Q)
  51. {
  52. double angle,r;
  53. int s;
  54. if(I==0.0 && Q==0.0)
  55. return(0.0);
  56. if (Q < 0) {
  57. s=-1;
  58. Q=-Q;
  59. } else {
  60. s=1;
  61. }
  62. if (I>=0) {
  63. r = (I - Q) / (I + Q);
  64. angle = 0.5 - 0.5 * r;
  65. } else {
  66. r = (I + Q) / (Q - I);
  67. angle = 1.5 - 0.5 * r;
  68. }
  69. if(s>0)
  70. return(angle);
  71. else
  72. return(-angle);
  73. }
  74. static float pll(double I, double Q)
  75. {
  76. /* pll coeff */
  77. #define K1 5e-3
  78. #define K2 3e-6
  79. static double PhaseOsc = 0.0;
  80. double Io, Qo;
  81. double Ip, Qp;
  82. double DPhi;
  83. double DF;
  84. /* quadrature oscillator */
  85. Io = cos(PhaseOsc);
  86. Qo = sin(PhaseOsc);
  87. /* phase detector */
  88. Ip = I*Io+Q*Qo;
  89. Qp = Q*Io-I*Qo;
  90. DPhi = Phase(Ip,Qp);
  91. printf("%g\n",DPhi);
  92. /* loop filter */
  93. DF = K1 * DPhi + FreqOsc;
  94. PhaseOsc += 2.0 * M_PI * DF;
  95. if (PhaseOsc > M_PI)
  96. PhaseOsc -= 2.0 * M_PI;
  97. if (PhaseOsc <= -M_PI)
  98. PhaseOsc += 2.0 * M_PI;
  99. FreqOsc += K2 * DPhi;
  100. if (FreqOsc > ((Fc + DFc) / Fe))
  101. FreqOsc = (Fc + DFc) / Fe;
  102. if (FreqOsc < ((Fc - DFc) / Fe))
  103. FreqOsc = (Fc - DFc) / Fe;
  104. return ((float)Ip);
  105. }
  106. static int getamp(float *ambuff, int nb)
  107. {
  108. #define BLKIN 1024
  109. static float inbuff[BLKIN];
  110. static int idxin=0;
  111. static int nin=0;
  112. int n;
  113. for (n = 0; n < nb; n++) {
  114. double I,Q;
  115. if (nin < IQFilterLen*2) {
  116. int res;
  117. memmove(inbuff, &(inbuff[idxin]), nin * sizeof(float));
  118. idxin = 0;
  119. res = getsample(&(inbuff[nin]), BLKIN - nin);
  120. nin += res;
  121. if (nin < IQFilterLen*2)
  122. return (n);
  123. }
  124. iqfir(&inbuff[idxin],iqfilter,IQFilterLen,&I,&Q);
  125. ambuff[n] = pll(I,Q);
  126. fr = 0.25 * FreqOsc + 0.75 * fr;
  127. idxin += 1;
  128. nin -= 1;
  129. }
  130. return (n);
  131. }
  132. int getpixelv(float *pvbuff, int nb)
  133. {
  134. #define BLKAMP 1024
  135. static float ambuff[BLKAMP];
  136. static int nam = 0;
  137. static int idxam = 0;
  138. int n,m;
  139. double mult;
  140. mult = (double) Fi *fr / Fc * FreqLine;
  141. m=RSFilterLen/mult+1;
  142. for (n = 0; n < nb; n++) {
  143. int shift;
  144. if (nam < m) {
  145. int res;
  146. memmove(ambuff, &(ambuff[idxam]), nam * sizeof(float));
  147. idxam = 0;
  148. res = getamp(&(ambuff[nam]), BLKAMP - nam);
  149. nam += res;
  150. if (nam < m)
  151. return (n);
  152. }
  153. pvbuff[n] = rsfir(&(ambuff[idxam]), rsfilter, RSFilterLen, offset, mult) * mult * 2 * 256.0;
  154. shift = ((int) floor((RSMULT - offset) / mult))+1;
  155. offset = shift*mult+offset-RSMULT ;
  156. idxam += shift;
  157. nam -= shift;
  158. }
  159. return (nb);
  160. }
  161. int getpixelrow(float *pixelv)
  162. {
  163. static float pixels[PixelLine + SyncFilterLen];
  164. static int npv = 0;
  165. static int synced = 0;
  166. static double max = 0.0;
  167. double corr, ecorr, lcorr;
  168. int res;
  169. if (npv > 0)
  170. memmove(pixelv, pixels, npv * sizeof(float));
  171. if (npv < SyncFilterLen + 2) {
  172. res = getpixelv(&(pixelv[npv]), SyncFilterLen + 2 - npv);
  173. npv += res;
  174. if (npv < SyncFilterLen + 2)
  175. return (0);
  176. }
  177. /* test sync */
  178. corr = fir(&(pixelv[1]), Sync, SyncFilterLen);
  179. ecorr = fir(pixelv, Sync, SyncFilterLen);
  180. lcorr = fir(&(pixelv[2]), Sync, SyncFilterLen);
  181. FreqLine = 1.0 + (ecorr - lcorr) / corr / PixelLine / 4.0;
  182. if (corr < 0.75 * max) {
  183. synced = 0;
  184. FreqLine = 1.0;
  185. }
  186. max = corr;
  187. if (synced < 8) {
  188. int shift, mshift;
  189. if (npv < PixelLine + SyncFilterLen) {
  190. res =
  191. getpixelv(&(pixelv[npv]), PixelLine + SyncFilterLen - npv);
  192. npv += res;
  193. if (npv < PixelLine + SyncFilterLen)
  194. return (0);
  195. }
  196. /* lookup sync start */
  197. mshift = 0;
  198. for (shift = 1; shift < PixelLine; shift++) {
  199. double corr;
  200. corr = fir(&(pixelv[shift + 1]), Sync, SyncFilterLen);
  201. if (corr > max) {
  202. mshift = shift;
  203. max = corr;
  204. }
  205. }
  206. if (mshift != 0) {
  207. memmove(pixelv, &(pixelv[mshift]),
  208. (npv - mshift) * sizeof(float));
  209. npv -= mshift;
  210. synced = 0;
  211. FreqLine = 1.0;
  212. } else
  213. synced += 1;
  214. }
  215. if (npv < PixelLine) {
  216. res = getpixelv(&(pixelv[npv]), PixelLine - npv);
  217. npv += res;
  218. if (npv < PixelLine)
  219. return (0);
  220. }
  221. if (npv == PixelLine) {
  222. npv = 0;
  223. } else {
  224. memmove(pixels, &(pixelv[PixelLine]),
  225. (npv - PixelLine) * sizeof(float));
  226. npv -= PixelLine;
  227. }
  228. return (1);
  229. }