%tangent quantization.  gives more bits to higher amplitude sounds
%can be done in blocks with each block having a 16-bit max value
function [qv] = quantize_tan(v,bits,blocks,curv,max_whole);
L = length(v);
block_size = floor(L/blocks);
qv = zeros(L,1);
maxx = zeros(blocks,1);
samples = 2^(bits-1)-1;
quant_fctr = samples/curv;
curv_fctr = atan(curv);

for j=1:blocks-1
   if max_whole == 0
      maxx(j) = max(abs(v((j-1)*block_size+1:j*block_size,1)));
   else
      maxx(j) = max_whole;
   end
   for i=(j-1)*block_size+1:j*block_size
      qv(i) = round(quant_fctr.*tan(v(i)./maxx(j).*curv_fctr));
   end
end

if max_whole == 0
   maxx(blocks) = max(abs(v((blocks-1)*block_size+1:L,1)));
else
   maxx(blocks) = max_whole;
end
for i=(blocks-1)*block_size+1:L
   qv(i) = round(quant_fctr.*tan(v(i)./maxx(blocks).*curv_fctr));
end

%subplot(2,1,1)
%plot(qv);

for j=1:blocks-1
   for i=(j-1)*block_size+1:j*block_size
      qv(i) = atan(qv(i)./quant_fctr)./curv_fctr.*maxx(j);
   end
end

for i=(blocks-1)*block_size+1:L
   qv(i) = atan(qv(i)./quant_fctr)./curv_fctr.*maxx(blocks);
end

%subplot(2,1,2)
%plot(qv);
