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.
nco.cpp
Go to the documentation of this file.
1 // Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
3 // written by Christian Daniel //
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 #include <QtGlobal>
20 #include <stdio.h>
21 #define _USE_MATH_DEFINES
22 #include <math.h>
23 #include "dsp/nco.h"
24 
25 #undef M_PI
26 #define M_PI 3.14159265358979323846
27 
29 bool NCO::m_tableInitialized = false;
30 
32 {
34  return;
35 
36  for(int i = 0; i < TableSize; i++)
37  m_table[i] = cos((2.0 * M_PI * i) / TableSize);
38 
39  m_tableInitialized = true;
40 }
41 
43 {
44  initTable();
45  m_phase = 0;
46  m_phaseIncrement = 0;
47 }
48 
49 void NCO::setFreq(Real freq, Real sampleRate)
50 {
51  m_phaseIncrement = (freq * TableSize) / sampleRate;
52  qDebug("NCO freq: %f phase inc %d", freq, m_phaseIncrement);
53 }
54 
55 float NCO::next()
56 {
57  nextPhase();
58  return m_table[m_phase];
59 }
60 
62 {
63  nextPhase();
64  return Complex(m_table[m_phase], -m_table[(m_phase + TableSize / 4) % TableSize]);
65 }
66 
68 {
69  nextPhase();
71 }
72 
74 {
75  nextPhase();
76  Real x = i;
77  Real y = q;
78  const Real& u = m_table[m_phase];
79  const Real& v = -m_table[(m_phase + TableSize / 4) % TableSize];
80  i = x*u - y*v;
81  q = x*v + y*u;
82 }
83 
84 float NCO::get()
85 {
86  return m_table[m_phase];
87 }
88 
90 {
91  return Complex(m_table[m_phase], -m_table[(m_phase + TableSize / 4) % TableSize]);
92 }
93 
95 {
96  c.real(m_table[m_phase]);
97  c.imag(-m_table[(m_phase + TableSize / 4) % TableSize]);
98 }
99 
101 {
102  return Complex(-m_table[(m_phase + TableSize / 4) % TableSize], m_table[m_phase]);
103 }
104 
106 {
107  c.imag(m_table[m_phase]);
108  c.real(-m_table[(m_phase + TableSize / 4) % TableSize]);
109 }
void nextPhase()
Definition: nco.h:44
Complex nextIQ()
Return next complex sample.
Definition: nco.cpp:61
Fixed< IntType, IntBits > cos(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2271
static void initTable()
Definition: nco.cpp:31
void nextIQMul(Real &i, Real &q)
multiply I,Q separately with next sample
Definition: nco.cpp:73
Complex nextQI()
Return next complex sample (reversed)
Definition: nco.cpp:67
int m_phase
Definition: nco.h:36
static bool m_tableInitialized
Definition: nco.h:31
Real next()
Return next real sample.
Definition: nco.cpp:55
int32_t i
Definition: decimators.h:244
void setFreq(Real freq, Real sampleRate)
Definition: nco.cpp:49
Complex getQI()
Return current complex sample (no phase increment, reversed)
Definition: nco.cpp:100
Complex getIQ()
Return current complex sample (no phase increment)
Definition: nco.cpp:89
static Real m_table[TableSize]
Definition: nco.h:30
std::complex< Real > Complex
Definition: dsptypes.h:43
#define M_PI
Definition: nco.cpp:26
float Real
Definition: dsptypes.h:42
Real get()
Return current real sample (no phase increment)
Definition: nco.cpp:84
NCO()
Definition: nco.cpp:42
int m_phaseIncrement
Definition: nco.h:35