Timeseries signal reversal cycle counter

조회 수: 2 (최근 30일)
Vince Badge
Vince Badge 2020년 7월 21일
답변: Piyush Kumar 2024년 8월 29일
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