Skip to content

Commit

Permalink
Merge pull request #8 from paulh002/Dev
Browse files Browse the repository at this point in the history
Noise reduction
  • Loading branch information
paulh002 authored Jun 14, 2022
2 parents 279a723 + 97e688c commit a2af6fb
Show file tree
Hide file tree
Showing 45 changed files with 3,078 additions and 3,942 deletions.
32 changes: 27 additions & 5 deletions AMDemodulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <thread>
#include "PeakLevelDetector.h"


static shared_ptr<AMDemodulator> sp_amdemod;
std::mutex amdemod_mutex;

Expand Down Expand Up @@ -81,16 +82,20 @@ void AMDemodulator::operator()()
const auto startTime = std::chrono::high_resolution_clock::now();
auto timeLastPrint = std::chrono::high_resolution_clock::now();
std::chrono::high_resolution_clock::time_point now, start1;

AudioProcessor Agc;

int ifilter {-1}, span, rcount {0}, dropped_frames {0};
SampleVector audiosamples, audioframes;
unique_lock<mutex> lock_am(amdemod_mutex);
IQSampleVector iqsamples;
long long pr_time{0};
int vsize, passes{0};

pNoisesp = make_unique<SpectralNoiseReduction>(m_pcmrate, tuple<float,float>(0, 2500));
//pLMS = make_unique<LMSNoisereducer>(); switched off memory leak in library
pXanr = make_unique<Xanr>();

Agc.prepareToPlay(audio_output->get_samplerate());
Agc.setThresholdDB(gagc.get_threshold());
Agc.setRatio(10);
Expand All @@ -114,7 +119,7 @@ void AMDemodulator::operator()()
set_filter(m_pcmrate, ifilter);
}

IQSampleVector iqsamples = m_source_buffer->pull();
iqsamples = m_source_buffer->pull();
if (iqsamples.empty())
{
printf("No samples queued 2\n");
Expand Down Expand Up @@ -148,9 +153,25 @@ void AMDemodulator::operator()()
{
if ((audio_output->queued_samples() / 2) < 4096)
{
SampleVector audio_stereo;
SampleVector audio_stereo, audio_noise;

mono_to_left_right(audioframes, audio_stereo);
switch (gbar.get_noise())
{
case 1:
pXanr->Process(audioframes, audio_noise);
break;
case 2:
pNoisesp->Process(audioframes, audio_noise);
mono_to_left_right(audio_noise, audio_stereo);
break;
case 3:
pNoisesp->Process_Kim1_NR(audioframes, audio_noise);
mono_to_left_right(audio_noise, audio_stereo);
break;
default:
mono_to_left_right(audioframes, audio_stereo);
break;
}
audio_output->write(audio_stereo);
audioframes.clear();
}
Expand Down Expand Up @@ -212,6 +233,7 @@ void AMDemodulator::process(const IQSampleVector& samples_in, SampleVector& audi
calc_if_level(filter1);
if (gsetup.get_cw())
pMDecoder->decode(filter1);

for (auto col : filter1)
{
float v;
Expand Down
5 changes: 5 additions & 0 deletions AMDemodulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "AudioOutput.h"
#include "Demodulator.h"
#include "MorseDecoder.h"
#include "LMSNoisereducer.h"
#include "SpectralNoiseReduction.h"

class AMDemodulator : public Demodulator
{
Expand All @@ -32,6 +34,9 @@ class AMDemodulator : public Demodulator
Agc_class agc;
int m_iagc = 0;
unique_ptr<MorseDecoder> pMDecoder;
unique_ptr<LMSNoisereducer> pLMS;
unique_ptr<Xanr> pXanr;
unique_ptr<SpectralNoiseReduction> pNoisesp;
};

void select_filter(int ifilter);
7 changes: 4 additions & 3 deletions AMModulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void AMModulator::process(const IQSampleVector& samples_in, SampleVector& sample
{
complex<float> f;
ampmodem_modulate(modAM, col, &f);
//printf("%f;%f;%f \n", col, f.real(), f.imag());
//printf("audio %f;I %f;Q %f \n", col, f.real(), f.imag());
buf_mod.push_back(f);
}
double if_rms = rms_level_approx(buf_mod);
Expand All @@ -169,9 +169,10 @@ void AMModulator::process(const IQSampleVector& samples_in, SampleVector& sample
complex<float> f;
int16_t i, q;

i = (int16_t)round(col.real() * 16384.0f);
q = (int16_t)round(col.imag() * 16384.0f);
i = (int16_t)round(col.real() * 16384.0f) *10;
q = (int16_t)round(col.imag() * 16384.0f) *10;
IQSample16 s16 {i, q};
//printf("i %d q %d\n",i, q);
buf_out16.push_back(s16);
}
mix_up_fft(buf_filter, buf_mod);
Expand Down
8 changes: 4 additions & 4 deletions AudioInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ bool AudioInput::open(unsigned int device)
void AudioInput::set_volume(int vol)
{
// log volume
m_volume = exp(((double)vol * 6.908) / 100.0) /10.0;
m_volume = exp(((double)vol * 6.908) / 100.0) / 5.0;
printf("mic vol %f\n", (float)m_volume);
}

Expand Down Expand Up @@ -188,13 +188,13 @@ double AudioInput::Nexttone()
{
double angle = (asteps*cw_keyer_sidetone_frequency)*TWOPIOVERSAMPLERATE;
if (++asteps >= 48000) asteps = 0;
return sin(angle);
return sin(angle) / 100.0;
}

void AudioInput::ToneBuffer()
{
SampleVector buf;
printf("tone %d \n", tune_tone);

for (int i = 0; i < bufferFrames; i++)
{
Sample f;
Expand All @@ -219,7 +219,7 @@ double AudioInput::NextTwotone()
double angle = (asteps*cw_keyer_sidetone_frequency)*TWOPIOVERSAMPLERATE;
double angle2 = (asteps*cw_keyer_sidetone_frequency2)*TWOPIOVERSAMPLERATE;
if (++asteps >= 48000) asteps = 0;
return (sin(angle) + sin(angle)) /2.0;
return (sin(angle) + sin(angle)) /200.0;
}

int AudioInput::queued_samples()
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(sdrberry)
file(GLOB_RECURSE SOURCES ./lvgl/src/*.c)
set(LIBRARIES_FROM_REFERENCES -lpthread -lbluetooth -lSoapySDR -latomic -ltinyb -lliquid -lasound -lfftw3 -Wl,-verbose)
include_directories(lvgl ft8)
add_executable(sdrberry sdrberry.cpp FT8Demodulator.cpp FT8Demodulator.h HidDev.cpp HidDev.h PeakLevelDetector.cpp PeakLevelDetector.h ft8/fft.cpp ft8/fft.h ft8/ft8.cpp ft8/ft8.h ft8/libldpc.cpp ft8/unpack.cpp ft8/util.cpp ft8/osd.cpp ft8/arrays.h gui_ft8.cpp gui_ft8.h gui_speech.cpp gui_speech.h FMDemodulator.cpp Agc_class.cpp AMDemodulator.cpp AMModulator.cpp AudioInput.cpp AudioOutput.cpp BandFilter.cpp ble_interface.cpp Catinterface.cpp configfile.cpp configoption.cpp Demodulator.cpp Filter.cpp FmDecode.cpp FMDemodulator.cpp FMModulator.cpp FreeSansOblique24.c FreeSansOblique32.c FreeSansOblique42.c FT891_CAT.cpp gui_agc.cpp Gui_band.cpp gui_bar.cpp gui_setup.cpp gui_top_bar.cpp gui_tx.cpp gui_vfo.cpp Keyboard.cpp MidiControle.cpp Mouse.cpp PCF8574.cpp RtMidi.cpp SdrDevice.cpp sdrstream.cpp Settings.cpp vfo.cpp RtAudio.cpp Waterfall.cpp wiringPiI2C.c wiringSerial.c lv_drivers/display/fbdev.c lv_drivers/indev/evdev.c lv_drivers/indev/mouse.c Agc_class.h AMDemodulator.h AMModulator.h Audiodefs.h AudioInput.h AudioOutput.h BandFilter.h ble_interface.h Catinterface.h configfile.h configoption.h DataBuffer.h Demodulator.h Filter.h FmDecode.h FMDemodulator.h FMModulator.h FT891_CAT.h gattlib.h gui_agc.h Gui_band.h gui_bar.h gui_setup.h gui_top_bar.h gui_tx.h gui_vfo.h Keyboard.h liquid.h liquid.internal.h lv_conf.h lv_drv_conf.h MidiControle.h Mouse.h PCF8574.h RtAudio.h RtMidi.h sdrberry.h SdrDevice.h sdrstream.h sma.h Settings.h vfo.h Waterfall.h wiringPiI2C.h wiringSerial.h strlib.cpp strlib.h MorseDecoder.cpp MorseDecoder.h EchoAudio.cpp EchoAudio.h FreeSans42.c ${SOURCES} lv_drivers/indev/evdev.h)
add_executable(sdrberry sdrberry.cpp FT8Demodulator.cpp FT8Demodulator.h SpectralNoiseReduction.cpp SpectralNoiseReduction.h LMSNoisereducer.cpp LMSNoisereducer.h HidDev.cpp HidDev.h PeakLevelDetector.cpp PeakLevelDetector.h ft8/fft.cpp ft8/fft.h ft8/ft8.cpp ft8/ft8.h ft8/libldpc.cpp ft8/unpack.cpp ft8/util.cpp ft8/osd.cpp ft8/arrays.h gui_ft8.cpp gui_ft8.h gui_rx.h gui_rx.cpp gui_speech.cpp gui_speech.h FMDemodulator.cpp Agc_class.cpp AMDemodulator.cpp AMModulator.cpp AudioInput.cpp AudioOutput.cpp BandFilter.cpp ble_interface.cpp Catinterface.cpp configfile.cpp configoption.cpp Demodulator.cpp Filter.cpp FmDecode.cpp FMDemodulator.cpp FMModulator.cpp FreeSansOblique24.c FreeSansOblique32.c FreeSansOblique42.c FT891_CAT.cpp gui_agc.cpp Gui_band.cpp gui_bar.cpp gui_setup.cpp gui_top_bar.cpp gui_tx.cpp gui_vfo.cpp Keyboard.cpp MidiControle.cpp Mouse.cpp PCF8574.cpp RtMidi.cpp SdrDevice.cpp sdrstream.cpp Settings.cpp vfo.cpp RtAudio.cpp Waterfall.cpp wiringPiI2C.c wiringSerial.c lv_drivers/display/fbdev.c lv_drivers/indev/evdev.c lv_drivers/indev/mouse.c Agc_class.h AMDemodulator.h AMModulator.h Audiodefs.h AudioInput.h AudioOutput.h BandFilter.h ble_interface.h Catinterface.h configfile.h configoption.h DataBuffer.h Demodulator.h Filter.h FmDecode.h FMDemodulator.h FMModulator.h FT891_CAT.h gattlib.h gui_agc.h Gui_band.h gui_bar.h gui_setup.h gui_top_bar.h gui_tx.h gui_vfo.h Keyboard.h liquid.h liquid.internal.h lv_conf.h lv_drv_conf.h MidiControle.h Mouse.h PCF8574.h RtAudio.h RtMidi.h sdrberry.h SdrDevice.h sdrstream.h sma.h Settings.h vfo.h Waterfall.h wiringPiI2C.h wiringSerial.h strlib.cpp strlib.h MorseDecoder.cpp MorseDecoder.h EchoAudio.cpp EchoAudio.h FreeSans42.c ${SOURCES} lv_drivers/indev/evdev.h)
target_compile_definitions(sdrberry PRIVATE __LINUX_ALSA__ LV_LVGL_H_INCLUDE_SIMPLE)
target_compile_options(sdrberry PRIVATE)
target_include_directories(sdrberry PRIVATE /usr/local/include/liquid)
Expand Down
10 changes: 6 additions & 4 deletions FmDecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ void* rx_fm_thread(void* fm_ptr)
usleep(5000);
continue;
}
int nosamples = iqsamples.size();
pfm->adjust_gain(iqsamples, gbar.get_if());
buf_mix.clear();
for (auto& col : iqsamples)
Expand All @@ -516,7 +517,7 @@ void* rx_fm_thread(void* fm_ptr)
buf_mix.push_back(v);
}

if (buf_mix.size() >= nfft_samples && fft_block == 5)
if (fft_block == 5)
{
fft_block = 0;
Fft_calc.process_samples(buf_mix);
Expand All @@ -528,7 +529,7 @@ void* rx_fm_thread(void* fm_ptr)
// Measure audio level.
samples_mean_rms(audiosamples, audio_mean, audio_rms);
audio_level = 0.95 * audio_level + 0.05 * audio_rms;

int noaudiosamples = audiosamples.size();
// Set nominal audio volume.
audio_output->adjust_gain(audiosamples);
for (auto& col : audiosamples)
Expand All @@ -540,11 +541,12 @@ void* rx_fm_thread(void* fm_ptr)
}
}
const auto now = std::chrono::high_resolution_clock::now();
if (timeLastPrint + std::chrono::seconds(1) < now)
if (timeLastPrint + std::chrono::seconds(20) < now)
{
timeLastPrint = now;
const auto timePassed = std::chrono::duration_cast<std::chrono::microseconds>(now - startTime);
printf("Queued Audio Samples %d underrun %d\n", audio_output->queued_samples() / 2, audio_output->get_underrun());
printf("Buffer queue %d Radio samples %d Audio Samples %d Queued Audio Samples %d underrun %d\n", fm_demod->source_buffer->size(),nosamples, noaudiosamples, audio_output->queued_samples() / 2, audio_output->get_underrun());
audio_output->clear_underrun();
}
iqsamples.clear();
audiosamples.clear();
Expand Down
17 changes: 17 additions & 0 deletions HidDev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ void HidDev::step_vfo()

if (bevent && in_event.type == EV_REL && in_event.code == 11)
{

switch (in_event.value)
{
case -240:
Expand All @@ -216,6 +217,9 @@ void HidDev::step_vfo()
case -480:
value = -3;
break;
case -600:
value = -4;
break;
case 240:
value = 1;
break;
Expand All @@ -225,6 +229,9 @@ void HidDev::step_vfo()
case 480:
value = 3;
break;
case 600:
value = 4;
break;
}
}

Expand All @@ -250,6 +257,11 @@ void HidDev::step_vfo()
vfo.step_vfo(step, false);
last_time = now;
}
if (timePassed.count() > 1 && value == 4)
{
vfo.step_vfo(1.5*step, false);
last_time = now;
}
if (timePassed.count() > 20 && value == -1)
{
vfo.step_vfo(-1 * step, false);
Expand All @@ -265,6 +277,11 @@ void HidDev::step_vfo()
vfo.step_vfo(-1 * step, false);
last_time = now;
}
if (timePassed.count() > 1 && value == -4)
{
vfo.step_vfo(-1.5 * step, false);
last_time = now;
}
return;
}

Expand Down
Loading

0 comments on commit a2af6fb

Please sign in to comment.