0

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.

2
  • 1
    Well, your problem is that i goes 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 far i should 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. Commented Feb 6, 2021 at 13:34
  • 1
    Given u and step you can simply calculate the minimum and maximum acceptable values of ui, and from that you can directly calculate your minimum and maximum acceptable value of i knowing p. Then you take the (I assume) first integer in that range (ceil(i_min) for example). Commented Feb 8, 2021 at 7:40

1 Answer 1

2

Assuming u_min>0 and 0<p<1, you can simplify (u > u_i/(1+step)) && (u <= u_i/(1-step)) to:

u/u_min > p^(1-i) && p^-i >= u/u_min

Which since log is monotonic, simplifies to

-log(u/u_min)/log(p) > i-1 && i >= -log(u/u_min)/log(p)

Which makes the while loop equivalent to simply

i = floor(-log(u/u_min)/log(p));
q_u = p^(1-i) * u_o;

Furthermore, (u >= 0) in the elseif branch is always true, and you probably can get rid of the u<0 test, by replacing u by abs(u) at the right places.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.