-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathmyquant.m
29 lines (23 loc) · 821 Bytes
/
myquant.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
function [Q,I] = myquant(X,M,N)
% [Q,I] = myquant(X,M,N) Quantize values uniformly
% Q returns X quantized to N levels (default 256), and I returns
% integer indexes corresponding to each value in Q. M specifies
% the range of levels; for M scalar (default 1.0), the values
% are between -M and M. If M has two values, quantization is
% between M(1) and M(2). Q is 'dequantized' back to be an
% approximation of X; to get the quantized level, multiply
% N and divide by 2M (or M(2) - M(1)).
% 2001-03-20 dpwe@ee.columbia.edu
if nargin < 2
M = 1.0;
end
if nargin < 3
N = 256;
end
if length(M) == 1
M = [-M,M];
end
qstep = (M(2) - M(1))/N;
% Q = max(M(1), min(M(2), M(1) + qstep*round( (X - M(1)) / qstep )));
I = max(0, min(N-1, round( (X - M(1)) / qstep )));
Q = M(1) + qstep*I;