Alternating between if conditions in a loop.
    5 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
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 Commenti
  Star Strider
      
      
 il 19 Giu 2023
				
      Spostato: Star Strider
      
      
 il 19 Giu 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.  
Risposta accettata
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



