Added a noise blanker for SSB and DSB

This commit is contained in:
AlexandreRouma
2021-12-10 22:25:22 +01:00
parent 241632288e
commit 15010cff01
11 changed files with 84 additions and 65 deletions

View File

@ -267,7 +267,7 @@ namespace dsp {
public:
NoiseBlanker() {}
NoiseBlanker(stream<complex_t>* in, float attack, float decay, float threshold, float level, float sampleRate) { init(in, attack, decay, threshold, level, sampleRate); }
NoiseBlanker(stream<complex_t>* in, float level) { init(in, level); }
~NoiseBlanker() {
if (!generic_block<NoiseBlanker>::_block_init) { return; }
@ -275,16 +275,9 @@ namespace dsp {
volk_free(ampBuf);
}
void init(stream<complex_t>* in, float attack, float decay, float threshold, float level, float sampleRate) {
void init(stream<complex_t>* in, float level) {
_in = in;
_attack = attack;
_decay = decay;
_threshold = powf(10.0f, threshold / 10.0f);
_level = level;
_sampleRate = sampleRate;
_inv_attack = 1.0f - _attack;
_inv_decay = 1.0f - _decay;
_level = powf(10.0f, level / 10.0f);;
ampBuf = (float*)volk_malloc(STREAM_BUFFER_SIZE*sizeof(float), volk_get_alignment());
@ -293,28 +286,8 @@ namespace dsp {
generic_block<NoiseBlanker>::_block_init = true;
}
void setAttack(float attack) {
_attack = attack;
_inv_attack = 1.0f - _attack;
}
void setDecay(float decay) {
_decay = decay;
_inv_decay = 1.0f - _decay;
}
void setThreshold(float threshold) {
_threshold = powf(10.0f, threshold / 10.0f);
spdlog::warn("Threshold {0}", _threshold);
}
void setLevel(float level) {
_level = level;
}
void setSampleRate(float sampleRate) {
_sampleRate = sampleRate;
// TODO: Change parameters if the algo needs it
_level = powf(10.0f, level / 10.0f);
}
void setInput(stream<complex_t>* in) {
@ -334,33 +307,13 @@ namespace dsp {
// Get amplitudes
volk_32fc_magnitude_32f(ampBuf, (lv_32fc_t*)_in->readBuf, count);
// Apply filtering and threshold
float val;
// Hard limit the amplitude
complex_t inVal;
for (int i = 0; i < count; i++) {
// Filter using attack/threshold methode
val = ampBuf[i];
if (val > lastValue) {
lastValue = (_inv_attack*lastValue) + (_attack*val);
}
else {
lastValue = (_inv_decay*lastValue) + (_decay*val);
}
// Apply threshold and invert
if (lastValue > _threshold) {
ampBuf[i] = _threshold / (lastValue * _level);
if (ampBuf[i] == 0) {
spdlog::warn("WTF???");
}
}
else {
ampBuf[i] = 1.0f;
}
inVal = _in->readBuf[i];
out.writeBuf[i] = (ampBuf[i] > _level) ? inVal * (_level / inVal.amplitude()) : inVal;
}
// Multiply
volk_32fc_32f_multiply_32fc((lv_32fc_t*)out.writeBuf, (lv_32fc_t*)_in->readBuf, ampBuf, count);
_in->flush();
if (!out.swap(count)) { return -1; }
return count;
@ -371,15 +324,7 @@ namespace dsp {
private:
float* ampBuf;
float _attack;
float _decay;
float _inv_attack;
float _inv_decay;
float _threshold;
float _level;
float _sampleRate;
float lastValue = 0.0f;
stream<complex_t>* _in;