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.
ncof.cpp
Go to the documentation of this file.
1 // Copyright (C) 2016 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include <QtGlobal>
19 #include <stdio.h>
20 #define _USE_MATH_DEFINES
21 #include <math.h>
22 #include "dsp/ncof.h"
23 
24 #undef M_PI
25 #define M_PI 3.14159265358979323846
26 
28 bool NCOF::m_tableInitialized = false;
30 
32 {
33  if(m_tableInitialized) {
34  return;
35  }
36 
37  for(int i = 0; i <= TableSize; i++) {
38  m_table[i] = cos((2.0 * M_PI * i) / TableSize);
39  }
40 
41  m_tableInitialized = true;
42 }
43 
45 {
46  initTable();
47  m_phase = 0.0f;
48  m_phaseIncrement = 0.0f;
49 }
50 
51 void NCOF::setFreq(Real freq, Real sampleRate)
52 {
53  m_phaseIncrement = (freq * TableSize) / sampleRate;
54  qDebug("NCOF::setFreq: freq: %f m_phaseIncrement: %f", freq, m_phaseIncrement);
55 }
56 
57 float NCOF::next()
58 {
59  int phase = nextPhase();
60  return m_table[phase];
61 }
62 
64 {
65  int phase = nextPhase();
66  return Complex(m_table[phase], -m_table[(phase + TableSize / 4) % TableSize]);
67 }
68 
69 Complex NCOF::nextIQ(float imbalance)
70 {
71  int phase = nextPhase();
72  int phaseQ = imbalance < 0.0 ? phase + (int) (imbalance*TableSize) : phase;
73  int phaseI = imbalance < 0.0 ? phase : phase + (int) (imbalance*TableSize);
74  return Complex(m_table[phaseI % TableSize], -m_table[(phaseQ + TableSize / 4) % TableSize]);
75 }
76 
78 {
79  int phase = nextPhase();
80  return Complex(-m_table[(phase + TableSize / 4) % TableSize], m_table[phase]);
81 }
82 
83 float NCOF::get()
84 {
85  return m_table[(int) m_phase];
86 }
87 
89 {
90  return Complex(m_table[(int) m_phase], -m_table[((int) m_phase + TableSize / 4) % TableSize]);
91 }
92 
94 {
95  c.real(m_table[(int) m_phase]);
96  c.imag(-m_table[((int) m_phase + TableSize / 4) % TableSize]);
97 }
98 
100 {
101  return Complex(-m_table[((int) m_phase + TableSize / 4) % TableSize], m_table[(int) m_phase]);
102 }
103 
105 {
106  c.imag(m_table[(int) m_phase]);
107  c.real(-m_table[((int) m_phase + TableSize / 4) % TableSize]);
108 }
Real m_phaseIncrement
Definition: ncof.h:35
Fixed< IntType, IntBits > cos(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2271
static float m_tableSizeLimit
Definition: ncof.h:31
Real get()
Return current real sample (no phase increment)
Definition: ncof.cpp:83
Complex nextIQ()
Return next complex sample.
Definition: ncof.cpp:63
static void initTable()
Definition: ncof.cpp:31
int nextPhase()
Definition: ncof.h:44
int32_t i
Definition: decimators.h:244
#define M_PI
Definition: ncof.cpp:25
Complex nextQI()
Return next complex sample (reversed)
Definition: ncof.cpp:77
static bool m_tableInitialized
Definition: ncof.h:30
Real m_phase
Definition: ncof.h:36
void setFreq(Real freq, Real sampleRate)
Definition: ncof.cpp:51
NCOF()
Definition: ncof.cpp:44
static Real m_table[TableSize+1]
Definition: ncof.h:29
Real next()
Return next real sample.
Definition: ncof.cpp:57
std::complex< Real > Complex
Definition: dsptypes.h:43
float Real
Definition: dsptypes.h:42
Complex getQI()
Return current complex sample (no phase increment, reversed)
Definition: ncof.cpp:99
Complex getIQ()
Return current complex sample (no phase increment)
Definition: ncof.cpp:88