Test and Measurement Forum

Counting the number of rising/falling edges of a signal

Post your MATLAB discussions here

Counting the number of rising/falling edges of a signal

Postby Hitesh » Fri Dec 20, 2013 4:11 pm

This example is based on the following article by MathWorks and demonstrates how to count the number of rising and falling edges of a signal acquired from a PicoScope 2205A using the PicoScope 2000 Series MATLAB Instrument Driver:

Counting Complex Events Using Analog Input Data




  • PicoScope 2205A
  • Signal generator source (in this case a TTi TG5011 50MHz Function/Arbitrary/Pulse Generator)
  • BNC - BNC cable
  • 50Ohm feed through terminator


  • MATLAB(R) v8.0 (2012b)
  • Instrument Control Toolbox(TM) v3.2
  • PicoScope 2000 Series Instrument Driver Package
  • PS2000, ps2000Wrap, and PicoIpp dynamic link library (dll) files

The PicoScope 2000 Series Instrument Driver Package can be downloaded from the MathWorks File Exchange pages:


The dll files can be found in the Software Development Kit for the PicoScope 2000 Series which is available from:


Acquiring the Data

The signal generator output was passed through via the feed-through terminator into Channel A on the Oscilloscope.

The signal was set to a 20 Hz, 4 Volts peak-peak square wave.

Using the Instrument Driver, a connection is established to the device:

Code: Select all


% Create a device object.
ps2000DeviceObj = icdevice('picotech_ps2000_generic.mdd');

% Connect device object to hardware.

The device channels settings, sampling interval and the number of samples to collect is then configured. A simple trigger set for a rising edge at 500mV is also set:

Code: Select all

% Execute device object function(s).

% Channel     : 0 (PS2000_CHANNEL_A)
% Enabled     : 1 (True)
% DC          : 1 (DC Coupling)
% Range       : 8 (PS2000_5V)
[status.setChA] = invoke(ps2000DeviceObj, 'ps2000SetChannel', 0, 1, 1, 8);    % 5V range

% Channel     : 0 (PS2000_CHANNEL_B)
% Enabled     : 0 (False)
% DC          : 1 (DC Coupling)
% Range       : 7 (PS2000_2V)
[status.setChB] = invoke(ps2000DeviceObj, 'ps2000SetChannel', 1, 0, 1, 7);    % 2V range

[samplingIntervalUs, maxBlockSamples] = invoke(ps2000DeviceObj, 'setBlockIntervalUs', 100);

% Query property value(s).
timebaseIndex = get(ps2000DeviceObj, 'timebase'); % Confirm the timebase index selected

% Configure property value(s).
set(ps2000DeviceObj, 'numberOfSamples', 2048);

% Execute device object function(s).

% Source          : 0 (PS2000_CHANNEL_A)
% Threshold       : 500 (mv)
% Direction       : 0
% Delay           : -50 (Trigger point in centre of block)
% Auto Trigger Ms : 0

[simpleTriggerStatus] = invoke(ps2000DeviceObj, 'setSimpleTrigger', 0, 500, 0, -50, 0);

The data is then collected:

Code: Select all
[bufferTimes, bufferChA, bufferChB, numDataValues, timeIndisposedMs] = invoke(ps2000DeviceObj, 'getBlockData');

Post-processing the Data

Once the data has been collected, it can be post-processed within the MATLAB environment:

Code: Select all
% Times returned in nanoseconds - convert to milliseconds
bufferTimesMs = bufferTimes / 1e6;

% Append to string
time_label = strcat('Time (ms)');

% Plot

plot(bufferTimesMs, bufferChA, 'b-');
title('Plot of Voltage vs. Time');
ylabel('Voltage (mv)');
legend('Channel A');

% Set the threshold to 0 V.
threshold = 0.0;

% Create the offset data.  Need to append a NaN to the final sample since
% both vectors need to have the same length.
offsetData = [bufferChA(2:end); NaN];

% Find the rising edge(s).
risingEdge = find(bufferChA < threshold & offsetData > threshold);

% Find the falling edge(s).
fallingEdge = find(bufferChA > threshold & offsetData < threshold);

% Show the rising edges with red x's.
hold on
plot(bufferTimesMs(risingEdge), threshold, 'MarkerSize',8,'Marker','x', ...
    'LineWidth', 2, 'LineStyle','none','Color',[1 0 0]);

% Show the falling edges with green o's.
plot(bufferTimesMs(fallingEdge), threshold, 'MarkerSize',8,'Marker','o', ...
    'LineWidth', 2, 'LineStyle','none', 'Color',[0 1 0]);

hold off

fprintf(' Num. rising edges: %d\n', length(risingEdge));
fprintf('Num. falling edges: %d\n\n', length(fallingEdge));

The output at the command line is as follows:

Num. rising edges: 3
Num. falling edges: 4

Plotting the data this shows:

MATLAB Plot of Data

Try this code and post your results in this topic :D

Technical Specialist
Pico Technology
Site Admin
Site Admin
Posts: 2009
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Counting the number of rising/falling edges of a signal

Postby Hitesh » Mon Apr 25, 2016 9:46 am

PicoScope 6 software now features Edge counting as part of the Measurements feature.

Select 'Edge Count' from the drop-down list in the 'Add Measurement' dialog:

Add Measurement dialog

The software will then display the number of edges detected in the Measurements table:

PicoScope 6 display with Edge Count in Measurements table

The PicoScope version used above is PicoScope (Beta).

Technical Specialist
Pico Technology
Site Admin
Site Admin
Posts: 2009
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Return to MATLAB

Who is online

Users browsing this forum: No registered users and 1 guest