function E = percentile_from_mode(x,freq,perc)

freq_n = freq./sum(freq);
[~, peak_i] = max(freq);
peak_v = x(peak_i);
n_bins = size(x,2);

X(:,1) = freq_n;
X(:,2) = x;
Y = sortrows(X,1);
Y = Y(end:-1:1,:);
perc_i=1;

for i=1:size(perc,2)
while sum(Y(1:perc_i,1)) < perc(i)
    perc_i = perc_i+1;
end
    E.x_min(i) = min(Y(1:perc_i,2));
    E.x_max(i) = max(Y(1:perc_i,2));
end
    %
%perc_right = 0;
%i = peak_i;

%while perc_right<(perc/2)
%    perc_right = perc_right + freq_n(i);
%    i++;
%end
%right_x_bar = 


end