-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsoft_quantize.m
More file actions
49 lines (42 loc) · 1.47 KB
/
soft_quantize.m
File metadata and controls
49 lines (42 loc) · 1.47 KB
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
function [second_value, p2]=soft_quantize(value,levels,window)
% SOFT_QUANTIZE = quantize a value with linear soft borders
%
% value = the value to quantize
% levels = the level border vector
% window = the width of the soft border (in [0,0.5])
%
% second_value = the second state level
% p2 = the weight of the second factor
% find the first value and initialize
first_value = quantize(value, levels);
second_value = 0;
p2 = 0;
lower_distance = 0.5;
% the lowest level has no lower border
if (first_value == 1),
value = value - levels(1) / 2;
else
value = value - levels(first_value - 1);
end
% calculate the distance from the lower border
if (first_value <= length(levels) && first_value > 1),
lower_distance = value / (levels(first_value) - levels(first_value-1));
end
% special cases: lowest and highest level
if (first_value == 1),
lower_distance = value / (levels(first_value + 1) - levels(first_value));
end
if (first_value > length(levels)),
lower_distance = value / (levels(first_value - 1) - levels(first_value - 2));
end
% find the weight of the linear combination if the second level is lower
if (lower_distance < window && first_value > 1),
second_value = first_value - 1;
p2 = 0.5 - 0.5 * lower_distance / window;
end
% find the weight of the linear combination if the second level is higher
if (1 - lower_distance < window && first_value <= length(levels)),
second_value = first_value + 1;
p2 = 0.5 - 0.5 * (1 - lower_distance) / window;
end
end