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.
fftcorr.cpp
Go to the documentation of this file.
1 // Copyright (C) 2018 F4EXB //
3 // written by Edouard Griffiths //
4 // //
5 // FFT based cross correlation //
6 // //
7 // See: http://liquidsdr.org/blog/pll-howto/ //
8 // Fixed filter registers saturation //
9 // Added order for PSK locking. This brilliant idea actually comes from this //
10 // post: https://www.dsprelated.com/showthread/comp.dsp/36356-1.php //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation as version 3 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License V3 for more details. //
21 // //
22 // You should have received a copy of the GNU General Public License //
23 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
25 
26 #include <algorithm>
27 #include "fftcorr.h"
28 
30 {
31  fftA = new g_fft<float>(flen);
32  fftB = new g_fft<float>(flen);
33 
34  dataA = new cmplx[flen];
35  dataB = new cmplx[flen];
36  dataBj = new cmplx[flen];
37  dataP = new cmplx[flen];
38 
39  std::fill(dataA, dataA+flen, 0);
40  std::fill(dataB, dataB+flen, 0);
41 
42  inptrA = 0;
43  inptrB = 0;
44  outptr = 0;
45 }
46 
47 fftcorr::fftcorr(int len) : flen(len), flen2(len>>1)
48 {
49  init_fft();
50 }
51 
53 {
54  delete fftA;
55  delete fftB;
56  delete[] dataA;
57  delete[] dataB;
58  delete[] dataBj;
59  delete[] dataP;
60 }
61 
62 int fftcorr::run(const cmplx& inA, const cmplx* inB, cmplx **out)
63 {
64  dataA[inptrA++] = inA;
65 
66  if (inB) {
67  dataB[inptrB++] = *inB;
68  }
69 
70  if (inptrA < flen2) {
71  return 0;
72  }
73 
75 
76  if (inB) {
78  }
79 
80  if (inB) {
81  std::transform(dataB, dataB+flen, dataBj, [](const cmplx& c) -> cmplx { return std::conj(c); });
82  } else {
83  std::transform(dataA, dataA+flen, dataBj, [](const cmplx& c) -> cmplx { return std::conj(c); });
84  }
85 
86  std::transform(dataA, dataA+flen, dataBj, dataP, [](const cmplx& a, const cmplx& b) -> cmplx { return a*b; });
87 
89 
90  std::fill(dataA, dataA+flen, 0);
91  inptrA = 0;
92 
93  if (inB)
94  {
95  std::fill(dataB, dataB+flen, 0);
96  inptrB = 0;
97  }
98 
99  *out = dataP;
100  return flen2;
101 }
102 
103 const fftcorr::cmplx& fftcorr::run(const cmplx& inA, const cmplx* inB)
104 {
105  cmplx *dummy;
106 
107  if (run(inA, inB, &dummy)) {
108  outptr = 0;
109  }
110 
111  return dataP[outptr++];
112 }
cmplx * dataBj
Definition: fftcorr.h:50
int inptrA
Definition: fftcorr.h:52
int flen2
half FFT length
Definition: fftcorr.h:45
void InverseComplexFFT(std::complex< FFT_TYPE > *buf)
Definition: gfft.h:3325
void ComplexFFT(std::complex< FFT_TYPE > *buf)
Definition: gfft.h:3309
int flen
FFT length.
Definition: fftcorr.h:44
~fftcorr()
Definition: fftcorr.cpp:52
std::complex< float > cmplx
Definition: fftcorr.h:35
g_fft< float > * fftA
Definition: fftcorr.h:46
void init_fft()
Definition: fftcorr.cpp:29
cmplx * dataA
Definition: fftcorr.h:48
g_fft< float > * fftB
Definition: fftcorr.h:47
cmplx * dataP
Definition: fftcorr.h:51
fftcorr(int len)
Definition: fftcorr.cpp:47
int inptrB
Definition: fftcorr.h:53
int run(const cmplx &inA, const cmplx *inB, cmplx **out)
if inB = 0 then run auto-correlation
Definition: fftcorr.cpp:62
cmplx * dataB
Definition: fftcorr.h:49
int outptr
Definition: fftcorr.h:54