-
Notifications
You must be signed in to change notification settings - Fork 0
/
enhanced_equidistribution.m
95 lines (89 loc) · 2.61 KB
/
enhanced_equidistribution.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function gu = enhanced_equidistribution(u, bins)
%%=========================================================================
% Copyright ?2018, SoC Design Lab., Dong-A University. All Right Reserved.
%==========================================================================
% ?Date : 2018/05/21
% ?Author : Dat Ngo
% ?Affiliation: SoC Design Lab. - Dong-A University
% ?Design : Enhanced equidistribution
%==========================================================================
%
%ENHANCED_EQUIDISTRIBUTION Enhanced equidistribution.
% U = ENHANCED_EQUIDISTRIBUTION(U, BINS) performs enhanced equidistribution
% on U with BINS histogram bins.
h = size(u,1);
w = size(u,2);
n = histcounts(u, bins);
thres = round(h*w/bins);
for h = 1:bins
if (n(h) < thres)
loc = find(n > thres);
diff = n(loc)-thres;
for k = 1:length(diff)
loc2 = find(u == loc(k));
for j = 1:diff(k)
u(loc2(j)) = h;
n(h) = n(h)+1;
n(loc(k)) = n(loc(k))-1;
if (n(h) >= thres)
break;
elseif (n(loc(k)) <= thres)
break;
end
end
if (n(h) >= thres)
break;
end
end
elseif (n(h) > thres)
loc = find(n < thres);
diff = thres-n(loc);
for k = 1:length(diff)
loc2 = find(u == h);
for j = 1:diff(k)
u(loc2(j)) = loc(k);
n(h) = n(h)-1;
n(loc(k)) = n(loc(k))+1;
if (n(h) <= thres)
break;
elseif (n(loc(k)) >= thres)
break;
end
end
if (n(h) <= thres)
break;
end
end
else
% do nothing
end
end
hloc = find(n~=thres);
inc = 0;
dec = 0;
for index = 1:length(hloc)
h = hloc(index);
if (n(h) < thres)
diff = thres-n(h);
for k = 1:diff
loc = find(u == (mod(dec,bins)+1));
u(loc(1)) = h;
n(h) = n(h)+1;
n(mod(dec,bins)+1) = n(mod(dec,bins)+1)-1;
dec = mod(dec,bins)+1;
end
elseif (n(h) > thres)
loc = find(u == h);
diff = n(h)-thres;
for k = 1:diff
u(loc(k)) = mod(inc,bins)+1;
n(h) = n(h)-1;
n(mod(inc,bins)+1) = n(mod(inc,bins)+1)+1;
inc = mod(inc,bins)+1;
end
else
% do nothing
end
end
gu = u;
end