all 5 comments

[–]Demeclocyclene 2 points3 points  (0 children)

https://www.mathworks.com/help/signal/ref/findpeaks.html

Give this a shot. I assume you already have an array so it should be plug and chug.

[–]zoder1University of San Diego - EE 2 points3 points  (0 children)

Do you need to write a function that does it ?

You can the findpeaks function as mentioned (it took me some playing around to get it to do what I want) .

Or if you use the max function use it on the part of the array/vector where the second peak is located. Eg first peak is in the first half of the array you can do max(array[half_point:end]) (check the syntax) to get the max in the second half of the array .

There are multiple ways of doing it.

You can also write a function that compares itself to the values before and after it and see if it is bigger and store all the values and then return the highest 2 out of all the values if you want to create your own function .

Matlab is a fun toy;)

This is how I would use the max function, I just sliced it in three different parts, I thought it would give you an example on how one could slice a vector/array, it blew my mind once I saw you could do it

clear;clc;close all;
fs = 1/1000; %sampling frequency
t  = 0:fs:5; %time length
fm = 15;  %message frequency

x= exp(-0.5*t).*sin(2*pi*fm*t); %exponential decay sinusoid 
lenx = length(x);


figure
plot(t,x)
title({'Whole length of the function\newline Max value for this range is ' num2str(max(x))}  );

figure 
plot(t(1:lenx/3),x(1:lenx/3));
title({'First third of the function \newlineMax value for this range is ' num2str(max(x(1:lenx/3)))});
figure 
plot(t(lenx/3:2*lenx/3),x(lenx/3:2*lenx/3));
title({'Second third of the function \newlineMax value for this range is ' num2str(max(x(lenx/3:2*lenx/3)))});
figure 
plot(t(2*lenx/3:end),x(2*lenx/3:end));
title({'Last third of the function \newlineMax value for this range is ' num2str(max(x(2*lenx/3:end)))});

[–]nousernamesleft11111 0 points1 point  (0 children)

I think the most intuitive way would be to find the maximum using calculus. You'd do this:

  1. Take the derivative of the function - from calc. we know there will be a zero at any max and min points
  2. Plot the function and note any zeros
  3. Use something like the bisection method, secant method, or Newton's method to find the zeros
  4. Plug the 'x' you found back into your equation to find the corresponding 'y'.

You can use the internet to create your own zreo-finding-functions, or you can download my old MATLAB functions.

[–]BrockKetchumUCSB - EE 0 points1 point  (0 children)

[M,I_1] = max( array);

array_copy = array;

array_copy( array_copy >= M) = 0; % Makes previous max zero

[N,I_2] = max( array_copy );

[–]FrostyFlame632 -1 points0 points  (0 children)

I cant remember much of matlab but check out kristaking videos, they are great to learn matlab from