Houston, we have a trunk.
[debian/gnuradio] / gr-atsc / src / lib / GrAtscConvert2xTo20.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio 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 General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <GrAtscConvert2xTo20.h>
24 #include <atsc_consts.h>
25 #include <cmath>
26 #include <cstdio>
27
28 static const int    N_OUTPUTS = 1;
29 static const double DEC_RATIO = (2.0 * ATSC_SYMBOL_RATE) / 20e6;        // ~ 1.076
30
31 GrAtscConvert2xTo20::GrAtscConvert2xTo20 ()
32   : VrDecimatingSigProc<float,float> (N_OUTPUTS, (int) rint (DEC_RATIO))
33 {
34   d_next_input = 0;
35   d_frac_part = 0;
36   history = 2 * d_interp.ntaps ();      // some slack
37 }
38
39 GrAtscConvert2xTo20::~GrAtscConvert2xTo20 ()
40 {
41   // Nop
42 }
43
44 void
45 GrAtscConvert2xTo20::pre_initialize ()
46 {
47   fprintf (stderr,
48            "GrAtscConvert2xTo20: input freq = %g\n", getInputSamplingFrequencyN(0));
49   fprintf (stderr,
50            "GrAtscConvert2xTo20: DEC_RATIO = %g\n", DEC_RATIO);
51   fprintf (stderr,
52            "GrAtscConvert2xTo20: argument to setSamplingFrequency = %g\n",
53            getInputSamplingFrequencyN(0) / DEC_RATIO);
54   
55   int   r;
56   r = setSamplingFrequency (getInputSamplingFrequencyN (0) / DEC_RATIO);
57
58   fprintf (stderr, "GrAtscConvert2xTo20: result = %d\n", r);
59
60   fprintf (stderr, "GrAtscConvert2xTo20: getSamplingFrequency = %g\n",
61            getSamplingFrequency ());
62 }
63
64
65 int
66 GrAtscConvert2xTo20::forecast (VrSampleRange output,
67                                VrSampleRange inputs[])
68 {
69   assert (numberInputs == 1);   // I hate these free references to
70                                 // superclass's instance variables...
71
72   inputs[0].index = d_next_input;
73   inputs[0].size =
74     ((long unsigned int) (output.size * DEC_RATIO) + history - 1);
75
76   return 0;
77 }
78
79 int
80 GrAtscConvert2xTo20::work (VrSampleRange output, void *ao[],
81                            VrSampleRange inputs[], void *ai[])
82 {
83   float *in = ((float **) ai)[0];
84   float *out = ((float **) ao)[0];
85
86   sync (output.index);
87
88   unsigned long  si = 0;                // source index
89   unsigned long  oi = 0;                // output index
90   double frac_part = d_frac_part;
91
92   for (oi = 0; oi < output.size; oi++){
93     assert (si + d_interp.ntaps () < inputs[0].size);
94     out[oi] = d_interp.interpolate (&in[si], (1. - frac_part));
95
96     double s = frac_part + DEC_RATIO;
97     double float_incr = floor (s);
98     frac_part = s - float_incr;
99     int incr = (int) float_incr;
100     si += incr;
101   }
102   
103   d_next_input += si;
104   d_frac_part = frac_part;
105   return output.size;
106 }