【问题标题】:How to include Annotate extends ... in this MATLAB findpeaks?如何在这个 MATLAB findpeaks 中包含 Annotate extends ...?
【发布时间】:2016-10-16 21:19:41
【问题描述】:

我现在在输出中有最小值和最大值(图 1),但我想为排序的最大值(最高得到 1,...)和最小值(最低得到 1)获取标签(图 2) . 我可以通过以下方式完成图 1 的输出,但我无法将这些注释集成到函数中

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'); 不理解相应变量中单行器所产生的额外内容

[p l]=findpeaks(PeakSig,'Annotate','extents','WidthReference','halfheight')
[p l]=findpeaks(PeakSig, x, 'Annotate','extents','WidthReference','halfheight')

图。 1 没有这些注释的当前输出, 图 2 预期输出,但带有最大值和最小值的注释

MATLAB:2016b
操作系统:Debian 8.5 64 位
硬件:华硕 Zenbook UX303UA

【问题讨论】:

    标签: matlab annotations matlab-figure


    【解决方案1】:

    这是解决此问题的一种方法:

    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')
    

    【讨论】:

      【解决方案2】:

      这是另一种方法,使用gscatter。而不是调用plot 两次(如果需要其他注释可能更多),您将所有位置(x-y)和注释类型连接到一个数组(pks 波纹管),并按他们的类型一次绘制它们(即组)与gscatter

      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:
      pks = [[xmin xmax];[-pn-0.2 p+0.2];[zeros(1,numel(pn)) ones(1,numel(p))]].';
      blue = [0 0.447 0.741];
      ax = axes;
      plot(ax,x,PeakSig)
      hold on
      gscatter(pks(:,1),pks(:,2),pks(:,3),blue,'^v')
      hold off
      ax.Children(1).MarkerFaceColor = blue;
      ax.Children(2).MarkerFaceColor = blue;
      text(maxsrt(:,1),maxsrt(:,2)+0.2,num2str((1:numel(p)).'),'FontSize',14,...
          'VerticalAlignment','bottom','HorizontalAlignment','center')
      text(minsrt(:,1),minsrt(:,2)-0.2,num2str((1:numel(pn)).'),'FontSize',14,...
          'VerticalAlignment','top','HorizontalAlignment','center')
      ylim([-10 3]);
      legend off
      grid on
      title('Signal Peak Widths')
      

      结果正是same,但它更短,也许更优雅。

      【讨论】:

      • 不,它使用findpeaks,它只是替换了第一个答案中的% plotting: 部分。只是注释峰的方式不同。
      • 您能否在答案的开头简短地口头补充一下这里的注释有何不同。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 2019-04-17
      • 2017-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多