I am currently working on a Simulink block designed to perform an online computation of the weighted kurtosis typical of a certain signal. This block is part of a larger control algorithm, which will then be compiled as a DLL and run in both simulations and lab tests.
So far the Simulink block diagram is as follows.
The circular buffer performs the following
function y = fcn(u,bufferIC,bufferLength)
persistent buffer;
if isempty(buffer)
if isequal(numel(bufferIC),bufferLength)
buffer = bufferIC;
elseif isscalar(bufferIC)
buffer = bufferIC*ones(1,bufferLength);
else
error('IC must either be scalar or the same dimensions as buffer length')
end
end
% Output
y = buffer;
% Update
buffer = [u buffer(1:end-1)];
end %fcn
and the weighted kurtosis will then be calculated according to the following algorithm:
function WKurt = fcn(BuffSig)
N = length(BuffSig);
y = (1.005.^(1:N));
weights = 2*(y/max(y));
WMean = sum((weights.*BuffSig))/sum(weights);
WStd = sqrt(sum(weights.*((BuffSig - WMean).^2))/sum(weights));
WKurt = squeeze(((sum(weights.*((((BuffSig - WMean)./WStd)).^4))/sum(weights))) - 3);
WKurt(WKurt<0) = 0.0;
WKurt(isnan(WKurt)) = 0.0;
WKurt(isinf(WKurt)) = 0.0;
If I run simulations, everything is alright and I do not happen to run into any CPU computation issue. That does not hold valid as soon as I move to lab tests, which will be terminated because the block requires too much CPU memory.
Therefore, I would very much appreciate whether a work-around exists so as to achieve the same kurtosis estimation, but in a more CPU-effcient way.
sum(weights)
by a variable and compute it only once? Have you tried to get rid of squeeze()? Have you tried to replace exponentiation by multiple multiplication (a^4 = a*a*a*a
, then withb = a*a, a^4 = b*b
), etc. $\endgroup$max(y)
by a direct call to the last index.y
follows a simple formula where you know the position of the maximum beforehand (last entry). You should really check the MATLAB profiler to find the culprit that causes the CPU load. $\endgroup$