2.3.6.4.2. Instant signal acquisition
2.3.6.4.2.1. Description
This example shows how to instantly acquire 16k samples of a signal on fast analog inputs. The time length of the acquired signal depends on the time scale of a buffer that can be set with a decimation factor. The decimations and time scales of a buffer are given in the sample rate and decimation. Voltage and frequency ranges depend on the Red Pitaya model.
2.3.6.4.2.2. Required hardware
Red Pitaya device
Signal (function) generator
Wiring example for STEMlab 125-14 & STEMlab 125-10:
2.3.6.4.2.3. Circuit
2.3.6.4.2.4. SCPI Code Examples
Note
This code is written for 2.00-23 or higher OS. For older OS versions, please check when specific commands were released (a note is added to each command introduced in 2.00 or higher verisons).
Note
With the latest OS versions you can use ACQ:DEC:F <decimation_factor>
command for more precise control over the acquisition. The decimation factor can be any of [1, 2, 4, 8, 16, 17, 18, 19, ..., 65535, 65536]
.
2.3.6.4.2.4.1. Code - MATLAB®
The code is written in MATLAB. In the code, we use SCPI commands and TCP client communication.
Copy the code from below into the MATLAB editor, save the project, and hit the "Run" button.
%% Define Red Pitaya as TCP/IP object
clear all
close all
clc
IP = '192.168.178.111'; % Input IP of your Red Pitaya...
port = 5000;
RP = tcpclient(IP, port);
%% Open connection with your Red Pitaya
RP.ByteOrder = 'big-endian';
configureTerminator(RP,'CR/LF');
flush(RP);
% Reset Acquisition
writeline(RP,'ACQ:RST');
%% ACQUISITION
% Set decimation value (sampling rate) in respect to your
% acquired signal frequency
writeline(RP,'ACQ:DEC 4');
% Set trigger delay to 0 samples
% 0 samples delay sets trigger to center of the buffer
% Signal on your graph will have trigger in the center (symmetrical)
% Samples from left to the center are samples before the trigger
% Samples from center to the right are samples after the trigger
writeline(RP,'ACQ:TRig:DLY 0');
% for SIGNALlab device there is a possiblity to set the trigger threshold
% writeline(RP,'ACQ:TRig:EXT:LEV 1')
%% Start & Trigg
% Trigger source setting must be after ACQ:START
% Set trigger to source 1 positive edge
writeline(RP,'ACQ:START');
% After acquisition is started some time delay is needed in order to acquire fresh samples in to buffer
pause(1);
% Here we have used time delay of one second but you can calculate the exact value taking in to account buffer
% length and sampling rate
writeline(RP,'ACQ:TRig NOW');
% Wait for trigger
% Until trigger is true wait with acquiring
% Be aware of while loop if trigger is not achieved
% Ctrl+C will stop code execution in MATLAB
while 1
trig_rsp = writeread(RP,'ACQ:TRig:STAT?')
if strcmp('TD',trig_rsp(1:2)) % Read only TD
break;
end
end
%%! OS 2.00 or higher only !%%
% wait for fill adc buffer
while 1
fill_state = writeread(RP,'ACQ:TRig:FILL?')
if strcmp('1', fill_state(1:1))
break;
end
end
% Read data from buffer
signal_str = writeread(RP,'ACQ:SOUR1:DATA?');
signal_str_2 = writeread(RP,'ACQ:SOUR2:DATA?');
% Convert values to numbers.
% The first character in string is “{“
% and the last 3 are 2 spaces and “}”.
signal_num = str2num(signal_str (1, 2:length(signal_str) - 3));
signal_num_2 = str2num(signal_str_2(1, 2:length(signal_str_2) - 3));
plot(signal_num)
hold on
plot(signal_num_2,'r')
grid on
ylabel('Voltage / V')
xlabel('samples')
clear RP;
2.3.6.4.2.4.2. Code - Python
Using just SCPI commands:
#!/usr/bin/env python3
import sys
import redpitaya_scpi as scpi
import matplotlib.pyplot as plot
IP = 'rp-f066c8.local'
rp_s = scpi.scpi(IP)
rp_s.tx_txt('ACQ:RST')
rp_s.tx_txt('ACQ:DEC 4')
rp_s.tx_txt('ACQ:START')
rp_s.tx_txt('ACQ:TRig NOW')
while 1:
rp_s.tx_txt('ACQ:TRig:STAT?')
if rp_s.rx_txt() == 'TD':
break
## ! OS 2.00 or higher only ! ##
while 1:
rp_s.tx_txt('ACQ:TRig:FILL?')
if rp_s.rx_txt() == '1':
break
rp_s.tx_txt('ACQ:SOUR1:DATA?')
buff_string = rp_s.rx_txt()
buff_string = buff_string.strip('{}\n\r').replace(" ", "").split(',')
buff = list(map(float, buff_string))
plot.plot(buff)
plot.ylabel('Voltage')
plot.show()
Using functions:
#!/usr/bin/env python3
import sys
import redpitaya_scpi as scpi
import matplotlib.pyplot as plot
IP = 'rp-f066c8.local'
rp_s = scpi.scpi(IP)
rp_s.tx_txt('ACQ:RST')
dec = 4
# Function for configuring Acquisition
rp_s.acq_set(dec)
rp_s.tx_txt('ACQ:START')
rp_s.tx_txt('ACQ:TRig NOW')
while 1:
rp_s.tx_txt('ACQ:TRig:STAT?')
if rp_s.rx_txt() == 'TD':
break
## ! OS 2.00 or higher only ! ##
while 1:
rp_s.tx_txt('ACQ:TRig:FILL?')
if rp_s.rx_txt() == '1':
break
# function for Data Acquisition
buff = rp_s.acq_data(1, convert= True)
plot.plot(buff)
plot.ylabel('Voltage')
plot.show()
Note
The Python functions are accessible with the latest version of the redpitaya_scpi.py document available on our GitHub. The functions represent a quality-of-life improvement as they combine the SCPI commands in an optimal order and also check for improper user inputs. The code should function at approximately the same speed without them.
For further information on functions please consult the redpitaya_scpi.py code.
2.3.6.4.2.5. API Code Examples
Note
The API code examples don’t require the use of the SCPI server. Instead, the code should be compiled and executed on the Red Pitaya itself (inside Linux OS). Instructions on how to compile the code and other useful information are here.
2.3.6.4.2.5.1. Code - C API
2.3.6.4.2.5.2. Code - Python API
#!/usr/bin/python3
import time
import numpy as np
import rp
#? Possible decimations:
#? RP_DEC_1, RP_DEC_2, RP_DEC_4, RP_DEC_8, RP_DEC_16 , RP_DEC_32 , RP_DEC_64 ,
#? RP_DEC_128, RP_DEC_256, RP_DEC_512, RP_DEC_1024, RP_DEC_2048, RP_DEC_4096, RP_DEC_8192,
#? RP_DEC_16384, RP_DEC_32768, RP_DEC_65536
dec = rp.RP_DEC_1
trig_lvl = 0.5
trig_dly = 0
#? Possible acquisition trigger sources:
#? RP_TRIG_SRC_DISABLED, RP_TRIG_SRC_NOW, RP_TRIG_SRC_CHA_PE, RP_TRIG_SRC_CHA_NE, RP_TRIG_SRC_CHB_PE,
#? RP_TRIG_SRC_CHB_NE, RP_TRIG_SRC_EXT_PE, RP_TRIG_SRC_EXT_NE, RP_TRIG_SRC_AWG_PE, RP_TRIG_SRC_AWG_NE,
#? RP_TRIG_SRC_CHC_PE, RP_TRIG_SRC_CHC_NE, RP_TRIG_SRC_CHD_PE, RP_TRIG_SRC_CHD_NE
acq_trig_sour = rp.RP_TRIG_SRC_NOW
N = 16384
# Initialize the interface
rp.rp_Init()
# Reset Acquisition
rp.rp_AcqReset()
##### Acquisition #####
# Set Decimation
rp.rp_AcqSetDecimation(dec)
# Set trigger level and delay
rp.rp_AcqSetTriggerLevel(rp.RP_T_CH_1, trig_lvl)
rp.rp_AcqSetTriggerDelay(trig_dly)
# Start Acquisition
print("Acq_start")
rp.rp_AcqStart()
# Specify trigger - immediately
rp.rp_AcqSetTriggerSrc(acq_trig_sour)
# Trigger state
while 1:
trig_state = rp.rp_AcqGetTriggerState()[1]
if trig_state == rp.RP_TRIG_STATE_TRIGGERED:
break
## ! OS 2.00 or higher only ! ##
# Fill state
while 1:
if rp.rp_AcqGetBufferFillState()[1]:
break
### Get data ###
# RAW
ibuff = rp.i16Buffer(N)
res = rp.rp_AcqGetOldestDataRaw(rp.RP_CH_1, N, ibuff.cast())
# Volts
fbuff = rp.fBuffer(N)
res = rp.rp_AcqGetDataV(rp.RP_CH_1, 0, N, fbuff)
data_V = np.zeros(N, dtype = float)
data_raw = np.zeros(N, dtype = int)
for i in range(0, N, 1):
data_V[i] = fbuff[i]
data_raw[i] = ibuff[i]
print(f"Data in Volts: {data_V}")
print(f"Raw data: {data_raw}")
# Release resources
rp.rp_Release()