# 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.

1 visualizzazione (ultimi 30 giorni)
Risposto: Jaynik il 1 Apr 2024
Freq = data.Value;
Time = datetime(data.Time);
logind = [false; Freq<=50 & Freq >=49.90 ; 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);
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Alexander il 27 Mar 2024
What is your question? It would be helpful to supply the csv also.

Accedi per commentare.

### Risposta accettata

Jaynik il 1 Apr 2024
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:
Freq = data.Value;
Time = datetime(data.Time, 'InputFormat', 'yyyy-MM-dd HH:mm:ss');
logind = [false; Freq<=50 & Freq>=49.90; false];
% If the data ends within a cycle
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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Spectral Measurements in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by