% -------------------------------------------------------------------------------------------------
% (c) 2025 Leon F. Heffer, David J. Sly, James B. Fallon, Rick Minter, Rebecca C. Felsheim, 
% Mathias Dietz and Stephen J. O’Leary.
% 
% This file is part of a repository that contains the data originally recorded by Heffer et
% al. (2010). Additionally, code to extract spike times from the voltage traces is provided.
% 
% This code is free software: you can redistribute it and/or modify it under the terms of the 
% CC BY 4.0. To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0/ 
% 
% The code 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. 
% 
% Please cite 
% Heffer L, Sly DJ, Fallon JB, Minter R, Felsheim RC, Dietz M, O’Leary SJ. Recordings of 188 electrically
% stimulated auditory nerve fibers of guinea pigs. Zenodo. 2025. doi: 10.5281/zenodo.15827116
% -------------------------------------------------------------------------------------------------
%
% [voltage_trace,pattern_start, pattern_stop] = remove_artifacts(voltage_trace, num_segments, parameters)
% this function detects the bi-phasic pulse artifacts from the electrical stimulation and removes
% the artifacts by interpolating linearly between the start and end points
% Parameters:
%   - voltage_trace:    the recorded voltage traces    
%   - num_segments:     the number of stimuli that should be detected in this signal
%   - parameters:       parameter struct, the only relevant parameter here is the offset value (in
%                       samples applied to the end point of a detected artifact
% Returns:
%   - voltage_trace:        the voltage_traces without the artifacts 
%   - pattern_start:        the start points of the artifacts
%   - pattern_stop:         the end points of the artifacts
%   - detected_pulse_rate:  the stimulating pulse rate as detected from the artifacts

function [voltage_trace,pattern_start, pattern_stop, detected_pulse_rate] = ...
                                remove_artifacts(voltage_trace, num_segments, parameters)
 

    % a pulse template was extracted manually and is used to detect the stimulating pulses in the
    % voltage traces
    load("AN_Data/extract_spike_times/templates.mat", "pattern_template")

    % find the patterns in the signal
    [pattern_start, pattern_stop] = findsignal(voltage_trace, pattern_template, 'Normalization', 'center', ...
                                         'MaxNumSegments', num_segments);

    % interpolate linearly between the start and the end of the found patterns
    for ind = 1:length(pattern_start)
        pattern_stop(ind) = min(length(voltage_trace), pattern_stop(ind) + parameters.artifact_offset);
        snippet = interp1([pattern_start(ind),pattern_stop(ind)],...
                          [voltage_trace(pattern_start(ind)), voltage_trace(pattern_stop(ind))], ...
                          pattern_start(ind):pattern_stop(ind));

        voltage_trace(pattern_start(ind):pattern_stop(ind)) = snippet;

    end

    d = diff(sort(pattern_start));
    detected_pulse_rate = 1/ (median(d) * 1e-5);

end

