ENVEXTRACT - Extract envelope of input signal

Program code:

function inoutsig = envextract(inoutsig,fs,cutofffreq)

% ------ Checking of input parameters --------------------------------

error(nargchk(2,3,nargin));

if nargin==2
  cutofffreq=1000;
end;

if ~isnumeric(inoutsig)
  error('%s: The input signal must be numeric.',upper(mfilename));
end;

if ~isnumeric(fs) || ~isscalar(fs) || fs<=0
  error('%s: fs must be a positive scalar.',upper(mfilename));
end;

if ~isnumeric(cutofffreq) || ~isscalar(cutofffreq) || cutofffreq<0
  error('%s: cutofffreq must be a non-negative scalar.',upper(mfilename));
end;

% ------ Computation -------------------------------------------------

% Calculate filter coefficients for the low-pass filtering following
% half-wave rectification. 2nd order butterworth
[b, a] = butter(2, cutofffreq*2/fs);

% 'haircell' envelope extraction. Part 1: Half-wave rectification
inoutsig = max( inoutsig, 0 );

% 'haircell' envelope extraction. Part 2: Low-pass filtering
inoutsig = filter(b,a, inoutsig);