combineInputSignals.m 2.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function u = combineInputSignals(myStateSpace, t, varargin)
%MISC.SS.COMBINEINPUTSIGNALS reads the input varargin and searches for name-value-pairs that
%coincide with one of myStateSpace.InputName. If a pair is found, that signal is used
%as elements of u. For inputs that are not found, u-elements are set to zero.
%
% Inputs:
%	myStateSpace	state space for which an input signal is designed
%	t				time for which u shall be defined
%	varargin		name-value-pairs containing input signals with names coinciding
%					with myStateSpace.InputName
% Outputs:
%	u				quantity vector of input signals suiting to myStateSpace for the
%					time t.
%
% Example:
% -------------------------------------------------------------------------
17
18
19
20
21
% 	myStateSpace = ss(-1, [1, 2], 1, [], "InputName", {'control', 'disturbance'});
%	t = quantity.Domain("t", linspace(0, 4, 201));
%	disturbanceSignal = quantity.Symbolic(sin(sym("t")), t, "name", "disturbance");
%	u = misc.ss.combineInputSignals(myStateSpace, t.grid, "disturbance", disturbanceSignal);
%	y = quantity.Discrete(lsim(myStateSpace, u.on(), t.grid), t, "name", "y");
22
23
24
25
26
27
%	plot([y; u]);
% -------------------------------------------------------------------------

% read name of input names from myStateSpace and calculate input lengths
myInputName = misc.ss.removeEnumeration(myStateSpace.InputName);
myInputNameUnique = unique(myInputName, 'stable');
28
myInputNameUniqueValidFieldName = strrep(myInputNameUnique, '.', 'POINT');
29
30
31
32
33
34
35
36
myInputLength = zeros(size(myInputNameUnique));
for it = 1 : numel(myInputLength)
	myInputLength(it) = sum(strcmp(myInputNameUnique{it}, myInputName));
end

% search for input signals in varargin or use default value = 0 instead
myParser = misc.Parser();
for it = 1 : numel(myInputNameUnique)
37
	myParser.addParameter(myInputNameUniqueValidFieldName{it}, ...
38
		quantity.Discrete(zeros(numel(t), myInputLength(it)), ...
Jakob Gabriel's avatar
Jakob Gabriel committed
39
			quantity.Domain("t", t), 'name', myInputNameUnique{it}), ...
40
41
		@(v) isa(v, 'quantity.Discrete') & isvector(v) & (numel(v)==myInputLength(it)));
end
42
43
44
45

% replace '.' in varargin names, to obtain valid fieldnames
newVarargin = varargin;
for it = 1 : numel(newVarargin)
46
47
	if isstring(newVarargin{it}) || ischar(newVarargin{it})
		newVarargin{it} = strrep(newVarargin{it}, ".", "POINT");
48
49
50
	end
end
myParser.parse(newVarargin{:});
51
52

% finally, create resulting input vector
53
u = myParser.Results.(myInputNameUniqueValidFieldName{1});
54
for it = 2 : numel(myInputNameUnique)
55
	u = [u; myParser.Results.(myInputNameUniqueValidFieldName{it})];
56
57
58
end % for

end % combineInputSignals()