- Initial State: Below the positive threshold.
- First Positive Threshold Crossing: The signal goes from 0 to 1 to 2 (crosses the positive threshold).
- Negative Threshold Crossing: The signal goes from 2 to 1 to -1 to -2 (crosses the negative threshold).
- Second Positive Threshold Crossing: The signal goes from -2 to -1 to 1 to 2 (crosses the positive threshold again).
Timeseries signal reversal cycle counter
3 次查看(过去 30 天)
显示 更早的评论
I am looking for help around creating a code that will count the number of times a signal passes a positive and negative threshold which will count as a cycle. For example, the cycle is defined as when the timeseries y value passes the positive threshold, then the negative threshold and then the positive threshold again in the time domain. I am having throuble trying to figure out how to code a trigger/counter that will ignore the in between data that passes the threshold but doesn't follow the cycle definition since if the positive threshold is passed and additional data points are past it those would not count until the negative threshold is passed.
0 个评论
回答(1 个)
Piyush Kumar
2024-8-29
"....the cycle is defined as when the timeseries y value passes the positive threshold, then the negative threshold and then the positive threshold again in the time domain....." - To achieve this, you can try the following code -
% Example signal
y = [0, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1, -2, -1, 1, 2];
positive_threshold = 1;
negative_threshold = -1;
state = 'below_positive';
cycle_count = 0;
for i = 1:length(y)
switch state
case 'below_positive'
if y(i) > positive_threshold
state = 'above_positive';
end
case 'above_positive'
if y(i) < negative_threshold
state = 'below_negative';
end
case 'below_negative'
if y(i) > positive_threshold
state = 'above_positive';
cycle_count = cycle_count + 1;
end
end
end
disp(['Number of cycles: ', num2str(cycle_count)]);
Example of a cycle in the code -
This sequence completes one cycle. Hope it helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!