Home > atmlab > graphs > plot_extrema_shaded.m

plot_extrema_shaded

PURPOSE ^

Plot shaded area between local minima and maxima

SYNOPSIS ^

function fh = plot_extrema_shaded(x, y, no_min, no_max, varargin)

DESCRIPTION ^

 Plot shaded area between local minima and maxima

 For y(x), plot shaded area between lines connecting extrema.

 FORMAT

   plot_extrema_shaded(x, y, no_min, no_max[, ...])

 IN

   x
   y
   no_min      number of local minima to use
   no_max      number of local maxima to use

   ... all remaining arguments passed on to fill ...

 OUT

   fh          handle to patch returned by fill

 See also: find_extrema_with_prominence, fill

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

plot_extrema_shaded.m

SOURCE CODE ^

0001 function fh = plot_extrema_shaded(x, y, no_min, no_max, varargin)
0002 
0003 % Plot shaded area between local minima and maxima
0004 %
0005 % For y(x), plot shaded area between lines connecting extrema.
0006 %
0007 % FORMAT
0008 %
0009 %   plot_extrema_shaded(x, y, no_min, no_max[, ...])
0010 %
0011 % IN
0012 %
0013 %   x
0014 %   y
0015 %   no_min      number of local minima to use
0016 %   no_max      number of local maxima to use
0017 %
0018 %   ... all remaining arguments passed on to fill ...
0019 %
0020 % OUT
0021 %
0022 %   fh          handle to patch returned by fill
0023 %
0024 % See also: find_extrema_with_prominence, fill
0025 
0026 % find_extrema_with_prominence guarantees that x is monotonically
0027 % increasing
0028 [local_min, local_max, prom_min, prom_max] = find_extrema_with_prominence(x, y);
0029 
0030 mins_with_prom = [find(local_min) prom_min];
0031 maxs_with_prom = [find(local_max) prom_max];
0032 
0033 mins_sorted = sortrows(mins_with_prom, [2 1]);
0034 maxs_sorted = sortrows(maxs_with_prom, [2 1]);
0035 
0036 % interpolate to draw polygon
0037 
0038 x_min = [x(1); x(mins_sorted(end-no_min+1:end, 1)); x(end)];
0039 y_min = [y(1); y(mins_sorted(end-no_min+1:end, 1)); y(end)];
0040 x_max = [x(1); x(maxs_sorted(end-no_max+1:end, 1)); x(end)];
0041 y_max = [y(1); y(maxs_sorted(end-no_max+1:end, 1)); y(end)];
0042 
0043 % resort according to x_min
0044 [~, i_min] = sort(x_min);
0045 [~, i_max] = sort(x_max);
0046 
0047 % this was an attempt to use splines
0048 % x_below = x_min(i_min);
0049 % x_above = x_max(i_max(end:-1:1));
0050 % y_below = y_min(i_min);
0051 % y_above = y_max(i_max(end:-1:1));
0052 % xx_below = linspace(x_below(1), x_below(end), 500);
0053 % xx_above = linspace(x_above(1), x_above(end), 500);
0054 % yy_below = spline(x_below, y_below, xx_below);
0055 % yy_above = spline(x_above, y_above, xx_above);
0056 %fh = fill([xx_below, xx_above], [yy_below, yy_above], [.1 .1 .1], varargin{:});
0057 
0058 fh = fill([x_min(i_min); x_max(i_max(end:-1:1))], [y_min(i_min); y_max(i_max(end:-1:1))], [.1 .1 .1], varargin{:});
0059 
0060 %plot(x_min(i_min), y_min(i_min), x_max(i_max), y_max(i_max), '-');
0061 %set(fh,'FaceAlpha',.5,'EdgeAlpha',.5);
0062 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005