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.

21 年之前
21 年之前
21 年之前
21 年之前
21 年之前
21 年之前
21 年之前
21 年之前
21 年之前
21 年之前
21 年之前
21 年之前
21 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <math.h>
  26. #include "filter.h"
  27. #include "filtercoeff.h"
  28. extern int nbch,nch;
  29. extern int getsample(short *inbuff, int nb);
  30. #define Fc 2400.0
  31. #define PixelLine 2080
  32. #define RSMULT 30
  33. #define Fi (PixelLine*2*RSMULT)
  34. double Fe;
  35. static double FreqOsc;
  36. static double DPLL_C1,DPLL_C2,AGCK;
  37. static double FreqLine = 1.0;
  38. #define sig 20
  39. #define RSFilterLen (5*sig+1)
  40. static float rsfilter[RSFilterLen];
  41. int init_dsp(void)
  42. {
  43. int i;
  44. /* gausian filter coeff */
  45. for(i=0;i<RSFilterLen;i++) {
  46. double x=i-RSFilterLen/2;
  47. rsfilter[i]=exp(-x*x/2.0/sig/sig)/sqrt(2.0*M_PI)/sig;
  48. }
  49. /* pll coeff */
  50. FreqOsc=Fc/Fe;
  51. DPLL_C2=M_SQRT2*20.0/Fe;
  52. DPLL_C1=DPLL_C2*DPLL_C2/2.0;
  53. AGCK=exp(-1.0/Fe/0.25);
  54. return(0);
  55. }
  56. static int getamp(double *ambuff, int nb)
  57. {
  58. #define BLKIN 4096
  59. static short inbuff[2*BLKIN];
  60. int n,ns;
  61. static double PhaseOsc = 0.0;
  62. static double mamp=0.5;
  63. double amp;
  64. double DPhi;
  65. #define DFc 50.0
  66. ns=getsample(inbuff, nb > BLKIN ? BLKIN : nb);
  67. for (n = 0; n < ns; n++) {
  68. double in;
  69. in=((double)inbuff[n*nbch+nch]/32768.0);
  70. mamp=AGCK*mamp+(1.0-AGCK)*fabs(in);
  71. amp=2.0*in*cos(PhaseOsc);
  72. ambuff[n] = amp;
  73. //fprintf(stderr,"%g %g %g\n",in,cos(PhaseOsc),amp);
  74. /* delta phase */
  75. DPhi=-in/(mamp*M_SQRT2)*(sin(PhaseOsc)-sin(3.0*PhaseOsc));
  76. /* loop filter */
  77. PhaseOsc += 2.0 * M_PI * (DPLL_C2 * DPhi + FreqOsc);
  78. if (PhaseOsc > M_PI)
  79. PhaseOsc -= 2.0 * M_PI;
  80. if (PhaseOsc <= -M_PI)
  81. PhaseOsc += 2.0 * M_PI;
  82. FreqOsc += DPLL_C1 * DPhi;
  83. if (FreqOsc > ((Fc + DFc) / Fe))
  84. FreqOsc = (Fc + DFc) / Fe;
  85. if (FreqOsc < ((Fc - DFc) / Fe))
  86. FreqOsc = (Fc - DFc) / Fe;
  87. }
  88. return (ns);
  89. }
  90. int getpixelv(float *pvbuff, int nb)
  91. {
  92. static double offset = 0.0;
  93. #define BLKAMP 4096
  94. static double ambuff[BLKAMP];
  95. static int nam = 0;
  96. static int idxam = 0;
  97. int n,m;
  98. double mult;
  99. float v;
  100. static float ppv=0,pv=0;
  101. mult = (double) Fi/Fe*FreqLine;
  102. m=RSFilterLen/mult+1;
  103. for (n = 0; n < nb; n++) {
  104. int shift;
  105. if (nam < m) {
  106. int res;
  107. memmove(ambuff, &(ambuff[idxam]), nam * sizeof(double));
  108. idxam = 0;
  109. res = getamp(&(ambuff[nam]), BLKAMP - nam);
  110. nam += res;
  111. if (nam < m)
  112. return (n);
  113. }
  114. v = rsfir(&(ambuff[idxam]), rsfilter, RSFilterLen, offset, mult) * mult * 256.0;
  115. if(v<0.0) {
  116. v = 2.0*(pv-ppv);
  117. if (v<0.0) v=0.0;
  118. if (v>=256.0) v=255.0;
  119. pvbuff[n] = v;
  120. ppv=pv;
  121. } else {
  122. if (v>=256.0) v=255.0;
  123. pvbuff[n] = v;
  124. ppv=pv;pv=v;
  125. }
  126. shift = ((int) floor((RSMULT - offset) / mult))+1;
  127. offset = shift*mult+offset-RSMULT ;
  128. idxam += shift;
  129. nam -= shift;
  130. }
  131. return (nb);
  132. }
  133. int getpixelrow(float *pixelv)
  134. {
  135. static float pixels[PixelLine + SyncFilterLen];
  136. static int npv = 0;
  137. static int synced = 0;
  138. static double max = 0.0;
  139. double corr, ecorr, lcorr;
  140. int res;
  141. if (npv > 0)
  142. memmove(pixelv, pixels, npv * sizeof(float));
  143. if (npv < SyncFilterLen + 2) {
  144. res = getpixelv(&(pixelv[npv]), SyncFilterLen + 2 - npv);
  145. npv += res;
  146. if (npv < SyncFilterLen + 2)
  147. return (0);
  148. }
  149. /* test sync */
  150. ecorr = fir(pixelv, Sync, SyncFilterLen);
  151. corr = fir(&(pixelv[1]), Sync, SyncFilterLen);
  152. lcorr = fir(&(pixelv[2]), Sync, SyncFilterLen);
  153. FreqLine = 1.0+((ecorr-lcorr) / corr / PixelLine / 4.0);
  154. if (corr < 0.75 * max) {
  155. synced = 0;
  156. FreqLine = 1.0;
  157. }
  158. max = corr;
  159. if (synced < 8) {
  160. int shift, mshift;
  161. if (npv < PixelLine + SyncFilterLen) {
  162. res =
  163. getpixelv(&(pixelv[npv]), PixelLine + SyncFilterLen - npv);
  164. npv += res;
  165. if (npv < PixelLine + SyncFilterLen)
  166. return (0);
  167. }
  168. /* lookup sync start */
  169. mshift = 0;
  170. for (shift = 1; shift < PixelLine; shift++) {
  171. double corr;
  172. corr = fir(&(pixelv[shift + 1]), Sync, SyncFilterLen);
  173. if (corr > max) {
  174. mshift = shift;
  175. max = corr;
  176. }
  177. }
  178. if (mshift != 0) {
  179. memmove(pixelv, &(pixelv[mshift]),
  180. (npv - mshift) * sizeof(float));
  181. npv -= mshift;
  182. synced = 0;
  183. FreqLine = 1.0;
  184. } else
  185. synced += 1;
  186. }
  187. if (npv < PixelLine) {
  188. res = getpixelv(&(pixelv[npv]), PixelLine - npv);
  189. npv += res;
  190. if (npv < PixelLine)
  191. return (0);
  192. }
  193. if (npv == PixelLine) {
  194. npv = 0;
  195. } else {
  196. memmove(pixels, &(pixelv[PixelLine]),
  197. (npv - PixelLine) * sizeof(float));
  198. npv -= PixelLine;
  199. }
  200. return (1);
  201. }