function [ olg, ss] = olgFromSimulink(mdl, freq, dof) %olgFromSimulink %by m.nakano % Validate required arguments if ~ischar(mdl) error('The model name is not a string'); elseif ~isreal(freq) error('The frequency vector is not real-valued'); elseif ~ischar(dof) error('The DOF name is not a string'); end %% Gather all NbNoiseSink and NbNoiseCal blocks load_system(mdl); % getBlocksByDof() is a local function defined below OLGSinksByDof = getBlocksByDof(mdl, 'OLGSink'); if OLGSinksByDof.isKey(dof) OLGSink = OLGSinksByDof(dof); disp(['DOF ' dof ' is selected']); else error(['The requested DOF name (' dof ') is not defined in the model']); end OLGSinkVars = get_param(OLGSink, 'MaskWSVariables'); OLGSinkVars = containers.Map({OLGSinkVars.Name}, {OLGSinkVars.Value}); groupName = OLGSinkVars('group'); groupedOLG = groupOLG(mdl,dof,groupName); ii = 1; if isempty(groupedOLG) disp('OLG group is not specified. All loop(s) in the model are closed.'); else disp(['Loop(s) includeing OLGsink grouped (' groupName ') is opened except for the requested DOF (' dof ')']); for n = 1:numel(groupedOLG) io(ii) = linio(groupedOLG{n},1,'loopbreak'); ii = ii+1; end end io(ii) = linio(OLGSink,1,'looptransfer'); ss = linearize(mdl,io); [mag,phase]=bode(ss,freq * 2 * pi); olg = squeeze(mag).*exp(1i * pi /180 * squeeze(phase)); end function [ blockTable ] = getBlocksByDof(mdl, tag) %% Locate the blocks with the requested tag blks = findInSystemOrRefs(mdl, 'Tag', tag); %% Organize them in a hashtable (containers.Map object), indexed by the DOF name blockTable = containers.Map(); for n = 1:numel(blks) blk = blks{n}; blkVars = get_param(blk, 'MaskWSVariables'); blkVars = containers.Map({blkVars.Name}, {blkVars.Value}); if ~ischar(blkVars('dof')) error(['Invalid ' tag ' block ' blk char(10) ... 'The DOF name (' get_param(blk, 'dof') ') must be a string']); end disp([' ' blk ' :: ' blkVars('dof') ' / ' blkVars('group')]); if ~blockTable.isKey(blkVars('dof')) blockTable(blkVars('dof')) = blk; else error(['The DOF name cannot be shared by multiple ' tag ' blocks' char(10) ... 'Blocks ' blk ' and ' blockTable(blkVars('dof')) ' both have dof=' blkVars('dof')]); end end end function [ groupedOLG ] = groupOLG(mdl, dof, group) % Validate required arguments if ~ischar(mdl) error('The model name is not a string'); end groupedOLG = {}; if strcmp(group,'') return; end %% Organize the noises in a hashtable (containers.Map object), indexed by group blks = findInSystemOrRefs(mdl, 'Tag', 'OLGSink'); for n = 1:numel(blks) blk = blks{n}; blkVars = get_param(blk, 'MaskWSVariables'); blkVars = containers.Map({blkVars.Name}, {blkVars.Value}); if strcmp(blkVars('group'),group) if ~strcmp(blkVars('dof'),dof) groupedOLG = [groupedOLG blk]; end end end if isempty(groupedOLG) disp('OLG group is not specified (group name is empty or the requested group is not defined in the model. All loop(s) in the model are open.'); return; end end