function mov_error_detail( fileName )
%--------------------------------------------------------------------------
% Moving Spectral Average Error Comparision Detail
%
% Cooper Baker - 2014
%--------------------------------------------------------------------------

close all;

name = 'Moving Spectral Average Error';

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

% set path
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;
overlap  = 4;

% make file names
rawFile  = sprintf( '%s.classic.mov.err.csv' , fileName );
lockFile = sprintf( '%s.lock.mov.err.csv', fileName );
peakFile = sprintf( '%s.peak.mov.err.csv', fileName );
bankFile = sprintf( '%s.bank.mov.err.csv', fileName );
solaFile = sprintf( '%s.sola.mov.err.csv', fileName );
olaFile  = sprintf( '%s.ola.mov.err.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;
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;

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

% calculate data averages
rawAvg  = sum( rawData  ) / size;
lockAvg = sum( lockData ) / size;
peakAvg = sum( peakData ) / size;
bankAvg = sum( bankData ) / size;
solaAvg = sum( solaData ) / size;
olaAvg  = sum( olaData  ) / size;

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

% find maximum average and minimum average
MaxAvg = max( [ rawAvg lockAvg peakAvg bankAvg solaAvg olaAvg ] );
MinAvg = min( [ rawAvg lockAvg peakAvg bankAvg solaAvg olaAvg ] );

% calculate cropping values
side = round( size * detail );
head = round( size * headLoc );
mid  = round( size * midLoc );
tail = round( size * 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.04;
yTxt       = MaxMax * .82;

% set up plot window
fig = figure( 1 );
set( fig, 'Name', sprintf( '%s - %s.wav', name, fileName ) );
set( fig, 'Position', [ 0 0 800 1000 ] );
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 ] );
plot( rawHead );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
legStr = sprintf( ' %s\n Avg: %2.5f \n Max: %2.5f ', algo, rawAvg, rawMax );
text( xTxt, yTxt, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );
title( 'Head', 'FontSize', fontSize );

% mid
set( subplot( 6, 3, 2 ), 'Position', [ 0.3666, 5.5/7, 0.26, 1/7.333 ] );
plot( rawMid );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTickLabel', [] );
title( 'Middle', 'FontSize', fontSize );

% tail
set( subplot( 6, 3, 3 ), 'Position', [ 0.6333, 5.5/7, 0.26, 1/7.333 ] );
plot( rawTail );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTickLabel', [] );
title( 'Tail', 'FontSize', fontSize );

% lock
%--------------------------------------------------------------------------

% head
algo = 'Lock';
set( subplot( 6, 3, 4 ), 'Position', [ 0.1, 4.5/7, 0.26, 1/7.333 ] );
plot( lockHead );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
legStr = sprintf( ' %s\n Avg: %2.5f \n Max: %2.5f ', algo, lockAvg, lockMax );
text( xTxt, yTxt, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% mid
set( subplot( 6, 3, 5 ), 'Position', [ 0.3666, 4.5/7, 0.26, 1/7.333 ] );
plot( lockMid );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTickLabel', [] );

% tail
set( subplot( 6, 3, 6 ), 'Position', [ 0.6333, 4.5/7, 0.26, 1/7.333 ] );
plot( lockTail );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTickLabel', [] );

% peak
%--------------------------------------------------------------------------

% head
algo = 'Peak';
set( subplot( 6, 3, 7 ), 'Position', [ 0.1, 3.5/7, 0.26, 1/7.333 ] );
plot( peakHead );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
legStr = sprintf( ' %s\n Avg: %2.5f \n Max: %2.5f ', algo, peakAvg, peakMax );
text( xTxt, yTxt, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% mid
set( subplot( 6, 3, 8 ), 'Position', [ 0.3666, 3.5/7, 0.26, 1/7.333 ] );
plot( peakMid );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTickLabel', [] );

% tail
set( subplot( 6, 3, 9 ), 'Position', [ 0.6333, 3.5/7, 0.26, 1/7.333 ] );
plot( peakTail );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTickLabel', [] );

% bank
%--------------------------------------------------------------------------

% head
algo = 'Bank';
set( subplot( 6, 3, 10 ), 'Position', [ 0.1, 2.5/7, 0.26, 1/7.333 ] );
plot( bankHead );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
legStr = sprintf( ' %s\n Avg: %2.5f \n Max: %2.5f ', algo, bankAvg, bankMax );
text( xTxt, yTxt, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% mid
set( subplot( 6, 3, 11 ), 'Position', [ 0.3666, 2.5/7, 0.26, 1/7.333 ] );
plot( bankMid );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTickLabel', [] );

% tail
set( subplot( 6, 3, 12 ), 'Position', [ 0.6333, 2.5/7, 0.26, 1/7.333 ] );
plot( bankTail );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTickLabel', [] );

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

% head
algo = 'Sola';
set( subplot( 6, 3, 13 ), 'Position', [ 0.1, 1.5/7, 0.26, 1/7.333 ] );
plot( solaHead );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
legStr = sprintf( ' %s\n Avg: %2.5f \n Max: %2.5f ', algo, solaAvg, solaMax );
text( xTxt, yTxt, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% mid
set( subplot( 6, 3, 14 ), 'Position', [ 0.3666, 1.5/7, 0.26, 1/7.333 ] );
plot( solaMid );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTickLabel', [] );

% tail
set( subplot( 6, 3, 15 ), 'Position', [ 0.6333, 1.5/7, 0.26, 1/7.333 ] );
plot( solaTail );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [] );
set( gca, 'YTickLabel', [] );

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

% head
algo = 'Ola';
set( subplot( 6, 3, 16 ), 'Position', [ 0.1, 0.5/7, 0.26, 1/7.333 ] );
plot( olaHead );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [ headMsec : rangeMsec / 4 : headMsec + rangeMsec ] );
legStr = sprintf( ' %s\n Avg: %2.5f \n Max: %2.5f ', algo, olaAvg, olaMax );
text( xTxt, yTxt, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );
yLab = ylabel( 'Sones' );

% mid
set( subplot( 6, 3, 17 ), 'Position', [ 0.3666, 0.5/7, 0.26, 1/7.333 ] );
plot( olaMid );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [ midMsec : rangeMsec / 4 : midMsec + rangeMsec ] );
set( gca, 'YTickLabel', [] );
xlabel( 'Milliseconds' );

% tail
set( subplot( 6, 3, 18 ), 'Position', [ 0.6333, 0.5/7, 0.26, 1/7.333 ] );
plot( olaTail );
axis( [ 0 range 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ range * 0.25 : ( range / 4 ) : range * 0.75 ] );
set( gca, 'XTickLabel', [ tailMsec : rangeMsec / 4 : tailMsec + rangeMsec ] );
set( gca, 'YTickLabel', [] );

% tighten up graph borders
tightfig();

% move y label
set( yLab, 'Position', [ range * -0.15 ( ( MaxMax * 6.25 ) / 2 ) ] );

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

% EOF