SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
projector.cpp
Go to the documentation of this file.
1 // Copyright (C) 2017 F4EXB //
3 // written by Edouard Griffiths //
4 // //
5 // This program is free software; you can redistribute it and/or modify //
6 // it under the terms of the GNU General Public License as published by //
7 // the Free Software Foundation as version 3 of the License, or //
8 // (at your option) any later version. //
9 // //
10 // This program is distributed in the hope that it will be useful, //
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
13 // GNU General Public License V3 for more details. //
14 // //
15 // You should have received a copy of the GNU General Public License //
16 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
18 
19 #define _USE_MATH_DEFINES
20 #include <math.h>
21 #include "projector.h"
22 
24  m_projectionType(projectionType),
25  m_prevArg(0.0f),
26  m_cache(0),
27  m_cacheMaster(true)
28 {
29 }
30 
32 {
33 }
34 
36 {
37  Real v;
38 
39  if ((m_cache) && !m_cacheMaster) {
40  return m_cache[(int) m_projectionType];
41  }
42  else
43  {
44  switch (m_projectionType)
45  {
46  case ProjectionImag:
47  v = s.m_imag / SDR_RX_SCALEF;
48  break;
49  case ProjectionMagLin:
50  {
51  Real re = s.m_real / SDR_RX_SCALEF;
52  Real im = s.m_imag / SDR_RX_SCALEF;
53  Real magsq = re*re + im*im;
54  v = std::sqrt(magsq);
55  }
56  break;
57  case ProjectionMagSq:
58  {
59  Real re = s.m_real / SDR_RX_SCALEF;
60  Real im = s.m_imag / SDR_RX_SCALEF;
61  v = re*re + im*im;
62  }
63  break;
64  case ProjectionMagDB:
65  {
66  Real re = s.m_real / SDR_RX_SCALEF;
67  Real im = s.m_imag / SDR_RX_SCALEF;
68  Real magsq = re*re + im*im;
69  v = log10f(magsq) * 10.0f;
70  }
71  break;
72  case ProjectionPhase:
73  v = std::atan2((float) s.m_imag, (float) s.m_real) / M_PI;
74  break;
75  case ProjectionDPhase:
76  {
77  Real curArg = std::atan2((float) s.m_imag, (float) s.m_real);
78  Real dPhi = (curArg - m_prevArg) / M_PI;
79  m_prevArg = curArg;
80 
81  if (dPhi < -1.0f) {
82  dPhi += 2.0f;
83  } else if (dPhi > 1.0f) {
84  dPhi -= 2.0f;
85  }
86 
87  v = dPhi;
88  }
89  break;
90  case ProjectionBPSK:
91  {
92  Real arg = std::atan2((float) s.m_imag, (float) s.m_real);
93  v = normalizeAngle(2*arg) / (2.0*M_PI); // generic estimation around 0
94  // mapping on 2 symbols
95  if (arg < -M_PI/2) {
96  v -= 1.0/2;
97  } else if (arg < M_PI/2) {
98  v += 1.0/2;
99  } else if (arg < M_PI) {
100  v -= 1.0/2;
101  }
102  }
103  break;
104  case ProjectionQPSK:
105  {
106  Real arg = std::atan2((float) s.m_imag, (float) s.m_real);
107  v = normalizeAngle(4*arg) / (4.0*M_PI); // generic estimation around 0
108  // mapping on 4 symbols
109  if (arg < -3*M_PI/4) {
110  v -= 3.0/4;
111  } else if (arg < -M_PI/4) {
112  v -= 1.0/4;
113  } else if (arg < M_PI/4) {
114  v += 1.0/4;
115  } else if (arg < 3*M_PI/4) {
116  v += 3.0/4;
117  } else if (arg < M_PI) {
118  v -= 3.0/4;
119  }
120  }
121  break;
122  case Projection8PSK:
123  {
124  Real arg = std::atan2((float) s.m_imag, (float) s.m_real);
125  v = normalizeAngle(8*arg) / (8.0*M_PI); // generic estimation around 0
126  // mapping on 8 symbols
127  if (arg < -7*M_PI/8) {
128  v -= 7.0/8;
129  } else if (arg < -5*M_PI/8) {
130  v -= 5.0/8;
131  } else if (arg < -3*M_PI/8) {
132  v -= 3.0/8;
133  } else if (arg < -M_PI/8) {
134  v -= 1.0/8;
135  } else if (arg < M_PI/8) {
136  v += 1.0/8;
137  } else if (arg < 3*M_PI/8) {
138  v += 3.0/8;
139  } else if (arg < 5*M_PI/8) {
140  v += 5.0/8;
141  } else if (arg < 7*M_PI/8) {
142  v += 7.0/8;
143  } else if (arg < M_PI) {
144  v -= 7.0/8;
145  }
146  }
147  break;
148  case Projection16PSK:
149  {
150  Real arg = std::atan2((float) s.m_imag, (float) s.m_real);
151  v = normalizeAngle(16*arg) / (16.0*M_PI); // generic estimation around 0
152  // mapping on 16 symbols
153  if (arg < -15*M_PI/16) {
154  v -= 15.0/16;
155  } else if (arg < -13*M_PI/16) {
156  v -= 13.0/6;
157  } else if (arg < -11*M_PI/16) {
158  v -= 11.0/16;
159  } else if (arg < -9*M_PI/16) {
160  v -= 9.0/16;
161  } else if (arg < -7*M_PI/16) {
162  v -= 7.0/16;
163  } else if (arg < -5*M_PI/16) {
164  v -= 5.0/16;
165  } else if (arg < -3*M_PI/16) {
166  v -= 3.0/16;
167  } else if (arg < -M_PI/16) {
168  v -= 1.0/16;
169  } else if (arg < M_PI/16) {
170  v += 1.0/16;
171  } else if (arg < 3.0*M_PI/16) {
172  v += 3.0/16;
173  } else if (arg < 5.0*M_PI/16) {
174  v += 5.0/16;
175  } else if (arg < 7.0*M_PI/16) {
176  v += 7.0/16;
177  } else if (arg < 9.0*M_PI/16) {
178  v += 9.0/16;
179  } else if (arg < 11.0*M_PI/16) {
180  v += 11.0/16;
181  } else if (arg < 13.0*M_PI/16) {
182  v += 13.0/16;
183  } else if (arg < 15.0*M_PI/16) {
184  v += 15.0/16;
185  } else if (arg < M_PI) {
186  v -= 15.0/16;
187  }
188  }
189  break;
190  case ProjectionReal:
191  default:
192  v = s.m_real / SDR_RX_SCALEF;
193  break;
194  }
195 
196  if (m_cache) {
197  m_cache[(int) m_projectionType] = v;
198  }
199 
200  return v;
201  }
202 }
203 
205 {
206  while (angle <= -M_PI) {
207  angle += 2.0*M_PI;
208  }
209  while (angle > M_PI) {
210  angle -= 2.0*M_PI;
211  }
212  return angle;
213 }
214 
215 
ProjectionType m_projectionType
Definition: projector.h:53
Calculate logarithmic (dB) of squared magnitude.
Definition: projector.h:31
Projector(ProjectionType projectionType)
Definition: projector.cpp:23
ProjectionType
Definition: projector.h:25
Phase comparator 8-PSK evaluation.
Definition: projector.h:36
#define M_PI
Definition: rdsdemod.cpp:27
Phase comparator 16-PSK evaluation.
Definition: projector.h:37
Fixed< IntType, IntBits > arg(const std::complex< Fixed< IntType, IntBits > > &val)
Definition: fixed.h:2401
#define SDR_RX_SCALEF
Definition: dsptypes.h:33
Phase comparator BPSK evaluation.
Definition: projector.h:34
Calculate phase.
Definition: projector.h:32
FixReal m_real
Definition: dsptypes.h:64
Real * m_cache
Definition: projector.h:55
Calculate linear magnitude or modulus.
Definition: projector.h:29
Fixed< IntType, IntBits > sqrt(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2283
Calculate linear squared magnitude or power.
Definition: projector.h:30
Real m_prevArg
Definition: projector.h:54
Calculate phase derivative i.e. instantaneous frequency scaled to sample rate.
Definition: projector.h:33
bool m_cacheMaster
Definition: projector.h:56
Extract imaginary part.
Definition: projector.h:28
FixReal m_imag
Definition: dsptypes.h:65
static Real normalizeAngle(Real angle)
Definition: projector.cpp:204
float Real
Definition: dsptypes.h:42
Phase comparator QPSK evaluation.
Definition: projector.h:35
Extract real part.
Definition: projector.h:27
Real run(const Sample &s)
Definition: projector.cpp:35