function dif_error_detail( fileName )
%--------------------------------------------------------------------------
% Error Spectrogram Detail Comparison
%
% Cooper Baker - 2014
%--------------------------------------------------------------------------

close all;

name = 'Error Spectrograms Detail';

% detail settings
headLoc = 0.25;
midLoc  = 0.5;
tailLoc = 0.75;
detail  = 0.01;

% set paths
p = genpath( '../../analyze' );
addpath( p );
p = genpath( '../../tones' );
addpath( p );

% get audio file info
fileInfo    = audioinfo( [ fileName, '_ideal.wav' ] );
sr          = fileInfo.SampleRate;
samps       = fileInfo.TotalSamples;
winSize     = 512;
halfWinSize = winSize / 2;
overlap     = 4;

% make file names
rawFile  = sprintf( '%s.classic.spect.csv', fileName );
lockFile = sprintf( '%s.lock.spect.csv'   , fileName );
peakFile = sprintf( '%s.peak.spect.csv'   , fileName );
bankFile = sprintf( '%s.bank.spect.csv'   , fileName );
solaFile = sprintf( '%s.sola.spect.csv'   , fileName );
olaFile  = sprintf( '%s.ola.spect.csv'    , fileName );

% load file data
rawData  = csvread( rawFile  );
lockData = csvread( lockFile );
peakData = csvread( peakFile );
bankData = csvread( bankFile );
solaData = csvread( solaFile );
olaData  = csvread( olaFile  );

% get data size
size = length( rawData );

% calculate msec grid values
msec      = ( samps / sr ) * 1000;
gridMsec  = 500;
gridHops  = gridMsec / ( ( ( winSize / overlap ) / sr ) * 1000 );
hopSize   = winSize / overlap;
hopMsec   = ( hopSize / sr ) * 1000;
% frames    = ( size / winSize ) * overlap - ( overlap - 1 );
frames = length( rawData );
rangeMsec = detail * msec;
headMsec  = ( headLoc - ( detail / 2 ) ) * msec;
midMsec   = ( midLoc  - ( detail / 2 ) ) * msec;
tailMsec  = ( tailLoc - ( detail / 2 ) ) * msec;
headMsec  = headMsec + rangeMsec * 0.25;
midMsec   = midMsec  + rangeMsec * 0.25;
tailMsec  = tailMsec + rangeMsec * 0.25;
% winSum    = sum( window );

% calculate data maximums
rawMax  = max( rawData  );
lockMax = max( lockData );
peakMax = max( peakData );
bankMax = max( bankData );
solaMax = max( solaData );
olaMax  = max( olaData  );

% find maximum maximum and minimum maximum
MaxMax = max( [ rawMax lockMax peakMax bankMax solaMax olaMax ] );
MinMax = min( [ rawMax lockMax peakMax bankMax solaMax olaMax ] );

% calculate cropping values
side = round( frames * detail );
head = round( frames * headLoc );
mid  = round( frames * midLoc );
tail = round( frames * tailLoc );
range = side * 2;

% copy detail sections of data
rawHead  = rawData ( :, ( head - side ) : ( head + side ) );
rawMid   = rawData ( :, ( mid  - side ) : ( mid  + side ) );
rawTail  = rawData ( :, ( tail - side ) : ( tail + side ) );
lockHead = lockData( :, ( head - side ) : ( head + side ) );
lockMid  = lockData( :, ( mid  - side ) : ( mid  + side ) );
lockTail = lockData( :, ( tail - side ) : ( tail + side ) );
peakHead = peakData( :, ( head - side ) : ( head + side ) );
peakMid  = peakData( :, ( mid  - side ) : ( mid  + side ) );
peakTail = peakData( :, ( tail - side ) : ( tail + side ) );
bankHead = bankData( :, ( head - side ) : ( head + side ) );
bankMid  = bankData( :, ( mid  - side ) : ( mid  + side ) );
bankTail = bankData( :, ( tail - side ) : ( tail + side ) );
solaHead = solaData( :, ( head - side ) : ( head + side ) );
solaMid  = solaData( :, ( mid  - side ) : ( mid  + side ) );
solaTail = solaData( :, ( tail - side ) : ( tail + side ) );
olaHead  = olaData ( :, ( head - side ) : ( head + side ) );
olaMid   = olaData ( :, ( mid  - side ) : ( mid  + side ) );
olaTail  = olaData ( :, ( tail - side ) : ( tail + side ) );

% graph formatting
fontName   = 'Times New Roman';
fontSize   = 12;
xTxt       = range * 0.0666;
yTxt       = halfWinSize * 0.9;

% color mapping
colormap( [ [ 1 : -0.2/64 : 0.8 ]; ( [ 1 : -0.5/64 : 0.5 ] .^ 10 ) ; [ 1 : -0.5/64 : 0.5 ] .^ 20 ]' );

% set up plot window
fig = figure( 1 );
set( fig, 'Name', sprintf( '%s - %s.wav', name, fileName ) );
set( fig, 'Position', [ 0 0 800 1050 ] );
set( fig, 'defaultAxesFontName', fontName );
set( fig, 'defaultTextFontName', fontName );
set( fig, 'defaultlinelinewidth', 3 );
set( fig, 'defaultaxescolororder', [ 0.8 0.2 0.2 ] );

% classic
%--------------------------------------------------------------------------

% head
algo = 'Classic';
set( subplot( 6, 3, 1 ), 'Position', [ 0.1, 5.5/7, 0.26, 1/7.333 ] );
imagesc( rawHead );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [ 0 : 2000 : ( sr / 2 ) ] );
text( xTxt, yTxt, algo, 'EdgeColor', 'k', 'BackgroundColor', 'w' );
title( 'Head', 'FontSize', fontSize );

%middle
set( subplot( 6, 3, 2 ), 'Position', [ 0.3666, 5.5/7, 0.26, 1/7.333 ] );
imagesc( rawMid );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [] );
title( 'Middle', 'FontSize', fontSize );

%tail
set( subplot( 6, 3, 3 ), 'Position', [ 0.6333, 5.5/7, 0.26, 1/7.333 ] );
imagesc( rawTail );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [] );
bar = colorbar( 'location', 'East' );
set( get( bar, 'YLabel'), 'String', 'Sones' );
set( get( bar, 'YLabel'), 'Position', [ 0.75 MaxMax * 0.15 ] );
title( 'Tail', 'FontSize', fontSize );

% phase locked
%--------------------------------------------------------------------------

% head
algo = 'Lock';
set( subplot( 6, 3, 4 ), 'Position', [ 0.1, 4.5/7, 0.26, 1/7.333 ] );
imagesc( lockHead );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [ 0 : 2000 : ( sr / 2 ) ] );
text( xTxt, yTxt, algo, 'EdgeColor', 'k', 'BackgroundColor', 'w' );

%middle
set( subplot( 6, 3, 5 ), 'Position', [ 0.3666, 4.5/7, 0.26, 1/7.333 ] );
imagesc( lockMid );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [] );

%tail
set( subplot( 6, 3, 6 ), 'Position', [ 0.6333, 4.5/7, 0.26, 1/7.333 ] );
imagesc( lockTail );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [] );
bar = colorbar( 'location', 'East' );
set( get( bar, 'YLabel'), 'String', 'Sones' );
set( get( bar, 'YLabel'), 'Position', [ 0.75 MaxMax * 0.15 ] );

% peak tracking
%--------------------------------------------------------------------------

% head
algo = 'Peak';
set( subplot( 6, 3, 7 ), 'Position', [ 0.1, 3.5/7, 0.26, 1/7.333 ] );
imagesc( peakHead );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [ 0 : 2000 : ( sr / 2 ) ] );
text( xTxt, yTxt, algo, 'EdgeColor', 'k', 'BackgroundColor', 'w'  );

%middle
set( subplot( 6, 3, 8 ), 'Position', [ 0.3666, 3.5/7, 0.26, 1/7.333 ] );
imagesc( peakMid );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [] );

%tail
set( subplot( 6, 3, 9 ), 'Position', [ 0.6333, 3.5/7, 0.26, 1/7.333 ] );
imagesc( peakTail );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [] );
bar = colorbar( 'location', 'East' );
set( get( bar, 'YLabel'), 'String', 'Sones' );
set( get( bar, 'YLabel'), 'Position', [ 0.75 MaxMax * 0.15 ] );

% oscillator bank
%--------------------------------------------------------------------------

% head
algo = 'Bank';
set( subplot( 6, 3, 10 ), 'Position', [ 0.1, 2.5/7, 0.26, 1/7.333 ] );
imagesc( bankHead );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [ 0 : 2000 : ( sr / 2 ) ] );
text( xTxt, yTxt, algo, 'EdgeColor', 'k', 'BackgroundColor', 'w'  );

%middle
set( subplot( 6, 3, 11 ), 'Position', [ 0.3666, 2.5/7, 0.26, 1/7.333 ] );
imagesc( bankMid );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [] );

%tail
set( subplot( 6, 3, 12 ), 'Position', [ 0.6333, 2.5/7, 0.26, 1/7.333 ] );
imagesc( bankTail );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [] );
bar = colorbar( 'location', 'East' );
set( get( bar, 'YLabel'), 'String', 'Sones' );
set( get( bar, 'YLabel'), 'Position', [ 0.75 MaxMax * 0.15 ] );

% sola
%--------------------------------------------------------------------------

% head
algo = 'Sola';
set( subplot( 6, 3, 13 ), 'Position', [ 0.1, 1.5/7, 0.26, 1/7.333 ] );
imagesc( solaHead );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [ 0 : 2000 : ( sr / 2 ) ] );
text( xTxt, yTxt, algo, 'EdgeColor', 'k', 'BackgroundColor', 'w'  );

%middle
set( subplot( 6, 3, 14 ), 'Position', [ 0.3666, 1.5/7, 0.26, 1/7.333 ] );
imagesc( solaMid );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [] );

%tail
set( subplot( 6, 3, 15 ), 'Position', [ 0.6333, 1.5/7, 0.26, 1/7.333 ] );
imagesc( solaTail );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [] );
bar = colorbar( 'location', 'East' );
set( get( bar, 'YLabel'), 'String', 'Sones' );
set( get( bar, 'YLabel'), 'Position', [ 0.75 MaxMax * 0.15 ] );

% ola
%--------------------------------------------------------------------------

% head
algo = 'Ola';
set( subplot( 6, 3, 16 ), 'Position', [ 0.1, 0.5/7, 0.26, 1/7.333 ] );
imagesc( olaHead );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [ headMsec : rangeMsec / 4 : headMsec + rangeMsec ] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [ 0 : 2000 : ( sr / 2 ) ] );
text( xTxt, yTxt, algo, 'EdgeColor', 'k', 'BackgroundColor', 'w'  );
yLab = ylabel( 'Frequency' );

%middle
set( subplot( 6, 3, 17 ), 'Position', [ 0.3666, 0.5/7, 0.26, 1/7.333 ] );
imagesc( olaMid );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [ midMsec : rangeMsec / 4 : midMsec + rangeMsec ] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [] );
xlabel( 'Milliseconds' );

%tail
set( subplot( 6, 3, 18 ), 'Position', [ 0.6333, 0.5/7, 0.26, 1/7.333 ] );
imagesc( olaTail );
axis xy;
caxis( [ 0 MaxMax ] );
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [ tailMsec : rangeMsec / 4 : tailMsec + rangeMsec ] );
set( gca, 'YTick', [ 1 : ( halfWinSize / ( sr / 4000 ) ) : halfWinSize ] );
set( gca, 'YTickLabel', [] );
bar = colorbar( 'location', 'East' );
set( get( bar, 'YLabel'), 'String', 'Sones' );
set( get( bar, 'YLabel'), 'Position', [ 0.75 MaxMax * 0.15 ] );

% tighten up borders
tightfig();

% move y label to middle of graphs
set( yLab, 'Position', [ (-range / 6 ) ( ( halfWinSize * 6.25 ) / 2 ) ] );

% write plot to file
hgexport( fig, [ fileName, '.err.spect.zoom.eps' ] );

% EOF