From 35eb56e636e4c980b55e6f14030a826a07adff0e Mon Sep 17 00:00:00 2001
From: Elian Dib <elian.dib@inria.fr>
Date: Tue, 22 Jan 2019 15:37:15 +0100
Subject: [PATCH] Added functions to compute and plot convex envelope

---
 convexEnvelope.m | 30 ++++++++++++++++++++++++++++++
 convexPlot.m     |  8 ++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 convexEnvelope.m
 create mode 100644 convexPlot.m

diff --git a/convexEnvelope.m b/convexEnvelope.m
new file mode 100644
index 0000000..f3251b9
--- /dev/null
+++ b/convexEnvelope.m
@@ -0,0 +1,30 @@
+function [bitrate,peaksnr] = convexEnvelope(bitrate,peaksnr)
+%CONVEX_ENVELOPE Compute and plot upper convex hull of 2D
+%points
+%   Detailed explanation goes here
+
+assert(isequal(size(bitrate),size(peaksnr)));
+if isscalar(bitrate)
+else
+    conv_ind = convhull(bitrate(:),peaksnr(:));
+    bitrate = bitrate(conv_ind); peaksnr = peaksnr(conv_ind);   % Convex Hull
+    
+    min_bit_ind = find(bitrate==min(bitrate));    % Min Bitrate Indices
+    max_bit_ind = find(bitrate==max(bitrate));    % Max Bitrate Indices
+    
+    [~,min_psnr_ind] = max(peaksnr(min_bit_ind)); % Max PSNR Index
+    [~,max_psnr_ind] = max(peaksnr(max_bit_ind)); % Max PSNR Index
+    
+    min_ind = min_bit_ind(min_psnr_ind);          % Min Bitrate Max PSNR Index
+    max_ind = max_bit_ind(max_psnr_ind);          % Max Bitrate Max PSNR Index
+    
+    shift_ind = circshift(1:numel(conv_ind),1-max_ind);  % Shift hull indices so max bitrate is the first entry
+    [~,shift_max_ind] = find(shift_ind==max_ind); % Shift Max Bitrate
+    [~,shift_min_ind] = find(shift_ind==min_ind); % Shift Min Bitrate
+    
+    inds = shift_ind(shift_max_ind:shift_min_ind);% Indices corresponding to upper convex hull
+    
+    bitrate = flip(bitrate(inds));
+    peaksnr = flip(peaksnr(inds));
+end
+end
\ No newline at end of file
diff --git a/convexPlot.m b/convexPlot.m
new file mode 100644
index 0000000..7db8410
--- /dev/null
+++ b/convexPlot.m
@@ -0,0 +1,8 @@
+function [bitrate,peaksnr] = convexPlot(bitrate,peaksnr,varargin)
+%CONVEX_BITRATE_PSNR Compute and plot upper convex hull of 2D
+%points
+%   Detailed explanation goes here
+
+[bitrate,peaksnr] = utils.convexEnvelope(bitrate,peaksnr);
+plot(bitrate,peaksnr,varargin{:});
+end
\ No newline at end of file
-- 
GitLab