function [  ] = pitch_plots( )
%PITCH_PLOTS
% Plots praat pitch track (imported into Matlab using code by Christina Bjorndahl) and Voicebox pitch track side by side for comparison. Also includes waveform and phone segmentation

clear all; close all;

% Import data
src = '/Users/acasa/some/';
disp(['Loading some.mat from ', src]);
load([src,'some.mat'],'some');
disp(['Loaded some.mat from ', src]);

for i=1:length(some)
    
    dat = some(i);

    % Cut measurements for plotting only region of interest
    wav_dur = length(dat.wav)/dat.fs;
    if dat.ws == 1
        starts = 1/dat.fs;
        context = '';
    elseif dat.ws == 2
        starts = 1/dat.fs; context = dat.word(dat.ws-1).text;
    elseif dat.ws == 3
        starts = 1/dat.fs; context = [dat.word(dat.ws-2).text, ' ', dat.word(dat.ws-1).text];
    else
        starts = dat.word(dat.ws - 2).xmin; context = [dat.word(dat.ws-2).text, ' ', dat.word(dat.ws-1).text];
    end
    ends = dat.word(dat.we).xmax + 0.05; if ends > wav_dur, ends = wav_dur; end;
    
    % wav file to region of interest
    wav_cut = dat.wav(round(starts*dat.fs):round(ends*dat.fs));
    
    % find frames that include "some X" +/- 50msec
    indv = dat.vb_pitch.t(:,1) > starts & dat.vb_pitch.t(:,2) < ends;   % voicebox
    indp = dat.praat_pitch.t(:,1) > starts & dat.praat_pitch.t(:,2) < ends; % praat
    
    % create x axis for plotting f0
    xtv = mean(dat.vb_pitch.t(indv,1:2),2); % x axis - time series for voicebox F0
    xtp = mean(dat.praat_pitch.t(indp,1:2),2); % x axis - time series for Praat F0
    
    % Plot wav file
    twav_cut = linspace(starts,ends, length(wav_cut));
    h = figure; set(h, 'Units','inches','position', [1 1 15 10], 'PaperPositionMode','auto');
    
    ax(1) = subplot(3,1,1); plot(twav_cut,wav_cut);
    ypos = ylim;
    
    for j = dat.ps : dat.pe
        phone_start = dat.phon(j).xmin;
        line([phone_start phone_start],get(ax(1),'YLim'),'Color','m');
        text(phone_start+0.001, ypos(2),['\color{magenta}', dat.phon(j).text],'FontSize',16,'VerticalAlignment','top','FontWeight','bold');
    end
    
    title([int2str(dat.ind), '   ', lower(context), ' some ', dat.N], 'Fontsize', 18);
    
    ax(2) = subplot(3,1,2); plot(xtp,dat.praat_pitch.f0(indp),'o-');
    line([dat.phon(dat.ps+1).xmin dat.phon(dat.ps+1).xmin],get(ax(2),'YLim'),'Color','m');
    line([dat.phon(dat.ps+3).xmin dat.phon(dat.ps+3).xmin],get(ax(2),'YLim'),'Color','m'); % FAIL; if SP intervenes, then this is not where the boundary is
    if strcmpi(dat.phon(dat.ps+4).text, 'AH1') | strcmpi(dat.phon(dat.ps+4).text, 'IY1')
        line([dat.phon(dat.ps+4).xmax dat.phon(dat.ps+4).xmax],get(ax(2),'YLim'),'Color','m');
    else
        line([dat.phon(dat.ps+4).xmin dat.phon(dat.ps+4).xmin],get(ax(2),'YLim'),'Color','m');
        line([dat.phon(dat.ps+5).xmax dat.phon(dat.ps+5).xmax],get(ax(2),'YLim'),'Color','m');
    end
        
    title('Praat pitch tracker, range 75-400, smooth 10.')
    
    ax(3) = subplot(3,1,3); plot(xtv,dat.vb_pitch.f0(indv),'o-');
    title('Voicebox pitch tracker; smoothed, interpolated, outliers removed.');
    linkaxes([ax(1), ax(2), ax(3)],'x');
    linkaxes([ax(2),ax(3)], 'y');
    
    % Prep for exporting to file
    formatOut = 'mm.dd HHMM';
    timestmp = datestr(now,formatOut);
    
    if strcmpi(dat.F, 'focused')
        filename = ['F ', timestmp, ' - ', int2str(dat.ind), ' - Praat 75-400, Voicebox + context, segmentation.eps'];
    else
        filename = ['D ', timestmp, ' - ', int2str(dat.ind), ' - Praat, Voicebox + context, segmentation.eps'];
    end
    
    print(h, '-depsc', filename);
    
    close(h);
    
    
end

end

