Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_mpsk_receiver_cc.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2007 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 #ifndef INCLUDED_GR_MPSK_RECEIVER_CC_H
24 #define INCLUDED_GR_MPSK_RECEIVER_CC_H
25
26 #include <gr_block.h>
27 #include <gr_complex.h>
28 #include <fstream>
29
30 class gri_mmse_fir_interpolator_cc;
31
32 class gr_mpsk_receiver_cc;
33 typedef boost::shared_ptr<gr_mpsk_receiver_cc> gr_mpsk_receiver_cc_sptr;
34
35 // public constructor
36 gr_mpsk_receiver_cc_sptr 
37 gr_make_mpsk_receiver_cc (unsigned int M, float theta, 
38                           float alpha, float beta,
39                           float fmin, float fmax,
40                           float mu, float gain_mu, 
41                           float omega, float gain_omega, float omega_rel);
42
43 /*!
44  * \brief This block takes care of receiving M-PSK modulated signals through phase, frequency, and symbol
45  * synchronization. 
46  * \ingroup sync_blk
47  * \ingroup demod_blk
48  *
49  * This block takes care of receiving M-PSK modulated signals through phase, frequency, and symbol
50  * synchronization. It performs carrier frequency and phase locking as well as symbol timing recovery. 
51  * It works with (D)BPSK, (D)QPSK, and (D)8PSK as tested currently. It should also work for OQPSK and 
52  * PI/4 DQPSK.
53  *
54  * The phase and frequency synchronization are based on a Costas loop that finds the error of the incoming
55  * signal point compared to its nearest constellation point. The frequency and phase of the NCO are 
56  * updated according to this error. There are optimized phase error detectors for BPSK and QPSK, but 8PSK
57  * is done using a brute-force computation of the constellation points to find the minimum.
58  *
59  * The symbol synchronization is done using a modified Mueller and Muller circuit from the paper:
60  * 
61  *    G. R. Danesfahani, T.G. Jeans, "Optimisation of modified Mueller and Muller 
62  *    algorithm,"  Electronics Letters, Vol. 31, no. 13,  22 June 1995, pp. 1032 - 1033.
63  *
64  * This circuit interpolates the downconverted sample (using the NCO developed by the Costas loop)
65  * every mu samples, then it finds the sampling error based on this and the past symbols and the decision
66  * made on the samples. Like the phase error detector, there are optimized decision algorithms for BPSK
67  * and QPKS, but 8PSK uses another brute force computation against all possible symbols. The modifications
68  * to the M&M used here reduce self-noise.
69  *
70  */
71
72 class gr_mpsk_receiver_cc : public gr_block
73 {
74  public:
75   ~gr_mpsk_receiver_cc ();
76   void forecast(int noutput_items, gr_vector_int &ninput_items_required);
77   int general_work (int noutput_items,
78                     gr_vector_int &ninput_items,
79                     gr_vector_const_void_star &input_items,
80                     gr_vector_void_star &output_items);
81
82
83   // Member functions related to the symbol tracking portion of the receiver
84   //! (M&M) Returns current value of mu
85   float mu() const { return d_mu;}
86
87   //! (M&M) Returns current value of omega
88   float omega() const { return d_omega;}
89
90   //! (M&M) Returns mu gain factor
91   float gain_mu() const { return d_gain_mu;}
92
93   //! (M&M) Returns omega gain factor
94   float gain_omega() const { return d_gain_omega;}
95
96   //! (M&M) Sets value of mu
97   void set_mu (float mu) { d_mu = mu; }
98   
99   //! (M&M) Sets value of omega and its min and max values 
100   void set_omega (float omega) { 
101     d_omega = omega;
102     d_min_omega = omega*(1.0 - d_omega_rel);
103     d_max_omega = omega*(1.0 + d_omega_rel);
104     d_omega_mid = 0.5*(d_min_omega+d_max_omega);
105   }
106
107   //! (M&M) Sets value for mu gain factor
108   void set_gain_mu (float gain_mu) { d_gain_mu = gain_mu; }
109
110   //! (M&M) Sets value for omega gain factor
111   void set_gain_omega (float gain_omega) { d_gain_omega = gain_omega; }
112
113
114
115   // Member function related to the phase/frequency tracking portion of the receiver
116   //! (CL) Returns the value for alpha (the phase gain term)
117   float alpha() const { return d_alpha; }
118   
119   //! (CL) Returns the value of beta (the frequency gain term)
120   float beta() const { return d_beta; }
121
122   //! (CL) Returns the current value of the frequency of the NCO in the Costas loop
123   float freq() const { return d_freq; }
124
125   //! (CL) Returns the current value of the phase of the NCO in the Costal loop
126   float phase() const { return d_phase; }
127
128   //! (CL) Sets the value for alpha (the phase gain term)
129   void set_alpha(float alpha) { d_alpha = alpha; }
130   
131   //! (CL) Setss the value of beta (the frequency gain term)
132   void set_beta(float beta) { d_beta = beta; }
133
134   //! (CL) Sets the current value of the frequency of the NCO in the Costas loop
135   void set_freq(float freq) { d_freq = freq; }
136
137   //! (CL) Setss the current value of the phase of the NCO in the Costal loop
138   void set_phase(float phase) { d_phase = phase; }
139
140
141 protected:
142
143  /*!
144    * \brief Constructor to synchronize incoming M-PSK symbols
145    *
146    * \param M           modulation order of the M-PSK modulation
147    * \param theta       any constant phase rotation from the real axis of the constellation
148    * \param alpha       gain parameter to adjust the phase in the Costas loop (~0.01)
149    * \param beta        gain parameter to adjust the frequency in the Costas loop (~alpha^2/4)  
150    * \param fmin        minimum normalized frequency value the loop can achieve
151    * \param fmax        maximum normalized frequency value the loop can achieve
152    * \param mu          initial parameter for the interpolator [0,1]
153    * \param gain_mu     gain parameter of the M&M error signal to adjust mu (~0.05)
154    * \param omega       initial value for the number of symbols between samples (~number of samples/symbol)
155    * \param gain_omega  gain parameter to adjust omega based on the error (~omega^2/4)
156    * \param omega_rel   sets the maximum (omega*(1+omega_rel)) and minimum (omega*(1+omega_rel)) omega (~0.005)
157    *
158    * The constructor also chooses which phase detector and decision maker to use in the work loop based on the
159    * value of M.
160    */
161   gr_mpsk_receiver_cc (unsigned int M, float theta, 
162                        float alpha, float beta,
163                        float fmin, float fmax,
164                        float mu, float gain_mu, 
165                        float omega, float gain_omega, float omega_rel);
166
167   void make_constellation();
168   void mm_sampler(const gr_complex symbol);
169   void mm_error_tracking(gr_complex sample);
170   void phase_error_tracking(gr_complex sample);
171
172
173 /*!
174    * \brief Phase error detector for MPSK modulations.
175    *
176    * \param sample   the I&Q sample from which to determine the phase error
177    *
178    * This function determines the phase error for any MPSK signal by creating a set of PSK constellation points
179    * and doing a brute-force search to see which point minimizes the Euclidean distance. This point is then used
180    * to derotate the sample to the real-axis and a atan (using the fast approximation function) to determine the
181    * phase difference between the incoming sample and the real constellation point
182    *
183    * This should be cleaned up and made more efficient.
184    *
185    * \returns the approximated phase error.
186  */
187   float phase_error_detector_generic(gr_complex sample) const; // generic for M but more costly
188
189  /*!
190    * \brief Phase error detector for BPSK modulation.
191    *
192    * \param sample   the I&Q sample from which to determine the phase error
193    *
194    * This function determines the phase error using a simple BPSK phase error detector by multiplying the real
195    * and imaginary (the error signal) components together. As the imaginary part goes to 0, so does this error.
196    *
197    * \returns the approximated phase error.
198  */
199   float phase_error_detector_bpsk(gr_complex sample) const;    // optimized for BPSK
200
201  /*!
202    * \brief Phase error detector for QPSK modulation.
203    *
204    * \param sample   the I&Q sample from which to determine the phase error
205    *
206    * This function determines the phase error using the limiter approach in a standard 4th order Costas loop
207    *
208    * \returns the approximated phase error.
209  */
210   float phase_error_detector_qpsk(gr_complex sample) const;
211
212
213
214  /*!
215    * \brief Decision maker for a generic MPSK constellation.
216    *
217    * \param sample   the baseband I&Q sample from which to make the decision
218    *
219    * This decision maker is a generic implementation that does a brute-force search 
220    * for the constellation point that minimizes the error between it and the incoming signal.
221    *
222    * \returns the index to d_constellation that minimizes the error/
223  */
224   unsigned int decision_generic(gr_complex sample) const;
225
226
227  /*!
228    * \brief Decision maker for BPSK constellation.
229    *
230    * \param sample   the baseband I&Q sample from which to make the decision
231    *
232    * This decision maker is a simple slicer function that makes a decision on the symbol based on its
233    * placement on the real axis of greater than 0 or less than 0; the quadrature component is always 0.
234    *
235    * \returns the index to d_constellation that minimizes the error/
236  */
237   unsigned int decision_bpsk(gr_complex sample) const;
238   
239
240  /*!
241    * \brief Decision maker for QPSK constellation.
242    *
243    * \param sample   the baseband I&Q sample from which to make the decision
244    *
245    * This decision maker is a simple slicer function that makes a decision on the symbol based on its
246    * placement versus both axes and returns which quadrant the symbol is in.
247    *
248    * \returns the index to d_constellation that minimizes the error/
249  */
250   unsigned int decision_qpsk(gr_complex sample) const;
251
252   private:
253   unsigned int d_M;
254   float        d_theta;
255
256   // Members related to carrier and phase tracking
257   float d_alpha;
258   float d_beta;
259   float d_freq, d_max_freq, d_min_freq;
260   float d_phase;
261
262 /*!
263    * \brief Decision maker function pointer 
264    *
265    * \param sample   the baseband I&Q sample from which to make the decision
266    *
267    * This is a function pointer that is set in the constructor to point to the proper decision function
268    * for the specified constellation order.
269    *
270    * \return index into d_constellation point that is the closest to the recieved sample
271  */
272   unsigned int (gr_mpsk_receiver_cc::*d_decision)(gr_complex sample) const; // pointer to decision function
273
274
275   std::vector<gr_complex> d_constellation;
276   unsigned int d_current_const_point;
277
278   // Members related to symbol timing
279   float d_mu, d_gain_mu;
280   float d_omega, d_gain_omega, d_omega_rel, d_max_omega, d_min_omega, d_omega_mid;
281   gr_complex d_p_2T, d_p_1T, d_p_0T;
282   gr_complex d_c_2T, d_c_1T, d_c_0T;
283
284  /*!
285    * \brief Phase error detector function pointer 
286    *
287    * \param sample   the I&Q sample from which to determine the phase error
288    *
289    * This is a function pointer that is set in the constructor to point to the proper phase error detector
290    * function for the specified constellation order.
291  */
292   float (gr_mpsk_receiver_cc::*d_phase_error_detector)(gr_complex sample) const;
293
294
295   //! get interpolated value
296   gri_mmse_fir_interpolator_cc  *d_interp;
297   
298   //! delay line length.
299   static const unsigned int DLLEN = 8;
300   
301   //! delay line plus some length for overflow protection
302   gr_complex d_dl[2*DLLEN] __attribute__ ((aligned(8)));
303   
304   //! index to delay line
305   unsigned int d_dl_idx;
306
307   friend gr_mpsk_receiver_cc_sptr
308   gr_make_mpsk_receiver_cc (unsigned int M, float theta,
309                             float alpha, float beta,
310                             float fmin, float fmax,
311                             float mu, float gain_mu, 
312                             float omega, float gain_omega, float omega_rel);
313 };
314
315 #endif