Timer Implementation In Simulink

Hi, I need to implement a timer in simulink. The timer will have a square wave signal at its input with varying pulse width. What I want is, it should save the data in workspace in a 2-D Array format, with first column showing number of pulses and second column containing the respective time duration of the pulse. How can I implement it?

 Accepted Answer

It might be easier if done in MATLAB. You want the result in MATLAB base workspace anyway. a is your input pulse, Out is the result, here time is assumed to be 1,2,3,...
a=[0 0 1 1 0 1 1 1 1 1 0 0 1 1 1 0];
df=diff([0 a 0]);
b=find(df==1);
c=find(df==-1);
e=c-b;
Out=[b;e]';

7 Comments

Hi Fangjun,
Thanks for the help. It helped in understanding the method. I'll try to implement a model with this logic.
Can you suggest a block similar to 'find' in simulink. The output of compare to constant is '1' when it detects the required constant. But I need the instance at which a '1' or a '-1' is found...
Simulink has a triggered subsystem block. The trigger can be rising edge or falling edge. So you can use the pulse as the trigger signal, put a counter inside the subsystem and it will count how many pulses. The pulse duration is a little tricky. You'll probably need an integrator block. The rising edge starts the integration. the falling edge re-sets the integration. That will give you a saw-tooth type of output indicating the duration of the pulse. It contains enough information about the pulse but not exactly the format you wanted. You'll still need to do post-processing.
That is the difference between MATLAB and Simulink. In MATLAB, you have all the pulse information at once, all you do is kind of post-processing. You can get the pulse count and pulse duration at once. In Simulink, the algorithm runs as the time goes, at a particular point of time, you don't know whether the pulse is going to last for 1 second or 10 second because you don't see the falling edge yet.
So think about your use case and pick the right approach.
Thanks again Fangjun,
I tried the part of what you suggested previously. The suggestion to use integrator is very useful. I was not sure about if you can get the entire job done in alone Simulink, thanks to your answer that confirms I'll have to take help of MATLAB scripting, as I'm doing rt nw. Thanks again!!!
There is another way to do it, using the Clock block and two triggered subsystem. Inside the subsystem triggered by the rising edge, write the clock time to a DataStoreWrite block. Inside the subsystem triggered by the falling edge, subtract the clock time with the value from a DataStoreRead block with the same tag. The output will be the pulse duration, but not in a discrete format, as the interim values are all filled with the previous duration value.
yeah, dats true. I think to get the required results, i'll have to take help of MATLAB script. Because, with simulink, I'm getting the signals at its output, but not in proper format - two dimensional array, with 1st column as number of pulses and second column as the duration of each pulse.
Thanks for all the help and suggestions... :)
Then the code in my answer provided the solution, right? Right now, the first column is the time stamp of the rising edge. If you want the count of the pulse, change the last line to be: Out=[1:length(b);e]'
Yes, the code serves the purpose. I even multiplied the time period obtained by the method of '1's and '-1's by the sampling time, to get the actual time period. As my model runs on different sampling rate, i had to multiply the acquired time by the sampling time to get the actual answer...:) I'll also try ur suggestion in last comment...
Thanks again...

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!