Documentation and Examples for Jupyter Notebooks:
Jupyter Notebooks file:
When you have opened the Jupyter notebook app, drag the .ipynb file into the window.
OpAmpCalcGainPhaseLEDS.ipynb141.4KB
Code Explanation
Updated Code Explanation
import time
import numpy as np
from matplotlib import pyplot as plt
from rp_overlay import overlay
import rp
- Imports: The code imports the required libraries:
time
for sleep functions.numpy
for numerical operations.matplotlib.pyplot
for plotting the acquired signals.rp_overlay
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 prepare 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 the 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, 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.
# Gain calculation
input_amplitude = np.max(data_V_ch1) - np.min(data_V_ch1)
output_amplitude = np.max(data_V_ch2) - np.min(data_V_ch2)
gain = 20 * np.log10(output_amplitude / input_amplitude)
print(f"At {frequency} Hz: Gain = {gain:.2f} dB, Vin Peak-to-Peak = {input_amplitude:.2f} V, Vout Peak-to-Peak = {output_amplitude:.2f} V")
- Gain Calculation:
- Calculates the peak-to-peak amplitude of the input and output signals.
- Computes the gain in dB using the ratio of the output amplitude to the input amplitude.
- Prints the gain and the peak-to-peak voltages of the input and output signals.
# 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.
Summary of Updates
- Signal Generation and Acquisition Setup: The code sets up the Red Pitaya to generate a sine wave signal and acquire the data.
- Data Acquisition: The code retrieves the acquired data from the input and output channels.
- Gain Calculation: The code calculates the gain of the system based on the acquired data.
- Plotting: The code plots the acquired input and output signals for visualization.
- Resource Management: The code ensures that resources are properly released after the experiment.
These updates make the code more comprehensive by not only generating and acquiring the signals but also processing and visualizing the data.