%% License Info
%A Model of Feedforward, Global, and Lateral Inhibition in the Locust Visual System.

%This model examines the architecture and function of inhibitory mechanisms in the 
%visual system of locusts, namely those involved in the processing of inputs to a
%key looming-sensitive neuron, the lobula giant movement detector (LGMD).

%Copyright (c) 2026, Erik Olson, Travis Wiens, and Jack Gray

%CITATION:
%When using the model code for scientific publications, cite the following work:
%Olson EGN, Wiens TK, Gray JR. A model of feedforward, global, and lateral inhibition 
%in the locust visual system predicts responses to looming stimuli. Biol Cybern. 2021 
%Jun;115(3):245-265. doi: 10.1007/s00422-021-00876-8. Epub 2021 May 16. PMID: 33997912.

%This program is free software: you can redistribute it and/or modify
%it under the terms of the GNU General Public License as published by
%the Free Software Foundation, either version 3 of the License, or
%(at your option) any later version.

%This program is distributed in the hope that it will be useful,
%but WITHOUT ANY WARRANTY; without even the implied warranty of
%MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%GNU General Public License for more details.

%You should have received a copy of the GNU General Public License
%along with this program.  If not, see <https://www.gnu.org/licenses/>.

%Contact: erik.olson@usask.ca

%% Code
function [f_mean,f_all,t_f] = FFIGratingTrial(parfile,smoothing,inhibition_mod)
%Testing FFI output for grating stimuli, where a sinusoidal grating appears
%at a time specified in the parameter file, and the grating stripes begin
%to move at another specified time. Inputs:
%parfile = name of the parameter file containing model parameters
%smoothing = width of the Gaussian window [ms] used to generate firing rate
%values from the spike timing histogram
%inhibition_mod = vector of three values which can override lateral 
%inhibitory gain, global inhibitory gain, and lateral inhibitory delay time
%constant, respectively.  To avoid override, set to NaN.
%Outputs:
%f_mean = population firing rate [spikes/s] summed across the modelled
%FFI neurons, averaged across trials.
%f_all = population firing rate [spikes/s]  summed across the modelled FFI 
%neurons, with each column corresponding to a single trial.
%t_f = corresponding time values [s] for "f_mean" and "f_all"

results = NaN(22002,40);
t_results = NaN(22002,40);

parfor i = 1:40
    [f,t_f] = subIterationFFIGrating(parfile,smoothing,inhibition_mod);
    results(:,i) = f;
    t_results(:,i) = t_f;
end

f_all = results;
f_mean = mean(results,2);
t_f = t_results(:,1);

end