function [xPhonon] = FindPhonons(Length, Phonon, x)
% [xPhonon] = FindPhonons(Length, Phonon, x)
%
% Description:
% This function will determine the location of the phononsa along the x-axis
% given the lengths of the potential segments and the segment the phonon is
% on.
%
% Outputs:
% xPhonon - Spatial array corresponding to input array 'x' with a '1' at
%           the phonon location.
%
% Inputs:
% Length - Array containing the location of each potential segment
% Phonon - Array containing the phonon location. A '1' in any entry means
%          that the phonon is located on the left hand side of that
%          potential segment. A '2' located in the last entry means the
%          phonon is located on the left hand side of the last segment.  An
%          empty array means that there is on phonon.
% x      - Spatial array containing the x-axis

if sum(Phonon) == 0
    % If no phonon, return empty array
    xPhonon = zeros(1,length(x));
    
else
    if Phonon(end) == 2
    % Put last phonon on end of Phonon
        Phonon(end) = 0
        Phonon = [Phonon 1];
    end
    
    % Must find phonon
    xPhonon = zeros(1,length(x));

    % Find phonon locations
    PhononLocations = Length(logical(Phonon));
    
    for PhononIndex = 1:length(PhononLocations)
        [z,PhononLocIndex] = min(abs(x - PhononLocations(PhononIndex)));
        
        % Adjust phonon so that potential change occur between PhononLocIndex and PhononLocIndex  1
        if x(PhononLocIndex) > Length(logical(Phonon))
            PhononLocIndex = PhononLocIndex - 1;
        end
    
        xPhonon(PhononLocIndex) = 1;
    end
end
end