1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2025-11-10 08:10:25 -05:00
sdrangel/plugins/channelrx/demodchirpchat/chirpchatdemodsink.h

146 lines
6.3 KiB
C
Raw Normal View History

2019-12-03 18:49:52 +01:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2019 Edouard Griffiths, F4EXB //
// //
// 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/>. //
///////////////////////////////////////////////////////////////////////////////////
2020-03-03 21:52:46 +01:00
#ifndef INCLUDE_CHIRPCHATDEMODSINK_H
#define INCLUDE_CHIRPCHATDEMODSINK_H
2019-12-03 18:49:52 +01:00
#include <vector>
#include <queue>
2019-12-03 18:49:52 +01:00
#include "dsp/channelsamplesink.h"
#include "dsp/nco.h"
#include "dsp/interpolator.h"
2020-02-08 18:21:38 +01:00
#include "dsp/fftwindow.h"
#include "util/message.h"
#include "util/movingaverage.h"
2019-12-03 18:49:52 +01:00
2020-03-03 21:52:46 +01:00
#include "chirpchatdemodsettings.h"
2019-12-03 18:49:52 +01:00
class BasebandSampleSink;
2020-02-08 18:21:38 +01:00
class FFTEngine;
2020-03-03 21:52:46 +01:00
namespace ChirpChatDemodMsg {
class MsgDecodeSymbols;
}
class MessageQueue;
2019-12-03 18:49:52 +01:00
2020-03-03 21:52:46 +01:00
class ChirpChatDemodSink : public ChannelSampleSink {
2019-12-03 18:49:52 +01:00
public:
2020-03-03 21:52:46 +01:00
ChirpChatDemodSink();
~ChirpChatDemodSink();
2019-12-03 18:49:52 +01:00
virtual void feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end);
bool getDemodActive() const { return m_demodActive; }
void setDecoderMessageQueue(MessageQueue *messageQueue) { m_decoderMsgQueue = messageQueue; }
2019-12-03 18:49:52 +01:00
void setSpectrumSink(BasebandSampleSink* spectrumSink) { m_spectrumSink = spectrumSink; }
void applyChannelSettings(int channelSampleRate, int bandwidth, int channelFrequencyOffset, bool force = false);
2020-03-03 21:52:46 +01:00
void applySettings(const ChirpChatDemodSettings& settings, bool force = false);
2020-02-23 16:33:21 +01:00
double getCurrentNoiseLevel() const { return m_magsqOffAvg.instantAverage() / (1<<m_settings.m_spreadFactor); }
double getTotalPower() const { return m_magsqTotalAvg.instantAverage() / (1<<m_settings.m_spreadFactor); }
2019-12-03 18:49:52 +01:00
private:
2020-03-03 21:52:46 +01:00
enum ChirpChatState
2020-02-08 18:21:38 +01:00
{
2020-03-03 21:52:46 +01:00
ChirpChatStateReset, //!< Reset everything to start all over
ChirpChatStateDetectPreamble, //!< Look for preamble
ChirpChatStatePreambleResyc, //!< Synchronize with what is left of preamble chirp
ChirpChatStatePreamble, //!< Preamble is found and look for SFD start
ChirpChatStateSkipSFD, //!< Skip SFD
ChirpChatStateReadPayload,
ChirpChatStateTest
2020-02-08 18:21:38 +01:00
};
2020-03-03 21:52:46 +01:00
ChirpChatDemodSettings m_settings;
ChirpChatState m_state;
bool m_demodActive;
2020-03-03 21:52:46 +01:00
ChirpChatDemodMsg::MsgDecodeSymbols *m_decodeMsg;
MessageQueue *m_decoderMsgQueue;
2020-02-08 18:21:38 +01:00
int m_bandwidth;
2019-12-03 18:49:52 +01:00
int m_channelSampleRate;
int m_channelFrequencyOffset;
int m_chirp;
2020-02-08 18:21:38 +01:00
int m_chirp0;
static const unsigned int m_requiredPreambleChirps = 4; //!< Number of chirps required to estimate preamble
2020-02-08 18:21:38 +01:00
static const unsigned int m_maxSFDSearchChirps = 8; //!< Maximum number of chirps when looking for SFD after preamble detection
2020-02-12 13:17:15 +01:00
static const unsigned int m_fftInterpolation = 2; //!< FFT interpolation factor (usually a power of 2)
2020-02-08 18:21:38 +01:00
FFTEngine *m_fft;
FFTEngine *m_fftSFD;
2020-03-14 23:52:43 +01:00
int m_fftSequence;
int m_fftSFDSequence;
2020-02-08 18:21:38 +01:00
FFTWindow m_fftWindow;
Complex *m_downChirps;
Complex *m_upChirps;
2020-02-12 13:17:15 +01:00
Complex *m_spectrumLine;
2020-02-08 18:21:38 +01:00
unsigned int m_fftCounter;
int m_argMaxHistory[m_requiredPreambleChirps];
2020-02-08 18:21:38 +01:00
unsigned int m_argMaxHistoryCounter;
unsigned int m_preambleHistory[m_maxSFDSearchChirps];
unsigned int m_syncWord;
double m_magsqMax;
MovingAverageUtil<double, double, 10> m_magsqOnAvg;
MovingAverageUtil<double, double, 10> m_magsqOffAvg;
2020-02-23 16:33:21 +01:00
MovingAverageUtil<double, double, 10> m_magsqTotalAvg;
std::queue<double> m_magsqQueue;
2020-02-08 18:21:38 +01:00
unsigned int m_chirpCount; //!< Generic chirp counter
unsigned int m_sfdSkip; //!< Number of samples in a SFD skip or slide (1/4) period
unsigned int m_sfdSkipCounter; //!< Counter of skip or slide periods
2019-12-03 18:49:52 +01:00
NCO m_nco;
Interpolator m_interpolator;
Real m_sampleDistanceRemain;
2020-02-08 18:21:38 +01:00
Real m_interpolatorDistance;
2019-12-03 18:49:52 +01:00
BasebandSampleSink* m_spectrumSink;
2020-02-08 18:21:38 +01:00
Complex *m_spectrumBuffer;
2019-12-03 18:49:52 +01:00
unsigned int m_nbSymbols; //!< Number of symbols = length of base FFT
unsigned int m_nbSymbolsEff; //!< effective symbols considering DE bits
unsigned int m_fftLength; //!< Length of base FFT
unsigned int m_interpolatedFFTLength; //!< Length of interpolated FFT
int m_deLength; //!< Number of FFT bins collated to represent one symbol
int m_preambleTolerance; //!< Number of FFT bins to collate when looking for preamble
2020-02-08 18:21:38 +01:00
void processSample(const Complex& ci);
void initSF(unsigned int sf, unsigned int deBits, FFTWindow::Function fftWindow); //!< Init tables, FFTs, depending on spread factor
2020-02-08 18:21:38 +01:00
void reset();
unsigned int argmax(
const Complex *fftBins,
unsigned int fftMult,
unsigned int fftLength,
double& magsqMax,
2020-02-23 16:33:21 +01:00
double& magSqTotal,
2020-02-08 18:21:38 +01:00
Complex *specBuffer,
unsigned int specDecim
);
unsigned int argmaxSpreaded( //!< count energy in adjacent bins for same symbol (needs DE bits > 0)
const Complex *fftBins,
unsigned int fftMult,
unsigned int fftLength,
double& magsqMax,
double& magsqNoise,
2020-02-23 16:33:21 +01:00
double& magSqTotal,
Complex *specBuffer,
unsigned int specDecim
);
2020-02-08 18:21:38 +01:00
void decimateSpectrum(Complex *in, Complex *out, unsigned int size, unsigned int decimation);
int toSigned(int u, int intSize);
2020-02-12 13:17:15 +01:00
unsigned int evalSymbol(unsigned int rawSymbol);
2019-12-03 18:49:52 +01:00
};
2020-03-03 21:52:46 +01:00
#endif // INCLUDE_CHIRPCHATDEMODSINK_H