mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2025-06-26 12:27:51 +02:00
Beginning of code for the RSPduo + bugfix for the hackrf
This commit is contained in:
@ -84,6 +84,8 @@ namespace dsp {
|
||||
omegaMax = _omega + (_omega * _omegaRelLimit);
|
||||
_dynOmega = _omega;
|
||||
|
||||
memset(delay, 0, 1024 * sizeof(T));
|
||||
|
||||
generic_block<MMClockRecovery<T>>::registerInput(_in);
|
||||
generic_block<MMClockRecovery<T>>::registerOutput(&out);
|
||||
}
|
||||
|
@ -35,7 +35,6 @@ namespace dsp {
|
||||
count = _in->read();
|
||||
if (count < 0) { return -1; }
|
||||
|
||||
|
||||
// Copy data into work buffer
|
||||
memcpy(bufferStart, _in->readBuf, count - 1);
|
||||
|
||||
@ -61,6 +60,7 @@ namespace dsp {
|
||||
// Else, check for a header
|
||||
else if (memcmp(buffer + i, _syncword, _syncLen) == 0) {
|
||||
bitsRead = 0;
|
||||
//printf("Frame found!\n");
|
||||
badFrameCount = 0;
|
||||
continue;
|
||||
}
|
||||
@ -70,6 +70,7 @@ namespace dsp {
|
||||
// try to save
|
||||
if (badFrameCount < 5) {
|
||||
badFrameCount++;
|
||||
//printf("Frame found!\n");
|
||||
bitsRead = 0;
|
||||
continue;
|
||||
}
|
||||
@ -111,4 +112,252 @@ namespace dsp {
|
||||
stream<uint8_t>* _in;
|
||||
|
||||
};
|
||||
|
||||
inline int MachesterHammingDistance(float* data, uint8_t* syncBits, int n) {
|
||||
int dist = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if ((data[(2*i) + 1] > data[2*i]) != syncBits[i]) { dist++; }
|
||||
}
|
||||
return dist;
|
||||
}
|
||||
|
||||
inline int HammingDistance(uint8_t* data, uint8_t* syncBits, int n) {
|
||||
int dist = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (data[i] != syncBits[i]) { dist++; }
|
||||
}
|
||||
return dist;
|
||||
}
|
||||
|
||||
class ManchesterDeframer : public generic_block<ManchesterDeframer> {
|
||||
public:
|
||||
ManchesterDeframer() {}
|
||||
|
||||
ManchesterDeframer(stream<float>* in, int frameLen, uint8_t* syncWord, int syncLen) { init(in, frameLen, syncWord, syncLen); }
|
||||
|
||||
void init(stream<float>* in, int frameLen, uint8_t* syncWord, int syncLen) {
|
||||
_in = in;
|
||||
_frameLen = frameLen;
|
||||
_syncword = new uint8_t[syncLen];
|
||||
_syncLen = syncLen;
|
||||
memcpy(_syncword, syncWord, syncLen);
|
||||
|
||||
buffer = new float[STREAM_BUFFER_SIZE + (syncLen * 2)];
|
||||
memset(buffer, 0, syncLen * 2 * sizeof(float));
|
||||
bufferStart = &buffer[syncLen * 2];
|
||||
|
||||
generic_block<ManchesterDeframer>::registerInput(_in);
|
||||
generic_block<ManchesterDeframer>::registerOutput(&out);
|
||||
}
|
||||
|
||||
int run() {
|
||||
count = _in->read();
|
||||
if (count < 0) { return -1; }
|
||||
|
||||
int readable;
|
||||
|
||||
// Copy data into work buffer
|
||||
memcpy(bufferStart, _in->readBuf, (count - 1) * sizeof(float));
|
||||
|
||||
// Iterate through all symbols
|
||||
for (int i = 0; i < count;) {
|
||||
|
||||
// If already in the process of reading bits
|
||||
if (bitsRead >= 0) {
|
||||
readable = std::min<int>(count - i, _frameLen - bitsRead);
|
||||
memcpy(&out.writeBuf[bitsRead], &buffer[i], readable * sizeof(float));
|
||||
bitsRead += readable;
|
||||
i += readable;
|
||||
if (bitsRead >= _frameLen) {
|
||||
out.swap(_frameLen);
|
||||
bitsRead = -1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Else, check for a header
|
||||
if (MachesterHammingDistance(&buffer[i], _syncword, _syncLen) <= 2) {
|
||||
bitsRead = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
i++;
|
||||
|
||||
}
|
||||
|
||||
// Keep last _syncLen symbols
|
||||
memcpy(buffer, &_in->readBuf[count - (_syncLen * 2)], _syncLen * 2 * sizeof(float));
|
||||
|
||||
_in->flush();
|
||||
return count;
|
||||
}
|
||||
|
||||
stream<float> out;
|
||||
|
||||
private:
|
||||
float* buffer;
|
||||
float* bufferStart;
|
||||
uint8_t* _syncword;
|
||||
int count;
|
||||
int _frameLen;
|
||||
int _syncLen;
|
||||
int bitsRead = -1;
|
||||
|
||||
stream<float>* _in;
|
||||
|
||||
};
|
||||
|
||||
class SymbolDeframer : public generic_block<SymbolDeframer> {
|
||||
public:
|
||||
SymbolDeframer() {}
|
||||
|
||||
SymbolDeframer(stream<uint8_t>* in, int frameLen, uint8_t* syncWord, int syncLen) { init(in, frameLen, syncWord, syncLen); }
|
||||
|
||||
void init(stream<uint8_t>* in, int frameLen, uint8_t* syncWord, int syncLen) {
|
||||
_in = in;
|
||||
_frameLen = frameLen;
|
||||
_syncword = new uint8_t[syncLen];
|
||||
_syncLen = syncLen;
|
||||
memcpy(_syncword, syncWord, syncLen);
|
||||
|
||||
buffer = new uint8_t[STREAM_BUFFER_SIZE + syncLen];
|
||||
memset(buffer, 0, syncLen);
|
||||
bufferStart = &buffer[syncLen];
|
||||
|
||||
generic_block<SymbolDeframer>::registerInput(_in);
|
||||
generic_block<SymbolDeframer>::registerOutput(&out);
|
||||
}
|
||||
|
||||
int run() {
|
||||
count = _in->read();
|
||||
if (count < 0) { return -1; }
|
||||
|
||||
int readable;
|
||||
|
||||
// Copy data into work buffer
|
||||
memcpy(bufferStart, _in->readBuf, count - 1);
|
||||
|
||||
// Iterate through all symbols
|
||||
for (int i = 0; i < count;) {
|
||||
|
||||
// If already in the process of reading bits
|
||||
if (bitsRead >= 0) {
|
||||
readable = std::min<int>(count - i, _frameLen - bitsRead);
|
||||
memcpy(&out.writeBuf[bitsRead], &buffer[i], readable);
|
||||
bitsRead += readable;
|
||||
i += readable;
|
||||
if (bitsRead >= _frameLen) {
|
||||
out.swap(_frameLen);
|
||||
bitsRead = -1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Else, check for a header
|
||||
if (HammingDistance(&buffer[i], _syncword, _syncLen) <= 2) {
|
||||
bitsRead = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
i++;
|
||||
|
||||
}
|
||||
|
||||
// Keep last _syncLen symbols
|
||||
memcpy(buffer, &_in->readBuf[count - _syncLen], _syncLen);
|
||||
|
||||
_in->flush();
|
||||
return count;
|
||||
}
|
||||
|
||||
stream<uint8_t> out;
|
||||
|
||||
private:
|
||||
uint8_t* buffer;
|
||||
uint8_t* bufferStart;
|
||||
uint8_t* _syncword;
|
||||
int count;
|
||||
int _frameLen;
|
||||
int _syncLen;
|
||||
int bitsRead = -1;
|
||||
|
||||
stream<uint8_t>* _in;
|
||||
|
||||
};
|
||||
|
||||
class ManchesterDecoder : public generic_block<ManchesterDecoder> {
|
||||
public:
|
||||
ManchesterDecoder() {}
|
||||
|
||||
ManchesterDecoder(stream<float>* in, bool inverted) { init(in, inverted); }
|
||||
|
||||
void init(stream<float>* in, bool inverted) {
|
||||
_in = in;
|
||||
_inverted = inverted;
|
||||
generic_block<ManchesterDecoder>::registerInput(_in);
|
||||
generic_block<ManchesterDecoder>::registerOutput(&out);
|
||||
}
|
||||
|
||||
int run() {
|
||||
int count = _in->read();
|
||||
if (count < 0) { return -1; }
|
||||
|
||||
if (_inverted) {
|
||||
for (int i = 0; i < count; i += 2) {
|
||||
out.writeBuf[i/2] = (_in->readBuf[i + 1] < _in->readBuf[i]);
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < count; i += 2) {
|
||||
out.writeBuf[i/2] = (_in->readBuf[i + 1] > _in->readBuf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
_in->flush();
|
||||
out.swap(count / 2);
|
||||
return count;
|
||||
}
|
||||
|
||||
stream<uint8_t> out;
|
||||
|
||||
private:
|
||||
stream<float>* _in;
|
||||
bool _inverted;
|
||||
|
||||
};
|
||||
|
||||
class BitPacker : public generic_block<BitPacker> {
|
||||
public:
|
||||
BitPacker() {}
|
||||
|
||||
BitPacker(stream<uint8_t>* in) { init(in); }
|
||||
|
||||
void init(stream<uint8_t>* in) {
|
||||
_in = in;
|
||||
|
||||
generic_block<BitPacker>::registerInput(_in);
|
||||
generic_block<BitPacker>::registerOutput(&out);
|
||||
}
|
||||
|
||||
int run() {
|
||||
int count = _in->read();
|
||||
if (count < 0) { return -1; }
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
if ((i % 8) == 0) { out.writeBuf[i / 8] = 0; }
|
||||
out.writeBuf[i / 8] |= (_in->readBuf[i] & 1) << (7 - (i % 8));
|
||||
}
|
||||
|
||||
_in->flush();
|
||||
out.swap((count / 8) + (((count % 8) == 0) ? 0 : 1));
|
||||
return count;
|
||||
}
|
||||
|
||||
stream<uint8_t> out;
|
||||
|
||||
private:
|
||||
|
||||
stream<uint8_t>* _in;
|
||||
|
||||
};
|
||||
}
|
@ -500,7 +500,7 @@ namespace dsp {
|
||||
public:
|
||||
MSKDemod() {}
|
||||
MSKDemod(stream<complex_t>* input, float sampleRate, float deviation, float baudRate, float omegaGain = (0.01*0.01) / 4, float muGain = 0.01f, float omegaRelLimit = 0.005f) {
|
||||
init(input, sampleRate, deviation, baudRate);
|
||||
init(input, sampleRate, deviation, baudRate, omegaGain, muGain, omegaRelLimit);
|
||||
}
|
||||
|
||||
void init(stream<complex_t>* input, float sampleRate, float deviation, float baudRate, float omegaGain = (0.01*0.01) / 4, float muGain = 0.01f, float omegaRelLimit = 0.005f) {
|
||||
@ -567,11 +567,11 @@ namespace dsp {
|
||||
class PSKDemod : public generic_hier_block<PSKDemod<ORDER, OFFSET>> {
|
||||
public:
|
||||
PSKDemod() {}
|
||||
PSKDemod(stream<complex_t>* input, float sampleRate, float baudRate, int RRCTapCount = 32, float RRCAlpha = 0.32f, float agcRate = 10e-4, float costasLoopBw = 0.004f, float omegaGain = (0.01*0.01) / 4, float muGain = 0.01f, float omegaRelLimit = 0.005f) {
|
||||
PSKDemod(stream<complex_t>* input, float sampleRate, float baudRate, int RRCTapCount = 31, float RRCAlpha = 0.32f, float agcRate = 10e-4, float costasLoopBw = 0.004f, float omegaGain = (0.01*0.01) / 4, float muGain = 0.01f, float omegaRelLimit = 0.005f) {
|
||||
init(input, sampleRate, baudRate, RRCTapCount, RRCAlpha, agcRate, costasLoopBw, omegaGain, muGain, omegaRelLimit);
|
||||
}
|
||||
|
||||
void init(stream<complex_t>* input, float sampleRate, float baudRate, int RRCTapCount = 32, float RRCAlpha = 0.32f, float agcRate = 10e-4, float costasLoopBw = 0.004f, float omegaGain = (0.01*0.01) / 4, float muGain = 0.01f, float omegaRelLimit = 0.005f) {
|
||||
void init(stream<complex_t>* input, float sampleRate, float baudRate, int RRCTapCount = 31, float RRCAlpha = 0.32f, float agcRate = 10e-4, float costasLoopBw = 0.004f, float omegaGain = (0.01*0.01) / 4, float muGain = 0.01f, float omegaRelLimit = 0.005f) {
|
||||
_RRCTapCount = RRCTapCount;
|
||||
_RRCAlpha = RRCAlpha;
|
||||
_sampleRate = sampleRate;
|
||||
@ -680,4 +680,89 @@ namespace dsp {
|
||||
float _muGain;
|
||||
float _omegaRelLimit;
|
||||
};
|
||||
|
||||
class PMDemod : public generic_hier_block<PMDemod> {
|
||||
public:
|
||||
PMDemod() {}
|
||||
PMDemod(stream<complex_t>* input, float sampleRate, float baudRate, float agcRate = 0.02e-3f, float pllLoopBandwidth = (0.06f*0.06f) / 4.0f, int rrcTapCount = 31, float rrcAlpha = 0.6f, float omegaGain = (0.01*0.01) / 4, float muGain = 0.01f, float omegaRelLimit = 0.005f) {
|
||||
init(input, sampleRate, baudRate, agcRate, pllLoopBandwidth, rrcTapCount, rrcAlpha, omegaGain, muGain, omegaRelLimit);
|
||||
}
|
||||
|
||||
void init(stream<complex_t>* input, float sampleRate, float baudRate, float agcRate = 0.02e-3f, float pllLoopBandwidth = (0.06f*0.06f) / 4.0f, int rrcTapCount = 31, float rrcAlpha = 0.6f, float omegaGain = (0.01*0.01) / 4, float muGain = 0.01f, float omegaRelLimit = 0.005f) {
|
||||
_sampleRate = sampleRate;
|
||||
_baudRate = baudRate;
|
||||
_agcRate = agcRate;
|
||||
_pllLoopBandwidth = pllLoopBandwidth;
|
||||
_rrcTapCount = rrcTapCount;
|
||||
_rrcAlpha = rrcAlpha;
|
||||
_omegaGain = omegaGain;
|
||||
_muGain = muGain;
|
||||
_omegaRelLimit = omegaRelLimit;
|
||||
|
||||
agc.init(input, 1.0f, 65535, _agcRate);
|
||||
pll.init(&agc.out, _pllLoopBandwidth);
|
||||
rrcwin.init(_rrcTapCount, _sampleRate, _baudRate, _rrcAlpha);
|
||||
rrc.init(&pll.out, &rrcwin);
|
||||
recov.init(&rrc.out, _sampleRate / _baudRate, _omegaGain, _muGain, _omegaRelLimit);
|
||||
|
||||
out = &recov.out;
|
||||
|
||||
generic_hier_block<PMDemod>::registerBlock(&agc);
|
||||
generic_hier_block<PMDemod>::registerBlock(&pll);
|
||||
generic_hier_block<PMDemod>::registerBlock(&rrc);
|
||||
generic_hier_block<PMDemod>::registerBlock(&recov);
|
||||
}
|
||||
|
||||
void setInput(stream<complex_t>* input) {
|
||||
agc.setInput(input);
|
||||
}
|
||||
|
||||
void setAgcRate(float agcRate) {
|
||||
_agcRate = agcRate;
|
||||
agc.setRate(_agcRate);
|
||||
}
|
||||
|
||||
void setPllLoopBandwidth(float pllLoopBandwidth) {
|
||||
_pllLoopBandwidth = pllLoopBandwidth;
|
||||
pll.setLoopBandwidth(_pllLoopBandwidth);
|
||||
}
|
||||
|
||||
void setRRCParams(int rrcTapCount, float rrcAlpha) {
|
||||
_rrcTapCount = rrcTapCount;
|
||||
_rrcAlpha = rrcAlpha;
|
||||
rrcwin.setTapCount(_rrcTapCount);
|
||||
rrcwin.setAlpha(_rrcAlpha);
|
||||
rrc.updateWindow(&rrcwin);
|
||||
}
|
||||
|
||||
void setMMGains(float omegaGain, float muGain) {
|
||||
_omegaGain = omegaGain;
|
||||
_muGain = muGain;
|
||||
recov.setGains(_omegaGain, _muGain);
|
||||
}
|
||||
|
||||
void setOmegaRelLimit(float omegaRelLimit) {
|
||||
_omegaRelLimit = omegaRelLimit;
|
||||
recov.setOmegaRelLimit(_omegaRelLimit);
|
||||
}
|
||||
|
||||
stream<float>* out = NULL;
|
||||
|
||||
private:
|
||||
dsp::ComplexAGC agc;
|
||||
dsp::CarrierTrackingPLL<float> pll;
|
||||
dsp::RRCTaps rrcwin;
|
||||
dsp::FIR<float> rrc;
|
||||
dsp::MMClockRecovery<float> recov;
|
||||
|
||||
float _sampleRate;
|
||||
float _baudRate;
|
||||
float _agcRate;
|
||||
float _pllLoopBandwidth;
|
||||
int _rrcTapCount;
|
||||
float _rrcAlpha;
|
||||
float _omegaGain;
|
||||
float _muGain;
|
||||
float _omegaRelLimit;
|
||||
};
|
||||
}
|
@ -115,4 +115,206 @@ namespace dsp {
|
||||
stream<complex_t>* _in;
|
||||
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class CarrierTrackingPLL: public generic_block<CarrierTrackingPLL<T>> {
|
||||
public:
|
||||
CarrierTrackingPLL() {}
|
||||
CarrierTrackingPLL(stream<complex_t>* in, float loopBandwidth) { init(in, loopBandwidth); }
|
||||
|
||||
void init(stream<complex_t>* in, float loopBandwidth) {
|
||||
_in = in;
|
||||
lastVCO.re = 1.0f;
|
||||
lastVCO.im = 0.0f;
|
||||
_loopBandwidth = loopBandwidth;
|
||||
|
||||
float dampningFactor = sqrtf(2.0f) / 2.0f;
|
||||
float denominator = (1.0 + 2.0 * dampningFactor * _loopBandwidth + _loopBandwidth * _loopBandwidth);
|
||||
_alpha = (4 * dampningFactor * _loopBandwidth) / denominator;
|
||||
_beta = (4 * _loopBandwidth * _loopBandwidth) / denominator;
|
||||
|
||||
generic_block<CarrierTrackingPLL<T>>::registerInput(_in);
|
||||
generic_block<CarrierTrackingPLL<T>>::registerOutput(&out);
|
||||
}
|
||||
|
||||
void setInput(stream<complex_t>* in) {
|
||||
generic_block<CarrierTrackingPLL<T>>::tempStop();
|
||||
generic_block<CarrierTrackingPLL<T>>::unregisterInput(_in);
|
||||
_in = in;
|
||||
generic_block<CarrierTrackingPLL<T>>::registerInput(_in);
|
||||
generic_block<CarrierTrackingPLL<T>>::tempStart();
|
||||
}
|
||||
|
||||
void setLoopBandwidth(float loopBandwidth) {
|
||||
generic_block<CarrierTrackingPLL<T>>::tempStop();
|
||||
_loopBandwidth = loopBandwidth;
|
||||
float dampningFactor = sqrtf(2.0f) / 2.0f;
|
||||
float denominator = (1.0 + 2.0 * dampningFactor * _loopBandwidth + _loopBandwidth * _loopBandwidth);
|
||||
_alpha = (4 * dampningFactor * _loopBandwidth) / denominator;
|
||||
_beta = (4 * _loopBandwidth * _loopBandwidth) / denominator;
|
||||
generic_block<CarrierTrackingPLL<T>>::tempStart();
|
||||
}
|
||||
|
||||
int run() {
|
||||
int count = _in->read();
|
||||
if (count < 0) { return -1; }
|
||||
|
||||
complex_t outVal;
|
||||
float error;
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
|
||||
// Mix the VFO with the input to create the output value
|
||||
outVal.re = (lastVCO.re*_in->readBuf[i].re) - ((-lastVCO.im)*_in->readBuf[i].im);
|
||||
outVal.im = ((-lastVCO.im)*_in->readBuf[i].re) + (lastVCO.re*_in->readBuf[i].im);
|
||||
|
||||
if constexpr (std::is_same_v<T, float>) {
|
||||
out.writeBuf[i] = outVal.fastPhase();
|
||||
}
|
||||
if constexpr (std::is_same_v<T, complex_t>) {
|
||||
out.writeBuf[i] = outVal;
|
||||
}
|
||||
|
||||
// Calculate the phase error estimation
|
||||
// TODO: Figure out why fastPhase doesn't work
|
||||
error = _in->readBuf[i].phase() - vcoPhase;
|
||||
if (error > 3.1415926535f) { error -= 2.0f * 3.1415926535f; }
|
||||
else if (error <= -3.1415926535f) { error += 2.0f * 3.1415926535f; }
|
||||
|
||||
// if (error > 1.0f) { error = 1.0f; }
|
||||
// else if (error < -1.0f) { error = -1.0f; }
|
||||
|
||||
// Integrate frequency and clamp it
|
||||
vcoFrequency += _beta * error;
|
||||
if (vcoFrequency > 1.0f) { vcoFrequency = 1.0f; }
|
||||
else if (vcoFrequency < -1.0f) { vcoFrequency = -1.0f; }
|
||||
|
||||
// Calculate new phase and wrap it
|
||||
vcoPhase += vcoFrequency + (_alpha * error);
|
||||
while (vcoPhase > (2.0f * FL_M_PI)) { vcoPhase -= (2.0f * FL_M_PI); }
|
||||
while (vcoPhase < (-2.0f * FL_M_PI)) { vcoPhase += (2.0f * FL_M_PI); }
|
||||
|
||||
// Calculate output
|
||||
lastVCO.re = cosf(vcoPhase);
|
||||
lastVCO.im = sinf(vcoPhase);
|
||||
|
||||
}
|
||||
|
||||
_in->flush();
|
||||
if (!out.swap(count)) { return -1; }
|
||||
return count;
|
||||
}
|
||||
|
||||
stream<T> out;
|
||||
|
||||
private:
|
||||
float _loopBandwidth = 1.0f;
|
||||
|
||||
float _alpha; // Integral coefficient
|
||||
float _beta; // Proportional coefficient
|
||||
float vcoFrequency = 0.0f;
|
||||
float vcoPhase = 0.0f;
|
||||
complex_t lastVCO;
|
||||
|
||||
stream<complex_t>* _in;
|
||||
|
||||
};
|
||||
|
||||
class PLL: public generic_block<PLL> {
|
||||
public:
|
||||
PLL() {}
|
||||
PLL(stream<complex_t>* in, float loopBandwidth) { init(in, loopBandwidth); }
|
||||
|
||||
void init(stream<complex_t>* in, float loopBandwidth) {
|
||||
_in = in;
|
||||
lastVCO.re = 1.0f;
|
||||
lastVCO.im = 0.0f;
|
||||
_loopBandwidth = loopBandwidth;
|
||||
|
||||
float dampningFactor = sqrtf(2.0f) / 2.0f;
|
||||
float denominator = (1.0 + 2.0 * dampningFactor * _loopBandwidth + _loopBandwidth * _loopBandwidth);
|
||||
_alpha = (4 * dampningFactor * _loopBandwidth) / denominator;
|
||||
_beta = (4 * _loopBandwidth * _loopBandwidth) / denominator;
|
||||
|
||||
generic_block<PLL>::registerInput(_in);
|
||||
generic_block<PLL>::registerOutput(&out);
|
||||
}
|
||||
|
||||
void setInput(stream<complex_t>* in) {
|
||||
generic_block<PLL>::tempStop();
|
||||
generic_block<PLL>::unregisterInput(_in);
|
||||
_in = in;
|
||||
generic_block<PLL>::registerInput(_in);
|
||||
generic_block<PLL>::tempStart();
|
||||
}
|
||||
|
||||
void setLoopBandwidth(float loopBandwidth) {
|
||||
generic_block<PLL>::tempStop();
|
||||
_loopBandwidth = loopBandwidth;
|
||||
float dampningFactor = sqrtf(2.0f) / 2.0f;
|
||||
float denominator = (1.0 + 2.0 * dampningFactor * _loopBandwidth + _loopBandwidth * _loopBandwidth);
|
||||
_alpha = (4 * dampningFactor * _loopBandwidth) / denominator;
|
||||
_beta = (4 * _loopBandwidth * _loopBandwidth) / denominator;
|
||||
generic_block<PLL>::tempStart();
|
||||
}
|
||||
|
||||
int run() {
|
||||
int count = _in->read();
|
||||
if (count < 0) { return -1; }
|
||||
|
||||
complex_t outVal;
|
||||
float error;
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
|
||||
// Mix the VFO with the input to create the output value
|
||||
outVal.re = (lastVCO.re*_in->readBuf[i].re) - ((-lastVCO.im)*_in->readBuf[i].im);
|
||||
outVal.im = ((-lastVCO.im)*_in->readBuf[i].re) + (lastVCO.re*_in->readBuf[i].im);
|
||||
|
||||
out.writeBuf[i] = lastVCO;
|
||||
|
||||
// Calculate the phase error estimation
|
||||
// TODO: Figure out why fastPhase doesn't work
|
||||
error = _in->readBuf[i].phase() - vcoPhase;
|
||||
if (error > 3.1415926535f) { error -= 2.0f * 3.1415926535f; }
|
||||
else if (error <= -3.1415926535f) { error += 2.0f * 3.1415926535f; }
|
||||
|
||||
// if (error > 1.0f) { error = 1.0f; }
|
||||
// else if (error < -1.0f) { error = -1.0f; }
|
||||
|
||||
// Integrate frequency and clamp it
|
||||
vcoFrequency += _beta * error;
|
||||
if (vcoFrequency > 1.0f) { vcoFrequency = 1.0f; }
|
||||
else if (vcoFrequency < -1.0f) { vcoFrequency = -1.0f; }
|
||||
|
||||
// Calculate new phase and wrap it
|
||||
vcoPhase += vcoFrequency + (_alpha * error);
|
||||
while (vcoPhase > (2.0f * FL_M_PI)) { vcoPhase -= (2.0f * FL_M_PI); }
|
||||
while (vcoPhase < (-2.0f * FL_M_PI)) { vcoPhase += (2.0f * FL_M_PI); }
|
||||
|
||||
// Calculate output
|
||||
lastVCO.re = cosf(vcoPhase);
|
||||
lastVCO.im = sinf(vcoPhase);
|
||||
|
||||
}
|
||||
|
||||
_in->flush();
|
||||
if (!out.swap(count)) { return -1; }
|
||||
return count;
|
||||
}
|
||||
|
||||
stream<complex_t> out;
|
||||
|
||||
private:
|
||||
float _loopBandwidth = 1.0f;
|
||||
|
||||
float _alpha; // Integral coefficient
|
||||
float _beta; // Proportional coefficient
|
||||
float vcoFrequency = 0.0f;
|
||||
float vcoPhase = 0.0f;
|
||||
complex_t lastVCO;
|
||||
|
||||
stream<complex_t>* _in;
|
||||
|
||||
};
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <dsp/block.h>
|
||||
#include <dsp/buffer.h>
|
||||
#include <fstream>
|
||||
|
||||
namespace dsp {
|
||||
template <class T>
|
||||
@ -128,4 +129,53 @@ namespace dsp {
|
||||
stream<T>* _in;
|
||||
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class FileSink : public generic_block<FileSink<T>> {
|
||||
public:
|
||||
FileSink() {}
|
||||
|
||||
FileSink(stream<T>* in, std::string path) { init(in, path); }
|
||||
|
||||
~FileSink() {
|
||||
generic_block<FileSink<T>>::stop();
|
||||
if (file.is_open()) { file.close(); }
|
||||
}
|
||||
|
||||
void init(stream<T>* in, std::string path) {
|
||||
_in = in;
|
||||
file = std::ofstream(path, std::ios::binary);
|
||||
generic_block<FileSink<T>>::registerInput(_in);
|
||||
}
|
||||
|
||||
void setInput(stream<T>* in) {
|
||||
std::lock_guard<std::mutex> lck(generic_block<FileSink<T>>::ctrlMtx);
|
||||
generic_block<FileSink<T>>::tempStop();
|
||||
generic_block<FileSink<T>>::unregisterInput(_in);
|
||||
_in = in;
|
||||
generic_block<FileSink<T>>::registerInput(_in);
|
||||
generic_block<FileSink<T>>::tempStart();
|
||||
}
|
||||
|
||||
bool isOpen() {
|
||||
return file.is_open();
|
||||
}
|
||||
|
||||
int run() {
|
||||
int count = _in->read();
|
||||
if (count < 0) { return -1; }
|
||||
|
||||
if (file.is_open()) {
|
||||
file.write((char*)_in->readBuf, count * sizeof(T));
|
||||
}
|
||||
|
||||
_in->flush();
|
||||
return count;
|
||||
}
|
||||
|
||||
private:
|
||||
stream<T>* _in;
|
||||
std::ofstream file;
|
||||
|
||||
};
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
#include <gui/widgets/symbol_diagram.h>
|
||||
|
||||
namespace ImGui {
|
||||
SymbolDiagram::SymbolDiagram() {
|
||||
SymbolDiagram::SymbolDiagram(float scale) {
|
||||
_scale = scale;
|
||||
memset(buffer, 0, 1024 * sizeof(float));
|
||||
}
|
||||
|
||||
@ -23,9 +24,11 @@ namespace ImGui {
|
||||
window->DrawList->AddRectFilled(min, ImVec2(min.x+size.x, min.y+size.y), IM_COL32(0,0,0,255));
|
||||
ImU32 col = ImGui::GetColorU32(ImGuiCol_CheckMark, 0.7f);
|
||||
float increment = size.x / 1024.0f;
|
||||
float val;
|
||||
for (int i = 0; i < 1024; i++) {
|
||||
if (buffer[i] > 1.0f || buffer[i] < -1.0f) { continue; }
|
||||
window->DrawList->AddCircleFilled(ImVec2(((float)i * increment) + min.x, ((buffer[i] + 1) * (size.y*0.5f)) + min.y), 2, col);
|
||||
val = buffer[i] * _scale;
|
||||
if (val > 1.0f || val < -1.0f) { continue; }
|
||||
window->DrawList->AddCircleFilled(ImVec2(((float)i * increment) + min.x, ((val + 1) * (size.y*0.5f)) + min.y), 2, col);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
namespace ImGui {
|
||||
class SymbolDiagram {
|
||||
public:
|
||||
SymbolDiagram();
|
||||
SymbolDiagram(float _scale = 1.0f);
|
||||
|
||||
void draw(const ImVec2& size_arg = ImVec2(0, 0));
|
||||
|
||||
@ -19,6 +19,7 @@ namespace ImGui {
|
||||
private:
|
||||
std::mutex bufferMtx;
|
||||
float buffer[1024];
|
||||
float _scale;
|
||||
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user