Find the local maxima and minima of a data set by using the functions diff, sign, find

4 views (last 30 days)
I have a vector containing the time of analysis and three other vectors containing the cellule concentrations. This system shows an oscillatory behavior. I need to plot the concentrations in function of the time -> plot(t,C) and highlighting on the graph the local maxima and minima with a circle -> plot(t,Min,'o'). I am asked to use the functions diff, sign, find (logical index). I am able to solve this with findpeaks or a for loops. I know it is possible to determine the maxima and minima with sign(diff(C)) but then i don't know how to do.
Any suggestion?
Thank you very much, Michael Daldini

Answers (1)

Star Strider
Star Strider on 2 Oct 2015
This should get you started:
t = linspace(0, 10, 1000); % Time
C = 2 + sin(2*pi*t + cos(5*pi+t)*pi); % Signal
dC = diff([0, C])/(t(2)-t(1)); % Approximate Derivative
dCzx = dC .* circshift(dC, [0 -1]); % Negative Values => Zero Crossings (Mostly)
zx_idx = find(dCzx <= 0); % Derivative Zeros Are Maxima, Minima Of ‘C’
figure(1)
plot(t, C)
hold on
plot(t(zx_idx), C(zx_idx), 'or')
hold off
grid
axis([xlim 0 5])
Note that the dCzx calculation results in a zero-crossing at the end that really is not one. I will let you decide how best to deal with that if it occurs with your data, since that’s part of the fun or real-world data analysis.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!