diff options
| author | hibby <d@vehibberd.com> | 2026-10-06 21:24:40 +0100 |
|---|---|---|
| committer | hibby <d@vehibberd.com> | 2026-10-06 21:24:40 +0100 |
| commit | c25cbdf6fb6d946175f4335fab77bfea601b56db (patch) | |
| tree | 6f7a94b7b81f999e434f61b73d47e337dfd26bf2 /lib/jtty/wava | |
| parent | 71f5689252c9f13e70e1c24856904475b2307809 (diff) | |
| parent | 0685fcada9f95fe90f70b387cd7d757da8209624 (diff) | |
Merge tag 'debian/3.2.0_rc1+dfsg-2' into debian/trixie-backportsdebian/trixie-backports
tagging wsjtx 3.2.0~rc1+dfsg-2
Diffstat (limited to 'lib/jtty/wava')
| -rw-r--r-- | lib/jtty/wava/plot_results.py | 57 | ||||
| -rw-r--r-- | lib/jtty/wava/rcu_bound_4fsk.f90 | 217 | ||||
| -rw-r--r-- | lib/jtty/wava/tbcc_4fsk_simulation.f90 | 167 | ||||
| -rw-r--r-- | lib/jtty/wava/tbcc_performance_curves.png | bin | 0 -> 263436 bytes |
4 files changed, 441 insertions, 0 deletions
diff --git a/lib/jtty/wava/plot_results.py b/lib/jtty/wava/plot_results.py new file mode 100644 index 00000000..0ef97628 --- /dev/null +++ b/lib/jtty/wava/plot_results.py @@ -0,0 +1,57 @@ + +import pandas as pd +import matplotlib.pyplot as plt + +# Read data logged from the Fortran testbench suite +try: + df = pd.read_csv('simulation_results.csv') +except FileNotFoundError: + print("Error: 'simulation_results.csv' not found. Execute the Fortran simulation program first.") + exit() + +# Strip whitespace from columns to avoid parsing mismatches +df['channel'] = df['channel'].str.strip() + +# Initialize Figure with high resolution DPI properties +fig, ax1 = plt.subplots(figsize=(10, 6), dpi=120) + +# Set up Primary Axis: Frame Correct-Decode Probability P(Correct) +ax1.set_xlabel('SNR in 2500 Hz Reference Bandwidth [dB]', fontsize=11, fontweight='bold') +ax1.set_ylabel('P(Correct Decode)', color='navy', fontsize=11, fontweight='bold') +ax1.set_xlim(0, -20) # Invert axis view to scan standard drop off descending to -20 dB +ax1.set_ylim(-0.05, 1.05) +ax1.grid(True, which='both', linestyle='--', alpha=0.5) + +# Set up Secondary Axis on the right side for 100 * UER (%) +ax2 = ax1.twinx() +ax2.set_ylabel('Undetected Error Rate: 100 * UER [%]', color='crimson', fontsize=11, fontweight='bold') +ax2.set_ylim(-0.1, 5.0) # Standard percentage visualization window + +# Isolate channel tracking configurations +awgn_data = df[df['channel'] == 'AWGN'].sort_values(by='snr_db', ascending=False) +rayleigh_data = df[df['channel'] == 'RAYLEIGH'].sort_values(by='snr_db', ascending=False) + +# Plot Primary Curves - P(Correct) +line1 = ax1.plot(awgn_data['snr_db'], awgn_data['p_correct'], + 'o-', color='navy', linewidth=2.0, label='AWGN: P(Correct)') +line2 = ax1.plot(rayleigh_data['snr_db'], rayleigh_data['p_correct'], + 's-', color='royalblue', linewidth=1.8, label='Rayleigh: P(Correct)') + +# Plot Secondary Curves - 100 * UER (%) +line3 = ax2.plot(awgn_data['snr_db'], awgn_data['uer_pct'], + '^--', color='crimson', linewidth=1.5, label='AWGN: 100*UER') +line4 = ax2.plot(rayleigh_data['snr_db'], rayleigh_data['uer_pct'], + 'v--', color='orange', linewidth=1.5, label='Rayleigh: 100*UER') + +# Align Legend structures coherently across shared dual axis space +lines = line1 + line2 + line3 + line4 +labels = [l.get_label() for l in lines] +ax1.legend(lines, labels, loc='center left', frameon=True, shadow=True) + +plt.title('TBCC (92,46) Noncoherent 4-FSK Performance Profile\nConstraint Length K=11, List Size L=8, Rate=1/2', + fontsize=12, fontweight='bold', pad=15) + +plt.tight_layout() +plt.savefig('tbcc_performance_curves.png', dpi=300) +print("Plot successfully rendered and saved as 'tbcc_performance_curves.png'") +plt.show() diff --git a/lib/jtty/wava/rcu_bound_4fsk.f90 b/lib/jtty/wava/rcu_bound_4fsk.f90 new file mode 100644 index 00000000..ae58793f --- /dev/null +++ b/lib/jtty/wava/rcu_bound_4fsk.f90 @@ -0,0 +1,217 @@ +program rcu_bound_4fsk + ! Random Coding Union (RCU) achievability bound (Polyanskiy-Poor-Verdu + ! 2010, eq. 2) for noncoherent 4-FSK over AWGN and Rayleigh-fading + ! channels, matched to the exact channel model and SNR convention used + ! in tbcc_4fsk_simulation.f90. This bounds the frame-error probability + ! of the BEST POSSIBLE code at (n=46 symbols, M=2^k messages) -- a + ! benchmark for how close an actual code+decoder gets to optimal, not a + ! statement about any particular code. + ! + ! Method: for a fixed received sequence r^n, each per-symbol term of the + ! competitor information density i(X_bar_t; r_t) is an exact discrete + ! 4-valued random variable (X_bar_t uniform over the 4 tones), so its + ! mean/variance are closed-form -- no inner Monte Carlo. Summing 46 such + ! terms and invoking the CLT gives the inner tail probability via erfc. + ! Only the outer expectation (over the true (x^n, r^n) pair) needs Monte + ! Carlo. + use, intrinsic :: iso_fortran_env, only: real32, real64, int32 + use omp_lib + implicit none + + integer(int32), save :: rng_state = 0 + !$omp threadprivate(rng_state) + + integer(int32), parameter :: N_SYM = 46 ! channel uses; matches TOTAL_K + + character(len=8) :: channel_type + character(len=16) :: arg + integer(int32) :: nargs, num_trials, kbits, e, s + real(real64) :: Mminus1, rcu + real(real32) :: snr_2500_db, esno_linear + real(real64) :: t_wall_start, t_wall_end + + nargs = iargc() + if (nargs /= 2) then + print *, 'Usage: rcu_bound_4fsk <num_trials> <k_bits>' + print *, ' k_bits: log2(number of equiprobable messages), e.g. 34' + stop + end if + call getarg(1, arg); read(arg,*) num_trials + call getarg(2, arg); read(arg,*) kbits + Mminus1 = 2.0_real64**kbits - 1.0_real64 + + call seed_random_generator() + + print *, "==========================================================================" + print *, "RCU achievability bound: noncoherent 4-FSK, n =", N_SYM, " symbols" + print *, "M = 2^", kbits, "equiprobable messages | OpenMP threads:", omp_get_max_threads() + print *, "==========================================================================" + + open(unit=25, file="rcu_bound_results.csv", status="unknown", action="write") + write(25,'(A)') "channel,snr_db,rcu_bound" + + t_wall_start = omp_get_wtime() + + do e = 1, 2 + channel_type = merge("AWGN ", "RAYLEIGH", e == 1) + print *, "" + print *, "--- Channel: ", trim(channel_type), " ---" + write(*,'(A10,A16)') "SNR_2500", "RCU bound" + do s = -5, -20, -1 + snr_2500_db = real(s, real32) + esno_linear = (10.0_real32**(snr_2500_db/10.0_real32)) * 80.0_real32 + call compute_rcu(channel_type, esno_linear, Mminus1, num_trials, rcu) + write(*,'(F10.1,ES16.5)') snr_2500_db, rcu + write(25,'(A,A,F6.1,A,ES14.6)') trim(channel_type), ",", snr_2500_db, ",", rcu + end do + end do + + close(25) + t_wall_end = omp_get_wtime() + print '(A,F8.2,A)', "Total wall-clock time: ", t_wall_end - t_wall_start, " s" + +contains + + subroutine compute_rcu(channel_type, esno, Mm1, ntrials, rcu_out) + character(len=*), intent(in) :: channel_type + real(real32), intent(in) :: esno + real(real64), intent(in) :: Mm1 + integer(int32), intent(in) :: ntrials + real(real64), intent(out) :: rcu_out + + integer(int32) :: trial, t, x_true, j + real(real32) :: r(0:3), n_i, n_q, hfade + real(real64) :: logL(0:3), S, i_true, mu_t, var_t, term + real(real64) :: T_sum, MU_sum, VAR_sum, tail, bound_val, acc + + acc = 0.0_real64 + + !$omp parallel do default(shared) & + !$omp private(trial, t, x_true, j, r, n_i, n_q, hfade, logL, S, & + !$omp i_true, mu_t, var_t, term, T_sum, MU_sum, VAR_sum, & + !$omp tail, bound_val) & + !$omp reduction(+:acc) schedule(static) + do trial = 1, ntrials + T_sum = 0.0_real64 + MU_sum = 0.0_real64 + VAR_sum = 0.0_real64 + + do t = 1, N_SYM + x_true = int(thread_uniform()*4.0_real32) + if (x_true > 3) x_true = 3 + + if (channel_type == "AWGN") then + hfade = 1.0_real32 + else + call box_muller(n_i); call box_muller(n_q) + hfade = sqrt(0.5_real32*(n_i**2 + n_q**2)) + end if + + do j = 0, 3 + call box_muller(n_i); call box_muller(n_q) + n_i = n_i*sqrt(0.5_real32); n_q = n_q*sqrt(0.5_real32) + if (j == x_true) then + r(j) = (hfade*sqrt(esno) + n_i)**2 + n_q**2 + else + r(j) = n_i**2 + n_q**2 + end if + end do + + ! log-likelihood-ratio log L(r) = log f_sig(r) - log f_noise(r) + do j = 0, 3 + if (channel_type == "AWGN") then + logL(j) = -real(esno,real64) & + + log_bessel_i0(2.0_real64*sqrt(real(esno,real64)*real(r(j),real64))) + else + logL(j) = -log(1.0_real64+real(esno,real64)) & + + real(r(j),real64)*real(esno,real64)/(1.0_real64+real(esno,real64)) + end if + end do + + S = logsumexp4(logL) ! log( sum_j L(r_j) ) + i_true = log(4.0_real64) + logL(x_true) - S + + mu_t = 0.0_real64 + do j = 0, 3 + mu_t = mu_t + (log(4.0_real64) + logL(j) - S) + end do + mu_t = mu_t/4.0_real64 + + var_t = 0.0_real64 + do j = 0, 3 + term = (log(4.0_real64) + logL(j) - S) - mu_t + var_t = var_t + term*term + end do + var_t = var_t/4.0_real64 + + T_sum = T_sum + i_true + MU_sum = MU_sum + mu_t + VAR_sum = VAR_sum + var_t + end do + + ! CLT approximation to Pr[ sum_t i(X_bar_t;r_t) >= T_sum ] + tail = 0.5_real64*erfc( (T_sum-MU_sum) / sqrt(2.0_real64*VAR_sum) ) + bound_val = min(1.0_real64, Mm1*tail) + acc = acc + bound_val + end do + !$omp end parallel do + + rcu_out = acc/real(ntrials, real64) + end subroutine compute_rcu + + pure function logsumexp4(v) result(s) + real(real64), intent(in) :: v(0:3) + real(real64) :: s, m + m = maxval(v) + s = m + log(sum(exp(v-m))) + end function logsumexp4 + + ! log(I0(x)), x>=0, via the standard Abramowitz & Stegun 9.8.1/9.8.2 + ! rational approximations (~1.6e-7 relative accuracy), evaluated in log + ! space for large x to avoid overflowing exp(x). + pure function log_bessel_i0(x) result(logi0) + real(real64), intent(in) :: x + real(real64) :: logi0, ax, y, bessi0 + + ax = abs(x) + if (ax < 3.75_real64) then + y = (x/3.75_real64)**2 + bessi0 = 1.0_real64 + y*(3.5156229_real64 + y*(3.0899424_real64 + & + y*(1.2067492_real64 + y*(0.2659732_real64 + & + y*(0.0360768_real64 + y*0.0045813_real64))))) + logi0 = log(bessi0) + else + y = 3.75_real64/ax + logi0 = ax - 0.5_real64*log(ax) + & + log(0.39894228_real64 + y*(0.01328592_real64 + & + y*(0.00225319_real64 + y*(-0.00157565_real64 + & + y*(0.00916281_real64 + y*(-0.02057706_real64 + & + y*(0.02635537_real64 + y*(-0.01647633_real64 + & + y*0.00392377_real64)))))))) + end if + end function log_bessel_i0 + + subroutine box_muller(rand_normal) + real(real32), intent(out) :: rand_normal + real(real32) :: u1, u2 + u1 = thread_uniform(); u2 = thread_uniform() + if (u1 < 1.0e-30_real32) u1 = 1.0e-30_real32 + rand_normal = sqrt(-2.0_real32*log(u1)) * cos(6.28318530718_real32*u2) + end subroutine box_muller + + function thread_uniform() result(u) + real(real32) :: u + rng_state = ieor(rng_state, ishft(rng_state, 13)) + rng_state = ieor(rng_state, ishft(rng_state, -17)) + rng_state = ieor(rng_state, ishft(rng_state, 5)) + u = real(iand(rng_state, huge(rng_state)), real32) / real(huge(rng_state), real32) + end function thread_uniform + + subroutine seed_random_generator() + !$omp parallel default(shared) + rng_state = 9999_int32 + 104729_int32*int(omp_get_thread_num(), int32) + if (rng_state == 0_int32) rng_state = 1_int32 + !$omp end parallel + end subroutine seed_random_generator + +end program rcu_bound_4fsk diff --git a/lib/jtty/wava/tbcc_4fsk_simulation.f90 b/lib/jtty/wava/tbcc_4fsk_simulation.f90 new file mode 100644 index 00000000..d6512f27 --- /dev/null +++ b/lib/jtty/wava/tbcc_4fsk_simulation.f90 @@ -0,0 +1,167 @@ +program tbcc_4fsk_simulation + + use, intrinsic :: iso_fortran_env, only: real32, int32, real64 + use omp_lib + use tbcc + implicit none + + real(real64) :: t_wall_start, t_wall_end + + character(len=8) :: channel_type + character(len=8) :: arg + real(real32) :: snr_2500_db, esno_linear + + + integer(int32) :: tx_payload(PAYLOAD_BITS), tx_symbols(TOTAL_K) + real(real32) :: rx_tone_energies(0:3, TOTAL_K) + integer(int32) :: rx_decoded(PAYLOAD_BITS) + + integer(int32) :: correct_frames, undetected_errors, total_detected_errors, total_frames + integer(int32) :: num_frames, L_size, wava_iters, isnr1, isnr2 + real(real32) :: p_correct, uer + integer(int32) :: f, e, t, s, i, nargs + real(real32) :: n_i, n_q, h_fade, ch_i, ch_q, xsnr + logical :: decode_success, payload_match + + nargs=iargc() + if(nargs.ne.5) then + print*,'Usage: tbcc_sim nu L iters Nmax SNR' + print*,'Example: tbcc_sim 9 4 2 10000 0' + stop + endif + + call getarg(1,arg) + read(arg,*) memory_nu + call getarg(2,arg) + read(arg,*) L_size ! List size + call getarg(3,arg) + read(arg,*) wava_iters ! Trellis wraps for tail-biting convergence + call getarg(4,arg) + read(arg,*) num_frames + call getarg(5,arg) + read(arg,*) xsnr + + call seed_random_generator() + + print *, "==========================================================================" + print *, "Tail-Biting Convolutional Code (92,46) Noncoherent 4-FSK Simulation Suite" + print *, "Symbol Rate = 31.25 Baud | Reference Bandwidth = 2500 Hz" + print *, "OpenMP threads available: ", omp_get_max_threads() + print *, "Writing data logs directly to simulation_results.csv..." + print *, "==========================================================================" + + ! Open CSV data output buffer file + open(unit=24, file="simulation_results.dat", status="unknown", action="write") + open(unit=25, file="simulation_results.csv", status="unknown", action="write") + write(24, '(A)') "snr,frames,good,uer" + write(25, '(A)') "channel,snr_db,p_correct,uer_pct" + + t_wall_start = omp_get_wtime() + call tbcc_init(memory_nu) + total_frames = 0 + + do e = 1, 2 + channel_type = merge("AWGN ", "RAYLEIGH", e == 1) + print *, "" + print *, "--- Channel Profile: ", trim(channel_type), " ---" + write(24,'(a,a,a)') "--- Channel Profile: ", trim(channel_type), " ---" + + write(*,1000) + write(24,1000) +1000 format(' SNR_2500 Frames Good UER '/39('-')) + + isnr1 = -5 + isnr2 = -20 + do s = isnr1, isnr2, -1 + snr_2500_db = s + if(xsnr.ne.0.0) snr_2500_db = xsnr + esno_linear = (10.0_real32 ** (snr_2500_db / 10.0_real32)) * 80.0_real32 + correct_frames = 0 + undetected_errors = 0 + total_detected_errors = 0 + + ! Each frame (generate -> encode -> channel -> WAVA decode) is + ! completely independent of every other frame, so the frame loop + ! is embarrassingly parallel. Every array/scalar that a single + ! frame writes must be private -- otherwise threads clobber each + ! other's tx_payload/tx_encoded/etc. The three tallies are + ! combined across threads via REDUCTION. + + !$omp parallel do default(shared) & + !$omp private(f, t, i, n_i, n_q, h_fade, ch_i, ch_q, decode_success, payload_match, & + !$omp tx_payload, tx_symbols, rx_tone_energies, rx_decoded) & + !$omp reduction(+:correct_frames, undetected_errors, total_detected_errors) & + !$omp schedule(static) + do f = 1, num_frames + call generate_random_bits(tx_payload, PAYLOAD_BITS) + call tbcc_encode(tx_payload, tx_symbols) + + ! Channel Simulation + do t = 1, TOTAL_K + if (channel_type == "AWGN") then + h_fade = 1.0_real32 + else + call box_muller(n_i) + call box_muller(n_q) + h_fade = sqrt(0.5_real32 * (n_i**2 + n_q**2)) + end if + + do i = 0, 3 + call box_muller(n_i) + call box_muller(n_q) + n_i = n_i * sqrt(0.5_real32) + n_q = n_q * sqrt(0.5_real32) + + if (i == tx_symbols(t)) then + ch_i = (h_fade * sqrt(esno_linear)) + n_i + ch_q = n_q + else + ch_i = n_i + ch_q = n_q + end if + rx_tone_energies(i, t) = ch_i**2 + ch_q**2 + end do + end do + + ! Decoding Execution + call tbcc_wava_fsk_decode(rx_tone_energies, L_size, wava_iters, & + rx_decoded, decode_success) + + if (decode_success) then + payload_match = all(tx_payload == rx_decoded) + if (payload_match) then + correct_frames = correct_frames + 1 + else + undetected_errors = undetected_errors + 1 + end if + else + total_detected_errors = total_detected_errors + 1 + end if + end do + !$omp end parallel do + + p_correct = real(correct_frames, real32) / real(num_frames, real32) + uer = real(undetected_errors, real32) / real(num_frames, real32) + total_frames = total_frames + num_frames + + write(*,1001) snr_2500_db, num_frames, p_correct, uer + write(24,1001) snr_2500_db, num_frames, p_correct, uer +1001 format(f8.1,i10,2f10.6) + + ! Write tracking vectors to the spreadsheet data table (Multiplying UER by + ! 100 for percentage scale) + write(25, '(A,A,F6.1,A,F7.5,A,F7.5)') trim(channel_type), ",", snr_2500_db, & + ",", p_correct, ",", uer * 100.0_real32 + if(xsnr.ne.0.0) exit + end do !Loop over SNRs + end do !Loop over channel types (AWGN, then Rayleigh) + + close(24) + close(25) + + t_wall_end = omp_get_wtime() + print '(A,F8.2,A)', "Total wall-clock time: ", t_wall_end - t_wall_start, " s" + print '(a,f7.3,a)' ,"Average time per frame: ", & + 1000.0*(t_wall_end - t_wall_start)/total_frames, " ms" + +end program tbcc_4fsk_simulation diff --git a/lib/jtty/wava/tbcc_performance_curves.png b/lib/jtty/wava/tbcc_performance_curves.png Binary files differnew file mode 100644 index 00000000..88d63b07 --- /dev/null +++ b/lib/jtty/wava/tbcc_performance_curves.png |
