2020-09-18 11:36:27 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2019 Edouard Griffiths, F4EXB //
|
|
|
|
// Copyright (C) 2020 Jon Beniston, M7RCE //
|
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef INCLUDE_PACKETMODSOURCE_H
|
|
|
|
#define INCLUDE_PACKETMODSOURCE_H
|
|
|
|
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QDebug>
|
2020-12-20 20:30:29 -05:00
|
|
|
#include <QVector>
|
2020-09-18 11:36:27 -04:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include "dsp/channelsamplesource.h"
|
|
|
|
#include "dsp/nco.h"
|
|
|
|
#include "dsp/ncof.h"
|
|
|
|
#include "dsp/interpolator.h"
|
2020-10-31 16:30:45 -04:00
|
|
|
#include "dsp/firfilter.h"
|
2020-09-23 08:42:29 -04:00
|
|
|
#include "dsp/raisedcosine.h"
|
2020-09-18 11:36:27 -04:00
|
|
|
#include "dsp/fmpreemphasis.h"
|
2020-09-23 08:42:29 -04:00
|
|
|
#include "util/lfsr.h"
|
2020-09-18 11:36:27 -04:00
|
|
|
#include "util/movingaverage.h"
|
|
|
|
|
|
|
|
#include "packetmodsettings.h"
|
|
|
|
|
|
|
|
#define AX25_MAX_FLAGS 1024
|
|
|
|
#define AX25_MAX_BYTES (2*AX25_MAX_FLAGS+1+28+2+256+2+1)
|
|
|
|
#define AX25_MAX_BITS (AX25_MAX_BYTES*2)
|
|
|
|
#define AX25_FLAG 0x7e
|
|
|
|
#define AX25_NO_L3 0xf0
|
|
|
|
|
|
|
|
class BasebandSampleSink;
|
2020-12-20 20:30:29 -05:00
|
|
|
class ChannelAPI;
|
2020-09-18 11:36:27 -04:00
|
|
|
|
|
|
|
class PacketModSource : public ChannelSampleSource
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PacketModSource();
|
|
|
|
virtual ~PacketModSource();
|
|
|
|
|
|
|
|
virtual void pull(SampleVector::iterator begin, unsigned int nbSamples);
|
|
|
|
virtual void pullOne(Sample& sample);
|
2020-11-14 05:13:32 -05:00
|
|
|
virtual void prefetch(unsigned int nbSamples) { (void) nbSamples; }
|
2020-09-18 11:36:27 -04:00
|
|
|
|
|
|
|
double getMagSq() const { return m_magsq; }
|
|
|
|
void getLevels(qreal& rmsLevel, qreal& peakLevel, int& numSamples) const
|
|
|
|
{
|
|
|
|
rmsLevel = m_rmsLevel;
|
|
|
|
peakLevel = m_peakLevelOut;
|
|
|
|
numSamples = m_levelNbSamples;
|
|
|
|
}
|
|
|
|
void setSpectrumSink(BasebandSampleSink *sampleSink) { m_spectrumSink = sampleSink; }
|
|
|
|
void applySettings(const PacketModSettings& settings, bool force = false);
|
|
|
|
void applyChannelSettings(int channelSampleRate, int channelFrequencyOffset, bool force = false);
|
|
|
|
void addTXPacket(QString callsign, QString to, QString via, QString data);
|
2021-04-07 16:06:00 -04:00
|
|
|
void addTXPacket(QByteArray data);
|
|
|
|
void encodePacket(uint8_t *packet, int packet_length, uint8_t *crc_start, uint8_t *packet_end);
|
2020-12-20 20:30:29 -05:00
|
|
|
void setChannel(ChannelAPI *channel) { m_channel = channel; }
|
2020-09-18 11:36:27 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
int m_channelSampleRate;
|
|
|
|
int m_channelFrequencyOffset;
|
2020-09-23 08:42:29 -04:00
|
|
|
int m_spectrumRate;
|
2020-09-18 11:36:27 -04:00
|
|
|
PacketModSettings m_settings;
|
2020-12-20 20:30:29 -05:00
|
|
|
ChannelAPI *m_channel;
|
2020-09-18 11:36:27 -04:00
|
|
|
|
|
|
|
NCO m_carrierNco;
|
|
|
|
Real m_audioPhase;
|
2020-09-23 08:42:29 -04:00
|
|
|
double m_fmPhase; // Double gives cleaner spectrum than Real
|
|
|
|
double m_phaseSensitivity;
|
|
|
|
Real m_linearGain;
|
2020-09-18 11:36:27 -04:00
|
|
|
Complex m_modSample;
|
|
|
|
|
2020-09-23 08:42:29 -04:00
|
|
|
int m_nrziBit; // Output of NRZI coder
|
|
|
|
int m_scrambledBit; // Output from scrambler to be pulse shaped
|
|
|
|
RaisedCosine<Real> m_pulseShape; // Pulse shaping filter
|
|
|
|
Bandpass<Real> m_bandpass; // Baseband bandpass filter for AFSK
|
2020-09-18 11:36:27 -04:00
|
|
|
Lowpass<Complex> m_lowpass; // Low pass filter to limit RF bandwidth
|
|
|
|
FMPreemphasis m_preemphasisFilter; // FM preemphasis filter to amplify high frequencies
|
|
|
|
|
|
|
|
BasebandSampleSink* m_spectrumSink; // Spectrum GUI to display baseband waveform
|
|
|
|
SampleVector m_sampleBuffer;
|
|
|
|
Interpolator m_interpolator; // Interpolator to downsample to 4k in spectrum
|
|
|
|
Real m_interpolatorDistance;
|
|
|
|
Real m_interpolatorDistanceRemain;
|
|
|
|
bool m_interpolatorConsumed;
|
|
|
|
|
|
|
|
double m_magsq;
|
|
|
|
MovingAverageUtil<double, double, 16> m_movingAverage;
|
|
|
|
|
|
|
|
quint32 m_levelCalcCount;
|
|
|
|
qreal m_rmsLevel;
|
|
|
|
qreal m_peakLevelOut;
|
|
|
|
Real m_peakLevel;
|
|
|
|
Real m_levelSum;
|
|
|
|
|
|
|
|
static const int m_levelNbSamples = 480; // every 10ms assuming 48k Sa/s
|
|
|
|
|
|
|
|
int m_sampleIdx; // Sample index in to symbol
|
|
|
|
int m_samplesPerSymbol; // Number of samples per symbol
|
|
|
|
Real m_pow; // In dB
|
|
|
|
Real m_powRamp; // In dB
|
|
|
|
enum PacketModState {
|
|
|
|
idle, ramp_up, tx, ramp_down, wait
|
|
|
|
} m_state; // States for sample modulation
|
|
|
|
int m_packetRepeatCount;
|
|
|
|
uint64_t m_waitCounter; // Samples to wait before retransmission
|
|
|
|
|
|
|
|
uint8_t m_bits[AX25_MAX_BITS]; // HDLC encoded bits to transmit
|
|
|
|
int m_byteIdx; // Index in to m_bits
|
|
|
|
int m_bitIdx; // Index in to current byte of m_bits
|
|
|
|
int m_last5Bits; // Last 5 bits to be HDLC encoded
|
|
|
|
int m_bitCount; // Count of number of valid bits in m_bits
|
|
|
|
int m_bitCountTotal;
|
|
|
|
|
2020-09-23 08:42:29 -04:00
|
|
|
LFSR m_scrambler; // Scrambler
|
|
|
|
|
2020-09-18 11:36:27 -04:00
|
|
|
std::ofstream m_audioFile; // For debug output of baseband waveform
|
|
|
|
|
2020-12-20 20:30:29 -05:00
|
|
|
QVector<qint16> m_demodBuffer;
|
|
|
|
int m_demodBufferFill;
|
|
|
|
|
2020-09-18 11:36:27 -04:00
|
|
|
bool bitsValid(); // Are there and bits to transmit
|
|
|
|
int getBit(); // Get bit from m_bits
|
|
|
|
void addBit(int bit); // Add bit to m_bits, with zero stuffing
|
|
|
|
void initTX();
|
|
|
|
|
|
|
|
void calculateLevel(Real& sample);
|
|
|
|
void modulateSample();
|
|
|
|
void sampleToSpectrum(Real sample);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // INCLUDE_PACKETMODSOURCE_H
|