how to perform signal processing online in simulink using s_function ?

1 view (last 30 days)
In my simulink project ,I must do signal processing of a signal online ,by taking frames of this signal and process it .I want to mimaic fault detection procedure by taking a certain window (framing the signal consecutively with a window size of 1 cycle of samples of a discrete state mode) ,that window will be processed with a matlab s2function .I tried to use the block programming already written in matlab example sfunctions which is msfunmdl_basic.m ,and having some changes for my signal to fit it, but I am having problems in getting errors .the window size is 64 samples ..The whole problem is on doing framing online and processing each frame individually ,can any one help me with that program if it has any problems ..for example: for i=1:64:3*64 block.outputport(1).data=block.inputport.data(1)+block.inputport.data(2); end the body of s2function is as follows: function test_basic(block) %MSFUNTMPL_BASIC A Template for a Level-2 MATLAB S-Function % The MATLAB S-function is written as a MATLAB function with the % same name as the S-function. Replace 'msfuntmpl_basic' with the % name of your S-function. % % It should be noted that the MATLAB S-function is very similar % to Level-2 C-Mex S-functions. You should be able to get more % information for each of the block methods by referring to the % documentation for C-Mex S-functions. % % Copyright 2003-2010 The MathWorks, Inc.
%% %% The setup method is used to set up the basic attributes of the %% S-function such as ports, parameters, etc. Do not add any other %% calls to the main body of the function. %% setup(block);
%endfunction
%% Function: setup =================================================== %% Abstract: %% Set up the basic characteristics of the S-function block such as: %% - Input ports %% - Output ports %% - Dialog parameters %% - Options %% %% Required : Yes %% C-Mex counterpart: mdlInitializeSizes %% function setup(block)
% Register number of ports block.NumInputPorts = 3; block.NumOutputPorts = 1;
% Setup port properties to be inherited or dynamic block.SetPreCompInpPortInfoToDynamic; block.SetPreCompOutPortInfoToDynamic;
% Override input port properties block.InputPort(1).Dimensions = 1; block.InputPort(1).DatatypeID = 0; % double block.InputPort(1).Complexity = 'Real'; block.InputPort(2).Dimensions =1; block.InputPort(2).DatatypeID = 0; % double block.InputPort(2).Complexity = 'Real';
block.InputPort(3).Dimensions = 1; block.InputPort(3).DatatypeID = 0; % double block.InputPort(3).Complexity = 'Real';
% Override output port properties block.OutputPort(1).Dimensions = 1; block.OutputPort(1).DatatypeID = 0; % double block.OutputPort(1).Complexity = 'Real';
% Register parameters block.NumDialogPrms = 0;
% Register sample times % [0 offset] : Continuous sample time % [positive_num offset] : Discrete sample time % % [-1, 0] : Inherited sample time % [-2, 0] : Variable sample time block.SampleTimes = [-1 0];
% Specify the block simStateCompliance. The allowed values are: % 'UnknownSimState', < The default setting; warn and assume DefaultSimState % 'DefaultSimState', < Same sim state as a built-in block % 'HasNoSimState', < No sim state % 'CustomSimState', < Has GetSimState and SetSimState methods % 'DisallowSimState' < Error out when saving or restoring the model sim state block.SimStateCompliance = 'DefaultSimState';
%% ----------------------------------------------------------------- %% The MATLAB S-function uses an internal registry for all %% block methods. You should register all relevant methods %% (optional and required) as illustrated below. You may choose %% any suitable name for the methods and implement these methods %% as local functions within the same file. See comments %% provided for each function for more information. %% -----------------------------------------------------------------
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup); block.RegBlockMethod('InitializeConditions', @InitializeConditions); block.RegBlockMethod('Start', @Start); block.RegBlockMethod('Outputs', @Outputs); % Required block.RegBlockMethod('Update', @Update); block.RegBlockMethod('Derivatives', @Derivatives); block.RegBlockMethod('Terminate', @Terminate); % Required
%end setup
%% %% PostPropagationSetup: %% Functionality : Setup work areas and state variables. Can %% also register run-time methods here %% Required : No %% C-Mex counterpart: mdlSetWorkWidths %% function DoPostPropSetup(block)
block.NumDworks = 1;
block.Dwork(1).Name = 'a';
block.Dwork(1).Dimensions = 1;
block.Dwork(1).DatatypeID = 0; % double
block.Dwork(1).Complexity = 'Real'; % real
block.Dwork(1).UsedAsDiscState = true;
%% %% InitializeConditions: %% Functionality : Called at the start of simulation and if it is %% present in an enabled subsystem configured to reset %% states, it will be called when the enabled subsystem %% restarts execution to reset the states. %% Required : No %% C-MEX counterpart: mdlInitializeConditions %% function InitializeConditions(block)
%end InitializeConditions
%% %% Start: %% Functionality : Called once at start of model execution. If you %% have states that should be initialized once, this %% is the place to do it. %% Required : No %% C-MEX counterpart: mdlStart %% function Start(block)
block.Dwork(1).Data = 0;
%end Start
%% %% Outputs: %% Functionality : Called to generate block outputs in %% simulation step %% Required : Yes %% C-MEX counterpart: mdlOutputs %% function Outputs(block) for i=1:63:2*64 block.OutputPort(1).Data=sqrt((block.InputPort(1).Data)+(block.InputPort(2).Data)+(block.InputPort(3).Data); end
%end Outputs
%% %% Update: %% Functionality : Called to update discrete states %% during simulation step %% Required : No %% C-MEX counterpart: mdlUpdate %% function Update(block)
%end Update
%% %% Derivatives: %% Functionality : Called to update derivatives of %% continuous states during simulation step %% Required : No %% C-MEX counterpart: mdlDerivatives %% function Derivatives(block)
%end Derivatives
%% %% Terminate: %% Functionality : Called at the end of simulation for cleanup %% Required : Yes %% C-MEX counterpart: mdlTerminate %% function Terminate(block)
%end Terminate

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!