Alternating between if conditions in a loop.

5 views (last 30 days)
I am trying to write code to isolate repeating patterns in a dataset. The example I will give is an oscillatory dataset to isolate peaks and troughs, and save the values within vectors.
My raw data looks like this:
I can fit a poor model to this dataset to get:
from the reproducable model:
model = @(p, x) p(1)*2*pi*sin(2*pi*p(2)*x +p(3));
params = [3.6e+03 46.5 1000];
x = linspace(1.5, 2.5, 100000);
y = model(params, x);
As can be seen from the first figure, the peaks and troughs occur at different amplitudes, and the green model does not refect this.
So what I want to do is to loop through the y-data presented in the first figure to find when the value at each peak and trough. To do this I was thinking about using a for - loop to run through the entire dataset, and use conditions to locate the values of the peaks:
itr = length(y);
for i = 2:itr
a = y(i) - y(i-1);
if a < 0
%Save a%
Max(1) == y(i)
end
end
This will save the maximum value of y from the first oscillation period. From this point onwards, a = y(i) - y(i-1); will be negative until y(i) reaches the minima. So after saving Max(1), I would like to switch the condition to if a > 0, to find when a becomes positive and then save y(i) as a new object, Min(1), and to then revert back to the original condition of if a < 0. I would like to repeat this process until i = itr, appending maximum and minimum y values to the objects Max and Min, respectively.
I'd like to achieve this without using inbuilt functions if possible
  2 Comments
Star Strider
Star Strider on 19 Jun 2023
Moved: Star Strider on 19 Jun 2023
I am not dertain what you want to do. I would use either findpeaks (with normal and negated arguments to get the peaks and troughs respectively) or islocalmax and islocalmin to do the same. These funcitons already exist and are optimised, so much more efficient than repeated explicit loops.
James
James on 19 Jun 2023
Moved: Star Strider on 19 Jun 2023
Hi Star Strider. I would rather avoid inbuilt functions, as the methods that I described could be useful for other processes. I'm trying to ascertain how to to gain greater control over loops and condition statements.

Sign in to comment.

Accepted Answer

James
James on 19 Jun 2023
Okay, so there's a really simple solution that requires a condition to be pre-set:
itr = length(y_1);
Max = []; % Initialize empty array to store maximum values
Min = []; % Initialize empty array to store minimum values
maxCondition = true; % Flag to indicate the current condition
MInd1 = []; % Maximum indeces
MInd2 = []; % Minimum Indeces
for i = 2:itr
a = y_1(i) - y_1(i-1);
if maxCondition && a < 0
Max(end+1) = y_1(i-1); % Append maximum value to Max array
maxCondition = false; % Switch condition to look for minimum
MInd1(end+1) = i-1; % Append indeces array for the maximum values
elseif ~maxCondition && a > 0
Min(end+1) = y_1(i-1); % Append minimum value to Min array
maxCondition = true; % Switch condition to look for maximum
MInd2(end+1) = i-1; % Append indeces array for the minumum values
end
end
Here we set two conditions that correspond to whether searching for a maximum or a minimum value I know from the data I am searching for a maximum value so I pre-set it to 'true'.
The conditions are altered if the if statement is triggered, so when a maximum value is found, the maxCondition is set to false.
Here are the charts to show the maxima and minima have been found:

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!