-
Notifications
You must be signed in to change notification settings - Fork 54
/
ESLselect.m
executable file
·55 lines (48 loc) · 1.45 KB
/
ESLselect.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
function sv = ESLselect(v,sy,dw,df)
%sv = ESLselect(v,sy,dw,df) Select the elements of a multi-element
%DAC to minimize the selection error, subject to the constraint that
%the nominal DAC output is v, i.e. v = sv'*dw.
%df is a vector of dac error factors. de is defined s.t. df'*de = 0.
%Assume that the preferred usage order is that given by dw.
% Go through sv possibilities one by one, until one which meets the
% v = sv'*dw constraint is found.
if v<0 | v>sum(dw)
error('v argument is invalid (too large or too small)');
end
n = length(dw);
sv = zeros(n,1);
if v==0; return; end;
[junk possibilities] = sort(-sy); %Determine the element priority
% Code to speed up selection in the usual case where all
% DAC elements are weighted at one. Suggested by J. A. Cherry, 5-Mar-97
if dw==ones(n,1);
sv(possibilities(1:v)) = ones(1,v);
return;
end;
i = 1; % Selection level
pointer = 1; % Array of pointers to selected elements
selected = []; % Selected elements
while 1
while pointer(i)>n;
% backtrack
i = i-1;
if i==0
break; %failure!
end
pointer(i) = pointer(i)+1;
selected = selected(1:i);
end
selected(i) = possibilities(pointer(i));
dv = sum(dw(selected));
if dv==v
break; %success!
elseif dv<v
% Proceed to the next level of selection
pointer(i+1) = pointer(i)+1;
i = i+1;
else
% Try the next element at the current level.
pointer(i) = pointer(i)+1;
end
end
sv(selected) = ones(1,length(selected));