clear all
%use an optimizer to find the best parameters
%for an utterance
%The encoder/decoder go into an iterative scheme to find the
%best 3 break points and 4 reconstruction values
%===================================%the Encoder
[d,r] = wavread('a-8khz.WAV'); %4
%scale to about unity
res = 1/(max(max(d),abs(min(d)) ));
dd = diff(d * res);

 OPTIONS = OPTIMSET('MAXFUNEVALS',2000000000000000, 'MAXITER', 2000000000000000000000000);
%parameters
value = [ 
   -0.3991   -0.3774   -0.3496   -0.3415   -0.0442   -0.0207    0.0011    0.0202    0.0224    0.0312    0.0321    0.1018    0.3522    0.3744    0.3982 0.5930];
   brkpt=[  -0.7139   -0.6688   -0.5144   -0.3381   -0.2695   -0.1562   -0.1300   -0.0740    0.0010    0.1948    0.2173    0.3701    0.5265    0.5330    0.5657];
         
%initial parameter guess
p0 = [brkpt(1), brkpt(2), brkpt(3), brkpt(4), brkpt(5), brkpt(6), brkpt(7), brkpt(8), ...
    brkpt(9), brkpt(10), brkpt(11), brkpt(12), brkpt(13), brkpt(14), brkpt(15), ...
    value(1),value(2),value(3),value(4), value(5), value(6), value(7), ...
    value(8), value(9), value(10), value(11), value(12), value(13),  ...
    value(14), value(15) value(16)];

% p = fminsearch(@FindOpt,p0,[],d,dd, OPTIONS);
  p = fminsearch(@FindOpt,p0, OPTIONS, d, dd);
%ChiMin = fmins ('chisq', density0, options, [], smat, observed,
%fixdensity);

%=================================%print parameters
brkpt=p(1:15)
value=p(16:length(p))