function avg_error( fileName )
%--------------------------------------------------------------------------
% Average Spectrum Error Comparison
%
% Cooper Baker - 2014
%--------------------------------------------------------------------------

close all;

name = 'Average Spectrum Error';

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

% get sample rate
fileInfo = audioinfo( [ fileName, '.classic.wav' ] );
sr = fileInfo.SampleRate;

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

% graph formatting
fontName   = 'Times New Roman';
fontSize   = 12;
xTxt       = size * .99;
yTxt       = MaxMax * .9;

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

% plot fft ifft raw
title = 'Classic';
set( subplot( 1, 1, 1 ), 'Position', [ 0.1, 5.5/7, 0.8, 1/7.333 ] );
plot( rawData );
axis( [ 0 size 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ 0 : ( size / ( sr / 4000 ) ) : size ] );
set( gca, 'XTickLabel', [] );
temp = get( gca );
yticks = get( gca, 'YTick' );
set( gca, 'YTickLabel', yticks );
legStr = sprintf( ' %s - Avg: %2.5f Max: %2.5f ', title, rawAvg, rawMax );
text( xTxt, yTxt, legStr, 'HorizontalAlignment', 'right', 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% plot fft ifft phase locked
title = 'Lock';
set( subplot( 6, 1, 2 ), 'Position', [ 0.1, 4.5/7, 0.8, 1/7.333 ] );
plot( lockData );
axis( [ 0 size 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ 0 : ( size / ( sr / 4000 ) ) : size ] );
set( gca, 'XTickLabel', [] );
temp = get( gca );
yticks = get( gca, 'YTick' );
set( gca, 'YTickLabel', yticks );
legStr = sprintf( ' %s - Avg: %2.5f Max: %2.5f ', title, lockAvg, lockMax );
text( xTxt, yTxt, legStr, 'HorizontalAlignment', 'right', 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% plot fft ifft peak tracking
title = 'Peak';
set( subplot( 6, 1, 3 ), 'Position', [ 0.1, 3.5/7, 0.8, 1/7.333 ] );
plot( peakData );
axis( [ 0 size 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ 0 : ( size / ( sr / 4000 ) ) : size ] );
set( gca, 'XTickLabel', [] );
temp = get( gca );
yticks = get( gca, 'YTick' );
set( gca, 'YTickLabel', yticks );
legStr = sprintf( ' %s - Avg: %2.5f Max: %2.5f ', title, peakAvg, peakMax );
text( xTxt, yTxt, legStr, 'HorizontalAlignment', 'right', 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% plot fft oscillator bank
title = 'Bank';
set( subplot( 6, 1, 4 ), 'Position', [ 0.1, 2.5/7, 0.8, 1/7.333 ] );
plot( bankData );
axis( [ 0 size 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ 0 : ( size / ( sr / 4000 ) ) : size ] );
set( gca, 'XTickLabel', [] );
temp = get( gca );
yticks = get( gca, 'YTick' );
set( gca, 'YTickLabel', yticks );
legStr = sprintf( ' %s - Avg: %2.5f Max: %2.5f ', title, bankAvg, bankMax );
text( xTxt, yTxt, legStr, 'HorizontalAlignment', 'right', 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% plot synchronous overlap add
title = 'Sola';
set( subplot( 6, 1, 5 ), 'Position', [ 0.1, 1.5/7, 0.8, 1/7.333 ] );
plot( solaData );
axis( [ 0 size 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ 0 : ( size / ( sr / 4000 ) ) : size ] );
set( gca, 'XTickLabel', [] );
temp = get( gca );
yticks = get( gca, 'YTick' );
set( gca, 'YTickLabel', yticks );
legStr = sprintf( ' %s - Avg: %2.5f Max: %2.5f ', title, solaAvg, solaMax );
text( xTxt, yTxt, legStr, 'HorizontalAlignment', 'right', 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% plot overlap add
title = 'Ola';
set( subplot( 6, 1, 6 ), 'Position', [ 0.1, 0.5/7, 0.8, 1/7.333 ] );
plot( olaData );
axis( [ 0 size 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ 0 : ( size / ( sr / 4000 ) ) : size ] );
set( gca, 'XTickLabel', [ 0 : 2000 : ( sr / 2 ) ] );
temp = get( gca );
yticks = get( gca, 'YTick' );
set( gca, 'YTickLabel', yticks );
xlabel( 'Frequency' );
ylabel( 'Sones' );
legStr = sprintf( ' %s - Avg: %2.5f Max: %2.5f ', title, olaAvg, olaMax );
text( xTxt, yTxt, legStr, 'HorizontalAlignment', 'right', 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% tighten up borders
tightfig();

% move y label to middle of graphs
ylabel( 'Sones', 'Position', [ -size / 23 ( ( MaxMax * 6.1666 ) / 2 ) ] );

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

% EOF