I have grid frequency data, first column is time(Time) and second is frequency (Value). I want to calculate the duration of each cycle between 50 to 49.90 Hz.

2 次查看(过去 30 天)
data = readtable("2023-01-01.csv");
Freq = data.Value;
Time = datetime(data.Time);
logind = [false; Freq<=50 & Freq >=49.90 ; false];
cyclestart = strfind(logind', [false true]);
cycleend = strfind(logind', [true false]);
cycleDurations = zeros(size(cyclestart));
what happen when the last value is 50.1 or something?
%% i = 1:length(cyclestart)
startTime = Time(cyclestart(i));
endTime = Time(cycleend(i)-1);
cycleDurations(i) = seconds(endTime - startTime);

采纳的回答

Jaynik
Jaynik 2024-4-1
Hi Muhammad,
Your approach to calculate the duration of each cycle is correct. But you need to end the loop and handle edge cases. One such case is to handle situations where the data ends within a cycle (e.g., the last value is 50.1 Hz or anything not marking the end of a cycle), we need to ensure that "cycleend" is properly determined even if the data stream ends within a cycle.
I have created some sample data based on the columns you mentioned. Here is the code which calculates the duration of each cycle:
% data = readtable("2023-01-01.csv");
Freq = data.Value;
Time = datetime(data.Time, 'InputFormat', 'yyyy-MM-dd HH:mm:ss');
logind = [false; Freq<=50 & Freq>=49.90; false];
cyclestart = strfind(logind', [false true]);
cycleend = strfind(logind', [true false]);
% If the data ends within a cycle
if logind(end-1) == true
cycleend = [cycleend, length(Freq)+1]; % Append a cycle end at the last position
end
cycleDurations = zeros(size(cyclestart));
for i = 1:length(cyclestart)
startTime = Time(cyclestart(i));
endTime = Time(cycleend(i)-1);
cycleDurations(i) = seconds(endTime - startTime);
end
disp(cycleDurations);
The way in which "endTime" is calculated, it lasts one second less. To address this, you can use the following inside the loop:
endTime = Time(cycleend(i));
cycleDurations(i) = seconds(endTime - startTime);
Please share more details so that I can assist you better. Hope this helps!

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by