MATLAB Answers

Calculating time duration of a signal

49 views (last 30 days)
Tom richards
Tom richards on 14 Jul 2019
Edited: Tom richards on 18 Jul 2019
I would like to calculate the time duration of a signal. In the example below, I want to write in a 2 column matrix the signal and the duration for the purposes for plotting a histogram.
signal=[0 0 0 1 1 1 1 0 1 1];
Output I'd like to see:
signal duration(ns)
0 3
1 4
0 1
1 2
This is my code so far, I'm stuck at trying to calculate the actual duration. I dont know how to calculate time in between when 0 transitions to 1 and vice versa.
duration=[];
signal=[0 0 0 1 1 1 1 0 1 1];
for c=1:length(signal)
diff=signal(c+1)-signal(c);
if diff~=0
????
end
end

  1 Comment

dpb
dpb on 14 Jul 2019
Look for FileExchange (FEX) submittal "runs"...

Sign in to comment.

Accepted Answer

infinity
infinity on 14 Jul 2019
Hello,
You also could refer this code to get the output
clear
signal=[0 0 0 1 1 1 1 0 1 1];
n = length(signal);
a = diff(signal);
b = find(a);
output = [signal(b) signal(end); diff([0 b n])]'

  1 Comment

Tom richards
Tom richards on 14 Jul 2019
Hi,
Your code definitely worked on a large data file as well.
Thanks very much!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2019a