如何在此MATLAB findpeaks中包括注释扩展? [英] How to include Annotate extends ... in this MATLAB findpeaks?
问题描述
我现在在输出中具有最小值和最大值(图1),但是我想获得标签(图2)以获得已排序的最大值(最高得到1,...),并且类似地获得最小值(最低得到1) . 我可以通过以下方式完成图1的输出,但无法将这些注释集成到函数中
I have minimums and maximums in the output now (Fig.1) but I would like to get labels (Fig. 2) for sorted maximums (tallest get 1, ...) and similarly for minimums (lowest gets 1). I can do the output of Fig. 1 by the following but I cannot integrate those annotations to the function
close all; clear all; clc;
% https://se.mathworks.com/help/signal/ref/findpeaks.html
% http://stackoverflow.com/a/26837689/54964
x = linspace(0,1,1000);
Pos = [1 2 3 5 7 8]/10;
Hgt = [4 4 2 2 2 3];
Wdt = [3 8 4 3 4 6]/100;
for n = 1:length(Pos)
Gauss(n,:) = Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2);
end
PeakSig = sum(Gauss) - exp(sum(Gauss))/10;
plot(x, PeakSig);
hold on;
[p l]=findpeaks(PeakSig); %,x); %,'Annotate','extents','WidthReference','halfheight')
plot(x(l), p, 'ko', 'MarkerFaceColor', 'g');
[pn ln]=findpeaks(-PeakSig); %,x); %,'Annotate','extents','WidthReference','halfheight')
plot(x(ln), -pn, 'ko', 'MarkerFaceColor', 'r');
title('Signal Peak Widths')
仅将'Annotate','extents','WidthReference','halfheight')
附加到[p l]=findpeaks(...)
在以下应用程序中不起作用,这显然是因为前进行plot(x(l), p, 'ko', 'MarkerFaceColor', 'g');
不能理解单行代码在相应变量中产生的多余内容
To just append 'Annotate','extents','WidthReference','halfheight')
to [p l]=findpeaks(...)
is not working in the application etc the following apparently because the proceeding line plot(x(l), p, 'ko', 'MarkerFaceColor', 'g');
does not understand the extra content made by the one-liner in the corresponding variables
[p l]=findpeaks(PeakSig,'Annotate','extents','WidthReference','halfheight')
[p l]=findpeaks(PeakSig, x, 'Annotate','extents','WidthReference','halfheight')
图. 1当前输出没有这些注释, 图2预期的输出,但带有最大值和最小值的注释
Fig. 1 Current output without those annotations, Fig. 2 Expected output but with notes of maximums and minimums
MATLAB:2016b
操作系统:Debian 8.5 64位
硬件:华硕Zenbook UX303UA
MATLAB: 2016b
OS: Debian 8.5 64 bit
Hardware: Asus Zenbook UX303UA
推荐答案
这是解决此问题的一种方法:
Here is one way to to this:
x = linspace(0,1,1000);
Pos = [1 2 3 5 7 8]/10;
Hgt = [4 4 2 2 2 3];
Wdt = [3 8 4 3 4 6]/100;
Gauss = zeros(numel(Pos),numel(x));
for n = 1:numel(Pos)
Gauss(n,:) = Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2);
end
PeakSig = sum(Gauss) - exp(sum(Gauss))/10;
% get the peaks:
[p,xmax] = findpeaks(PeakSig,x);
maxsrt = sortrows([xmax;p].',-2);
[pn,xmin] = findpeaks(-PeakSig,x);
minsrt = sortrows([xmin;-pn].',2);
% plotting:
blue = [0 0.447 0.741];
plot(x,PeakSig,xmax,p+0.2,'v','MarkerFaceColor',blue,'MarkerEdgeColor',blue);
% you can comment the line above and uncomment the line below, if you
% prefer:
% findpeaks(PeakSig,x,'Annotate','peaks');
text(maxsrt(:,1),maxsrt(:,2)+0.2,num2str((1:numel(p)).'),'FontSize',14,...
'VerticalAlignment','bottom','HorizontalAlignment','center')
ylim([-10 3]);
grid on
hold on
plot(xmin,-pn-0.2,'^','MarkerFaceColor',blue,'MarkerEdgeColor',blue);
hold off
text(minsrt(:,1),minsrt(:,2)-0.2,num2str((1:numel(pn)).'),'FontSize',14,...
'VerticalAlignment','top','HorizontalAlignment','center')
title('Signal Peak Widths')
这篇关于如何在此MATLAB findpeaks中包括注释扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!