Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

247 строки
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 <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. }