I'm not sure if this is possible, but my understanding of MATLAB could certainly be better.
I have some code that I want to highlight, since it causes a rather bottleneck in my program. This is part of the optimization procedure, which has many possible configurations of short-term average (STA), long-term average (LTA) and sensitivity (OnSense) for passing.
Time in vector format, FL2onSS is the main data (double Nx1), FL2onSSSTA is STA (NxSTA double), FL2onSSThresh is the Threshold value (NxLTAxOnSense double)
The idea is to calculate the red alarm matrix, which will be 4D - alarmStatexSTAxLTAxOnSense, which is used in the rest of the program.
Red = zeros(length(FL2onSS), length(STA), length(LTA), length(OnSense), 'double'); for i=1:length(STA) for j=1:length(LTA) for k=1:length(OnSense) Red(:,i,j,k) = calcRedAlarm(Time, FL2onSS, FL2onSSSTA(:,i), FL2onSSThresh(:,j,k)); end end end
Currently, I am getting this recurring function, trying to get a little more speed from it, but obviously it would be better if all of this could be vectorized. In other words, I do not need to save the function if there is a better solution.
function [Red] = calcRedAlarm(Time, FL2onSS, FL2onSSSTA, FL2onSSThresh) % Calculate Alarms % Alarm triggers when STA > Threshold zeroSize = length(FL2onSS); %Precompose Red = zeros(zeroSize, 1, 'double'); for i=2:zeroSize %Because of time chunks being butted up against each other, alarms can %go off when they shouldn't. To fix this, timeDiff has been %calculated to check if the last date is different to the current by 5 %seconds. If it isn't, don't generate an alarm as there is either a %validity or time gap. timeDiff = etime(Time(i,:), Time(i-1,:)); if FL2onSSSTA(i) > FL2onSSThresh(i) && FL2onSSThresh(i) ~= 0 && timeDiff == 5 %If Short Term Avg is > Threshold, Trigger Red(i) = 1; elseif FL2onSSSTA(i) < FL2onSSThresh(i) && FL2onSSThresh(i) ~= 0 && timeDiff == 5 %If Short Term Avg is < Threshold, Turn off Red(i) = 0; else %Otherwise keep current state Red(i) = Red(i-1); end end end
The code is simple enough, so I will not explain it. If you need to find out what a particular line does, let me know.