From 7d032cd0575dcd0d058d675d4da7532b697ef321 Mon Sep 17 00:00:00 2001
From: Daniel van der Schuur <schuur@astron.nl>
Date: Thu, 25 Jun 2015 09:58:13 +0000
Subject: [PATCH] -Added script to overwrite block gen waveform RAM.

---
 .../tb/python/tc_apertif_unb1_fn_bf_emu.py    | 135 ++++++++++++++++++
 1 file changed, 135 insertions(+)
 create mode 100644 applications/apertif/designs/apertif_unb1_fn_bf_emu/tb/python/tc_apertif_unb1_fn_bf_emu.py

diff --git a/applications/apertif/designs/apertif_unb1_fn_bf_emu/tb/python/tc_apertif_unb1_fn_bf_emu.py b/applications/apertif/designs/apertif_unb1_fn_bf_emu/tb/python/tc_apertif_unb1_fn_bf_emu.py
new file mode 100644
index 0000000000..6de49685a2
--- /dev/null
+++ b/applications/apertif/designs/apertif_unb1_fn_bf_emu/tb/python/tc_apertif_unb1_fn_bf_emu.py
@@ -0,0 +1,135 @@
+#! /usr/bin/env python
+###############################################################################
+#
+# Copyright (C) 2015
+# ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
+# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+###############################################################################
+
+# Purpose:
+# . Create lists of waveform samples and write them to the block gen RAM.
+# Description:
+# . Design apertif_unb1_fn_bf_emu uses 4 block generators to emulate the output
+#   of 4 BF units;
+# . Each of the 4 output streams carries 2 interleaved signals;
+# . Each of these 8 signals consists of 64 timesamples (stored in RAM) that are
+#   looped through by the block generator;
+# . Each BG stream contains 2*64 = 128 timesamples;
+# . This TC overwrites the default block gen RAM contents.
+# Usage:
+# . Change CHANNELS as desired;
+# . python tc_apertif_unb1_fn_bf_emu.py --unb 3 --fn 0:2
+
+import test_case
+import node_io
+import numpy as np
+import pi_diag_block_gen
+from common import *
+import matplotlib
+matplotlib.use('TkAgg')
+import matplotlib.pyplot as plt
+from scipy.fftpack import fft,ifft, fftfreq, fftshift
+
+NOF_STREAMS = 4 # emulates 4 BF units
+NOF_WORDS_PER_SIGNAL = 64
+NOF_RAM_WORDS_PER_STREAM = 2*NOF_WORDS_PER_SIGNAL # 128 words per BG stream RAM
+
+COMPLEX_WIDTH = 8 #8b real + 8b imag
+AMPL_MAX = 127 # 8b signed data so a range of -127..127
+NOF_INPUT_SIGNALS = 4
+
+###############################################################################
+# Instantiate TC, IO, BG instance.
+###############################################################################
+tc = test_case.Testcase('TC - ', '')
+io = node_io.NodeIO(tc.nodeImages, tc.base_ip)
+bg = pi_diag_block_gen.PiDiagBlockGen(tc, io, NOF_STREAMS, NOF_RAM_WORDS_PER_STREAM)
+
+###############################################################################
+# Define which channels are present in the BG output signal
+# . A sub-signal is created for each channel number (a.k.a. bin number) defined
+#   in CHANNELS.
+# . All sub-signals are added yielding our composite signal
+###############################################################################
+# Define your list of channel numbers 0..63) to put in the signal here
+#CHANNELS = [1]
+#CHANNELS = [60]
+#CHANNELS = [1,60]
+CHANNELS = [10]
+#CHANNELS = [1,5,12,17,21,25,28,34,41,47,54,55,60]
+
+# Sample spacing
+T = 1.0 / NOF_WORDS_PER_SIGNAL
+x = np.linspace(0.0, NOF_WORDS_PER_SIGNAL*T, NOF_WORDS_PER_SIGNAL)
+
+# Create a list of sub-signals; one per channel
+channel_signals = []
+NOF_CHANNELS = len(CHANNELS)
+for bin_nr in CHANNELS:
+    # Make sure the summed amplitude of all channels does not exceed AMPL_MAX
+    ampl=AMPL_MAX/NOF_CHANNELS
+    # Create the signal in this channel and append to list
+    channel_signal = ampl * np.exp( bin_nr*1.j*(2.0*np.pi*x) )    
+    channel_signals.append( channel_signal )
+
+# Adding all channel sub-signals yields our composite signal
+composite_signal=np.sum(channel_signals, axis=0)
+
+###############################################################################
+# Plot our composite signal + FFT
+# . This step is optional and not required to overwrite the RAM contents.
+###############################################################################
+# Convert the float values to 8-bit complex
+s_bits = []
+for fword in composite_signal:
+    re_signed = to_signed(fword.real, COMPLEX_WIDTH)
+    im_signed = to_signed(fword.imag, COMPLEX_WIDTH)
+    s_bits.append( complex(re_signed, im_signed) )
+
+# Define our axes and plot the signal
+s = np.array(s_bits)
+t = range(NOF_WORDS_PER_SIGNAL)
+plt.plot(t, s.real, 'b-', t, s.imag, 'r--')
+plt.legend(('real', 'imaginary'))
+plt.show()
+
+# Calculate and plot the FFT
+yf = fft(s)
+xf = fftfreq(NOF_WORDS_PER_SIGNAL, T)
+xf = range(NOF_WORDS_PER_SIGNAL)
+xf = fftshift(xf)
+yplot = fftshift(yf)
+plt.bar(xf, 1.0/NOF_WORDS_PER_SIGNAL * np.abs(yplot))
+plt.grid()
+plt.show()
+
+###############################################################################
+# Prepare the data to be written to RAM
+###############################################################################
+# Convert complex floats to concatenated integers
+composite_signal_concat = concat_complex(composite_signal, COMPLEX_WIDTH)
+
+# Interleave the 64-sample composite signal into 2*64=128 samples
+# . [0..63] -> [0,0,1,1,..63,63]
+composite_signal_concat_inter = interleave([composite_signal_concat,composite_signal_concat])
+
+###############################################################################
+# Write the 128-word list to BG RAMs
+###############################################################################
+for STREAM_INDEX in range(NOF_STREAMS):
+    bg.write_waveform_ram(composite_signal_concat_inter, STREAM_INDEX)
+
-- 
GitLab