From 3456a5cf285c6678a188b43255516a5453307f37 Mon Sep 17 00:00:00 2001
From: Daniel van der Schuur <schuur@astron.nl>
Date: Wed, 11 Feb 2015 12:23:32 +0000
Subject: [PATCH] -Updated test case to use the functions in
 $UPE/base/file_io.py and  $UPE/base/common_dsp.py.

---
 .../python/gen_hex_files_composite_signals.py | 108 ++++++++++++------
 .../src/vhdl/apertif_unb1_correlator.vhd      |  89 +++++++++++----
 .../tb/python/tc_wpfb_src_out_arr.py          |  63 +++++++---
 3 files changed, 185 insertions(+), 75 deletions(-)

diff --git a/applications/apertif_unb1_correlator/src/python/gen_hex_files_composite_signals.py b/applications/apertif_unb1_correlator/src/python/gen_hex_files_composite_signals.py
index 3719b44ae6..f4354bfda5 100644
--- a/applications/apertif_unb1_correlator/src/python/gen_hex_files_composite_signals.py
+++ b/applications/apertif_unb1_correlator/src/python/gen_hex_files_composite_signals.py
@@ -20,10 +20,13 @@
 ###############################################################################
 
 from common import *
-from common_dsp import *
-from mem_init_file import list_to_hex
 
+from mem_init_file import list_to_hex
+import matplotlib
+matplotlib.use('TkAgg')
+from common_dsp import *
 import matplotlib.pyplot as plt
+
 import numpy as np
 from scipy.fftpack import fft,ifft, fftfreq, fftshift
 
@@ -33,16 +36,15 @@ from scipy.fftpack import fft,ifft, fftfreq, fftshift
 # . 
 
 NOF_INPUTS = 12
-COMPLEX_WIDTH = 6
+NOF_INPUT_SIGNALS = 24
+COMPLEX_WIDTH = 8
 NOF_WORDS_PER_BLOCK = 64
-MEM_WIDTH  = COMPLEX_WIDTH*2
-MEM_DEPTH  = 2*NOF_WORDS_PER_BLOCK # We're interleaving, hence twice the depth
 
 PATH = "../hex"
 FILENAME = "composite_signals"
 
 ###############################################################################
-# Create a 120MHz sine wave
+# Create composite waves
 ###############################################################################
 N = NOF_WORDS_PER_BLOCK
 t=range(N)
@@ -52,51 +54,87 @@ T = 1.0 / N
 
 x = np.linspace(0.0, N*T, N)
 
-AMPL = 31 # 5 bits + sign available
-# my singal
-#s0 = 7 * np.exp( 1 * 1.j * 2.0*np.pi*x)
-#s1 = 7 * np.exp( 5 * 1.j * 2.0*np.pi*x)
-#s2 = 7 * np.exp( 10 * 1.j * 2.0*np.pi*x)
-#s3 = 7 * np.exp( 15 * 1.j * 2.0*np.pi*x)
-s4 =  31 * np.exp( 8 * 1.j * 2.0*np.pi*x)
-
-s = s4 #  +s1 + s2 + s3 + s4
-################################################################################
-## Plot the signal
-################################################################################
-plt.plot(t, s.real, 'b-', t, s.imag, 'r--')
-plt.legend(('real', 'imaginary'))
-plt.show()
+# 8 bit signed -> WAAROM LIJKT AMPL_MAX DAN TOCH 63 te zijn IPV 127??
+# . Boven ~70 wordt de waarde aan de FFT output jusit lager...
+AMPL_MAX = 63 # 7 bits+sign #HUH?! 100->62; 63->118; 80->105; 70->123; 72->121; 75->110
+NOF_CHANNELS = 1 # Note: starting from 0 in -31..31.
+
+input_signals = []
+for input_signal_nr in range(NOF_INPUT_SIGNALS):
+    phase_shift_deg = input_signal_nr
+    channel_signals = []
+    for bin_nr in range(NOF_CHANNELS):
+        phase_shift_rad = math.radians(phase_shift_deg)
+        ampl=AMPL_MAX/NOF_CHANNELS
+        channel_signals.append( ampl * np.exp( (bin_nr+1) * 1.j * (2.0*np.pi*x+phase_shift_rad) ) )
+    
+    composite_signal=numpy.sum(channel_signals, axis=0)
+    input_signals.append(composite_signal)
+
+s=input_signals[0]
+###############################################################################
+# Convert the float values to n-bits
+###############################################################################
+#s_bits = []
+#for fword in s:
+#    re_signed = to_signed(fword.real, COMPLEX_WIDTH)
+#    im_signed = to_signed(fword.imag, COMPLEX_WIDTH)
+#
+#    s_bits.append( complex(re_signed, im_signed) )
 #
-################################################################################
-## Plot FFT
-################################################################################
-yf = fft(s)
-xf = fftfreq(N, T)
-xf = fftshift(xf)
-yplot = fftshift(yf)
-plt.plot(xf, 1.0/N * np.abs(yplot))
-plt.grid()
-plt.show()
+#s = numpy.array(s_bits)
+#print s
+
+###############################################################################
+# Plot the signal
+###############################################################################
+#plt.plot(t, s.real, 'b-', t, s.imag, 'r--')
+#plt.legend(('real', 'imaginary'))
+#plt.show()
+
+
+
+###############################################################################
+# Plot FFT
+###############################################################################
+#yf = fft(s)
+#xf = fftfreq(N, T)
+#xf = fftshift(xf)
+#yplot = fftshift(yf)
+#plt.bar(xf, 1.0/N * np.abs(yplot))
+#plt.grid()
+#plt.show()
 
 ###############################################################################
 # Convert complex floats to concatenated integers
 ###############################################################################
-concat_list = concat_complex(s, COMPLEX_WIDTH)
+#concat_list = concat_complex(input_signals[0], COMPLEX_WIDTH)
+
+input_signals_concat = []
+for input_signal in input_signals:
+    input_signals_concat.append( concat_complex(input_signal, COMPLEX_WIDTH) )
 
 ###############################################################################
 # Interleave 2 lists into one
 ###############################################################################
-inter_list = interleave([concat_list,concat_list])
+#inter_list = interleave([concat_list,concat_list])
+
+input_signals_concat_inter = []
+for i in range(NOF_INPUTS):
+    input_signals_concat_inter.append( interleave([input_signals_concat[2*i],input_signals_concat[2*i+1]]) )
 
 ###############################################################################
 # Use this list for each block generator
 ###############################################################################
-bg_lists = NOF_INPUTS*[inter_list]
+#bg_lists = NOF_INPUTS*[inter_list]
+bg_lists = input_signals_concat_inter
 
 ###############################################################################
 # Write the HEX files
-############################################################################### 
+###############################################################################
+MEM_WIDTH  = COMPLEX_WIDTH*2
+MEM_DEPTH  = 2*NOF_WORDS_PER_BLOCK # We're interleaving, hence twice the depth
+
 for input_nr in range(NOF_INPUTS):
     list_to_hex( bg_lists[input_nr], PATH+"/"+FILENAME+'_'+str(input_nr)+".hex", MEM_WIDTH, MEM_DEPTH)
 
diff --git a/applications/apertif_unb1_correlator/src/vhdl/apertif_unb1_correlator.vhd b/applications/apertif_unb1_correlator/src/vhdl/apertif_unb1_correlator.vhd
index 35d4c1d19a..5720af9efa 100644
--- a/applications/apertif_unb1_correlator/src/vhdl/apertif_unb1_correlator.vhd
+++ b/applications/apertif_unb1_correlator/src/vhdl/apertif_unb1_correlator.vhd
@@ -123,14 +123,14 @@ ARCHITECTURE str OF apertif_unb1_correlator IS
   SIGNAL eth1g_ram_miso             : t_mem_miso;
   
   -- Correlator
-  CONSTANT c_nof_inputs             : NATURAL := 2;
+  CONSTANT c_nof_inputs             : NATURAL := 12; --2;
   CONSTANT c_nof_input_folds        : NATURAL := 1;
   CONSTANT c_nof_input_streams      : NATURAL := c_nof_inputs / pow2(c_nof_input_folds);
   CONSTANT c_nof_pre_mult_folds     : NATURAL := 1; 
-  CONSTANT c_complex_data_w         : NATURAL := 6; -- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
+  CONSTANT c_complex_data_w         : NATURAL := 8; -- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
   CONSTANT c_conjugate              : BOOLEAN := TRUE;
   CONSTANT c_nof_channels           : NATURAL := 64;
-  CONSTANT c_integration_period     : NATURAL := 12208;
+  CONSTANT c_integration_period     : NATURAL := sel_a_b(g_sim, 0, 12208);
 
   CONSTANT c_nof_visibilities       : NATURAL := (c_nof_inputs*(c_nof_inputs+1))/2;
 
@@ -140,8 +140,8 @@ ARCHITECTURE str OF apertif_unb1_correlator IS
   CONSTANT c_wpfb_nof_chan          : NATURAL := 1;                    -- = default 0, defines the number of channels (=time-m
   CONSTANT c_wpfb_nof_points        : NATURAL := 64;                   -- = 1024, N point FFT
   CONSTANT c_wpfb_nof_taps          : NATURAL := 8;                    -- = 8 nof taps n the filter
-  CONSTANT c_wpfb_in_dat_w          : NATURAL := 6;                    -- = 8, number of input bits                           
-  CONSTANT c_wpfb_out_dat_w         : NATURAL := 12;                   -- = 14, number of output bits: in_dat_w + natural((cei
+  CONSTANT c_wpfb_in_dat_w          : NATURAL := 8; --6;                    -- = 8, number of input bits                           
+  CONSTANT c_wpfb_out_dat_w         : NATURAL := 14; --12;                   -- = 14, number of output bits: in_dat_w + natural((cei
   CONSTANT c_wpfb_use_separate      : BOOLEAN := FALSE;                -- = false for complex input, true for two real inputs
 
   CONSTANT c_wpfb : t_wpfb  := (c_wpfb_wb_factor, c_wpfb_nof_points, c_wpfb_nof_chan, c_wpfb_nof_wb_streams,
@@ -206,6 +206,26 @@ BEGIN
     out_sosi_arr     => wpfb_snk_in_arr
   );
 
+  -----------------------------------------------------------------------------
+  -- To replace block generators: 10GbE receivers 0..2
+  -----------------------------------------------------------------------------
+  -- Placeholder
+
+  -----------------------------------------------------------------------------
+  -- To replace block generators: BSN aligner
+  -- . Produces 3 aligned streams
+  -----------------------------------------------------------------------------
+  -- Placeholder
+
+  -----------------------------------------------------------------------------
+  -- To replace block generators: 
+  -- . Extracts 
+  -----------------------------------------------------------------------------
+  -- Placeholder
+
+
+
+
   -----------------------------------------------------------------------------
   -- WPFB
   -----------------------------------------------------------------------------
@@ -237,7 +257,7 @@ BEGIN
   -----------------------------------------------------------------------------
   -- Stream recorder to record the WPFB output stream to a file
   -----------------------------------------------------------------------------
-  u_dp_stream_rec_play : ENTITY dp_lib.dp_stream_rec_play
+  u_dp_stream_rec_play_wpfb_unit : ENTITY dp_lib.dp_stream_rec_play
   GENERIC MAP (
     g_sim            => TRUE,
     g_pass_through   => FALSE,
@@ -256,24 +276,45 @@ BEGIN
   -----------------------------------------------------------------------------
   -- Correlator
   -----------------------------------------------------------------------------
---  u_correlator : ENTITY correlator_lib.correlator
---  GENERIC MAP (
---    g_nof_input_streams  => c_nof_input_streams,
---    g_nof_input_folds    => c_nof_input_folds,
---    g_nof_pre_mult_folds => c_nof_pre_mult_folds,
---    g_data_w             => c_complex_data_w,
---    g_conjugate          => c_conjugate,
---    g_nof_channels       => c_nof_channels,
---    g_integration_period => c_integration_period
---  )
---  PORT MAP (
---    clk         => dp_clk,
---    rst         => dp_rst,
---
---    snk_in_arr  => wpfb_src_out_arr,
---    src_out_arr => correlator_src_out_arr
---  );
---
+  u_correlator : ENTITY correlator_lib.correlator
+  GENERIC MAP (
+    g_nof_input_streams  => c_nof_input_streams,
+    g_input_unfold_factor => c_nof_input_folds,
+    g_nof_pre_mult_folds => c_nof_pre_mult_folds,
+    g_data_w             => 14, --c_complex_data_w,
+    g_conjugate          => c_conjugate,
+    g_nof_channels       => c_nof_channels,
+    g_integration_period => c_integration_period
+  )
+  PORT MAP (
+    clk         => dp_clk,
+    rst         => dp_rst,
+
+    snk_in_arr  => wpfb_src_out_arr,
+    src_out_arr => correlator_src_out_arr
+  );
+
+  -----------------------------------------------------------------------------
+  -- Stream recorder to record the correlator output stream to a file
+  -- . The data buffer can only take snapshots.
+  -----------------------------------------------------------------------------
+  u_dp_stream_rec_play_correlator : ENTITY dp_lib.dp_stream_rec_play
+  GENERIC MAP (
+    g_sim            => TRUE,
+    g_pass_through   => FALSE,
+    g_rec_not_play   => TRUE,
+    g_rec_play_file  => "../../../applications/apertif_unb1_correlator/tb/rec/correlator_src_out_arr0.rec",
+    g_record_invalid => FALSE
+  )
+  PORT MAP (
+    dp_clk  => dp_clk,
+    snk_in  => correlator_src_out_arr(0),
+    snk_out => OPEN,
+    src_out => OPEN,
+    src_in  => c_dp_siso_rdy
+  );
+
+
 --  -----------------------------------------------------------------------------
 --  -- Data buffer to be read out by Python
 --  -----------------------------------------------------------------------------
diff --git a/applications/apertif_unb1_correlator/tb/python/tc_wpfb_src_out_arr.py b/applications/apertif_unb1_correlator/tb/python/tc_wpfb_src_out_arr.py
index 644a9e1f06..492862929f 100644
--- a/applications/apertif_unb1_correlator/tb/python/tc_wpfb_src_out_arr.py
+++ b/applications/apertif_unb1_correlator/tb/python/tc_wpfb_src_out_arr.py
@@ -1,13 +1,17 @@
 
 from common import *
+from file_io import *
 import os
+import matplotlib
+matplotlib.use('TkAgg')
 import matplotlib.pyplot as plt
 import numpy as np
 from scipy.fftpack import fft,ifft, fftfreq, fftshift
 
+
 REC_FILE = os.environ['RADIOHDL']+'/applications/apertif_unb1_correlator/tb/rec/wpfb_src_out_arr0.rec'
 
-WPFB_OUT_DATA_WIDTH = 12
+WPFB_OUT_DATA_WIDTH = 14
 
 ################################################################################
 # Read the lines from the file and turn them into a list
@@ -22,27 +26,44 @@ complex_list = []
 raw_data_int = []
 raw_data_str = []
 
-for line_nr,line in enumerate(lines):
-#    line = line.replace('X', '0') # Get rid of the 'X' on bit 24 (we only use 0..23)
-    split_line = line.split(' ')
+#for line_nr,line in enumerate(lines):
+##    line = line.replace('X', '0') # Get rid of the 'X' on bit 24 (we only use 0..23)
+#    split_line = line.split(' ')
+#
+##    print split_line
+#    str_re = split_line[3]
+#    str_im = split_line[4]
+#    raw_data_str.append( [str_re, str_im] )
+#
+#    re = to_signed(int(str_re, 32), WPFB_OUT_DATA_WIDTH)
+#    im = to_signed(int(str_im, 32), WPFB_OUT_DATA_WIDTH) 
+#    raw_data_int.append( [re, im] )
+#    complex_word = complex(re,im)
+#
+#    if complex_word==(7563+194j):
+#        print complex_word, 'created from raw data (re_str,im_str):',  [str_re, str_im] 
+#
+#    complex_list.append( complex_word )
+
+
+
+complex_list = rec_file_to_complex(REC_FILE, complex_width=WPFB_OUT_DATA_WIDTH)
+
 
-#    print split_line
-    str_re = split_line[3]
-    str_im = split_line[4]
-    raw_data_str.append( [str_re, str_im] )
 
-    re = to_signed(int(str_re, 32), WPFB_OUT_DATA_WIDTH)
-    im = to_signed(int(str_im, 32), WPFB_OUT_DATA_WIDTH) 
-    raw_data_int.append( [re, im] )
-    complex_word = complex(re,im)
-    complex_list.append( complex_word )
 
 ################################################################################
 # Split the list into chunks of 128 (note SOP/EOP tags of WPFB output are
 # incorrect:64 samples/block instead of 128).
 ################################################################################
 complex_list_128 = split_list(complex_list, 128)
-f = deinterleave(complex_list_128[5], 2)[1]
+print 'COMPLEX_LIST_128', complex_list_128[5]
+print '\n'
+f = deinterleave(complex_list_128[5], 2, 64)[0]
+
+
+for word_nr,word in enumerate(f):
+    print word_nr, word, 
 
 #complex_list_64 = split_list(complex_list, 64)
 #f = complex_list_64[6]
@@ -50,14 +71,24 @@ f = deinterleave(complex_list_128[5], 2)[1]
 ################################################################################
 ## Plot FFT
 ################################################################################
+#10*sp.log10( )
+
 N=64
 T = 1.0 / N
 
-yf = fft(f)
+#yf = fft(f)
+yf = f
+
 xf = fftfreq(N, T)
 xf = fftshift(xf)
 yplot = fftshift(yf)
-plt.plot(xf, 1.0/N * np.abs(yplot))
+
+#plt.yscale('log', nonposy='clip')
+
+#plt.plot(xf, 1.0/N * np.abs(yplot))
+plt.bar(xf, 1.0/N * np.abs(yplot))
+
 plt.grid()
 plt.show()
 
+
-- 
GitLab