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.

dsp.c 5.4 KiB

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