function out=statevar(in,fc,d,fs,typ)

% statevar        - digital state variable filter
% out=statevar(in,fc,d,fs,[typ])
%
% Practicable implementation of a state variable filter
%
% input parameters:
% in:   input signal
% fc:   cut-off frequency [Hz]
% d:    damping factor
% fs:   sampling frequency
% typ:  type of filter ('lp', 'bp', 'hp'), optional (default='lp')

% (C) 2000 Piotr Majdak

F=2*sin(pi*fc/fs);
Q=2*d;
len=length(in)+1;
yl=zeros(len,1);
yb=zeros(len,1);
yh=zeros(len,1);

for n=2:len
    yh(n)= in(n-1)-yl(n-1)-Q*yb(n-1);
    yb(n)= F*yh(n)+yb(n-1);
    yl(n)= F*yb(n)+yl(n-1);
end

if ~exist('typ')
    out=yl(2:len);
    disp('Default:Low pass');
elseif typ=='lp'
    out=yl(2:len);
    disp('Low pass');
elseif typ=='hp'
    out=yh(2:len);
    disp('high pass');
elseif typ=='bp'
    out=yb(2:len);
    disp('Band pass');
else
    disp('Unknown type defined. Use lp, bp or hp ');
    out=0;
end



    
