image thumbnail

find pulsed trigger events

by

 

The function finds the start of pulsed events in a signal via a trigger limit.

get_trigger(sig, level, fs, prate, rf)
function triggerindex = get_trigger(sig, level, fs, prate, rf)
% Trigger on a pulsed signal. Includes down time DB. V.1.0. 2011/04/20
% 
% This function is build to find regularly repeated events in a signal
% stream. That is why it includes a down time, where no trigger will be
% found. It is build for signals that deviate from zero, so if needed
% include the line #55. It is not automaticllay included since when you
% are searching for short random signals, the mean value is ~= 0, and when
% your trigger level is small you'll get wrong resutls.
%  
%
% INPUT:
% triggerindex = get_trigger(sig, level, fs, prate)
%  sig:         Signal to be searched
%  level:       Trigger level (in absolute values)
%  fs:          Sampling frequency in Hz
%  prate:       no. of pulses/sec
%  rf(optional) select whether your event is triggered on rising ('r', default) or
%               falling slope ('f'). If invalid input: search both ways. If
%               falling slope is selected, the trigger level is multiplied
%               by -1.
% 
% OUTPUT:
% triggerindex  will give the indices of the found events
% 
%	Date        Version	Author	Remarks
%	2011/04/20  1.0     DB      Erstellung
%
% % Example 1
% % generate signal with two pulses of random noise:
% sig   = [zeros(1,200) .5-rand(1,20) zeros(1,200) .5-rand(1,20)];
% fs    = 440;
% level = .02;
% prate = 2;
% triggerindex = get_trigger(sig, level, fs, prate, 'r');
% % make it visible:
% figure;
% plot(sig); hold on
% plot(triggerindex, sig(triggerindex), 'or')
 
% catch inputs
if nargin < 4
    help get_trigger
    error('Not enough inputs!!')
elseif nargin < 5
    rf = 'r';
end

if ~strcmp(rf,'r') && ~strcmp(rf,'f')
    warning('No valid input for rising (''r'') or falling (''f'') slope detected. Searching both ways')
end

index = 1; % initialize
totz  = floor(fs*.8/prate); % let the down time be a little bit shorter than stated
% sig = detrend(sig, 'constant'); % let it be around 0 (can be helpful..)

% in case the signal stream starts in the middle of an event go on a
% little further
if abs(sig(index)) > level 
    index = index + round(2*totz/3);
end

% index for triggerindex
k = 0;

while (index < length(sig)) % loop over signal
    k = k+1;
    switch rf
        case 'r'
            temp_index = find(sig(index:end) > level, 1); % find first event for rising slope
        case 'f'
            temp_index = find(sig(index:end) < -level, 1); % find first event for falling slope
        otherwise            
            temp_index = find(abs(sig(index:end)) > level, 1); % find first event for rising/falling slope
    end
    if isempty(temp_index) % in case there is no event
        triggerindex = [];
        break;
    else
        triggerindex(k) = index + temp_index -1; %#ok<AGROW> % store index 
        index =  triggerindex(k) + totz; % increase search index
    end
end

Contact us