From ee53b28e60ca272ff8ba5f6913ad635a6aa64dde Mon Sep 17 00:00:00 2001 From: Brumi-2021 Date: Sun, 30 Apr 2023 18:03:40 +0200 Subject: [PATCH] Solving Noise generation in Signal gen App --- firmware/application/apps/ui_siggen.hpp | 22 +++++++++------- firmware/baseband/proc_siggen.cpp | 35 ++++++++++++++++--------- firmware/baseband/proc_siggen.hpp | 11 ++++---- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/firmware/application/apps/ui_siggen.hpp b/firmware/application/apps/ui_siggen.hpp index f55d19d1..58ed49b8 100644 --- a/firmware/application/apps/ui_siggen.hpp +++ b/firmware/application/apps/ui_siggen.hpp @@ -48,14 +48,16 @@ private: void update_tone(); void on_tx_progress(const uint32_t progress, const bool done); - const std::string shape_strings[7] = { - "CW", - "Sine", - "Triangle", - "Saw up", - "Saw down", - "Square", - "Noise" + const std::string shape_strings[9] = { + "CW ", + "Sine ", + "Triangle ", + "Saw up ", + "Saw down ", + "Square ", + "Noise n20Khz", + "Noise n10khz", + "Noise n5khz " }; bool auto_update { false }; @@ -78,7 +80,9 @@ private: { &bitmap_sig_saw_up, 3 }, { &bitmap_sig_saw_down, 4 }, { &bitmap_sig_square, 5 }, - { &bitmap_sig_noise, 6 } + { &bitmap_sig_noise, 6 }, + { &bitmap_sig_noise, 7 }, + { &bitmap_sig_noise, 8 } } }; diff --git a/firmware/baseband/proc_siggen.cpp b/firmware/baseband/proc_siggen.cpp index dce9d479..2fdc5545 100644 --- a/firmware/baseband/proc_siggen.cpp +++ b/firmware/baseband/proc_siggen.cpp @@ -49,7 +49,7 @@ void SigGenProcessor::execute(const buffer_c8_t& buffer) { // Sine sample = (sine_table_i8[(tone_phase & 0xFF000000) >> 24]); } else if (tone_shape == 2) { - // Tri + // Triangle int8_t a = (tone_phase & 0xFF000000) >> 24; sample = (a & 0x80) ? ((a << 1) ^ 0xFF) - 0x80 : (a << 1) + 0x80; } else if (tone_shape == 3) { @@ -61,24 +61,34 @@ void SigGenProcessor::execute(const buffer_c8_t& buffer) { } else if (tone_shape == 5) { // Square sample = (((tone_phase & 0xFF000000) >> 24) & 0x80) ? 127 : -128; - } else if (tone_shape == 6) { - // Noise - sample = (lfsr & 0xFF000000) >> 24; - feedback = ((lfsr >> 31) ^ (lfsr >> 29) ^ (lfsr >> 15) ^ (lfsr >> 11)) & 1; - lfsr = (lfsr << 1) | feedback; - if (!lfsr) lfsr = 0x1337; // Shouldn't do this :( + } else if (tone_shape == 6) { // taps: 6 5; feedback polynomial: x^6 + x^5 + 1 , Periode 63 = 2^n-1,it generates armonincs n x 20Khz + // White Noise generator, pseudo random noise generator, 8 bits linear-feedback shift register (LFSR) algorithm, variant Fibonacci. + // https://en.wikipedia.org/wiki/Linear-feedback_shift_register + bit = ((lfsr >> 2) ^ (lfsr >> 3)) & 1; + lfsr = (lfsr >> 1) | (bit << 7); + sample = lfsr; + } else if (tone_shape == 7) { // taps: 7 6; feedback polynomial: x^7 + x^6 + 1 , Periode 127 = 2^n-1,it generates armonincs n x 10Khz + bit = ((lfsr >> 1) ^ (lfsr >> 2)) & 1; + lfsr = (lfsr >> 1) | (bit << 7); + sample = lfsr; + } else if (tone_shape == 8) { //taps:8,6,5,4;feedback polynomial: x^8 + x^6 + x^5 + x^4 + 1,Periode 255= 2^n-1, armonics n x 5khz + bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 4)) & 1; + lfsr = (lfsr >> 1) | (bit << 7); + sample = lfsr; + } + + if (tone_shape < 6) { + tone_phase += tone_delta; } - tone_phase += tone_delta; - - // Do FM + // Do FM modulation delta = sample * fm_delta; phase += delta; sphase = phase + (64 << 24); re = (sine_table_i8[(sphase & 0xFF000000) >> 24]); - im = (sine_table_i8[(phase & 0xFF000000) >> 24]); + im = (sine_table_i8[( phase & 0xFF000000) >> 24]); } buffer.p[i] = {re, im}; @@ -104,7 +114,8 @@ void SigGenProcessor::on_message(const Message* const msg) { fm_delta = message.bw * (0xFFFFFFULL / 1536000); tone_shape = message.shape; - lfsr = 0x54DF0119; + // lfsr = 0x54DF0119; + lfsr = seed_value ; configured = true; break; diff --git a/firmware/baseband/proc_siggen.hpp b/firmware/baseband/proc_siggen.hpp index f547cc0f..c4adcd8b 100644 --- a/firmware/baseband/proc_siggen.hpp +++ b/firmware/baseband/proc_siggen.hpp @@ -38,13 +38,14 @@ private: BasebandThread baseband_thread { 1536000, this, NORMALPRIO + 20, baseband::Direction::Transmit }; - uint32_t tone_delta { 0 }, fm_delta { }; - uint32_t lfsr { }, feedback { }, tone_shape { }; + uint32_t tone_delta { 0 }, fm_delta { },tone_phase { 0 }; + uint8_t tone_shape { }; uint32_t sample_count { 0 }; bool auto_off { }; - uint32_t tone_phase { 0 }, phase { 0 }, delta { 0 }, sphase { 0 }; - int8_t sample { 0 }; - int8_t re { 0 }, im { 0 }; + int32_t phase { 0 }, sphase { 0 }, delta { 0 }; // they may have sign . + int8_t sample { 0 }, re { 0 }, im { 0 }; // they may have sign . + uint8_t seed_value = {0x56}; // seed : any nonzero start state will work. + uint8_t lfsr { }, bit { }; // bit must be 8-bit to allow bit<<7 later in the code */ TXProgressMessage txprogress_message { }; };