Flipping y-axis of findpeaks-plot

5 ビュー (過去 30 日間)
Nils Norlander
Nils Norlander 2017 年 3 月 9 日
コメント済み: Greg Dionne 2017 年 3 月 15 日
Hi!
To find minima-values of an array I multiplied my array with -1 and used findpeaks. To graphically show the minimas I want to reverse the findpeaks-plot but can't figure out how. Any suggestions?
E_bat3 = E_bat2.*(-1);
findpeaks(E_bat3(winter_hours)) %Graph I want to flip.
Thanks!

採用された回答

Star Strider
Star Strider 2017 年 3 月 9 日
You may be making this too difficult.
Example
a = linspace(0, 4*pi);
y = sin(a);
[pks,pkloc] = findpeaks( y,a); % Maxima
[vls,vlloc] = findpeaks(-y,a); % Minima
figure(1)
plot(a, y, '-b')
hold on
plot(pkloc, pks, '^r', 'MarkerFaceColor','r')
plot(vlloc, -vls, 'vg', 'MarkerFaceColor','g')
hold off
grid
axis([xlim -1.1 1.1])
legend('Data', 'Peaks', 'Valleys')
Note that to plot the valleys (‘vls’) or minima correctly, simply negate them in the plot call. So to find them, use:
[vls,vlloc] = findpeaks(-y,a); % Minima
and to plot them or use them in other calculations correctly, negate them again to restore their correct values:
plot(vlloc, -vls, 'vg', 'MarkerFaceColor','g')

その他の回答 (2 件)

Adam
Adam 2017 年 3 月 9 日
編集済み: Adam 2017 年 3 月 9 日
Don't you just want to negate the peak amplitude results rather than flip?
  2 件のコメント
Nils Norlander
Nils Norlander 2017 年 3 月 9 日
編集済み: Nils Norlander 2017 年 3 月 9 日
Yes, the peaks are now found as maximas at negative values - and I want to show them as minimas at positive values.
Adam
Adam 2017 年 3 月 9 日
But if they are minima then they will be larger as an absolute value than those around them so how can you display them still as minima if you plot the result as positive only?
I guess if you really want to do that then you need to subtract from whatever the largest absolute value in the results is.

サインインしてコメントする。


Greg Dionne
Greg Dionne 2017 年 3 月 9 日
編集済み: Greg Dionne 2017 年 3 月 9 日
I think Star Strider's answer is the right approach, but if you prefer the graphical 'look and feel' of the FINDPEAKS plot, then try this:
load mtlb
findpeaks(mtlb,Fs,'MinPeakProminence',1);
figure
findpeaks(-mtlb,Fs,'MinPeakProminence',1);
set(gca,'YDir','reverse');
set(findobj(gca,'Tag','Peak'),'Marker','^');
set(gca,'YTickLabel',cellfun(@(x)num2str(-str2num(x)),get(gca,'YTickLabel'),'UniformOutput',false))
  2 件のコメント
Nils Norlander
Nils Norlander 2017 年 3 月 9 日
Thank you so much! Think I've almost got it figured out but I get the error: "Expected Y to be a vector" in Findpeaks.
Should I just replace "mtlb" with my Y-values and Fs with my X-values?
Once again, thank you for taking your time to help me!
Greg Dionne
Greg Dionne 2017 年 3 月 15 日
Yes, that should work. FINDPEAKS expects vector input for Y. So in your case you could do findpeaks(-y, a) just like you did before, but then do the graphics commands above. I think they'll work no matter what input you have (so long as you actually have peaks). Otherwise, finding the 'peak' tag will fail because no peaks will actually be plotted.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by