简体   繁体   中英

Octave equivalent of MATLAB ltitr.m function

I am trying to get some MATLAB scripts to run in Octave, but have a problem with the following line of code:

x = ltitr( a, b, u, x0 ) ;

which throws an error in Octave.

Online research shows that the ltitr function is an internal MATLAB function that returns the Linear time-invariant time response kernel for the given inputs. This sounds as if it should be a common DSP requirement, so I feel that this must be implemented directly in Octave, or in the latest Control package from Source Forge. However, I can't seem to find an Octave equivalent. I've read the documentation for the latest Octave Control package and maybe I should be using the functions lsim.m or ss.m or dss.m or impulse.m but I'm not really sure.

Can anyone enlighten me? If it's not implemented in Octave, maybe some online reference to code that I could use to write my own ltitr function?

If you actually type in help ltitr in the MATLAB command prompt, you come up with this documentation:

%LTITR  Linear time-invariant time response kernel.
%
%   X = LTITR(A,B,U) calculates the time response of the
%   system:
%           x[n+1] = Ax[n] + Bu[n]
%
%   to input sequence U.  The matrix U must have as many columns as
%   there are inputs u.  Each row of U corresponds to a new time 
%   point.  LTITR returns a matrix X with as many columns as the
%   number of states x, and with as many rows as in U.
%
%   LTITR(A,B,U,X0) can be used if initial conditions exist.
%   Here is what it implements, in high speed:
%
%   for i=1:n
%          x(:,i) = x0;
%          x0 = a * x0 + b * u(i,:).';
%   end
%   x = x.';

%   Copyright 1984-2007 The MathWorks, Inc.
%   $Revision: 1.1.6.4 $  $Date: 2007/05/23 18:54:41 $

% built-in function

As such, they pretty much already give you the code for it. However, I'm assuming that this is written in MEX and so that's why it's built-in and is super fast. As such, if you want to port this over to Octave, you just have to use the code they reference above. It won't be as fast as MATLAB's version though, but that for loop is essentially the basic way to implement it.

However, for the sake of completeness, let's write our own Octave function for it:

function x = ltitr(A, B, U, x0)

%// Number of rows in U is the number of time points
num_points = size(U, 1);

%// Number of columns in U is how many inputs we have
num_inputs = size(U, 2);

x = zeros(num_inputs, num_points); %// Pre-allocate output

%// For each time point we have ...
for idx = 1 : num_points
    x(:,idx) = x0; %// Output the corresponding time point
    x0 = A*x0 + B*U(idx,:).'; %// Compute next time point
end
x = x.';

The above function is almost similar to the code you see in MATLAB, but there are some additional steps I did, such as pre-allocating the matrix for efficiency, as well as getting some relevant variables to help with the computation. Also, take note that the output matrix x is defined with the dimensions flipped . The reason why is because the computation of the output at each point in time is more easier to compute in this state. If you don't, then you'll have to do unnecessary transposing for the other variables defined in the x0 = ... statement. As such, it's easier to do the calculations in the transposed matrix. When you're done, you transpose the resulting matrix to give you the final output matrix x .

The default state for x0 I'm assuming is going to be all zeroes, so if you want to use this as the default state, specify x0 = zeros(n,1); where n is the total number of inputs for your LTI system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM