1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-10 18:43:28 -05:00
sdrangel/plugins/channelmimo/doa2/doa2corr.cpp

390 lines
11 KiB
C++
Raw Normal View History

2022-05-26 18:50:38 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2019-2020, 2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
2022-05-26 18:50:38 -04:00
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <functional>
#include "dsp/dspengine.h"
#include "dsp/fftfactory.h"
#include "dsp/fftengine.h"
#include "doa2corr.h"
std::complex<float> s2cNorm(const Sample& s)
{
float x = s.real() / SDR_RX_SCALEF;
float y = s.imag() / SDR_RX_SCALEF;
float m = sqrt(x*x + y*y);
return std::complex<float>{x/m, y/m};
}
Sample sFirst(const Sample& a, const Sample& b) {
(void) b;
return a;
}
Sample sSecond(const Sample& a, const Sample& b) {
(void) a;
return b;
}
Sample sSecondInv(const Sample& a, const Sample& b) {
(void) a;
return Sample{-b.real(), -b.imag()};
}
Sample invfft2s(const std::complex<float>& a) { //!< Complex float to Sample for 1 side time correlation
Sample s;
s.setReal(a.real()/2.0f);
s.setImag(a.imag()/2.0f);
return s;
}
DOA2Correlator::DOA2Correlator(int fftSize) :
2022-05-27 10:49:32 -04:00
m_corrType(DOA2Settings::CorrelationFFT),
2022-05-26 18:50:38 -04:00
m_fftSize(fftSize)
{
setPhase(0);
FFTFactory *fftFactory = DSPEngine::instance()->getFFTFactory();
m_window.create(FFTWindow::Function::Hanning, fftSize);
2022-05-29 03:59:22 -04:00
for (int i = 0; i < 2; i++) {
m_fftSequences[i] = fftFactory->getEngine(fftSize, false, &m_fft[i]);
2022-05-26 18:50:38 -04:00
}
2022-05-29 03:59:22 -04:00
m_invFFTSequence = fftFactory->getEngine(fftSize, true, &m_invFFT);
2022-05-26 18:50:38 -04:00
m_dataj = new std::complex<float>[2*fftSize]; // receives actual FFT result hence twice the data FFT size
m_tcorr.resize(fftSize);
2022-05-27 17:31:43 -04:00
m_xcorr.resize(fftSize);
2022-05-26 18:50:38 -04:00
m_tcorrSize = fftSize;
2022-05-27 17:31:43 -04:00
m_xcorrSize = fftSize;
2022-05-26 18:50:38 -04:00
}
DOA2Correlator::~DOA2Correlator()
{
FFTFactory *fftFactory = DSPEngine::instance()->getFFTFactory();
2022-05-29 03:59:22 -04:00
fftFactory->releaseEngine(m_fftSize, true, m_invFFTSequence);
2022-05-26 18:50:38 -04:00
delete[] m_dataj;
2022-05-29 03:59:22 -04:00
for (int i = 0; i < 2; i++) {
fftFactory->releaseEngine(m_fftSize, false, m_fftSequences[i]);
2022-05-26 18:50:38 -04:00
}
}
bool DOA2Correlator::performCorr(
const SampleVector& data0,
unsigned int size0,
const SampleVector& data1,
unsigned int size1
)
{
bool results = false;
if (m_phase == 0)
{
switch (m_corrType)
{
case DOA2Settings::Correlation0:
results = performOpCorr(data0, size0, data1, size1, sFirst);
break;
case DOA2Settings::Correlation1:
results = performOpCorr(data0, size0, data1, size1, sSecond);
break;
case DOA2Settings::CorrelationFFT:
results = performFFTProd(data0, size0, data1, size1);
break;
default:
break;
}
}
else if ((m_phase == -180) || (m_phase == 180))
{
2022-05-27 10:49:32 -04:00
if (m_corrType == DOA2Settings::CorrelationFFT)
2022-05-26 18:50:38 -04:00
{
if (size1 > m_data1p.size()) {
m_data1p.resize(size1);
}
std::transform(
data1.begin(),
data1.begin() + size1,
m_data1p.begin(),
[](const Sample& s) -> Sample {
return Sample{-s.real(), -s.imag()};
}
);
}
switch (m_corrType)
{
case DOA2Settings::Correlation0:
results = performOpCorr(data0, size0, data1, size1, sFirst);
break;
case DOA2Settings::Correlation1:
results = performOpCorr(data0, size0, data1, size1, sSecondInv);
break;
case DOA2Settings::CorrelationFFT:
results = performFFTProd(data0, size0, m_data1p, size1);
break;
default:
break;
}
}
else
{
if (size1 > m_data1p.size()) {
m_data1p.resize(size1);
}
std::transform(
data1.begin(),
data1.begin() + size1,
m_data1p.begin(),
[this](const Sample& s) -> Sample {
Sample t;
int64_t sx = s.real();
int64_t sy = s.imag();
int64_t x = sx*m_cos + sy*m_sin;
int64_t y = sy*m_cos - sx*m_sin;
t.setReal(x>>(SDR_RX_SAMP_SZ-1));
t.setImag(y>>(SDR_RX_SAMP_SZ-1));
return t;
}
);
switch (m_corrType)
{
case DOA2Settings::Correlation0:
results = performOpCorr(data0, size0, m_data1p, size1, sFirst);
break;
case DOA2Settings::Correlation1:
results = performOpCorr(data0, size0, m_data1p, size1, sSecond);
break;
case DOA2Settings::CorrelationFFT:
results = performFFTProd(data0, size0, m_data1p, size1);
break;
default:
break;
}
}
return results;
}
bool DOA2Correlator::performOpCorr(
const SampleVector& data0,
unsigned int size0,
const SampleVector& data1,
unsigned int size1,
Sample sampleOp(const Sample& a, const Sample& b)
)
{
unsigned int size = std::min(size0, size1);
adjustTCorrSize(size);
2022-05-27 17:31:43 -04:00
adjustXCorrSize(size);
2022-05-26 18:50:38 -04:00
std::transform(
data0.begin(),
data0.begin() + size,
data1.begin(),
m_tcorr.begin(),
sampleOp
);
m_processed = size;
m_remaining[0] = size0 - size;
m_remaining[1] = size1 - size;
return true;
}
bool DOA2Correlator::performFFTProd(
const SampleVector& data0,
unsigned int size0,
const SampleVector& data1,
unsigned int size1
)
{
unsigned int size = std::min(size0, size1);
int nfft = 0;
SampleVector::const_iterator begin0 = data0.begin();
SampleVector::const_iterator begin1 = data1.begin();
adjustTCorrSize(size);
2022-05-27 17:31:43 -04:00
adjustXCorrSize(size);
2022-05-26 18:50:38 -04:00
while (size >= m_fftSize)
{
// FFT[0]
std::transform(
begin0,
begin0 + m_fftSize,
2022-05-29 03:59:22 -04:00
m_fft[0]->in(),
2022-05-26 18:50:38 -04:00
s2cNorm
);
2022-05-29 03:59:22 -04:00
m_window.apply(m_fft[0]->in());
m_fft[0]->transform();
2022-05-26 18:50:38 -04:00
// FFT[1]
std::transform(
begin1,
begin1 + m_fftSize,
2022-05-29 03:59:22 -04:00
m_fft[1]->in(),
2022-05-26 18:50:38 -04:00
s2cNorm
);
2022-05-29 03:59:22 -04:00
m_window.apply(m_fft[1]->in());
m_fft[1]->transform();
2022-05-26 18:50:38 -04:00
// conjugate FFT[1]
std::transform(
2022-05-29 03:59:22 -04:00
m_fft[1]->out(),
m_fft[1]->out() + m_fftSize,
2022-05-26 18:50:38 -04:00
m_dataj,
[](const std::complex<float>& c) -> std::complex<float> {
return std::conj(c);
}
);
// product of FFT[1]* with FFT[0] and store in both results
std::transform(
2022-05-29 03:59:22 -04:00
m_fft[0]->out(),
m_fft[0]->out() + m_fftSize,
2022-05-26 18:50:38 -04:00
m_dataj,
2022-05-29 03:59:22 -04:00
m_invFFT->in(),
2022-05-26 18:50:38 -04:00
[this](std::complex<float>& a, const std::complex<float>& b) -> std::complex<float> {
return (a*b);
}
);
2022-05-27 17:31:43 -04:00
// copy to complex vector for DOA with re-orderong
std::copy(
2022-05-29 03:59:22 -04:00
m_invFFT->in(),
m_invFFT->in() + m_fftSize/2,
2022-05-27 17:31:43 -04:00
m_xcorr.begin() + nfft*m_fftSize + m_fftSize/2
2022-05-26 18:50:38 -04:00
);
2022-05-27 17:31:43 -04:00
std::copy(
2022-05-29 03:59:22 -04:00
m_invFFT->in() + m_fftSize/2,
m_invFFT->in() + m_fftSize,
2022-05-27 17:31:43 -04:00
m_xcorr.begin() + nfft*m_fftSize
);
// convert and scale to FFT size for scope time domain display
std::transform(
m_xcorr.begin() + nfft*m_fftSize,
m_xcorr.begin() + nfft*m_fftSize + m_fftSize,
2022-05-26 18:50:38 -04:00
m_tcorr.begin() + nfft*m_fftSize,
[](const std::complex<float>& a) -> Sample {
Sample s;
s.setReal(a.real()/2.0f);
s.setImag(a.imag()/2.0f);
return s;
}
);
2022-05-27 17:31:43 -04:00
// copy product to time domain - re-order, convert and scale to FFT size
// std::transform(
2022-05-29 03:59:22 -04:00
// m_invFFT->in(),
// m_invFFT->in() + m_fftSize/2,
2022-05-27 17:31:43 -04:00
// m_tcorr.begin() + nfft*m_fftSize + m_fftSize/2,
// [](const std::complex<float>& a) -> Sample {
// Sample s;
// s.setReal(a.real()/2.0f);
// s.setImag(a.imag()/2.0f);
// return s;
// }
// );
// std::transform(
2022-05-29 03:59:22 -04:00
// m_invFFT->in() + m_fftSize/2,
// m_invFFT->in() + m_fftSize,
2022-05-27 17:31:43 -04:00
// m_tcorr.begin() + nfft*m_fftSize,
// [](const std::complex<float>& a) -> Sample {
// Sample s;
// s.setReal(a.real()/2.0f);
// s.setImag(a.imag()/2.0f);
// return s;
// }
// );
2022-05-26 18:50:38 -04:00
size -= m_fftSize;
begin0 += m_fftSize;
begin1 += m_fftSize;
nfft++;
}
// update the samples counters
m_processed = nfft*m_fftSize;
m_remaining[0] = size0 - nfft*m_fftSize;
m_remaining[1] = size1 - nfft*m_fftSize;
return nfft > 0;
}
2022-05-27 17:31:43 -04:00
void DOA2Correlator::adjustTCorrSize(int size)
2022-05-26 18:50:38 -04:00
{
int nFFTSize = (size/m_fftSize)*m_fftSize;
2022-05-27 17:31:43 -04:00
if (nFFTSize > m_tcorrSize)
2022-05-26 18:50:38 -04:00
{
2022-05-27 17:31:43 -04:00
m_tcorr.resize(nFFTSize);
m_tcorrSize = nFFTSize;
2022-05-26 18:50:38 -04:00
}
}
2022-05-27 17:31:43 -04:00
void DOA2Correlator::adjustXCorrSize(int size)
2022-05-26 18:50:38 -04:00
{
int nFFTSize = (size/m_fftSize)*m_fftSize;
2022-05-27 17:31:43 -04:00
if (nFFTSize > m_xcorrSize)
2022-05-26 18:50:38 -04:00
{
2022-05-27 17:31:43 -04:00
m_xcorr.resize(nFFTSize);
m_xcorrSize = nFFTSize;
2022-05-26 18:50:38 -04:00
}
}
void DOA2Correlator::setPhase(int phase)
{
m_phase = phase;
if (phase == 0)
{
m_sin = 0;
m_cos = 1<<(SDR_RX_SAMP_SZ-1);
}
else if (phase == 90)
{
m_sin = 1<<(SDR_RX_SAMP_SZ-1);
m_cos = 0;
}
else if (phase == -90)
{
m_sin = -(1<<(SDR_RX_SAMP_SZ-1));
m_cos = 0;
}
else if ((phase == -180) || (phase == 180))
{
m_sin = 0;
m_cos = -(1<<(SDR_RX_SAMP_SZ-1));
}
else
{
m_phase = phase % 180;
double d_sin = sin(M_PI*(m_phase/180.0)) * (1<<(SDR_RX_SAMP_SZ-1));
double d_cos = cos(M_PI*(m_phase/180.0)) * (1<<(SDR_RX_SAMP_SZ-1));
m_sin = d_sin;
m_cos = d_cos;
}
}