-
Notifications
You must be signed in to change notification settings - Fork 1
/
dpcmenco.m
118 lines (108 loc) · 4.15 KB
/
dpcmenco.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
## Copyright (C) 2012 Leonardo Araujo <[email protected]>
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {@var{qidx} =} dpcmenco (@var{sig}, @var{codebook}, @var{partition}, @var{predictor})
## @deftypefnx {Function File} {[@var{qidx}, @var{q}] =} dpcmenco (@var{sig}, @var{codebook}, @var{partition}, @var{predictor})
## @deftypefnx {Function File} {[@var{qidx}, @var{q}, @var{d}] =} dpcmenco (@dots{})
## Encode using differential pulse code modulation (DPCM).
##
## @table @code
## @item qidx = dpcmenco (sig, codebook, partition, predictor)
## Determine position of the prediction error in a strictly monotonic table (partition).
## The predictor vector describes a m-th order prediction for the
## output according to the following equation
## y(k) = p(1)sig(k-1) + p(2)sig(k-2) + ... + p(m-1)sig(k-m+1) + p(m)sig(k-m) ,
## where the predictor vector is given by
## predictor = [0, p(1), p(2), p(3),..., p(m-1), p(m)].
##
## @item [qidx, q] = dpcmenco (sig, codebook, partition, predictor)
## Also return the quantized values.
##
## @item [qidx, q, d] = dpcmenco (...)
## Also compute distortion: mean squared distance of original sig from the
## corresponding quantized values.
##
## @end table
## @seealso{dpcmdeco, dpcmopt, quantiz}
## @end deftypefn
function [indx, quants, distor] = dpcmenco (sig, codebook, partition, predictor)
if (nargin != 4)
print_usage ();
endif
y = zeros (size (sig));
indx = []; quants = [];
for i = 1:length (y)
## use last predicted value to find the error
y(i) = y(max (i - length (predictor) + 1, 1):i) * predictor(end:-1:max (end-i+1, 1))'; # convolution
e(i) = sig(i) - y(i); # error
[indx(i), quants(i)] = quantiz (e(i), partition, codebook); # quantize the error
y(i) += quants(i); # update prediction value
endfor
## compute distortion
if (nargout > 2)
sigq = dpcmdeco (indx, codebook, predictor);
distor = sumsq (sig(:) - sigq(:)) / length (sig);
endif
endfunction
%!function y = my_sawtooth (t, width)
%! ## sawtooth function, so not to require the signal package for the test
%! if (nargin == 1)
%! width = 1;
%! endif
%! t = mod (t / (2 * pi), 1);
%! y = zeros (size (t));
%! if (width != 0)
%! y(t < width) = 2 * t(t < width) / width - 1;
%! endif
%! if (width != 1)
%! y(t >= width) = -2 * (t(t >= width) - width) / (1 - width) + 1;
%! endif
%!endfunction
%!demo
%! predictor = [0 1];
%! nbits = 4;
%! delta = 2^(-nbits+1);
%! codebook = [-1+delta/2 : delta : 1-delta/2];
%! partition = (codebook(1:end-1) + codebook(2:end))/2;
%! t = linspace (0, 2*pi, 128);
%! t_aux = 2*t;
%! width = 0.25;
%! t_aux = mod (t_aux / (2 * pi), 1);
%! x = zeros (size (t_aux));
%! x(t_aux < width) = 2 * t_aux(t_aux < width) / width - 1;
%! x(t_aux >= width) = -2 * (t_aux(t_aux >= width) - width) / (1 - width) + 1;
%! [idx, xq, distor] = dpcmenco (x, codebook, partition, predictor);
%! xr = dpcmdeco (idx, codebook, predictor);
%! plot (t, x, 'k--','linewidth',1, t, xr, 'b-','linewidth',1);
%! xlim ([0 2*pi]);
%! xlabel ('t'); ylabel ('x(t)');
%! legend ('original', 'dpcm');
%! title ( sprintf ('distortion = %.3f', distor) );
%% Test input validation
%!error dpcmenco ()
%!error dpcmenco (1)
%!error dpcmenco (1, 2)
%!error dpcmenco (1, 2, 3)
%!error dpcmenco (1, 2, 3, 4, 5)
%!test
%! predictor = [0 1];
%! partition = [-0.5, 0, 0.5];
%! codebook = [-1, -0.25, 0.25, 1];
%! t = [0:pi/2:2*pi];
%! x = my_sawtooth(2*t);
%! [idx, qx, distor] = dpcmenco(x, codebook, partition, predictor);
%! assert (idx, [0, 3, 0, 3, 0])
%! assert (qx, [-1, 1, -1, 1, -1])
%! assert (distor, 0)