Hi dear community,
I have a state array doesn't matter what value inside it but for example I only want to find array == 6 as a logical array. (State 6 means something to me.). So my array becomes array = [1 1 1 0 0 0 1 1 … etc].
I detected its rising and falling edges so that I want to find each event started in a point and finished in another point. My data resampled so I am working in sample base.
I want to find the intervals of these event all the array long because I will evaluate another array's values along these events.
For example if I find event rising edges = [7000 9000] and falling edges = [7300 9100] that means I have 2 events and first event between 7000:7300 and second is 9000:9100. So my another array's values between 7000:7300.
Here is example code:
array = [0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0];another_array = 1:18; % just for example it will have equal size with state array
sample_time = 0.1; loc = (array >= 0.5);d_loc = [false ; diff(loc)];rising_edges_index = find(d_loc == 1);loc = (array <= 0.5);d_loc = [false ; diff(loc)];falling_edges_index = find(d_loc == 1);another_array_mean_vals = [];for i = 1:numel(rising_edges_index) another_array_mean_vals = [another_array_mean_vals, another_array(rising_edges_index(i):falling_edges_index(i))]; % for example each interval I want to get mean values and save it
event_duration_in_seconds = (falling_edges_index(i) - rising_edges_index(i))*sample_time; end
The problem is sometimes rising_edges_index and falling_edges_index sizes are not equal! Because it starts with falling edge but there were no rising edge for corresponding event. Other way around sometimes there is a rising edge but no corresponding falling edge at the end of the array. Or both sometimes starts with falling edge and ends with rising edge.
How to handle this situation? Would you remove those inappropriate events or add zero to the beginning and pretend like the event started at the beginning of the state signal?
Just a sample visual for the situation. You can think it as a square wave which has no constant period.
Best Answer