smooth

PURPOSE ^

SMOOTH Perform windowed smoothing on a vector using mathematical functions

SYNOPSIS ^

function y = smooth(x,frame,mode)

DESCRIPTION ^

 SMOOTH Perform windowed smoothing on a vector using mathematical functions
 
 SYNTAX
 
     Y = smooth(X,FRAME)
     Y = smooth(X,FRAME,MODE)
 
 
 DESCRIPTION
 
 Y = smooth(X,FRAME) smooths the input vector X by calculating the running
 RMS over a series of frames. FRAME specifies the frame characteristics;
 it can be set to:
 
 a scalar - this will be used as the length of the frame, the window will
 be rectangular
 a vector - this specifies the shape of the analysis window, the frame
 length will be length(frame).
 
 Y = smooth(X,FRAME,MODE) allows the user to specify a different
 mathematical smoothing function. The options are:
 
 'rms' - calculates the running rms (default)
 'mean' - calculates the running mean (moving average filter)
 'median' - calculates the running median
 
 NOTE: SMOOTH uses a vectorized implementation that may be slow when X 
 and/or FRAME_LENGTH are very large. The number of elements that are used
 for calculation is length(X)*FRAME_LENGTH. The algorithm vectorizes the
 operation by creating a matrix of indexes and extracting its diagonals.
 E.g. for a vector of length 4 and frame_length of 2, the algorithm
 creates a temporary zero-padded matix x2 from which it creates a set of
 indexes:
 
 1 1
 2 2
 3 3
 4 4
 5 5
 6 6
 
 It then extracts the diagonals where -length(x2)+frame_length<=k<=0,
 yielding:
 
 1 2
 2 3
 3 4
 4 5
 
 this is used to index x2; operations are then perfromed along the rows.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function y = smooth(x,frame,mode)
0002 
0003 % SMOOTH Perform windowed smoothing on a vector using mathematical functions
0004 %
0005 % SYNTAX
0006 %
0007 %     Y = smooth(X,FRAME)
0008 %     Y = smooth(X,FRAME,MODE)
0009 %
0010 %
0011 % DESCRIPTION
0012 %
0013 % Y = smooth(X,FRAME) smooths the input vector X by calculating the running
0014 % RMS over a series of frames. FRAME specifies the frame characteristics;
0015 % it can be set to:
0016 %
0017 % a scalar - this will be used as the length of the frame, the window will
0018 % be rectangular
0019 % a vector - this specifies the shape of the analysis window, the frame
0020 % length will be length(frame).
0021 %
0022 % Y = smooth(X,FRAME,MODE) allows the user to specify a different
0023 % mathematical smoothing function. The options are:
0024 %
0025 % 'rms' - calculates the running rms (default)
0026 % 'mean' - calculates the running mean (moving average filter)
0027 % 'median' - calculates the running median
0028 %
0029 % NOTE: SMOOTH uses a vectorized implementation that may be slow when X
0030 % and/or FRAME_LENGTH are very large. The number of elements that are used
0031 % for calculation is length(X)*FRAME_LENGTH. The algorithm vectorizes the
0032 % operation by creating a matrix of indexes and extracting its diagonals.
0033 % E.g. for a vector of length 4 and frame_length of 2, the algorithm
0034 % creates a temporary zero-padded matix x2 from which it creates a set of
0035 % indexes:
0036 %
0037 % 1 1
0038 % 2 2
0039 % 3 3
0040 % 4 4
0041 % 5 5
0042 % 6 6
0043 %
0044 % It then extracts the diagonals where -length(x2)+frame_length<=k<=0,
0045 % yielding:
0046 %
0047 % 1 2
0048 % 2 3
0049 % 3 4
0050 % 4 5
0051 %
0052 % this is used to index x2; operations are then perfromed along the rows.
0053 
0054 %% Gather inputs
0055 
0056 if ~isvector(x)
0057     error('''x'' must be a vector')
0058 end
0059 
0060 if isscalar(frame)
0061     frame_length = frame;
0062     window = ones(frame_length,1);
0063 elseif isvector(frame)
0064     window = frame;
0065     frame_length = length(frame);
0066 else
0067     error('''frame'' must be a vector or a scalar')
0068 end
0069 
0070 if nargin<3
0071     mode = 'rms';
0072 end
0073 
0074 %% Smooth
0075 
0076 % zero pad
0077 x2 = [zeros(ceil((frame_length)/2)-1,1); x(:); zeros(floor(frame_length/2),1)];
0078 
0079 % get indexes
0080 index = spdiags(repmat((1:length(x2))',[1 frame_length]),0:-1:-length(x2)+frame_length);
0081 
0082 window = repmat(window,[1 length(x)]);
0083 
0084 % do calculations
0085 switch lower(mode)
0086     case 'rms'
0087         y = sqrt(mean((window.*x2(index)).^2));
0088     case 'median'
0089         y = median((window.*x2(index)));
0090     case 'mean'
0091         y = mean((window.*x2(index)));
0092     otherwise
0093         error('Unknown ''mode'' specified')
0094 end
0095 
0096 % transpose if necessary
0097 if size(y,1)~=size(x,1)
0098     y = y';
0099 end

Generated on Thu 21-Aug-2014 10:40:31 by m2html © 2005