2014-09-18 97 views
0

我試圖得到一些MATLAB腳本在八度運行,但與下面的代碼行的問題:八度相當於MATLAB ltitr.m功能

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

它拋出一個錯誤的八度。

在線研究顯示,ltitr函數是一個內部MATLAB函數,它返回給定輸入的線性時不變時間響應內核。這聽起來好像它應該是一個常見的DSP要求,所以我覺得這必須直接在Octave或SourceForge的最新控制包中實現。但是,我似乎無法找到一個八度相當的。我已經閱讀了最新的Octave Control軟件包的文檔,也許我應該使用函數lsim.m或ss.m或dss.m或impulse.m,但我並不確定。

任何人都可以啓發我嗎?如果它沒有在Octave中實現,可能是一些我可以用來編寫我自己的ltitr函數的代碼的在線參考?

回答

3

如果你確實在help ltitr在MATLAB命令提示符下鍵入,你想出這個文檔:

%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 

因此,他們幾乎已經給你它的代碼。不過,我假設這是用MEX編寫的,所以這就是爲什麼它是內置的,並且速度非常快。因此,如果您想將此轉換爲Octave,則只需使用上面提到的代碼即可。雖然它不會像MATLAB的版本一樣快,但是for循環基本上是實現它的基本方式。

然而,爲了完整起見,我們寫我們自己的八度音功能吧:

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.'; 

上述功能幾乎類似於您在MATLAB看到的代碼,但是有我做了一些額外的步驟例如預先分配矩陣以獲得效率,並獲得一些相關變量以幫助進行計算。另外,請注意,輸出矩陣x的尺寸爲翻轉。原因是因爲在這個狀態下計算每個時間點的輸出更容易計算。如果您不這樣做,則您必須爲x0 = ...聲明中定義的其他變量進行不必要的轉置。因此,在轉置矩陣中進行計算更容易。完成後,將轉矩矩陣轉換爲最終輸出矩陣x

x0我假設將是全零,所以如果你想用這個作爲默認狀態,指定x0 = zeros(n,1);其中n是爲您的LTI系統的總輸入量的默認狀態。