Capturing Single Shot Signal With MATLAB

Post your MATLAB discussions here
Post Reply
dnieltjhin
Newbie
Posts: 1
Joined: Fri Aug 17, 2018 11:24 am

Capturing Single Shot Signal With MATLAB

Post by dnieltjhin »

Hello,

In my current project I have been using the PicoScope 5244A to generate a single shot sine wave, which is then connected to a pulser, and eventually a diode. The problem I have is that I want to capture the signal from the diode with MATLAB through the same PicoScope. Is there a way to write a script to tell the PicoScope to 'wait' for a signal from the diode, which is pulsed from the PicoScope itself?

This is what I wrote for the shooting mechanism:

Code: Select all

%% SHOOTING SCRIPT
set(ps5000aDeviceObj, 'startFrequency', 500);
set(ps5000aDeviceObj, 'stopFrequency', 500);
offsetMv = 0;
pkToPkMv = 4000;
waveType = ps5000aEnuminfo.enPS5000AWaveType.PS5000A_SINE;
increment = 0; % Hz
dwellTime = 1;    % seconds
sweepType = ps5000aEnuminfo.enPS5000ASweepType.PS5000A_UP;
operatiom = PicoConstants.FALSE;
shots = 1;
sweeps = 0;
triggerType = ps5000aEnuminfo.enPS5000ASigGenTrigType.PS5000A_SIGGEN_RISING;
triggerSource = ps5000aEnuminfo.enPS5000ASigGenTrigSource.PS5000A_SIGGEN_SOFT_TRIG;
extInThresholdMv = 0;
invoke(ps5000aDeviceObj, 'setSigGenBuiltIn', offsetMv, pkToPkMv, waveType, increment, ...
    dwellTime, sweepType, operatiom, shots, sweeps, triggerType, triggerSource, extInThresholdMv);
invoke(ps5000aDeviceObj, 'ps5000aSigGenSoftwareControl', 1);
Then the simple trigger:

Code: Select all

%% SET DEVICE RESOLUTION

% Max. resolution with 2 channels enabled is 15 bits.
[status, resolution] = invoke(ps5000aDeviceObj, 'ps5000aSetDeviceResolution', 12);
set(ps5000aDeviceObj,'timebase',1);

%% SET SIMPLE TRIGGER

% Channel     : 0 (PS5000A_CHANNEL_A)
% Threshold   : 1000 (mV)
% Direction   : 2 (Rising)
% Delay       : 0
% Auto trigger: 0 (wait indefinitely)

[status] = invoke(ps5000aDeviceObj, 'setSimpleTrigger', 0, -200, 2, 0, 100);

%% GET TIMEBASE

% Driver default timebase index used - use ps5000aGetTimebase or
% ps5000aGetTimebase2 to query the driver as to suitability of using a
% particular timebase index then set the 'timebase' property if required.

% timebase     : 65 (default)
% segment index: 0

[status, timeIntervalNanoSeconds, maxSamples] = invoke(ps5000aDeviceObj, 'ps5000aGetTimebase', 1, 0);

%% SET BLOCK PARAMETERS AND CAPTURE DATA

% Set pre-trigger samples.
set(ps5000aDeviceObj, 'numPreTriggerSamples', 1024);

% Capture a block of data:
%
% segment index: 0

[status] = invoke(ps5000aDeviceObj, 'runBlock', 0);

% Retrieve data values:
%
% start index       : 0
% segment index     : 0
% downsampling ratio: 1
% downsampling mode : 0 (PS5000A_RATIO_MODE_NONE)

[chA, chB, chC, chD, numSamples, overflow] = invoke(ps5000aDeviceObj, 'getBlockData', 0, 0, 1, 0);

% Stop the device
[status] = invoke(ps5000aDeviceObj, 'ps5000aStop');

%% PROCESS DATA

% Plot data values.

figure;

% Calculate time (nanoseconds) and convert to milliseconds
% Use timeIntervalNanoSeconds output from ps5000aGetTimebase or
% ps5000aGetTimebase2 or calculate from Programmer's Guide.

timeNs = double(timeIntervalNanoSeconds) * double([0:numSamples - 1]);
timeMs = timeNs / 1e6;

% Channel A
axisHandleChA = subplot(2,1,1); 
plot(timeMs, chA, 'b');
title('Channel A');
xlabel(axisHandleChA, 'Time (ms)');
ylabel(axisHandleChA, 'Voltage (mV)');

% Channel B

axisHandleChB = subplot(2,1,2); 
plot(timeMs, chB, 'r');
title('Channel B');
xlabel(axisHandleChB, 'Time (ms)');
ylabel(axisHandleChB, 'Voltage (mV)');
Or do I have to work the scripts in parallel?

Hitesh

Re: Capturing Single Shot Signal With MATLAB

Post by Hitesh »

Hi dnieltjhin,

You can control the functionality from the same script. Here's what you will need to do:
  • Call setSigGenBuiltIn() before starting the data collection
  • Call the Instrument Driver's ps5000aRunBlock() function
  • Call ps5000aSigGenSoftwareControl()
  • Call ps5000aIsReady() in a loop until the device indicates that it is ready
Depending on the code that you have written, you may wish to use the latest Instrument Driver files and examples. Please note that functions are now organised into groups which is shown in the updated examples.

Regards,

dnieltjhin
Newbie
Posts: 1
Joined: Fri Aug 17, 2018 11:24 am

Re: Capturing Single Shot Signal With MATLAB

Post by dnieltjhin »

Hello Hitesh,

Thank you for the answer. I've tried to write the script as followed:

Code: Select all

%% SHOOTING SCRIPT PARAMETERS
set(ps5000aDeviceObj, 'startFrequency', 500);
set(ps5000aDeviceObj, 'stopFrequency', 500);
offsetMv = 0;
pkToPkMv = 4000;
waveType = ps5000aEnuminfo.enPS5000AWaveType.PS5000A_SINE;
increment = 0; % Hz
dwellTime = 1;    % seconds
sweepType = ps5000aEnuminfo.enPS5000ASweepType.PS5000A_UP;
operatiom = PicoConstants.FALSE;
shots = 1;
sweeps = 0;
triggerType = ps5000aEnuminfo.enPS5000ASigGenTrigType.PS5000A_SIGGEN_RISING;
triggerSource = ps5000aEnuminfo.enPS5000ASigGenTrigSource.PS5000A_SIGGEN_SOFT_TRIG;
extInThresholdMv = 0;

%% SET DEVICE RESOLUTION

% Max. resolution with 2 channels enabled is 15 bits.
[status, resolution] = invoke(ps5000aDeviceObj, 'ps5000aSetDeviceResolution', 12);
set(ps5000aDeviceObj,'timebase',1);

%% SET SIMPLE TRIGGER

% Channel     : 0 (PS5000A_CHANNEL_A)
% Threshold   : 1000 (mV)
% Direction   : 2 (Rising)
% Delay       : 0
% Auto trigger: 0 (wait indefinitely)

[status] = invoke(ps5000aDeviceObj, 'setSimpleTrigger', 0, -200, 2, 0, 100);

%% GET TIMEBASE

% Driver default timebase index used - use ps5000aGetTimebase or
% ps5000aGetTimebase2 to query the driver as to suitability of using a
% particular timebase index then set the 'timebase' property if required.

% timebase     : 65 (default)
% segment index: 0

[status, timeIntervalNanoSeconds, maxSamples] = invoke(ps5000aDeviceObj, 'ps5000aGetTimebase', 1, 0);

%% CALLING SIGGENBUILTIN

invoke(ps5000aDeviceObj, 'setSigGenBuiltIn', offsetMv, pkToPkMv, waveType, increment, ...
    dwellTime, sweepType, operatiom, shots, sweeps, triggerType, triggerSource, extInThresholdMv);

%% SET BLOCK PARAMETERS AND CAPTURE DATA

% Set pre-trigger samples.
set(ps5000aDeviceObj, 'numPreTriggerSamples', 1024);

% Capture a block of data:

[status] = invoke(ps5000aDeviceObj, 'runBlock', 0);

% Retrieve data values:
%
% start index       : 0
% segment index     : 0
% downsampling ratio: 1
% downsampling mode : 0 (PS5000A_RATIO_MODE_NONE)

[chA, chB, chC, chD, numSamples, overflow] = invoke(ps5000aDeviceObj, 'getBlockData', 0, 0, 1, 0);

%% CALLING SIGGENCONTROL

invoke(ps5000aDeviceObj, 'ps5000aSigGenSoftwareControl', 1);
%% LOOP

invoke(ps5000aDeviceObj, 'ps5000aIsReady')
Yet when I tried to run the script, I got this error:

Code: Select all

Error using privateExecuteMCode (line 44)
runBlock: Error in ps5000aRunBlock call - code 14

Error in instrgate (line 20)
   [varargout{1:nout}] = feval(varargin{:});

Error in icdevice/invoke (line 70)
        output = instrgate('privateExecuteMCode', code, obj, varargin, nargout);

Error in Control (line 56)
[status] = invoke(ps5000aDeviceObj, 'runBlock', 0);
Do you have any idea on what's causing it? Thank you in advance.

Hitesh

Re: Capturing Single Shot Signal With MATLAB

Post by Hitesh »

Hi dnieltjhin,

The error code returned from the driver function call can be converted to a hexadecimal number using the dec2hex() function in MATLAB. The PicoStatus.m class in the PicoScope Support Toolbox can be then used to look up the error/warning, in this case PICO_INVALID_TIMEBASE (0xE).

I can see that you queried the ps5000aGetTimebase() function with a timebase index of 1. At 15-bit resolution, the faster possible sampling rate is 125 MS/s for a single channel (timebase index 3).

Please set an appropriate timebase index, setting the Instrument Driver's timebase property before the call to ps5000aRunBlock() e.g.

Code: Select all

set(ps5000aDeviceObj, 'timebase', 3);
Don't forget to call the disconnect code in the event of an error before attempting to run the script again.

Regards,

Post Reply