function s1gWavGenPlotFieldOverlay(cfg,hf,varargin)
%s1gWavGenPlotFieldOverlay Featured example helper function
%
%   Overlays field names on a plot

%   Copyright 2016-2017 The MathWorks, Inc.

narginchk(2,3);

if nargin>2
    offset = varargin{1};
else
    offset = 0;
end

Nltf = wlan.internal.numVHTLTFSymbols(sum(cfg.NumSpaceTimeStreams));

figure(hf);
ax = gca;
hold(ax,'on');

switch packetFormat(cfg)
    case 'S1G-Short'

        Tstf = 80;  % Duration in microseconds
        Tltf1 = 80; % Duration in microseconds
        Tltf = 40;  % Duration in microseconds
        Tsig = 80;  % Duration in microseconds
        Tdata = 40; % Duration in microseconds

        overlayInfo();

    case 'S1G-1M'

        Tstf = 160;  % Duration in microseconds
        Tltf1 = 160; % Duration in microseconds
        Tltf = 40;   % Duration in microseconds
        Tsig = 240;  % Duration in microseconds
        Tdata = 40;  % Duration in microseconds

        overlayInfo();

    otherwise % 'S1G-Long'

        Tstf = 80;  % Duration in microseconds
        Tltf1 = 80; % Duration in microseconds
        Tltf = 40;  % Duration in microseconds
        Tsig = 80;  % Duration in microseconds
        Tdstf = 40; % Duration in microseconds
        Tsigb = 40; % Duration in microseconds
        Tdata = 40; % Duration in microseconds

        overlayInfo(); 
end

function overlayInfo()

    xlimits = xlim(ax);

    if offset<Tstf
        % STF
        text(ax,Tstf/2,10,'STF');
        if Tstf>=xlimits(2)
            return
        end
    end
    Tcum = Tstf;
    if offset<(Tstf+Tltf1)
        % LTF1
        plot(ax,[Tcum Tcum],ylim(gca),'k--');
        text(ax,Tcum+Tltf1/2,10,'LTF1');
        if (Tcum+Tltf1)>=xlimits(2)
            return
        end
    end
    Tcum = Tcum+Tltf1;
    if offset<(Tcum+Tsig)
        % SIG/SIG-A
        plot(ax,[Tcum Tcum],ylim(gca),'k--');
        if strcmp(packetFormat(cfg),'S1G-Long')
            text(ax,Tcum+Tsig/2,10,'SIG-A');
        else
            text(ax,Tcum+Tsig/2,10,'SIG');
        end
        if (Tcum+Tsig)>=xlimits(2)
            return
        end
    end
    Tcum = Tcum+Tsig;
    plot(ax,[Tcum Tcum],ylim(gca),'k--')
    if strcmp(packetFormat(cfg),'S1G-Long')
        % D-STF
        if offset<(Tcum+Tdstf)
            text(ax,(Tcum+Tdstf/4),10,'D-STF','FontSize',10);
            plot(ax,[(Tcum+Tdstf) (Tcum+Tdstf)],ylim(gca),'k--')
        end
        Tcum = Tcum+Tdstf;
        % D-LTF
        for i = 1:Nltf
            if (Tcum+i*Tltf)>=xlimits(2)
                return
            end
            text(ax,(Tcum+(i-1)*Tltf+Tltf/4),10,['D-LTF' num2str(i)],'FontSize',10);
            plot(ax,[(Tcum+i*Tltf) (Tcum+i*Tltf)],ylim(gca),'k--')
        end
        Tcum = Tcum+Nltf*Tltf;
        % SIG-B
        text(ax,(Tcum+Tsigb/4),10,'SIG-B');
        plot(ax,[(Tcum+Tsigb) (Tcum+Tsigb)],ylim(gca),'k--')
        if (Tcum+Tsigb)>=xlimits(2)
            return
        end
        Tcum = Tcum+Tsigb;
        % Data
        text(ax,(Tcum+Tdata/2),10,'Data');
    else
        % LTF2-N
        for i = 1:(Nltf-1)
            if (Tcum+i*Tltf)>=xlimits(2)
                return
            end
            text(ax,(Tcum+(i-1)*Tltf+Tltf/4),10,['LTF' num2str(i+1)],'FontSize',10);
            plot(ax,[(Tcum+i*Tltf) (Tcum+i*Tltf)],ylim(gca),'k--')
        end
        Tcum = Tcum+(Nltf-1)*Tltf;
        % Data
        text(ax,(Tcum+Tdata),10,'Data');
    end
end
hold(ax,'off');

end