###############################################################################
# Prepare x stimuli for block generator
x_re_arr    = []
x_im_arr    = []  
bg_data_arr = []

for i in range(c_nof_inputs):
    x_re_stream = []
    x_im_stream = []
    for h in range(c_nof_frames):
        for j in range(c_frame_size_in):
            x_re_stream.append(Din[i][j][0])  #j*c_nof_inputs + i)
            x_im_stream.append(Din[i][j][1])  #j*c_nof_inputs + i)
    x_re_arr.append(x_re_stream)
    x_im_arr.append(x_im_stream)
    bg_data_arr.append(dsp_test.concatenate_two_lists(x_re_stream, x_im_stream, c_dsp_data_w))  

# Write setting for the block generator:
bg.write_block_gen_settings(samplesPerPacket=c_frame_size_in, blocksPerSync=c_blocks_per_sync, gapSize=0, memLowAddr=0, memHighAddr=c_stimuli_length_par-1, BSNInit=0)

# Write the stimuli to the block generator and enable the block generator
for i in range(c_nof_inputs):
    bg.write_waveform_ram(data=bg_data_arr[i], channelNr= i)
    
bg.write_enable()

# Poll the databuffer to check if the response is there.
# Retry after 3 seconds so we don't issue too many MM reads in case of simulation.
do_until_ge(db_re.read_nof_words, ms_retry=3000, val=c_output_length_par, s_timeout=3600)

###############################################################################
 
 ###############################################################################
 #
 # Write the Wideband Poly Phase Filter Bank input stimuli to the BG
 #
 ###############################################################################        
 
 # Prepare xq stimuli order for block generator : list[time_nr * channel_nr]
 xq_real = []
 xq_imag = []
 for i in range(c_channel_length):
     for j in range(c_nof_input_channels):
         xq_real.append(xq_real_series[j][i])
         xq_imag.append(xq_imag_series[j][i])
 
 bg_vhdl_data = dsp_test.concatenate_two_lists(xq_real, xq_imag, g_in_dat_w)
 
 # Write setting for the block generator:
 bg.write_block_gen_settings(samplesPerPacket=c_frame_size, blocksPerSync=c_blocks_per_sync, gapSize=0, memLowAddr=0, memHighAddr=c_bg_ram_size-1, BSNInit=0)
 
 # Write the stimuli to the block generator and enable the block generator
 for i in range(g_wb_factor):
     send_data = []
     for j in range(c_stimuli_length/g_wb_factor):
         send_data.append(bg_vhdl_data[g_wb_factor*j+i])     #
     bg.write_waveform_ram(data=send_data, channelNr= i)
 bg.write_enable()
 
 # Poll the diag_data_buffer to check if the response is there.
 # Retry after 3 seconds so we don't issue too many MM reads in case of simulation.
 #do_until_ge(db_re.read_nof_words, ms_retry=3000, val=c_stimuli_length/g_wb_factor, s_timeout=3600)
 # Prepare stimuli order for block generator
 xf_real = []
 xf_imag = []
 for i in range(c_nof_streams):
     xf_real.append(dsp_test.create_waveform(sel=c_real_select, ampl=c_real_ampl, phase=c_real_phase, freq=c_real_freq, timeIndex=c_real_index, seed=c_real_seed, noiseLevel=c_real_noiselevel, length=c_channel_length))
     xf_imag.append(dsp_test.create_waveform(sel=c_imag_select, ampl=c_imag_ampl, phase=c_imag_phase, freq=c_imag_freq, timeIndex=c_imag_index, seed=c_imag_seed, noiseLevel=c_imag_noiselevel, length=c_channel_length)) 
 
 xq_real=[]
 xq_imag=[]
 for i in range(c_nof_streams):
     xq_real.append(dsp_test.adc_quantize_waveform(xf_real[i]))
     xq_imag.append(dsp_test.adc_quantize_waveform(xf_imag[i]))
        
 streams = []
 for i in range(c_nof_streams):
     streams.append(dsp_test.concatenate_two_lists(xq_real[i], xq_imag[i], g_in_dat_w))
    
 # Apply the alternated order due to the channels and create the wb_streams 
 wb_stream = []
 for h in range(g_nof_wb_streams):
     chnl_stream = [] 
     for i in range(c_channel_length):
         for j in range(c_nof_input_channels):
             chnl_stream.append(streams[h*c_nof_input_channels + j][i])   
     wb_stream.append(chnl_stream)  
 
 # Use the wb_streams to create the actual streams for the block generator
 # Write the stimuli to the block generator
 for h in range(g_nof_wb_streams):
     for i in range(g_wb_factor):
         send_data = []   
# Prepare data stimuli
# Create a real input signal. Imaginary input remains zero. 
xf_real_array=[]
for i in range(c_nof_input_channels):       
  xf_real_array.append(dsp_test.create_real_sinewave(ampl=1.0, phase=0.0, freq=16+12*i, noiseLevel=0, length=c_stimuli_length))

xf_real = []
for i in range(c_nof_integrations*c_nof_points):          
  for j in range(c_nof_input_channels):          
    xf_real.append(xf_real_array[j][i])
   
xf_imag = []
for i in range(c_stimuli_length): 
  xf_imag.append(0)

bg_vhdl_data = dsp_test.concatenate_two_lists(xf_real, xf_imag, c_in_dat_w)

# Write the stimuli to the block generator and enable the block generator         
for i in range(c_wb_factor):       
  send_data = []
  for j in range(c_stimuli_length/c_wb_factor):
    send_data.append(bg_vhdl_data[c_wb_factor*j+i])     #
  bg.write_waveform_ram(data=send_data, channelNr= i)
bg.write_enable()  

# Poll the databuffer to check if the response is there. 
# Retry after 3 seconds so we don't issue too many MM reads in case of simulation.
do_until_ge(db_re.read_nof_writes, ms_retry=3000, val=2, s_timeout=3600)

# Read out the statistics
stats_all = []