-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_quantizer.m
59 lines (50 loc) · 1.32 KB
/
my_quantizer.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
function [xq, centers] = my_quantizer(x, N, min_value, max_value);
%
% MY_QUANTIZER function file
% Implements a uniform quantization algorithm.
% After the run the quantized signal can be retrieved with: centers(xq(i)),
% since centers has the correct values and the xq the levels.
%
% Copyright 2015 George 'papanikge' Papanikolaou
% $Revision: 0.1 $ $Date: 2015/12/15 17:39:11 $
s = size(x);
% Basic checks.
if s(2) ~= 1
error('The signal (x) must be in column form.');
end
s = s(1);
% Change the dynamic range and eliminate outliers.
if min(x) < min_value
for i=1:s
if x(i) < min_value
x(i) = min_value;
end
end
end
if max(x) > max_value
for i=1:s
if x(i) > max_value
x(i) = max_value;
end
end
end
% Calculate the 'step'.
range = max_value - min_value;
levels = 2 ^ N;
step = range/levels;
% Calculate centers.
centers = [];
centers(1) = max_value - step/2;
for i=2:levels
centers(i) = centers(i-1) - step;
end
xq = [];
% We are not going to calculate the quantization zones at all. We are going to
% use the 'min' function here for assigning data points to centers. It returns
% the index of the closest value of our value this way.
for i=1:s
[distance index] = min(abs(centers - x(i)));
xq(i) = index;
end
% We have it transposed.
xq = xq';