new FM IF noise reduction + bugfix

pull/567/head
AlexandreRouma 2021-12-08 02:10:41 +01:00
rodzic 1594051a5d
commit 2a5671878f
13 zmienionych plików z 177 dodań i 5 usunięć

Wyświetl plik

@ -3,9 +3,143 @@
#include <dsp/utils/window_functions.h>
#include <fftw3.h>
#define NR_TAP_COUNT 4096
#define NR_TAP_COUNT 64
namespace dsp {
class FMIFNoiseReduction : public generic_block<FMIFNoiseReduction> {
public:
FMIFNoiseReduction() {}
FMIFNoiseReduction(stream<complex_t>* in, int tapCount) { init(in, tapCount); }
~FMIFNoiseReduction() {
if (!generic_block<FMIFNoiseReduction>::_block_init) { return; }
generic_block<FMIFNoiseReduction>::stop();
fftwf_destroy_plan(forwardPlan);
fftwf_destroy_plan(backwardPlan);
fftwf_free(delay);
fftwf_free(fft_in);
fftwf_free(fft_window);
fftwf_free(amp_buf);
fftwf_free(fft_cout);
fftwf_free(fft_fcout);
}
void init(stream<complex_t>* in, int tapCount) {
_in = in;
_tapCount = tapCount;
delay = (complex_t*)fftwf_malloc(sizeof(complex_t)*STREAM_BUFFER_SIZE);
fft_in = (complex_t*)fftwf_malloc(sizeof(complex_t)*_tapCount);
fft_window = (float*)fftwf_malloc(sizeof(float)*_tapCount);
amp_buf = (float*)fftwf_malloc(sizeof(float)*_tapCount);
fft_cout = (complex_t*)fftwf_malloc(sizeof(complex_t)*_tapCount);
fft_fcout = (complex_t*)fftwf_malloc(sizeof(complex_t)*_tapCount);
delay_start = &delay[_tapCount];
memset(delay, 0, sizeof(complex_t)*STREAM_BUFFER_SIZE);
memset(fft_in, 0, sizeof(complex_t)*_tapCount);
memset(amp_buf, 0, sizeof(float)*_tapCount);
memset(fft_cout, 0, sizeof(complex_t)*_tapCount);
memset(fft_fcout, 0, sizeof(complex_t)*_tapCount);
for (int i = 0; i < _tapCount; i++) {
fft_window[i] = window_function::blackman(i, _tapCount - 1);
}
forwardPlan = fftwf_plan_dft_1d(_tapCount, (fftwf_complex*)fft_in, (fftwf_complex*)fft_cout, FFTW_FORWARD, FFTW_ESTIMATE);
backwardPlan = fftwf_plan_dft_1d(_tapCount, (fftwf_complex*)fft_cout, (fftwf_complex*)fft_fcout, FFTW_BACKWARD, FFTW_ESTIMATE);
generic_block<FMIFNoiseReduction>::registerInput(_in);
generic_block<FMIFNoiseReduction>::registerOutput(&out);
generic_block<FMIFNoiseReduction>::_block_init = true;
}
void setInput(stream<complex_t>* in) {
assert(generic_block<FMIFNoiseReduction>::_block_init);
std::lock_guard<std::mutex> lck(generic_block<FMIFNoiseReduction>::ctrlMtx);
generic_block<FMIFNoiseReduction>::tempStop();
generic_block<FMIFNoiseReduction>::unregisterInput(_in);
_in = in;
generic_block<FMIFNoiseReduction>::registerInput(_in);
generic_block<FMIFNoiseReduction>::tempStart();
}
void setLevel(float level) {
_level = powf(10.0f, level / 10.0f);
}
int run() {
int count = _in->read();
if (count < 0) { return -1; }
// Bypass
if (!bypass) {
memcpy(out.writeBuf, _in->readBuf, count * sizeof(complex_t));
_in->flush();
if (!out.swap(count)) { return -1; }
return count;
}
// Write to delay buffer
memcpy(delay_start, _in->readBuf, count * sizeof(complex_t));
uint32_t idx = 0;
float actLevel = 0;
// Iterate the FFT
for (int i = 0; i < count; i++) {
// Apply windows
volk_32fc_32f_multiply_32fc((lv_32fc_t*)fft_in, (lv_32fc_t*)&delay[i], fft_window, _tapCount);
// Do forward FFT
fftwf_execute(forwardPlan);
// Process bins here
volk_32fc_magnitude_32f(amp_buf, (lv_32fc_t*)fft_cout, _tapCount);
volk_32f_index_max_32u(&idx, amp_buf, _tapCount);
for (int j = 0; j < _tapCount; j++) {
if (j == idx) { continue; }
fft_cout[j] = {0, 0};
}
// Do reverse FFT and get first element
fftwf_execute(backwardPlan);
out.writeBuf[i] = fft_fcout[_tapCount/2];
}
volk_32f_s32f_multiply_32f((float*)out.writeBuf, (float*)out.writeBuf, 1.0f/(float)_tapCount, count * 2);
// Copy last values to delay
memmove(delay, &delay[count], _tapCount * sizeof(complex_t));
_in->flush();
if (!out.swap(count)) { return -1; }
return count;
}
bool bypass = true;
stream<complex_t> out;
float _level = 0.0f;
private:
stream<complex_t>* _in;
fftwf_plan forwardPlan;
fftwf_plan backwardPlan;
complex_t* delay;
complex_t* fft_in;
float* fft_window;
float* amp_buf;
complex_t* delay_start;
complex_t* fft_cout;
complex_t* fft_fcout;
int _tapCount;
};
class FFTNoiseReduction : public generic_block<FFTNoiseReduction> {
public:
FFTNoiseReduction() {}

Wyświetl plik

@ -37,6 +37,7 @@ namespace demod {
virtual bool getPostProcEnabled() = 0;
virtual int getDefaultDeemphasisMode() = 0;
virtual double getAFBandwidth(double bandwidth) = 0;
virtual bool getFMIFNRAllowed() = 0;
virtual bool getDynamicAFBandwidth() = 0;
virtual dsp::stream<dsp::stereo_t>* getOutput() = 0;

Wyświetl plik

@ -67,6 +67,7 @@ namespace demod {
int getDefaultDeemphasisMode() { return DEEMP_MODE_NONE; }
double getAFBandwidth(double bandwidth) { return bandwidth / 2.0; }
bool getDynamicAFBandwidth() { return true; }
bool getFMIFNRAllowed() { return false; }
dsp::stream<dsp::stereo_t>* getOutput() { return &m2s.out; }
private:

Wyświetl plik

@ -68,6 +68,7 @@ namespace demod {
int getDefaultDeemphasisMode() { return DEEMP_MODE_NONE; }
double getAFBandwidth(double bandwidth) { return (bandwidth / 2.0) + 1000.0; }
bool getDynamicAFBandwidth() { return true; }
bool getFMIFNRAllowed() { return false; }
dsp::stream<dsp::stereo_t>* getOutput() { return &m2s.out; }
private:

Wyświetl plik

@ -69,6 +69,7 @@ namespace demod {
int getDefaultDeemphasisMode() { return DEEMP_MODE_NONE; }
double getAFBandwidth(double bandwidth) { return bandwidth / 2.0; }
bool getDynamicAFBandwidth() { return true; }
bool getFMIFNRAllowed() { return false; }
dsp::stream<dsp::stereo_t>* getOutput() { return &m2s.out; }
private:

Wyświetl plik

@ -69,6 +69,7 @@ namespace demod {
int getDefaultDeemphasisMode() { return DEEMP_MODE_NONE; }
double getAFBandwidth(double bandwidth) { return bandwidth; }
bool getDynamicAFBandwidth() { return true; }
bool getFMIFNRAllowed() { return false; }
dsp::stream<dsp::stereo_t>* getOutput() { return &m2s.out; }
private:

Wyświetl plik

@ -61,6 +61,7 @@ namespace demod {
int getDefaultDeemphasisMode() { return DEEMP_MODE_NONE; }
double getAFBandwidth(double bandwidth) { return bandwidth / 2.0; }
bool getDynamicAFBandwidth() { return true; }
bool getFMIFNRAllowed() { return true; }
dsp::stream<dsp::stereo_t>* getOutput() { return &demod.out; }
private:

Wyświetl plik

@ -62,6 +62,7 @@ namespace demod {
int getDefaultDeemphasisMode() { return DEEMP_MODE_NONE; }
double getAFBandwidth(double bandwidth) { return bandwidth; }
bool getDynamicAFBandwidth() { return false; }
bool getFMIFNRAllowed() { return false; }
dsp::stream<dsp::stereo_t>* getOutput() { return &c2s.out; }
private:

Wyświetl plik

@ -69,6 +69,7 @@ namespace demod {
int getDefaultDeemphasisMode() { return DEEMP_MODE_NONE; }
double getAFBandwidth(double bandwidth) { return bandwidth; }
bool getDynamicAFBandwidth() { return true; }
bool getFMIFNRAllowed() { return false; }
dsp::stream<dsp::stereo_t>* getOutput() { return &m2s.out; }
private:

Wyświetl plik

@ -85,6 +85,7 @@ namespace demod {
int getDefaultDeemphasisMode() { return DEEMP_MODE_50US; }
double getAFBandwidth(double bandwidth) { return 16000.0; }
bool getDynamicAFBandwidth() { return false; }
bool getFMIFNRAllowed() { return true; }
dsp::stream<dsp::stereo_t>* getOutput() { return stereo ? demodStereo.out : &demod.out; }
// ============= DEDICATED FUNCTIONS =============

Wyświetl plik

@ -59,8 +59,10 @@ public:
ifChainOutputChanged.handler = ifChainOutputChangeHandler;
ifChain.init(vfo->output, &ifChainOutputChanged);
fmnr.block.init(NULL, 64);
squelch.block.init(NULL, MIN_SQUELCH);
ifChain.add(&fmnr);
ifChain.add(&squelch);
// Load configuration for and enabled all demodulators
@ -254,6 +256,13 @@ private:
}
if (!_this->squelchEnabled && _this->enabled) { style::endDisabled(); }
// FM IF Noise Reduction
if (_this->FMIFNRAllowed) {
if (ImGui::Checkbox("IF Noise Reduction##_radio_fmifnr_ena_", &_this->FMIFNREnabled)) {
_this->setFMIFNREnabled(_this->FMIFNREnabled);
}
}
// Demodulator specific menu
_this->selectedDemod->showMenu();
@ -300,7 +309,9 @@ private:
deempMode = DEEMP_MODE_NONE;
squelchEnabled = false;
postProcEnabled = selectedDemod->getPostProcEnabled();
if (config.conf[name][selectedDemod->getName()].contains("snapInterval")) {
FMIFNRAllowed = selectedDemod->getFMIFNRAllowed();
FMIFNREnabled = false;
if (config.conf[name][selectedDemod->getName()].contains("bandwidth")) {
bandwidth = config.conf[name][selectedDemod->getName()]["bandwidth"];
bandwidth = std::clamp<double>(bandwidth, minBandwidth, maxBandwidth);
}
@ -316,6 +327,9 @@ private:
if (config.conf[name][selectedDemod->getName()].contains("deempMode")) {
deempMode = config.conf[name][selectedDemod->getName()]["deempMode"];
}
if (config.conf[name][selectedDemod->getName()].contains("FMIFNREnabled")) {
FMIFNREnabled = config.conf[name][selectedDemod->getName()]["FMIFNREnabled"];
}
deempMode = std::clamp<int>(deempMode, 0, _DEEMP_MODE_COUNT-1);
// Configure VFO
@ -326,7 +340,10 @@ private:
vfo->setSampleRate(selectedDemod->getIFSampleRate(), bandwidth);
}
// Configure IF chain
// Configure FM IF Noise Reduction
setFMIFNREnabled(FMIFNRAllowed ? FMIFNREnabled : false);
// Configure squelch
squelch.block.setLevel(squelchLevel);
setSquelchEnabled(squelchEnabled);
@ -439,6 +456,17 @@ private:
config.release(true);
}
void setFMIFNREnabled(bool enabled) {
FMIFNREnabled = enabled;
if (!selectedDemod) { return; }
ifChain.setState(&fmnr, FMIFNREnabled);
// Save config
config.acquire();
config.conf[name][selectedDemod->getName()]["FMIFNREnabled"] = FMIFNREnabled;
config.release(true);
}
static void vfoUserChangedBandwidthHandler(double newBw, void* ctx) {
RadioModule* _this = (RadioModule*)ctx;
_this->setBandwidth(newBw);
@ -521,6 +549,7 @@ private:
// IF chain
dsp::Chain<dsp::complex_t> ifChain;
dsp::ChainLink<dsp::FMIFNoiseReduction, dsp::complex_t> fmnr;
dsp::ChainLink<dsp::Squelch, dsp::complex_t> squelch;
// Audio chain
@ -547,6 +576,8 @@ private:
int deempMode = DEEMP_MODE_NONE;
bool deempAllowed;
bool postProcEnabled;
bool FMIFNRAllowed;
bool FMIFNREnabled = false;
const double MIN_SQUELCH = -100.0;
const double MAX_SQUELCH = 0.0;

Wyświetl plik

@ -53,7 +53,6 @@ bundle_install_binary $BUNDLE $BUNDLE/Contents/Plugins $BUILD_DIR/sink_modules/n
bundle_install_binary $BUNDLE $BUNDLE/Contents/Plugins $BUILD_DIR/decoder_modules/m17_decoder/m17_decoder.dylib
bundle_install_binary $BUNDLE $BUNDLE/Contents/Plugins $BUILD_DIR/decoder_modules/meteor_demodulator/meteor_demodulator.dylib
bundle_install_binary $BUNDLE $BUNDLE/Contents/Plugins $BUILD_DIR/decoder_modules/radio/radio.dylib
bundle_install_binary $BUNDLE $BUNDLE/Contents/Plugins $BUILD_DIR/decoder_modules/new_radio/new_radio.dylib
bundle_install_binary $BUNDLE $BUNDLE/Contents/Plugins $BUILD_DIR/misc_modules/discord_integration/discord_integration.dylib
bundle_install_binary $BUNDLE $BUNDLE/Contents/Plugins $BUILD_DIR/misc_modules/frequency_manager/frequency_manager.dylib

Wyświetl plik

@ -57,7 +57,6 @@ cp "C:/Program Files/codec2/lib/libcodec2.dll" sdrpp_windows_x64/
cp $build_dir/decoder_modules/meteor_demodulator/Release/meteor_demodulator.dll sdrpp_windows_x64/modules/
cp $build_dir/decoder_modules/radio/Release/radio.dll sdrpp_windows_x64/modules/
cp $build_dir/decoder_modules/new_radio/Release/new_radio.dll sdrpp_windows_x64/modules/
# Copy misc modules