Block mode in real time

Post your MATLAB discussions here
Post Reply
Posts: 0
Joined: Wed Nov 11, 2015 8:00 am

Block mode in real time

Post by lucky112 » Wed Nov 11, 2015 9:05 am

Hi! I bought several PICO 4424 to measure the real-time signal using MATLAB 2014b (win7 64bit).

I want to use a triggering pulse signal (on channel B) with the period of 1ms to synchronize the measurements. On channel A, I want to measure 'a block' about 50ms, synchronized with the trigger signal from channel B (50 periods). Sampling rate needs to be as high as possible.

And, there will be a time gap because of buffer problems. I need a little time to calculate 2D FFT as well.
After transferring and calculating, I'll show the results on GUI and continue the process again. The total time gap doesnt matter even a few seconds is okay.
That's what I want to implement using MATLAB. Is it possilbe? From my understanding, a block mode would be most appropriate but I have a few questions.

Which trigger modes do I have to use? Indeed I didnt get the difference between single, rapid, and repeat mode.

Sorry for my bad english. Thank you!!

Also, in PS4000_IC_Generic_Driver_Streaming.m, there is a while loop that can be used for real-time control.

Code: Select all

while(hasAutoStopped == PicoConstants.FALSE && status.getStreamingLatestValues ==  PicoStatus.PICO_OK) 
And It works well.
How can I write for a block mode so that I can see the FFT results in real-time?

Posts: 2855
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Block mode in real time

Post by Hitesh » Fri Nov 13, 2015 3:59 pm

Hi lucky112,

In the PicoScope 6 software, the trigger modes are defined as follows (please also refer to the PicoScope 6 User's Guide - section 7.10 Triggering toolbar):

Repeat: PicoScope waits indefinitely for a trigger event before displaying data. It repeats this process until you click the Stop button. If there is no trigger event, PicoScope displays nothing

Single: PicoScope waits once for a trigger event, then stops sampling. To make PicoScope repeat this process, click the Start button. The Single trigger is the only type that allows one capture to fill the entire buffer memory.

Rapid: PicoScope instructs the scope device to acquire a sequence of waveforms with the minimum possible delay between them. The display is not updated until the last waveform in the sequence has been captured. When the operation is finished, you can step through the waveforms using the Buffer Navigation toolbar.

As you need to capture 50 cycles of data with a 1ms trigger event, you will need to use the Rapid Block capture example as a basis for your application. You will need to do something along the lines of:

1. Connect to the PicoScope
2. Configure the PicoScope channels and trigger
3. Setup for rapid block capture
4. Start the capture
5. Retrieve the data from the device
6. Process the data and display
7. Repeat steps 4 to 6

Please note that the Instrument Driver does not fully support multiple devices at the present moment so you may need to call the API functions from the driver directly. If you find that the retrieval of data is a little slow, you will need to preallocate the memory for the data buffers and register them with the driver before the start of data capture.

I hope this helps,

Software Dev. Engineer

Posts: 0
Joined: Wed Nov 11, 2015 8:00 am

Re: Block mode in real time

Post by lucky112 » Mon Nov 23, 2015 5:59 am

Hi Hitesh,

Thanks for your explanation of the trigger modes. Now I understand the trigger modes are only for the Picoscope 6 software.

As I said before, I made the code using an infinite while loop. Measuring a block, processing the data, UDP communication, and repeating the process. And it works well with the block mode. (not rapid block)
Sampling interval = 100ns, the number of samples per one block = 1000000.
I need to measure one block without gaps. (continuously)

Here is a question. If I use the rapid block mode, there will be small (but inevitable) gaps between segments.
But you said I need to use the rapid Block capture.
Is it right that I have to use the block mode to measure without gaps?

And next question.

I made a simple code without a loop.

Code: Select all

%% Connect
archStr = computer('arch');
    addpath(strcat('./', archStr, '/'));
catch err
    error('Operating system not supported');
    set(handles.edit1,'String','Error : Operating system not supported');

if ~libisloaded('ps4000')
    if ~libisloaded('ps4000')
        error('library file is not found') 
        set(handles.edit1,'String','Error : library file is not found');

[ps4000Methodinfo, ps4000Structs,  ps4000Enuminfo, ...
    ps4000ThunkLibName] = ps4000MFile;

ps4000DeviceObj = icdevice('picotech_ps4000_generic', ''); 

%% Channel Setting
% Channel A
channelSettings(1).enabled = PicoConstants.TRUE;
channelSettings(1).coupling = 1;
channelSettings(1).range = ps4000Enuminfo.enPS4000Range.PS4000_5V;

% Channel B
channelSettings(2).enabled = PicoConstants.TRUE;
channelSettings(2).coupling = 1;
channelSettings(2).range = ps4000Enuminfo.enPS4000Range.PS4000_5V;

% Channel C
channelSettings(3).enabled = PicoConstants.FALSE;
channelSettings(3).coupling = 1;
channelSettings(3).range = ps4000Enuminfo.enPS4000Range.PS4000_5V;

% Channel D
channelSettings(4).enabled = PicoConstants.FALSE;
channelSettings(4).coupling = 1;
channelSettings(4).range = ps4000Enuminfo.enPS4000Range.PS4000_5V;

numChannels = get(ps4000DeviceObj, 'channelCount');
for ch = 1:numChannels
    status.setChannelStatus(ch) = invoke(ps4000DeviceObj, 'ps4000SetChannel', ...
    (ch - 1), channelSettings(ch).enabled, ...
    channelSettings(ch).coupling, channelSettings(ch).range);

%% Trigger & Timebase

triggerGroupObj = get(ps4000DeviceObj, 'Trigger');
triggerGroupObj = triggerGroupObj(1);

[status.SimpleTrigger] = invoke(triggerGroupObj, 'setSimpleTrigger', 1, 1000, 2);

totalSamplingTime = 100*1e6;
[status.getTimebase, timeIntervalNanoSeconds, maxSamples] = invoke(ps4000DeviceObj, 'ps4000GetTimebase2', 3, 0);

BUFFER_SIZE = totalSamplingTime/timeIntervalNanoSeconds;

set(ps4000DeviceObj, 'numPreTriggerSamples', 0);
set(ps4000DeviceObj, 'numPostTriggerSamples', BUFFER_SIZE);

%% Run block
blockGroupObj = get(ps4000DeviceObj, 'Block');
blockGroupObj = blockGroupObj(1);
[status.runBlock] = invoke(blockGroupObj, 'runBlock', 0);
[~, ~, chA, ~] = invoke(blockGroupObj, 'getBlockData', 0, 0, 1, 0);

%% Processing data
TRIG_PER = 500e-6;
Fs = 1/(timeIntervalNanoSeconds*1e-9);
numCol = TRIG_PER/(timeIntervalNanoSeconds*1e-9);

Signal_DCX = chA-mean(chA);
Varied_Signal = reshape(Signal_DCX  , numCol, []);
final_Signal = Varied_Signal; 
f1_num = 8*2^nextpow2(numel(final_Signal)/length(final_Signal));

Result_FFT2 = fft2(final_Signal,f2_num, f1_num);
absresult_FFT2 = fftshift(abs(Result_FFT2));

CalFig = figure;

%% Disconnent
status.stop = invoke(ps4000DeviceObj, 'ps4000Stop');
the 2dFFT result is what I want to get.
Surprisingly, If I use the above code, I get very noisy result (attachment1).
However, If I follow the same process with Picoscope 6, the result is good and different (attachment2).
I tried many times with different enviroments,
but always MATLAB gets noisy results and Picoscope6 gets better results.

How can I get a cleaner result like using Picoscope6??

Posts: 2855
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Block mode in real time

Post by Hitesh » Mon Nov 23, 2015 3:29 pm

Hi lucky112,

The trigger mode names given above are those specified in PicoScope 6 software. They can be replicated via the SDK depending on how you set up the data collection and whether you set up a trigger or not - for example:

Single - collect a block of data using a trigger
Repeat - collect several blocks of data using a trigger

If you need to collect data from start to finish without gaps and the amount of data is less than or equal to the buffer memory available on the device you should use block mode otherwise use streaming mode if you need to capture a quantity of data greater than the number of samples on the device.

Regarding your last question, I will contact you via our e-mail support system.

Update: I have run some tests and have found that the reason for the discrepancy in magnitude when collecting the data using the MATLAB Instrument Driver is that the Instrument Driver returns the values in millivolts whereas PicoScope 6 exports the data in Volts.

Try scaling the data from the MATLAB Instrument Driver to Volts and see how that affects your FFT calculations.


Software Dev. Engineer

Post Reply