Documentation and Examples for Jupyter Notebooks:
Jupyter Notebooks file:
When you have opened the Jupyter notebook app, drag the .ipynb file into the window.
Â
Code Explanation
import time import numpy as np from matplotlib import pyplot as plt from rp_overlay import overlay import rp
- Imports: The code starts by importing necessary libraries.
time
for sleep functions,numpy
for numerical operations,matplotlib.pyplot
for plotting the acquired signals, andrp
for interfacing with the Red Pitaya hardware.
# Initialize Red Pitaya overlay fpga = overlay() rp.rp_Init()
- Initialization: Initializes the Red Pitaya overlay and the Red Pitaya library to set up the device for signal generation and acquisition.
# Constants for the experiment frequency = 1000 # Hz ampl = 1 # Amplitude N = 16384 # Number of samples for acquisition dec = rp.RP_DEC_128 # Decimation sampling_rate = 125e6 / dec # Adjusted for decimation
- Constants: Defines constants used in the experiment:
frequency
: The frequency of the generated sine wave signal.ampl
: The amplitude of the generated signal.N
: The number of samples to acquire.dec
: Decimation factor for reducing the sampling rate.sampling_rate
: Calculated sampling rate based on the decimation factor.
# Acquisition settings trig_lvl = 0.5 trig_dly = 0 acq_trig_sour = rp.RP_TRIG_SRC_AWG_PE
- Acquisition Settings: Defines the settings for the acquisition process:
trig_lvl
: Trigger level voltage.trig_dly
: Trigger delay.acq_trig_sour
: Trigger source set to the Arbitrary Waveform Generator (AWG).
###### Generation ##### rp.rp_GenReset() rp.rp_AcqReset() print(f"Generating {frequency} Hz signal") rp.rp_GenWaveform(rp.RP_CH_1, rp.RP_WAVEFORM_SINE) rp.rp_GenFreqDirect(rp.RP_CH_1, frequency) rp.rp_GenAmp(rp.RP_CH_1, ampl) rp.rp_GenOutEnable(rp.RP_CH_1) rp.rp_GenTriggerOnly(rp.RP_CH_1)
- Signal Generation:
- Resets the signal generator and acquisition settings.
- Configures the signal generator to produce a sine wave at the specified frequency and amplitude.
- Enables the output channel and triggers the signal generation.
##### Acquisition ##### rp.rp_AcqSetDecimation(dec) rp.rp_AcqSetTriggerLevel(rp.RP_T_CH_1, trig_lvl) rp.rp_AcqSetTriggerDelay(trig_dly) rp.rp_AcqSetGain(rp.RP_CH_2, rp.RP_HIGH) rp.rp_AcqStart() rp.rp_AcqSetTriggerSrc(acq_trig_sour)
- Acquisition Configuration:
- Sets the decimation factor to reduce the sampling rate.
- Configures the trigger level and delay.
- Sets the gain for the acquisition channel.
- Starts the acquisition process and sets the trigger source.
time.sleep(1) # Wait to ensure acquisition is ready rp.rp_GenTriggerOnly(rp.RP_CH_1) # Trigger generation
- Wait and Trigger:
- Waits for 1 second to ensure the acquisition system is ready.
- Triggers the signal generation.
# Wait for acquisition trigger while rp.rp_AcqGetTriggerState()[1] != rp.RP_TRIG_STATE_TRIGGERED: pass # Wait until buffer is full while not rp.rp_AcqGetBufferFillState()[1]: pass
- Wait for Acquisition:
- Waits until the acquisition system is triggered.
- Waits until the acquisition buffer is full.
### Get data ### fbuff_ch1 = rp.fBuffer(N) rp.rp_AcqGetOldestDataV(rp.RP_CH_1, N, fbuff_ch1) fbuff_ch2 = rp.fBuffer(N) rp.rp_AcqGetOldestDataV(rp.RP_CH_2, N, fbuff_ch2) # Convert the buffer data to numpy arrays data_V_ch1 = np.array([fbuff_ch1[i] for i in range(N)]) data_V_ch2 = np.array([fbuff_ch2[i] for i in range(N)])
- Data Acquisition:
- Allocates buffers to store the acquired data.
- Retrieves the oldest data from both input (CH_1) and output (CH_2) channels.
- Converts the buffer data to numpy arrays for further processing or plotting.
# Plotting the signals plt.figure(figsize=(10, 5)) plt.plot(data_V_ch1, label='Input Signal (Vin)') plt.plot(data_V_ch2, label='Output Signal (Vout)') plt.title(f'Signals at {frequency} Hz') plt.xlabel('Sample Number') plt.ylabel('Voltage (V)') plt.legend() plt.grid(True) plt.show()
- Plotting:
- Plots the acquired input and output signals.
- Labels the plots and adds a legend for clarity.
- Displays the plot using
plt.show()
.
# Release resources rp.rp_Release()
- Resource Release: Releases the resources used by the Red Pitaya to ensure proper cleanup after the experiment.
This code generates a sine wave signal, acquires the input and output signals, and plots them for visualization. It is a basic template that can be expanded or modified for more complex experiments.
import time import numpy as np from matplotlib import pyplot as plt from rp_overlay import overlay import rp # Initialize Red Pitaya overlay fpga = overlay() rp.rp_Init() # Constants for the experiment frequency = 1000 # Hz ampl = 1 # Amplitude N = 16384 # Number of samples for acquisition dec = rp.RP_DEC_128 # Decimation sampling_rate = 125e6 / dec # Adjusted for decimation # Acquisition settings trig_lvl = 0.5 trig_dly = 0 acq_trig_sour = rp.RP_TRIG_SRC_AWG_PE ###### Generation ##### rp.rp_GenReset() rp.rp_AcqReset() print(f"Generating {frequency} Hz signal") rp.rp_GenWaveform(rp.RP_CH_1, rp.RP_WAVEFORM_SINE) rp.rp_GenFreqDirect(rp.RP_CH_1, frequency) rp.rp_GenAmp(rp.RP_CH_1, ampl) rp.rp_GenOutEnable(rp.RP_CH_1) rp.rp_GenTriggerOnly(rp.RP_CH_1) ##### Acquisition ##### rp.rp_AcqSetDecimation(dec) rp.rp_AcqSetTriggerLevel(rp.RP_T_CH_1, trig_lvl) rp.rp_AcqSetTriggerDelay(trig_dly) rp.rp_AcqSetGain(rp.RP_CH_2, rp.RP_HIGH) rp.rp_AcqStart() rp.rp_AcqSetTriggerSrc(acq_trig_sour) time.sleep(1) # Wait to ensure acquisition is ready rp.rp_GenTriggerOnly(rp.RP_CH_1) # Trigger generation # Wait for acquisition trigger while rp.rp_AcqGetTriggerState()[1] != rp.RP_TRIG_STATE_TRIGGERED: pass # Wait until buffer is full while not rp.rp_AcqGetBufferFillState()[1]: pass ### Get data ### fbuff_ch1 = rp.fBuffer(N) rp.rp_AcqGetOldestDataV(rp.RP_CH_1, N, fbuff_ch1) fbuff_ch2 = rp.fBuffer(N) rp.rp_AcqGetOldestDataV(rp.RP_CH_2, N, fbuff_ch2) # Convert the buffer data to numpy arrays data_V_ch1 = np.array([fbuff_ch1[i] for i in range(N)]) data_V_ch2 = np.array([fbuff_ch2[i] for i in range(N)]) # Plotting the signals plt.figure(figsize=(10, 5)) plt.plot(data_V_ch1, label='Input Signal (Vin)') plt.plot(data_V_ch2, label='Output Signal (Vout)') plt.title(f'Signals at {frequency} Hz') plt.xlabel('Sample Number') plt.ylabel('Voltage (V)') plt.legend() plt.grid(True) plt.show() # Release resources rp.rp_Release()