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

http://www.mathworks.co.uk/help/daq/examples/counting-complex-events-using-analog-input-data.html

Requirements:

Hardware:

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

Software:

  • 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:

http://www.mathworks.co.uk/matlabcentral/fileexchange/40134-picoscope%C2%AE-2000-series-matlab%C2%AE-generic-instrument-driver

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

http://www.picotech.com/software.html

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
PS2000Config;


%% DEVICE CONNECTION

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

% Connect device object to hardware.
connect(ps2000DeviceObj);


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
%% CONFIGURE DEVICE

% 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

figure;
plot(bufferTimesMs, bufferChA, 'b-');
title('Plot of Voltage vs. Time');
xlabel(time_label);
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:

PS2000_PulseCount_Example_01.jpg
MATLAB Plot of Data


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

Technical Support Engineer
Hitesh
Site Admin
Site Admin
 
Posts: 2515
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_Edge_Count.PNG
Add Measurement dialog

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

PicoScope_Measurement_Edge_Count.PNG
PicoScope 6 display with Edge Count in Measurements table

The PicoScope version used above is PicoScope 6.12.1.1691 (Beta).
Hitesh

Technical Support Engineer
Hitesh
Site Admin
Site Admin
 
Posts: 2515
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

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

Postby BJC » Wed Oct 05, 2016 4:36 pm

Hi,

Is there any estimate on when there might be a non-beta version of the software with this feature?

Thanks,
Bruce
BJC
Newbie
 
Posts: 0
Joined: Tue Mar 15, 2016 4:35 pm

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

Postby Hitesh » Thu Oct 06, 2016 8:29 am

Hi Bruce,

The software does go through a formal testing process before being released as a stable version so it is likely to be a little while - you can set the PicoScope software to notify you of updates via the Tools -> Preferences dialog.

There is a new beta due soon.

Regards,
Hitesh

Technical Support Engineer
Hitesh
Site Admin
Site Admin
 
Posts: 2515
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

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

Postby aboi » Mon May 08, 2017 1:04 pm

Hi there,

I am not really sure this is the right thread to post my question but it is kind of related.

I have a ps3404MSO at my disposal and I am currently trying to record the data from a rotary encoder. The encoder delivers a square signal of around 3kHz. The problem is that I need to detect with precision the edges of my signal (50ns) but over a longer period of time (20s at least).

Is there a way to only record the occurrence time of each rise/drop time instead of the huge amount of data that is generated by the high sampling rate (witch I actually don’t need)?

I hope you can tell me if there is a way to do it by writing my own program?

Thank you in advance!

Greetings from Austria
aboi
Newbie
 
Posts: 0
Joined: Mon May 08, 2017 12:45 pm

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

Postby Hitesh » Fri May 12, 2017 1:01 pm

Hi aboi,

Have you tried using the Mask Limit Testing feature in the PicoScope 6 software?

Otherwise, if you plan to write your own application which programming language are you using?

Regards,
Hitesh

Technical Support Engineer
Hitesh
Site Admin
Site Admin
 
Posts: 2515
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

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

Postby aboi » Mon May 15, 2017 10:09 am

Hi Hitesh,

Thanks for your answer. I have tried the Mask Limit feature but this doesn’t really help me in this case. As far as I understand it, this feature is made to detect glitches in some waveforms. In my case the waveform shape is only changing in frequency. What I’m interested here is if there is a way just to get every single triggering time (rising edge) in an array.
I sadly don’t have access to matlab and I would be ready to use C code I guess (or Python). The point is just that I’m still quite a beginner in Programming and I don’t want to invest a lot of time in doing so if I’m not sure if it is even possible. From the API I could neither validate nor invalidate this assumption. I was just hoping to get this kind of answer from experience users!

Thank you for your time

Greetings
aboi
Newbie
 
Posts: 0
Joined: Mon May 08, 2017 12:45 pm

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

Postby Martyn » Tue May 23, 2017 6:29 am

You would need to be looking at the data and trying to find the mid signal crossing points, and then you need to count the number of samples to go from Up/Down/Up, ie one complete cycle. As you will already know the sample interval, you then have the the time taken for that single cycle, which gives you the frequency. Repeat this through the waveform for each cycle and you can then plot the frequency as it changes.

Quite easy to do in any programming language.
Martyn
Technical Support Manager
Martyn
Site Admin
Site Admin
 
Posts: 2816
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

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

Postby aboi » Thu Jun 01, 2017 1:37 pm

Martyn,

I was actually already using post processing to get to the edge times, I was just wondering if there was a way to do it internally in the Picoscope, but thank you very much for your response!
aboi
Newbie
 
Posts: 0
Joined: Mon May 08, 2017 12:45 pm


Return to MATLAB

Who is online

Users browsing this forum: No registered users and 0 guests