Hi, I need help in writing a code formula

I'm writing a series of code which count total of activations, how long activated and time to next activation. I have so far managed the Start points and end points but i'm not sure how to go about get time between the points and time to start of next activation in time increments.
ATsyt2= T.ATSys.signals.values==2; % Channel
FATsyt2=find(ATsyt2==1); % Determine when condition is met
starATsyt2=FATsyt2([0; diff(FATsyt2)]>1); %Start of Activation
endFATsyt2=(starATsyt2-1);
endFATsyt2=endFATsyt2(2:end); % End of Activation

6 commentaires

Walter Roberson
Walter Roberson le 24 Mar 2019
Is this a timeseries with time stamps for each object? Is it a timeseries with start time and fixed interval? Is it just a regular struct with assumed fixed interval?
Franc
Franc le 24 Mar 2019
Hi, its a timeseries with fix intervals of 0.1 sec increments. In effect its a square ware of amplitude 1 with varying start and endpoints. I'm trying to code so i pass various timeseries through.
Then the associated times should be (starATsyt2(k):endFATsyt2(k)) * 0.1 or possibly 0.1 less than that (depending on whether the time of the first sample is 0.1 or 0.0).
arrayfun(@(start,stop) (start-1:stop-1)*0.1, starATsyt2, endFATsyt2, 'uniform', 0)
Franc
Franc le 24 Mar 2019
thanks, but not clear how/what the code returns, i tries it as is but it returned an error
"Error using arrayfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 339 in dimension 1. Input #3 has size 1"
I'm reading up on the function but not sure how it being used here
Walter Roberson
Walter Roberson le 24 Mar 2019
Is an activation a sequence of consecutive entries in which the signal value is exactly 2 ?
Franc
Franc le 24 Mar 2019
Yes that is correct

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 24 Mar 2019
dT = 0.1;
mask = (T.ATSys.signals.values(:)==2).'; %row vector
starts = strfind([false mask], [false true]);
stops = strfind([mask false], [true false]);
activation_durations = dT * (stops - starts + 1);
%an activation of 1 sample would have equal stop and start so difference would be 0, but that should be considered 1 timestep
activation_times = arrayfun(@(start,stop) (start:stop)*dT, starts, stops, 'uniform', 0); %this assumes first time is dT not 0
Now, activation_durations is a vector of durations (minimum dT for an activation of one point duration), and activation_times is a cell array of the timestamps of activations, if you need that for some reason (for example you might need it if you were doing curve fitting.)

4 commentaires

Franc
Franc le 25 Mar 2019
Thanks
Hi, could i get further clarification on
activation_times = arrayfun(@(ATsyt2starts,ATsyt2stops) (ATsyt2starts:ATsyt2stops)*dt, ATsyt2starts, ATsyt2stops, 'uniform', 0);
The out put from this is a matrix of cell arrays , is it possible to output all activation time as one cell array instead of below?
activation_times =
1×492 cell array
Columns 1 through 10
{1×402 double} {1×402 double} {1×288 double} {1×292 double} {1×284 double} {1×316 double} {1×197 double} {1×390 double} {1×87 double} {1×308 double}
Thanks
find(mask) * dT
or possibly
(find(mask)-1) * dT %if the first time is to be 0
Or if you have already calculated that cell array that I show, then
[activation_times{:}]
It depends what you need to do with them. It is not uncommon to need to know the times broken out by section rather than all in one array.
Like if you had another array, such as T.ATSys.signals.intensity with one entry for each corresponding .values entry, and you wanted to fit a quadratic to each distinct block, then
intens = T.ATSys.signals.intensity;
activation_intensities = arrayfun(@(ATsyt2starts,ATsyt2stops) intens(ATsyt2starts:ATsy2stops), ATsyt2starts, ATsyt2stops, 'uniform', 0);
cellfun(@(T,Intens) polyfit(T, Intens, 2), activation_times, activation_intensities, 'uniform', 0)
and that would be a per-block fit. Where-as if you had all of the entries together, like
intens = T.ATSys.signals.intensity;
activation_times = find(mask)*dT;
activation_intensities = intens(mask);
polyfit(activation_times, activation_intensities, 2)
would give you a single quadratic fit for the whole thing -- which could be valid if it all represented a single process, whereas the block-by-block fit would be appropriate if each block represented a (semi-) independent process.
I note, by the way, that you changed the variable names in the arrayfun. I had
arrayfun(@(start,stop) (start:stop)*dT, starts, stops, 'uniform', 0)
Notice that the names used as the parameters in the anonymous function are not the same as the names of the variables being processed. Each time the anonymous function fires, it is working on one particular start + stop pair, singular, while starts and stops are plural, collections (vectors) of the data. The plural or not is not the important part, but it is important that the variable names for the anonymous function, used to hold one particular value, are not the same as the variables being acted on, the ones being used to hold the collections of values. When you adjusted the names to what you did, you used the same variable names for the particular as you did for the collections.
It is not that MATLAB will malfunction or produce the wrong answer for what you did.. the problem is that humans will produce the wrong answer when asked to examine what you wrote. When humans read your code, they are likely to get confused about whether the variables inside the anonymous function refer to particular values or the collections.
To avoid this kind of confusion, I use informal (and not rigorous) naming conventions in my code, such as lower case variables for scalars or vectors, and upper case for arrays, such as
[X, Y] = ndgrid(x, y)
and I tend to use names like
arrayfun(@(this_x, this_y) this_x.^2 + this_y, X, Y)
Franc
Franc le 27 Mar 2019
Thank-you for taking the time to explain, now i unnder how the function works. The key usage for me in this code is to ensure the scheduling of the activation falls within the threshold of how long each and not more than expected within a time frame. I'll take your observations of application and correct accordingly.
Thank-you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by