%% 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,t_f,raw,t_raw,V_trace] = subIterationFFI(parFile,smoothing,l_over_v,h_offset,v_offset,inhibition_mod,cut)
%This function performs a single looming stimulus trial at a specified
%offset from the eye centerline along axes of the ommatidial arrangement
%which are 60 degrees apart (as per the hexagonal arrangment of ommatidia).

%load model parameters into the workspace
load(parFile);

%Overwrite the object size based on supplied l/|v|
l = l_over_v*v;

%Overwrite inhibition values if needed
if not(isnan(inhibition_mod(1)))
    K_lat_m = inhibition_mod(1);
end

if not(isnan(inhibition_mod(2)))
    K_gl_m = inhibition_mod(2);
end

options = simset('srcworkspace','current','ReturnWorkspaceOutputs','on');

results = sim("FFILoomingModel.slx",T_max,options);
t_raw = results.yout{1}.Values.Time; %Access time data
raw = results.yout{1}.Values.Data; %Access FFI output data
V_trace = results.yout{2}.Values.Data;%Access voltage traces

Simulink.sdi.clear;

t_raw = t_raw-t_raw(end);
start_element = find(t_raw>=-1*cut,1);
t_raw = t_raw(start_element:end);
t_raw = t_raw-t_raw(1);

raw = raw(start_element:end);
V_trace = V_trace(start_element:end,:);

[f,hist,t_f] = histoFiringRate(raw,t_raw,0.001,smoothing);
fprintf('sub-trial complete with offsets %.4f and %.4f \n',h_offset,v_offset);
end