Code covered by the BSD License  

Highlights from
Simuwave

image thumbnail

Simuwave

by

 

A Simulink library for wavelet processing on 1-D signals

FIR(t,x,u,flag,f,lu)
function [sys,x0,str,ts] = FIR(t,x,u,flag,f,lu)
%	An S-function which implements the FIR f on lu input channels
%	If lu is unspecified, then lu = 1.
% 	If l and lu are unspecified, f =1 and lu = 1.
%   Input: u :    Vector 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
%				  lu is the width of the input
% Note due to a featre of Simulink 2.1, a dynamically determined 
% state dimension must be equal to the input width when using
% m files. Mex files handle dynamic dimension correctly.
% If you are using the (compiled) mex version of FIR,
% do not specify the input width.
n = nargin;
if n == 4
	f = 1;
	lu = 1;
elseif n == 5
	lu = 1;
end

switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
	[sys,x0,str,ts] = mdlInitializeSizes(length(f),lu);
	%%%%%%%%%%  
	% Update %
	%%%%%%%%%%
case 2,                                               
    sys = mdlUpdate(t,x,u,length(f));
	%%%%%%%%%%
	% Output %
	%%%%%%%%%%
case 3,                                               
    sys = mdlOutputs(t,x,u,f);    
	%%%%%%%%%%%%%
	% 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,lu)
disp('Using FIR.m');
if W < 2
	sizes = simsizes;
	sizes.NumContStates  = 0;
	sizes.NumDiscStates  = 0;
	sizes.NumOutputs     = lu;
	sizes.NumInputs      = lu;
	sizes.DirFeedthrough = 1;
	sizes.NumSampleTimes = 1;
	sys = simsizes(sizes);
	x0  = [];
	str = [];
	ts  = [-1 0];
else
	sizes = simsizes;
	sizes.NumContStates  = 0;
	sizes.NumDiscStates  = -1;
	sizes.NumOutputs     = -1;
	sizes.NumInputs      = -1;
	sizes.DirFeedthrough = 1;
	sizes.NumSampleTimes = 1;
	sys = simsizes(sizes);
	%x0  = zeros(lu*(W-1),1);
	x0 = 0;
	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)
if W < 2
	sys = [];
else
	% Denoting the current time instant by t, we have:
	% x(1) = u(t), ..., x(W) = u(t-(W-1)).
	lu = length(u);
	if (size(x,1)*size(x,2)~=lu*(W-1))
		x = zeros(lu*(W-1),1);
		disp('Error on the state dimension')
	end
	x = reshape(x,lu,W-1);
	x(:,2:W-1) = x(:,1:W-2);
	x(:,1) = u;
	sys = reshape(x,lu*(W-1),1);
end
%end mdlUpdate
%
%=======================================================================
% mdlOutputs
% Return the output vector for the S-function
%=======================================================================
%
function sys = mdlOutputs(t,x,u,f)
if length(f) < 2
	sys = f*u;
else
	lu = length(u);
	W = length(f)-1;
	if (size(x,1)*size(x,2)~=lu*W)
		sys = f(1)*u;
	else
		x = reshape(x,lu,W);
		sys = [u x]*f';
	end
end
%end mdlOutputs
%
%=============================================================================
% mdlTerminate
% Perform any end of simulation tasks.
%=============================================================================
%
function sys = mdlTerminate
sys = [];
% end mdlTerminate

Contact us