<jupyter_start><jupyter_text>RFSoC QPSK Transceiver----Please use Jupyter Labs http://board_ip_address/lab for this notebook.This design is a full QPSK transceiver, which transmits and receives randomly-generated pulse-shaped symbols with full carrier and timing synchronisation. PYNQ is used to visualise the data at both the DAC and ADC side of the RFSoC data converters, as well as visualising various DSP stages throughout the transmit and receive signal path.  Contents    * [Introduction](introduction)    * [Hardware Setup](hardware-setup)    * [Software Setup](software-setup)* [RFSoC QPSK Transceiver](RFSoC-QPSK-Transceiver)    * [Inspecting the transmit path](Inspecting-the-transmit-path)    * [Inspecting the receive path](Inspecting-the-receive-path)    * [Reconfigure the RF Data Path](Reconfigure-the-RF-Data-Path)* [Conclusion](conclusion)     References* [Xilinx, Inc, "USP RF Data Converter: LogiCORE IP Product Guide", PG269, v2.3, June 2020](https://www.xilinx.com/support/documentation/ip_documentation/usp_rf_data_converter/v2_3/pg269-rf-data-converter.pdf) Revision History* **v1.0** | 02/11/2020 | RFSoC QPSK demonstrator* **v1.1** | 23/02/2021 | Reformatted notebook ----  Introduction The ZCU111 platform and XM500 development board can be configured as a simple QPSK transceiver. The RFSoC QPSK demonstrator uses the RFSoC's RF Data Converters (RF DCs) to transmit and receive QPSK modulated waveforms. There are setup steps for hardware and software that you must follow. Hardware Setup Your ZCU111 development board can be configured to host one QPSK transceiver channel. To setup your board for this demonstration, you can connect a channel in loopback as shown in [Figure 1](fig-1).The default loopback configuration is connected as follows:* Channel 0: DAC6 (Tile 229 Block 2) to ADC0 (Tile 224 Block 0)There are several XM500 board revisions, and some contain different silkscreen and labels for the ADCs and DACs. Use the image below for further guidance and pay attention to the associated Tile and Block.    Figure 1: ZCU111 and XM500 development board setup in loopback mode.**Do not** attach an antenna to any SMA interfaces labelled DAC.Caution:    In this demonstration, we generate tones using the RFSoC development board. Your device should be setup in loopback mode. You should understand that the RFSoC platform can also transmit RF signals wirelessly. Remember that unlicensed wireless transmission of RF signals may be illegal in your geographical location. Radio signals may also interfere with nearby devices, such as pacemakers and emergency radio equipment. Note that it is also illegal to intercept and decode particular RF signals. If you are unsure, please seek professional support. Software Setup Start by including the `xrfdc` drivers so we can configure the RF data converters, `ipywidgets` to make interactive controls, `numpy` for numerical analysis, and `rfsoc_qpsk` for the QPSK design.<jupyter_code>import xrfdc
import ipywidgets as ipw
import numpy as np

from rfsoc_qpsk.qpsk_overlay import QpskOverlay<jupyter_output><empty_output><jupyter_text>We can now initialise the overlay by downloading the bitstream and executing the drivers.<jupyter_code>ol = QpskOverlay()<jupyter_output><empty_output><jupyter_text>For a quick reference of all the things you can do with the QPSK overlay, ask the Python interpreter!Pop open a new console (right click here and select "_New Console for Notebook_") and type `ol.plot_group?` to query a method of our new overlay. Tab completion works for discovery too.----  RFSoC QPSK Transceiver We will now explore three interesting components of the QPSK demonstrator. Initially, the transmit path will be inspected and then the same inspection will also be carried out on the receive path. Finally, we will explore the control capabilities of our design and determine how these properties affect the transmit and receive signals. Inspecting the transmit path There are 3 main steps in the QPSK transmit IP signal path:1. Random symbol generation2. Pulse shaping3. Interpolation  This design "taps off" this path after the first two stages so we can inspect the signals in Jupyter Lab.The RF data converter can be reconfigured from Python too - we'll look at that [later](Reconfigure-the-RF-Data-Path).![](./images/block_diagram/QPSK_system_block_diagrams_Tx_only.svg)First we plot our raw QPSK symbols in the time domain.<jupyter_code>ol.plot_group(
  'tx_symbol',            # Plot group's ID
  ['time-binary'],        # List of plot types chosen from:
                          #   ['time','time-binary','frequency','constellation']
  ol.qpsk_tx.get_symbols, # Function to grab a buffer of samples
  500                     # Sample frequency (Hz)
)<jupyter_output><empty_output><jupyter_text>We can stream new samples into this plot using the play/stop buttons. By default the samples are stored in a rolling buffer, so we can keep this running for a while without worrying too much about total memory usage. As you continue to work through this notebook though, you should stop any previous plot streams to keep your browser happy.For the pulse shaped signal, let's have a look at the frequency domain too. This FFT is accelerated in the PL so we pass in an extra argument, `get_freq_data`, telling the plotting library how to grab the accelerated FFT data.<jupyter_code>ol.plot_group('tx_shaped', ['time', 'frequency'], ol.qpsk_tx.get_shaped_time, 4000,
              get_freq_data=ol.qpsk_tx.get_shaped_fft)<jupyter_output><empty_output><jupyter_text>Inspecting the receive path The receive side is nearly the inverse of the transmit path (there's just some extra work for properly synchronising).Again, there are taps off from a few places in the signal path:1. After decimation2. After coarse synchronisation3. After root-raised-cosine filtering4. and the data output![](./images/block_diagram/QPSK_system_block_diagrams_Rx_only.svg)Because there are a few different intermediate stages, let's reuse the same cells to plot any of them on-demand.First we describe how to generate plots for each of the intermediate taps.<jupyter_code>rx_domains = ['time', 'frequency', 'constellation']

plot_rx_decimated   = lambda : ol.plot_group(
    'rx_decimated',   rx_domains, ol.qpsk_rx.get_decimated,     4000
)

plot_rx_coarse_sync = lambda : ol.plot_group(
    'rx_coarse_sync', rx_domains, ol.qpsk_rx.get_coarse_synced, 4000
)

plot_rx_rrced       = lambda : ol.plot_group(
    'rx_rrced',       rx_domains, ol.qpsk_rx.get_rrced,         16000
)<jupyter_output><empty_output><jupyter_text>Now we can just execute the function whichever tap you want. For example, let's look at the tap after decimation below.<jupyter_code>plot_rx_decimated()<jupyter_output><empty_output><jupyter_text>And for the final plot, let's look at the synchronised output data. To recover the bits we need to take our sampled, synchronised signal (seen in the constellation plot below) and decide which quadrant each symbol has fallen into.<jupyter_code>def classify_bits(frame):
    bit_quantise    = lambda b: 1 if b>0 else 0
    symbol_quantise = lambda i, q: bit_quantise(i) + 1j*bit_quantise(q)
    return np.fromiter(
        map(symbol_quantise, np.real(frame), np.imag(frame)),
        dtype=np.complex
    )

ol.plot_group(
    'rx_data',
    ['constellation', 'time-binary'],
    lambda : classify_bits(ol.qpsk_rx.get_data()),
    500,
    get_const_data=ol.qpsk_rx.get_data
)<jupyter_output><empty_output><jupyter_text>Now is a good time to note that Jupyter Lab can manage multiple windows. Next we'll be playing with the RF settings, so you may want to make a new window for the constellation plot and leave it streaming. Make a new window for the plot by right clicking the plot and selecting "_Create New View for Output_". Feel free to snap this new window to the side by clicking the window's title ("Output View") and dragging it to the side of the web page. Now we can play with RF settings further down the notebook while still getting instant feedback about our received signal — pretty neat!  Reconfigure the RF Data Path  Transmit PowerThe QPSK bitstream includes a digital attenuator on the transmit path. We can configure this via a memory-mapped register.Let's use this as an example of interactive reconfiguration because the effects are quite clear in the constellation diagram. Try reducing the output power by setting a gain between 0 (off) and 1 (full scale).<jupyter_code>ol.qpsk_tx.set_gain(0.6)<jupyter_output><empty_output><jupyter_text>The constellation plot should shrink in a little towards the origin. Let's return to full power now.<jupyter_code>ol.qpsk_tx.set_gain(1)<jupyter_output><empty_output><jupyter_text>We can use some `ipywidgets` to make a more natural interface to control the gain too. Let's expose this as a slider with a callback to the `set_gain` function.<jupyter_code>pow_slider = ipw.SelectionSlider(
    options=[0.1, 0.3, 0.6, 1],
    value=1,
    description='',
)

accordion = ipw.Accordion(children=[pow_slider])
accordion.set_title(0, 'Transmitter power')
display(accordion)

def unwrap_slider_val(callback):
    return lambda slider_val : callback(slider_val['new'])

pow_slider.observe(unwrap_slider_val(ol.qpsk_tx.set_gain), names='value')<jupyter_output><empty_output><jupyter_text>Transmit and Receive Mixer SettingsSo far the RF Data Converter settings have been controlled by `QpskOverlay` but we can also reconfigure these on the fly in python with the `xrfdc` driver.First of all, consider the DAC block used for the transmit side.![](./images/block_diagram/RF_DAC.svg)There's a lot of scope for reconfiguration here — see the [IP product guide](https://www.xilinx.com/support/documentation/ip_documentation/usp_rf_data_converter/v2_1/pg269-rf-data-converter.pdf) or type `ol.dac_block?` for more details.As an example, let's play with the mixer settings. Try changing the DAC's mixer frequency from the deafult 1000 MHz to 900 MHz.<jupyter_code>def update_nco(rf_block, nco_freq):
    mixer_cfg = rf_block.MixerSettings
    mixer_cfg['Freq'] = nco_freq
    rf_block.MixerSettings = mixer_cfg
    rf_block.UpdateEvent(xrfdc.EVENT_MIXER)

update_nco(ol.dac_block, 900)<jupyter_output><empty_output><jupyter_text>The received signal should disappear until we configure the receiver's ADC to match the new carrier frequency. Set the new carrier frequency for the ADC side mixer below.<jupyter_code>update_nco(ol.adc_block, 900)<jupyter_output><empty_output><jupyter_text>Again, we can use `ipywidgets` to make an interactive interface for these settings. Below we setup an RX and a TX slider and a TX slider that are linked together so we can scrub along the spectrum keeping both sides in near lock-step. If you've got any analog RF filters to hand, try them out with different mixer settings!<jupyter_code>def new_nco_slider(title):
    return ipw.FloatSlider(
        value=1000,
        min=620,
        max=1220,
        step=20,
        description=title,
        disabled=False,
        continuous_update=False,
        orientation='horizontal',
        readout=True,
        readout_format='.1f',
    )

tx_nco_slider = new_nco_slider('TX (MHz)')
rx_nco_slider = new_nco_slider('RX (MHz)')

accordion = ipw.Accordion(children=[ipw.VBox([tx_nco_slider, rx_nco_slider])])
accordion.set_title(0, 'Carrier frequency')
display(accordion)

ipw.link((rx_nco_slider, 'value'), (tx_nco_slider, 'value'))
tx_nco_slider.observe(
    unwrap_slider_val(lambda v: update_nco(ol.dac_block, v)),
    names='value'
)
rx_nco_slider.observe(
    unwrap_slider_val(lambda v: update_nco(ol.adc_block, v)),
    names='value'
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07255304585900069, 'fraction_numerical': 0.012662559890485968, 'mean_word_length': 4.903535353535354, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 54, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '34378', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3603, 'n_tokens_neox': 3364, 'n_words': 1347, 'path': 'boards/ZCU111/rfsoc_qpsk/notebooks/rfsoc_qpsk_demonstrator.ipynb', 'repo_name': 'jogomojo/rfsoc_qpsk', 'size': '11688'}