Count rising edge in wave

53 views (last 30 days)
kyin gab
kyin gab on 4 May 2015
Commented: Star Strider on 16 Mar 2016
Hello,
I have the wave data in csv file which contains the time and the corresponding value.
I can plot the wave in matlab to see that it is ok.
However, I would like to count the rising edges of the wave.
I tried using simulink these are the steps I took
1. After CSVread, I saved the data in a .mat file.
2. I tried to read the signal using the 'From File' in simulink.
3. The output of the 'From File' was sent to Scope. (This I did just to make sure I am using the 'From File' correctly)----------It gave me error.
4. I also sent the output of 'From File' to Detect Rise positive----------It gave me error.
Please advice me on what to do to.
NB, I wrote a simple code that counts, but I somehow do not trust myself.
If there are other was of doing it, I would like to know.
Thanks
  2 Comments
Star Strider
Star Strider on 4 May 2015
Please attach your .mat file. We can’t help unless we know what your data are.
kyin gab
kyin gab on 4 May 2015
I have attached the .mat file

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 4 May 2015
If I understand correctly what you want to do, this seems to work (requires the Signal Processing Toolbox for findpeaks):
D = load('sigX');
x = D.req_read(:,1);
y = D.req_read(:,2);
dy = gradient(y, mean(diff(x))); % Take Derivative
[dypks,ix] = findpeaks(dy, 'MinPeakDistance',20, 'MinPeakHeight',1E+7);
figure(1)
plot(x, y)
hold on
plot(x, dy*1E-9)
plot(x(ix), dypks*1E-9, '^g', 'MarkerFaceColor','g')
hold off
grid
axis([0 1E-7 ylim])
It is fairly straightforward. It uses the gradient function to take the derivative of the signal, then uses the Signal Processing Toolbox findpeaks function to find the peaks in the derivative, corresponding to the rising edges of the signal. I included in the findpeaks call threshold values for the magnitude of the peaks to include (in 'MinPeakHeight') and the distance between then (in 'MinPeakDistance').
The findpeaks function returns the value (height) of the peaks in ‘dypks’ and their index locations in ix that you can then use to locate the peaks with respect to ‘x’. The plot calls illustrate this usage. The scaling factor of 1E-9 scales the derivative plot to the original signal so that I could overplot the derivative and the signal to verify that the derivatives did what I wanted them to. Also, the axis call shows a part of the signal in detail. Comment it out to see the entire signal.
  2 Comments
kyin gab
kyin gab on 4 May 2015
Thank you Star.
I am trying to count the number of rising edges of the wave OR the number of cycles in the wave.
Star Strider
Star Strider on 4 May 2015
My pleasure.
The number of rising edges (in my code) is:
RisingEdges = length(ix);
I get 904 from the file you provided.

Sign in to comment.

More Answers (1)

Jonathan
Jonathan on 16 Mar 2016
Star's solution worked a treat for me. Previously I was using a loop to check each bit individually to see if it was less than the subsequent bit, in order to find the index locations corresponding to my clock rising edge times.
The output is almost exactly the same for the two methods, but Star's solution takes a few seconds, as opposed to a few minutes, for my large (10 million sample) data set! Perfect, thanks :)
  1 Comment
Star Strider
Star Strider on 16 Mar 2016
As always, my pleasure!
I appreciate your compliment!

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!