I am implementing a logarithmic quantizer and what I would like to do is to optimize the code as much as possible. The precise point where I would like to make a change is the last else statement where the equation to be implemented is:
q(u) = u_i if u_i/(1+step) < u <= u_i/(1-step)
u_i = p^(1-i)u_o for i=1,2,...
The parameters p, step, u_o are some constants to be chosen.
More information regarding the quantizer can be found at this paper: Adaptive Backstepping Control of Uncertain Nonlinear Systems with Input Quantization.
In order to code a function to implement it in MATLAB, I wrote the following piece of code:
function q_u = logarithmic_hysteretic_quantizer(u,step,u_min)
u_o = u_min*(1+step);
p = (1-step)/(1+step);
if u < 0
q_u = -logarithmic_hysteretic_quantizer(-u,step,u_min);
elseif ( (u >= 0) && (u <= u_o/(1+step)) )
q_u = 0;
else
i = 1;
while (1)
u_i = p^(1-i) * u_o;
if ( (u > u_i/(1+step)) && (u <= u_i/(1-step)) )
q_u = u_i;
break;
end
i = i + 1;
end
end
end
Now, my issue is to improve the code as much as I can. For example, the while(1) loop, which codes the different quantization levels, is something that could probably go away and be replaced. Any thoughts would be really appreciated.
igoes to infinity if your condition( (u > u_i/(1+step)) && (u <= u_i/(1-step)) )is never true. I don't know much about logarithmic quantization and your link isn't accessible for me. I'd suggest you to think about how farishould go, from 1 to 100,1000,10000,... And then you can use a for-loop instead of the while-loop. Or create a large array which contains values from 1 to 100,1000,1000,... and work with that.uandstepyou can simply calculate the minimum and maximum acceptable values ofui, and from that you can directly calculate your minimum and maximum acceptable value ofiknowingp. Then you take the (I assume) first integer in that range (ceil(i_min)for example).