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.

回答(1 个)

Piyush Kumar
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)]);
Number of cycles: 2
Example of a cycle in the code -
  1. Initial State: Below the positive threshold.
  2. First Positive Threshold Crossing: The signal goes from 0 to 1 to 2 (crosses the positive threshold).
  3. Negative Threshold Crossing: The signal goes from 2 to 1 to -1 to -2 (crosses the negative threshold).
  4. Second Positive Threshold Crossing: The signal goes from -2 to -1 to 1 to 2 (crosses the positive threshold again).
This sequence completes one cycle. Hope it helps!

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by