Merge commit 'v3.3.0' into upstream
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_fmdet_cf.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 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 3, 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., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <gr_fmdet_cf.h>
29 #include <gr_io_signature.h>
30 #include <math.h>
31 #include <gr_math.h>
32
33 #define M_TWOPI (2*M_PI)
34
35 gr_fmdet_cf_sptr
36 gr_make_fmdet_cf (float samplerate, float freq_low, float freq_high, float scl)
37 {
38   return gr_fmdet_cf_sptr (new gr_fmdet_cf (samplerate, freq_low, freq_high, scl));
39 }
40
41 gr_fmdet_cf::gr_fmdet_cf (float samplerate, float freq_low, float freq_high, float scl)
42   : gr_sync_block ("fmdet_cf",
43                    gr_make_io_signature (1, 1, sizeof (gr_complex)),
44                    gr_make_io_signature (1, 1, sizeof (float))),
45     d_S1(0.1),d_S2(0.1),
46     d_S3(0.1),d_S4(0.1)
47 {
48   const float h[]={0.003118678733, -0.012139843428,  0.027270898036, -0.051318579352,
49              0.090406910552, -0.162926865366,  0.361885392563, 0.000000000000,
50              -0.361885392563,  0.162926865366, -0.090406910552,  0.051318579352,
51              -0.027270898036,  0.012139843428, -0.003118678733};
52
53
54
55
56   float delta;
57   std::vector<float> taps(15);
58   
59   d_freqhi = freq_high;
60   d_freqlo = freq_low;
61   delta = (d_freqhi - d_freqlo);
62   d_scl = scl;
63   d_bias = 0.5*scl*(d_freqhi+d_freqlo)/delta;
64   for (int i=0;i<15;i++) taps[i] = h[i];
65   //  d_filter = gr_fir_util::create_gr_fir_ccf(taps);
66   
67 }
68
69 int
70 gr_fmdet_cf::work (int noutput_items,
71                    gr_vector_const_void_star &input_items,
72                    gr_vector_void_star &output_items)
73 {
74   const gr_complex *iptr = (gr_complex *) input_items[0];
75   float *optr = (float *) output_items[0];
76   //  const gr_complex *scaleiptr = (gr_complex *) input_items[0];
77
78   int   size = noutput_items;
79
80   gr_complex Sdot,S0,S1=d_S1,S2=d_S2,S3=d_S3,S4=d_S4;
81   float d_8 = 8.0;
82
83   while (size-- > 0) {
84     S0=*iptr++;
85
86
87     Sdot = d_scl * (-S0+d_8*S1-d_8*S1+S4);
88
89     d_freq = (S2.real()*Sdot.imag()-S2.imag()*Sdot.real())/
90       (S2.real()*S2.real()+S2.imag()*S2.imag());
91
92     S4=S3;
93     S3=S2;
94     S2=S1;
95     S1=S0;
96
97     
98     *optr++ = d_freq-d_bias;
99   }
100   d_S1=S1;
101   d_S2=S2;
102   d_S3=S3;
103   d_S4=S4;
104   return noutput_items;
105 }