From 8fe6f96f8d03a2cdac90cb2f8437d2c4e26d22eb Mon Sep 17 00:00:00 2001
From: Erik Kooistra <kooistra@astron.nl>
Date: Tue, 5 Dec 2017 15:16:46 +0000
Subject: [PATCH] #1275 Verified correct wb order in fil_ppf_wide. Added check
 on exact expected SST value to detect unexpected code change.

---
 ..._apertif_unb1_bn_filterbank_stimuli_wg.vhd | 187 ++++++++++++++----
 1 file changed, 148 insertions(+), 39 deletions(-)

diff --git a/applications/apertif/designs/apertif_unb1_bn_filterbank/tb/vhdl/tb_apertif_unb1_bn_filterbank_stimuli_wg.vhd b/applications/apertif/designs/apertif_unb1_bn_filterbank/tb/vhdl/tb_apertif_unb1_bn_filterbank_stimuli_wg.vhd
index 190d688cbd..fa876e61ab 100644
--- a/applications/apertif/designs/apertif_unb1_bn_filterbank/tb/vhdl/tb_apertif_unb1_bn_filterbank_stimuli_wg.vhd
+++ b/applications/apertif/designs/apertif_unb1_bn_filterbank/tb/vhdl/tb_apertif_unb1_bn_filterbank_stimuli_wg.vhd
@@ -42,10 +42,10 @@
 --   3) Monitor WG input via ADUH monitor
 --   
 --   Enable calc mode for WG-0,1,2,3 = A, B, C, D via reg_diag_wg_0,1,2,3
---     c_subband_sp_0 = 64 +   0
---     c_subband_sp_1 = 64 +   1
---     c_subband_sp_2 = 64 +  24
---     c_subband_sp_3 = 64 + 383
+--     c_subband_sp_0 = 64.0 +   0.0
+--     c_subband_sp_1 = 64.0 +   1.0
+--     c_subband_sp_2 = 64.0 +  24.0
+--     c_subband_sp_3 = 64.0 + 383.0
 --   
 --   Read current BSN from reg_bsn_scheduler_wg and write reg_bsn_scheduler_wg to trigger start of WG at BSN.
 --     
@@ -61,16 +61,57 @@
 --   5) Read ADUH status via reg_adc_quad and verify that it is locked and stable
 --
 -- Usage:
---   > as 14
+--   > as 5    # default
+--   > as 14   # for detailed debugging
 --   > run 70 us  (dependent on c_nof_block_per_sync)
 --
 --   View sp_sample_* in radix decimal and analogue format in the Wave Window to see that the
 --   c_wideband_factor = 4 samples per word are represented in the proper order for both ADU
 --   input (first read of ADUH monitor) and WG input (second read of ADUH monitor). 
+--
+-- Remark:
+-- * With c_wpfb.fil_backoff_w = 0 the amplitude must remain <= 118 to avoid overflow.
+--   On hardware the SST are the same for Hajee FB 16579 as for FB 17790 (end nov 2017). Both
+--   show overflow artefacts when --ampl >= 119, --ampl <= 118 works. With one_pfb.m using
+--   the LOFAR subband filter does not show this and the wpfb_unit_dev has been dimensioned
+--   to allow --ampl 127 without overflow. The overflow occurs because c_wpfb is used and this
+--   has fil_backoff_w = 0. At some time we need to change to c_wpfb_apertif_subbands which
+--   sets fil_backoff_w = 0.
+-- * Use c_measured_subband_power_sp_* to verify the exact subband power values in order to
+--   detect if some detail in the code changed. When the code is changed, then it may be 
+--   necessary to update these c_measured_subband_power_sp_* values. The
+--   c_exp_subband_power_sp_* values should still remain if some detail in the code is changed.
+-- * Note that a WG at on frequency or another will have slightly different power, due to
+--   the discrete amplitude and sample times. 
+-- * There is also some crosstalk between SP 0,1 and between SP 2,3, because these share a
+--   WPFB.
+-- * Commented alternative SP with frequency offset for SP[0:3] is 0.0, 0.4, 0.5, 0.6.
+--   These SP show that the LOFAR subband filter has some gain > 1 near 0.4 and 0.6 and it
+--   shows that the attenuation is more than 3 dB at 0.5, because the subband_power_sum for
+--   SP 0 is < SP 1, 3 and SP 0 > SP 2. The sp_subband_power_ratio_* are not the same for 
+--   these alternative SP and therefore the asserts on c_exp_subband_power_sp_* should be 
+--   ignored.
+-- * Verify wideband FIR coefficients order in fil_ppf_wide.vhd
+--   Try g_file_index_arr => c_file_index_arr2(g_fil_ppf.wb_factor-1-P) instead of (P)
+--   With fir_wb_reverse=true in applications/apertif/matlab/run_pfir_coeff.m it shows in the
+--   the FIR transfer function as about factor 2 in dB less attenuation outside the passband
+--   for bins that are multiples of about N/P. For P = 4 the attenuation spikes occur at:
+--   . bin 255.6
+--   . bin 256.4
+--   . bin 511.6
+--   In a time domain simulation a CW at those bins will then also show in the DC bin. For any
+--   bin frequency +0.6 or -0.4 there will occur leakage c_wb_leakage_bin = 256 bins away.
+--   It appears that there is indeed leakage into this bin when the commented alternative SP
+--   are used that have frequency offset for SP[0:3] is 0.0, 0.4, 0.5, 0.6. With the active SP
+--   that have frequency in the cneter of the bin there is no leakage (as expected by
+--   run_pfir_coeff.m), so therefore these SP cannot be used to detect the proper wideband
+--   order of the FIR coefficients.
+--   
 
 LIBRARY IEEE, common_lib, mm_lib, unb1_board_lib, bf_lib, i2c_lib, aduh_lib, dp_lib, diag_lib, wpfb_lib;
 USE IEEE.std_logic_1164.ALL;
 USE IEEE.numeric_std.ALL;
+USE IEEE.math_real.ALL;
 USE common_lib.common_pkg.ALL;
 USE common_lib.common_math_pkg.ALL;
 USE common_lib.tb_common_pkg.ALL;
@@ -120,8 +161,8 @@ ARCHITECTURE tb OF tb_apertif_unb1_bn_filterbank_stimuli_wg IS
   -- ADU
   CONSTANT c_ai                  : t_c_aduh_dd_ai := c_aduh_dd_ai;
   
-  CONSTANT c_dp_factor           : NATURAL := c_ai.rx_factor * c_ai.dd_factor;
-  CONSTANT c_wideband_factor     : NATURAL := c_dp_factor;                      -- = 4
+  CONSTANT c_dp_factor           : NATURAL := c_ai.rx_factor * c_ai.dd_factor;  -- = 2 * 2
+  CONSTANT c_wideband_factor     : NATURAL := c_dp_factor;                      -- = 4 = c_wpfb_sim.wb_factor
   
   CONSTANT c_sample_freq         : NATURAL := c_wideband_factor*c_unb1_board_ext_clk_freq_200M/10**6;  -- 800 MSps
   CONSTANT c_sample_period       : TIME := (10**6 / c_sample_freq) * 1 ps;                             -- Tsample = 1250 ps
@@ -156,10 +197,21 @@ ARCHITECTURE tb OF tb_apertif_unb1_bn_filterbank_stimuli_wg IS
   CONSTANT c_ampl_sp_1           : NATURAL := 20;
   CONSTANT c_ampl_sp_2           : NATURAL := 30;
   CONSTANT c_ampl_sp_3           : NATURAL := 40;
-  CONSTANT c_subband_sp_0        : NATURAL := 64 +   0;  -- first select subband at index 64 = 50 MHz
-  CONSTANT c_subband_sp_1        : NATURAL := 64 +   1;  --  next select subband
-  CONSTANT c_subband_sp_2        : NATURAL := 64 +  24;  --  next select subband in next 300M/16 band
-  CONSTANT c_subband_sp_3        : NATURAL := 64 + 383;  --  last select subband
+  CONSTANT c_subband_sp_0        : REAL := 64.0;          -- first select subband at index 64 = 50 MHz
+  CONSTANT c_subband_sp_1        : REAL := 64.0 +   1.0;  --  next select subband
+  CONSTANT c_subband_sp_2        : REAL := 64.0 +  24.0;  --  next select subband in next 300M/16 band
+  CONSTANT c_subband_sp_3        : REAL := 64.0 + 383.0;  --  last select subband
+
+  -- Use these SP to view impact of reversed WB FIR coefficients into c_wb_leakage_bin, by means of temporary edit in fil_ppf_wide.vhd
+  --CONSTANT c_ampl_sp_0           : NATURAL := 40;  -- in number of lsb, range 0:127 (choose not too big <~40 to fit integer in SST)
+  --CONSTANT c_ampl_sp_1           : NATURAL := 40;
+  --CONSTANT c_ampl_sp_2           : NATURAL := 40;
+  --CONSTANT c_ampl_sp_3           : NATURAL := 40;
+  --CONSTANT c_subband_sp_0        : REAL := 31.0;
+  --CONSTANT c_subband_sp_1        : REAL := 31.4;
+  --CONSTANT c_subband_sp_2        : REAL := 31.5;
+  --CONSTANT c_subband_sp_3        : REAL := 31.6;
+  
   CONSTANT c_exp_wg_power_sp_0   : REAL := REAL(c_ampl_sp_0**2)/2.0 * REAL(c_wpfb_sim.nof_points*c_nof_block_per_sync);
   CONSTANT c_exp_wg_power_sp_1   : REAL := REAL(c_ampl_sp_1**2)/2.0 * REAL(c_wpfb_sim.nof_points*c_nof_block_per_sync);
   CONSTANT c_exp_wg_power_sp_2   : REAL := REAL(c_ampl_sp_2**2)/2.0 * REAL(c_wpfb_sim.nof_points*c_nof_block_per_sync);
@@ -169,11 +221,16 @@ ARCHITECTURE tb OF tb_apertif_unb1_bn_filterbank_stimuli_wg IS
   CONSTANT c_wpfb_nof_block_latency      : NATURAL := 5;
   CONSTANT c_nof_wpfb_units              : NATURAL := c_ai.nof_sp/c_nof_complex;            -- two real inputs per complex WPFB unit
   CONSTANT c_nof_subbands                : NATURAL := c_wpfb_sim.nof_points/c_nof_complex;  -- two real inputs per complex WPFB unit
+  CONSTANT c_wb_leakage_bin              : NATURAL := c_wpfb_sim.nof_points / c_wpfb_sim.wb_factor;   -- = 256, leakage will occur in this bin if FIR wb_factor is reversed 
   CONSTANT c_exp_sp_subband_power_ratio  : REAL := 32.0;   -- depends on internal WPFB quantization and FIR coefficients
   CONSTANT c_exp_subband_power_sp_0      : REAL := c_exp_wg_power_sp_0 * c_exp_sp_subband_power_ratio;
   CONSTANT c_exp_subband_power_sp_1      : REAL := c_exp_wg_power_sp_1 * c_exp_sp_subband_power_ratio;
   CONSTANT c_exp_subband_power_sp_2      : REAL := c_exp_wg_power_sp_2 * c_exp_sp_subband_power_ratio;
   CONSTANT c_exp_subband_power_sp_3      : REAL := c_exp_wg_power_sp_3 * c_exp_sp_subband_power_ratio;
+  CONSTANT c_measured_subband_power_sp_0 : NATURAL :=  16052506;  -- obtained from Wave window
+  CONSTANT c_measured_subband_power_sp_1 : NATURAL :=  65178421;
+  CONSTANT c_measured_subband_power_sp_2 : NATURAL := 145803923;
+  CONSTANT c_measured_subband_power_sp_3 : NATURAL := 259512489;
   
   TYPE t_nat_subbands_arr2 IS ARRAY (NATURAL RANGE <>) OF t_natural_arr(0 TO c_nof_subbands-1);
   
@@ -254,10 +311,10 @@ ARCHITECTURE tb OF tb_apertif_unb1_bn_filterbank_stimuli_wg IS
   SIGNAL dbg_c_lo_factor       : REAL := c_lo_factor;
   SIGNAL dbg_c_hi_factor       : REAL := c_hi_factor;
   
-  SIGNAL dbg_c_subband_sp_0    : NATURAL := c_subband_sp_0;
-  SIGNAL dbg_c_subband_sp_1    : NATURAL := c_subband_sp_1;
-  SIGNAL dbg_c_subband_sp_2    : NATURAL := c_subband_sp_2;
-  SIGNAL dbg_c_subband_sp_3    : NATURAL := c_subband_sp_3;
+  SIGNAL dbg_c_subband_sp_0    : REAL := c_subband_sp_0;
+  SIGNAL dbg_c_subband_sp_1    : REAL := c_subband_sp_1;
+  SIGNAL dbg_c_subband_sp_2    : REAL := c_subband_sp_2;
+  SIGNAL dbg_c_subband_sp_3    : REAL := c_subband_sp_3;
   
   -- WG
   SIGNAL dbg_c_exp_wg_power_sp_0   : REAL := c_exp_wg_power_sp_0;
@@ -278,23 +335,43 @@ ARCHITECTURE tb OF tb_apertif_unb1_bn_filterbank_stimuli_wg IS
   SIGNAL sp_power_sum_3        : NATURAL;
   
   -- WPFB
-  SIGNAL sp_subband_powers_arr2_hi : t_nat_subbands_arr2(c_ai.nof_sp-1 DOWNTO 0);
+  SIGNAL sp_subband_powers_arr2_hi : t_nat_subbands_arr2(c_ai.nof_sp-1 DOWNTO 0);   -- [sp][sub]
   SIGNAL sp_subband_powers_arr2_lo : t_nat_subbands_arr2(c_ai.nof_sp-1 DOWNTO 0);
 
   SIGNAL dbg_c_exp_subband_power_sp_0   : REAL := c_exp_subband_power_sp_0;
   SIGNAL dbg_c_exp_subband_power_sp_1   : REAL := c_exp_subband_power_sp_1;
   SIGNAL dbg_c_exp_subband_power_sp_2   : REAL := c_exp_subband_power_sp_2;
   SIGNAL dbg_c_exp_subband_power_sp_3   : REAL := c_exp_subband_power_sp_3;
-  SIGNAL sp_subband_power_0    : NATURAL;
-  SIGNAL sp_subband_power_1    : NATURAL;
-  SIGNAL sp_subband_power_2    : NATURAL;
-  SIGNAL sp_subband_power_3    : NATURAL;
-  
+  SIGNAL dbg_c_measured_subband_power_sp_0  : NATURAL := c_measured_subband_power_sp_0;
+  SIGNAL dbg_c_measured_subband_power_sp_1  : NATURAL := c_measured_subband_power_sp_1;
+  SIGNAL dbg_c_measured_subband_power_sp_2  : NATURAL := c_measured_subband_power_sp_2;
+  SIGNAL dbg_c_measured_subband_power_sp_3  : NATURAL := c_measured_subband_power_sp_3;
+  SIGNAL sp_subband_power_0       : NATURAL;
+  SIGNAL sp_subband_power_1       : NATURAL;
+  SIGNAL sp_subband_power_2       : NATURAL;
+  SIGNAL sp_subband_power_3       : NATURAL;
   SIGNAL sp_subband_power_ratio_0 : REAL;
   SIGNAL sp_subband_power_ratio_1 : REAL;
   SIGNAL sp_subband_power_ratio_2 : REAL;
-  SIGNAL sp_subband_power_ratio_3 : REAL;
-                                      
+  SIGNAL sp_subband_power_ratio_3 : REAL;                                      
+  SIGNAL sp_subband_power_sum     : t_nat_natural_arr(c_ai.nof_sp-1 DOWNTO 0) := (OTHERS=>0);
+  SIGNAL sp_subband_power_sum_0   : NATURAL;
+  SIGNAL sp_subband_power_sum_1   : NATURAL;
+  SIGNAL sp_subband_power_sum_2   : NATURAL;
+  SIGNAL sp_subband_power_sum_3   : NATURAL;
+  
+  SIGNAL sp_subband_power_leakage_bin_0 : NATURAL;
+  SIGNAL sp_subband_power_leakage_bin_1 : NATURAL;
+  SIGNAL sp_subband_power_leakage_bin_2 : NATURAL;
+  SIGNAL sp_subband_power_leakage_bin_3 : NATURAL;
+  
+  SIGNAL sp_sp_power_ratio_01           : REAL;
+  SIGNAL sp_sp_power_ratio_02           : REAL;
+  SIGNAL sp_sp_power_ratio_03           : REAL;
+  SIGNAL subband_subband_power_ratio_01 : REAL;
+  SIGNAL subband_subband_power_ratio_02 : REAL;
+  SIGNAL subband_subband_power_ratio_03 : REAL;
+  
   -- DUT
   SIGNAL WDI                 : STD_LOGIC;
   SIGNAL ext_pps             : STD_LOGIC := '0';
@@ -610,10 +687,10 @@ BEGIN
     mmf_mm_bus_wr(c_mm_file_reg_diag_wg_1, 1, INTEGER( 90.0 * c_diag_wg_phase_unit), tb_clk);
     mmf_mm_bus_wr(c_mm_file_reg_diag_wg_2, 1, INTEGER(180.0 * c_diag_wg_phase_unit), tb_clk);
     mmf_mm_bus_wr(c_mm_file_reg_diag_wg_3, 1, INTEGER(270.0 * c_diag_wg_phase_unit), tb_clk);
-    mmf_mm_bus_wr(c_mm_file_reg_diag_wg_0, 2, INTEGER(REAL(c_subband_sp_0) * c_wg_subband_freq_unit), tb_clk);  -- freq: first select subband at index 64 = 50 MHz
-    mmf_mm_bus_wr(c_mm_file_reg_diag_wg_1, 2, INTEGER(REAL(c_subband_sp_1) * c_wg_subband_freq_unit), tb_clk);  -- freq: next select subband
-    mmf_mm_bus_wr(c_mm_file_reg_diag_wg_2, 2, INTEGER(REAL(c_subband_sp_2) * c_wg_subband_freq_unit), tb_clk);  -- freq: next select subband in next 300M/16 band
-    mmf_mm_bus_wr(c_mm_file_reg_diag_wg_3, 2, INTEGER(REAL(c_subband_sp_3) * c_wg_subband_freq_unit), tb_clk);  -- freq: last select subband
+    mmf_mm_bus_wr(c_mm_file_reg_diag_wg_0, 2, INTEGER(c_subband_sp_0 * c_wg_subband_freq_unit), tb_clk);  -- freq: first select subband at index 64 = 50 MHz
+    mmf_mm_bus_wr(c_mm_file_reg_diag_wg_1, 2, INTEGER(c_subband_sp_1 * c_wg_subband_freq_unit), tb_clk);  -- freq: next select subband
+    mmf_mm_bus_wr(c_mm_file_reg_diag_wg_2, 2, INTEGER(c_subband_sp_2 * c_wg_subband_freq_unit), tb_clk);  -- freq: next select subband in next 300M/16 band
+    mmf_mm_bus_wr(c_mm_file_reg_diag_wg_3, 2, INTEGER(c_subband_sp_3 * c_wg_subband_freq_unit), tb_clk);  -- freq: last select subband
     mmf_mm_bus_wr(c_mm_file_reg_diag_wg_0, 3, INTEGER(REAL(c_ampl_sp_0) * c_wg_ampl_lsb), tb_clk);  -- ampl
     mmf_mm_bus_wr(c_mm_file_reg_diag_wg_1, 3, INTEGER(REAL(c_ampl_sp_1) * c_wg_ampl_lsb), tb_clk);
     mmf_mm_bus_wr(c_mm_file_reg_diag_wg_2, 3, INTEGER(REAL(c_ampl_sp_2) * c_wg_ampl_lsb), tb_clk);
@@ -729,28 +806,60 @@ BEGIN
         -- low part
         mmf_mm_bus_rd(c_mm_file_ram_st_sst, I, rd_data, tb_clk);
         sp_subband_powers_arr2_lo(v_S)(v_B) <= TO_UINT(rd_data);
+        sp_subband_power_sum(v_S) <= sp_subband_power_sum(v_S) + TO_UINT(rd_data);
       ELSE      
         -- high part = 0 in this simulation, because nof_block_per_sync is small
         mmf_mm_bus_rd(c_mm_file_ram_st_sst, I, rd_data, tb_clk);
         sp_subband_powers_arr2_hi(v_S)(v_B) <= TO_UINT(rd_data);
       END IF;
     END LOOP;
-    sp_subband_power_0 <= sp_subband_powers_arr2_lo(0)(c_subband_sp_0);
-    sp_subband_power_1 <= sp_subband_powers_arr2_lo(1)(c_subband_sp_1);
-    sp_subband_power_2 <= sp_subband_powers_arr2_lo(2)(c_subband_sp_2);
-    sp_subband_power_3 <= sp_subband_powers_arr2_lo(3)(c_subband_sp_3);
+    sp_subband_power_sum_0 <= sp_subband_power_sum(0);
+    sp_subband_power_sum_1 <= sp_subband_power_sum(1);
+    sp_subband_power_sum_2 <= sp_subband_power_sum(2);
+    sp_subband_power_sum_3 <= sp_subband_power_sum(3);
+    sp_subband_power_0 <= sp_subband_powers_arr2_lo(0)(INTEGER(ROUND(c_subband_sp_0)));
+    sp_subband_power_1 <= sp_subband_powers_arr2_lo(1)(INTEGER(ROUND(c_subband_sp_1)));
+    sp_subband_power_2 <= sp_subband_powers_arr2_lo(2)(INTEGER(ROUND(c_subband_sp_2)));
+    sp_subband_power_3 <= sp_subband_powers_arr2_lo(3)(INTEGER(ROUND(c_subband_sp_3)));
+    
     proc_common_wait_some_cycles(tb_clk, 1);
+    
+    -- verify expected subband power based on WG power
     ASSERT sp_subband_powers_arr2_hi=c_nat_subbands_zeros_arr2 REPORT "Wrong subband power statisics, expected < 2**32" SEVERITY ERROR;
-    ASSERT REAL(sp_subband_power_0) > c_lo_factor * c_exp_subband_power_sp_0 REPORT "Wrong subband power for SP 0" SEVERITY ERROR;
-    ASSERT REAL(sp_subband_power_1) > c_lo_factor * c_exp_subband_power_sp_1 REPORT "Wrong subband power for SP 1" SEVERITY ERROR;
-    ASSERT REAL(sp_subband_power_2) > c_lo_factor * c_exp_subband_power_sp_2 REPORT "Wrong subband power for SP 2" SEVERITY ERROR;
-    ASSERT REAL(sp_subband_power_3) > c_lo_factor * c_exp_subband_power_sp_3 REPORT "Wrong subband power for SP 3" SEVERITY ERROR;
+    IF sp_power_sum_0>0 THEN ASSERT REAL(sp_subband_power_0) > c_lo_factor * c_exp_subband_power_sp_0 REPORT "Wrong subband power for SP 0" SEVERITY ERROR; END IF;
+    IF sp_power_sum_1>0 THEN ASSERT REAL(sp_subband_power_1) > c_lo_factor * c_exp_subband_power_sp_1 REPORT "Wrong subband power for SP 1" SEVERITY ERROR; END IF;
+    IF sp_power_sum_2>0 THEN ASSERT REAL(sp_subband_power_2) > c_lo_factor * c_exp_subband_power_sp_2 REPORT "Wrong subband power for SP 2" SEVERITY ERROR; END IF;
+    IF sp_power_sum_3>0 THEN ASSERT REAL(sp_subband_power_3) > c_lo_factor * c_exp_subband_power_sp_3 REPORT "Wrong subband power for SP 3" SEVERITY ERROR; END IF;
+    
+    -- verify expected subband power based on earlier simulation of exactly measurement of subband power
+    ASSERT sp_subband_power_0 = c_measured_subband_power_sp_0 REPORT "Wrong exact subband power for SP 0" SEVERITY ERROR;
+    ASSERT sp_subband_power_1 = c_measured_subband_power_sp_1 REPORT "Wrong exact subband power for SP 1" SEVERITY ERROR;
+    ASSERT sp_subband_power_2 = c_measured_subband_power_sp_2 REPORT "Wrong exact subband power for SP 2" SEVERITY ERROR;
+    ASSERT sp_subband_power_3 = c_measured_subband_power_sp_3 REPORT "Wrong exact subband power for SP 3" SEVERITY ERROR;
+    
+    -- view impact of reversed WB FIR coefficients into c_wb_leakage_bin in Wave window
+    sp_subband_power_leakage_bin_0 <= sp_subband_powers_arr2_lo(0)((INTEGER(ROUND(c_subband_sp_0)) + c_wb_leakage_bin) MOD c_nof_subbands);
+    sp_subband_power_leakage_bin_1 <= sp_subband_powers_arr2_lo(1)((INTEGER(ROUND(c_subband_sp_1)) + c_wb_leakage_bin) MOD c_nof_subbands);
+    sp_subband_power_leakage_bin_2 <= sp_subband_powers_arr2_lo(2)((INTEGER(ROUND(c_subband_sp_2)) + c_wb_leakage_bin) MOD c_nof_subbands);
+    sp_subband_power_leakage_bin_3 <= sp_subband_powers_arr2_lo(3)((INTEGER(ROUND(c_subband_sp_3)) + c_wb_leakage_bin) MOD c_nof_subbands);
     
     -- view c_exp_sp_subband_power_ratio = 32.0 in Wave window
-    sp_subband_power_ratio_0 <= REAL(sp_subband_power_0)/REAL(sp_power_sum_0);
-    sp_subband_power_ratio_1 <= REAL(sp_subband_power_1)/REAL(sp_power_sum_1);
-    sp_subband_power_ratio_2 <= REAL(sp_subband_power_2)/REAL(sp_power_sum_2);
-    sp_subband_power_ratio_3 <= REAL(sp_subband_power_3)/REAL(sp_power_sum_3);
+    IF sp_power_sum_0>0 THEN sp_subband_power_ratio_0 <= REAL(sp_subband_power_0)/REAL(sp_power_sum_0); END IF;
+    IF sp_power_sum_1>0 THEN sp_subband_power_ratio_1 <= REAL(sp_subband_power_1)/REAL(sp_power_sum_1); END IF;
+    IF sp_power_sum_2>0 THEN sp_subband_power_ratio_2 <= REAL(sp_subband_power_2)/REAL(sp_power_sum_2); END IF;
+    IF sp_power_sum_3>0 THEN sp_subband_power_ratio_3 <= REAL(sp_subband_power_3)/REAL(sp_power_sum_3); END IF;
+    
+    -- view subband_subband_power_ratio_0* in Wave window
+    IF sp_power_sum_0>0 THEN
+       -- SP1,2,3 power ratio with to SP 0
+       sp_sp_power_ratio_01 <= REAL(sp_power_sum_1)/REAL(sp_power_sum_0);
+       sp_sp_power_ratio_02 <= REAL(sp_power_sum_2)/REAL(sp_power_sum_0);
+       sp_sp_power_ratio_03 <= REAL(sp_power_sum_3)/REAL(sp_power_sum_0);
+       -- SP1,2,3 subband power ratio with to SP 0
+       subband_subband_power_ratio_01 <= REAL(sp_subband_power_1)/REAL(sp_subband_power_0);
+       subband_subband_power_ratio_02 <= REAL(sp_subband_power_2)/REAL(sp_subband_power_0);
+       subband_subband_power_ratio_03 <= REAL(sp_subband_power_3)/REAL(sp_subband_power_0);
+    END IF;
 
     ----------------------------------------------------------------------------
     -- Read ADUH status (should be locked after some us)
-- 
GitLab