Code covered by the BSD License  

Highlights from
Simuwave

image thumbnail

Simuwave

by

 

A Simulink library for wavelet processing on 1-D signals

sFIR(t,x,u,flag,f,act)
function [sys,x0,str,ts] = sFIR(t,x,u,flag,f,act)
%	An S-function which implements a switched FIR filter f
%	Takes two parameters: the FIR filter f and the activation variable act.
%	The signal is filtered by the FIR filter f if act > 0. 
%	if act = 0, then the filter simply does nothing (direct feedthrough)
%	if act < 0 then the filter outputs 0
%   Input: u :    Single valued data at the current time which
%                 is being buffered.
%   Output:       filtered input
%   Parameters:   f is the FIR filter in ascending powers of z^-1
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
	[sys,x0,str,ts] = mdlInitializeSizes(length(f));
	%%%%%%%%%%  
	% Update %
	%%%%%%%%%%
case 2,                                               
    sys = mdlUpdate(t,x,u,length(f),act);
	%%%%%%%%%%
	% Output %
	%%%%%%%%%%
case 3,                                               
    sys = mdlOutputs(t,x,u,f,act);    
	%%%%%%%%%%%%%
	% Terminate %
	%%%%%%%%%%%%%
case 9,
    sys = mdlTerminate;
	%%%%%%%%%%%%%%%%%%%%
	% Unexpected flags %
	%%%%%%%%%%%%%%%%%%%%
otherwise
    error(['Unhandled flag = ',num2str(flag)]);
end
% end buffer
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts] = mdlInitializeSizes(W)
disp('Using sFIR.m');
if W < 2
	sizes = simsizes;
	sizes.NumContStates  = 0;
	sizes.NumDiscStates  = 0;
	sizes.NumOutputs     = 1;
	sizes.NumInputs      = 1;
	sizes.DirFeedthrough = 1;
	sizes.NumSampleTimes = 1;
	sys = simsizes(sizes);
	x0  = [];
	str = [];
	ts  = [-1 0];
else
	sizes = simsizes;
	sizes.NumContStates  = 0;
	sizes.NumDiscStates  = W-1;
	sizes.NumOutputs     = 1;
	sizes.NumInputs      = 1;
	sizes.DirFeedthrough = 1;
	sizes.NumSampleTimes = 1;
	sys = simsizes(sizes);
	x0  = zeros(W-1,1);
	str = [];
	ts  = [-1 0];
end
% end mdlInitializeSizes
%
%=======================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=======================================================================
%
function sys = mdlUpdate(t,x,u,W,act)
if (W < 2 | act <= 0)
	sys = [];
else
	% Denoting the current time instant by t, we have:
	% x(1) = u(t), ..., x(W) = u(t-(W-1)).
	x(2:W-1) = x(1:W-2);
	x(1) = u;
	sys = x;
end
%end mdlUpdate
%
%=======================================================================
% mdlOutputs
% Return the output vector for the S-function
%=======================================================================
%
function sys = mdlOutputs(t,x,u,f,act)
if act > 0
	if length(f) < 2
		sys = f*u;
	else
		sys = f*[u;x];
	end
elseif act == 0
	sys = u;
else
	sys = 0;
end
%end mdlOutputs
%
%=============================================================================
% mdlTerminate
% Perform any end of simulation tasks.
%=============================================================================
%
function sys = mdlTerminate
sys = [];
% end mdlTerminate

Contact us