Skip to content

Instantly share code, notes, and snippets.

@bsodmike
Created May 18, 2011 16:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsodmike/978979 to your computer and use it in GitHub Desktop.
Save bsodmike/978979 to your computer and use it in GitHub Desktop.
MATLAB S-function m-file template
% low-pass FIR filter code goes like this:
%trialfirS An M-file S-function
function [sys,x0,str,ts] = trialfirS(t,x,u,flag)
h=fir1(32,0.5);
ipv=randn(1,100);
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts]=mdlInitializeSizes();
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1,
sys=mdlDerivatives(t,x,u);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
[sys,a,b]=mdlOutputs(t,x,u,h,ipv);
disp(a)
disp(b)
%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
case {1,2,4,9},
sys = [];
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% end trialfirS function
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes()
sizes = simsizes; % creating a structure variable
sizes.NumContStates = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
%
sys = simsizes(sizes);
x0= zeros(1,1);
str = [];
ts = [0 0];
% end mdlInitializeSizes
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function [sys,a,b]=mdlOutputs(t,x,u,h,ipv)
sys=conv(ipv,h);
a=length(sys);
b=length(a);
return;
% end mdlOutputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment