function mov_file( idlPath, idlFile, strPath, strFile )
%--------------------------------------------------------------------------
% Moving Spectral Average of Audio File
%
% Cooper Baker - 2014
%--------------------------------------------------------------------------
close all;
% Settings
%--------------------------------------------------------------------------
winSize = 512;
overlap = 4;
win = chebwin( winSize, 125 );
name = 'Moving Spectral Average';
% Initializations
%--------------------------------------------------------------------------
if any( exist( 'idlFile' ) ~= 1 )
[ idlFile, idlPath ] = uigetfile( '*.wav', 'Ideal Audio File' );
[ strFile, strPath ] = uigetfile( '*.wav', 'Stretched Audio File' );
end
[ idlBuf, sr ] = audioread( [ idlPath, idlFile ] );
[ strBuf, sr ] = audioread( [ strPath, strFile ] );
halfWinSize = winSize / 2;
idlSize = length( idlBuf );
strSize = length( strBuf );
idlMsec = ( idlSize / sr ) * 1000;
gridMsec = 500;
gridHops = gridMsec / ( ( ( winSize / overlap ) / sr ) * 1000 );
hopSize = winSize / overlap;
hopMsec = ( hopSize / sr ) * 1000;
index = 1;
idlIndex = 1;
strIndex = 1;
winSum = sum( win );
% crop or pad stretch buffer to match ideal buffer times two
if ( idlSize * 2 ) > strSize
strBuf = [ strBuf ; zeros( ( idlSize * 2 ) - strSize, 1 ) ];
elseif strSize > ( idlSize * 2 )
strBuf = strBuf( 1 : idlSize * 2 );
end
strSize = length( strBuf );
% Normalization
%--------------------------------------------------------------------------
% get file metadata
idlInfo = audioinfo( [ idlPath, idlFile ] );
strInfo = audioinfo( [ strPath, strFile ] );
% get file normalization coefficient
idlFileNorm = str2num( idlInfo.Title );
strFileNorm = str2num( strInfo.Title );
% de-normalize normalized .wav file data
idlRaw = idlBuf / idlFileNorm;
strRaw = strBuf / strFileNorm;
% make hann windows
idlWin = hann( idlSize );
strWin = hann( strSize );
% window the files
idlNormWin = idlRaw .* idlWin;
strNormWin = strRaw .* strWin;
% calculate rms of windowed files
idlRms = rms( idlNormWin );
strRms = rms( strNormWin );
% calculate normalization scalar
strScale = 1 / ( strRms / idlRms );
% scale stretch buffer to match ideal buffer
idlNorm = idlRaw;
strNorm = strRaw * strScale;
% Analysis Loops
%--------------------------------------------------------------------------
% ideal file
hop = 1;
hopMax = length( idlBuf ) - winSize;
while hop < hopMax
% copy analysis frame from buffer
idlSamps = idlNorm( hop : hop + winSize - 1 );
% window the frame
idlWin = idlSamps .* win;
% perform fft
idlFullSpec = fft( idlWin );
% save relevant halves of spectra
idlSpec = idlFullSpec( 1 : halfWinSize );
% calculate magnitude spectra
idlMagSpec = abs( idlSpec );
% calculate spectral magnitude averages
idlAvg = sum( idlMagSpec ) / halfWinSize;
% store average values into arrays
idlMag( idlIndex ) = idlAvg;
% increment indices
hop = hop + hopSize;
idlIndex = idlIndex + 1;
end
% stretched file
hop = 1;
hopMax = length( strBuf ) - winSize;
while hop < hopMax
% copy analysis frame from buffer
strSamps = strNorm( hop : hop + winSize - 1 );
% window the frame
strWin = strSamps .* win;
% perform fft
strFullSpec = fft( strWin );
% save relevant halves of spectra
strSpec = strFullSpec( 1 : halfWinSize );
% calculate magnitude spectra
strMagSpec = abs( strSpec );
% calculate spectral magnitude averages
strAvg = sum( strMagSpec ) / halfWinSize;
% store average values into arrays
strMagLong( strIndex ) = strAvg;
% increment indices
hop = hop + hopSize;
strIndex = strIndex + 1;
end
% compress stretched array to match ideal array
while index < idlIndex;
% calculate stretched array index
strIndex = ( index * 2 - 1 );
% average adjacent pairs of values
strMag( index ) = ( strMagLong( strIndex ) + strMagLong( strIndex + 1 ) ) / 2;
%increment index
index = index + 1;
end
% calculate amplitude spectra
idlAmp = ( idlMag / winSum );
strAmp = ( strMag / winSum );
% calculate sones spectra
idlSones = idlAmp .^ 0.6;
strSones = strAmp .^ 0.6;
% calculate decibel spectra
idlDb = 20 * log10( idlAmp );
strDb = 20 * log10( strAmp );
% clip -infinity to -120 decibels
idlDb( idlDb < -120 ) = -120;
strDb( strDb < -120 ) = -120;
% copy spectra to plot arrays
idlPlot = idlSones;
strPlot = strSones;
% calculate ideal plot offset
offset = 0;
offset = abs( min( [ min( idlPlot ) min( strPlot ) ] ) );
% offset plots
idlPlot = idlPlot + offset;
strPlot = strPlot + offset;
% calculate difference between plots
difPlot = abs( idlPlot - strPlot );
% calculate mean error value
difMean = sum( difPlot ) / index;
% calculate max error value
difMax = max( difPlot );
% Plot
%--------------------------------------------------------------------------
fontName = 'Times New Roman';
fontSize = 12;
xSpc = 0.103;
ySpc = 0.64;
fig = figure( 1 );
set( fig, 'Name', name );
set( fig, 'Position', [ 0 0 800 275 ] );
set( fig, 'defaultAxesFontName', fontName );
set( fig, 'defaultTextFontName', fontName );
% curves
plot( idlPlot, 'Color', [ 0.2 0.2 0.8 ], 'LineWidth', 6 );
hold on;
plot( strPlot, 'Color', [ 0.2 0.8 0.2 ], 'LineWidth', 2 );
hold on;
plot( difPlot, 'Color', [ 0.8 0.2 0.2 ], 'LineWidth', 3 );
hold on;
% grid and labels
grid on
yMin = min( [ min( idlPlot ) min( strPlot ) min( difPlot ) ] );
yMax = max( [ max( idlPlot ) max( strPlot ) max( difPlot ) ] );
axis( [ 0 idlIndex yMin yMax ]);
set( gca, 'XTick', [ 0 : gridHops : idlIndex ] );
set( gca, 'XTickLabel', [ 0 : gridMsec : idlMsec ] );
xlabel( 'Milliseconds' );
ylabel( 'Sones' );
% legend
xPos = ( idlIndex + idlIndex * -xSpc );
yPos = yMax * ySpc;
leg = legend( 'Ideal', 'Stretch', 'Error' );
set( leg, 'Location', 'NorthEast' );
maxStr = sprintf( ' Max: %2.5f', difMax );
meanStr = sprintf( ' Avg: %2.5f', difMean );
legStr = sprintf( ' Sones Error\n%s\n%s', maxStr, meanStr );
text( xPos, yPos, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );
% tighten up figure borders
tightfig();
% Save Data to Files
%--------------------------------------------------------------------------
% get file name
[ a, fileName, b ] = fileparts( strFile );
% export graph
hgexport( fig, [ fileName, '.mov.eps' ] );
% write error data to files
csvFile = sprintf( '%s.mov.err.csv', fileName );
csvwrite( csvFile, difPlot );
% EOF
%--------------------------------------------------------------------------
% Moving Spectral Average of Audio File
%
% Cooper Baker - 2014
%--------------------------------------------------------------------------
close all;
% Settings
%--------------------------------------------------------------------------
winSize = 512;
overlap = 4;
win = chebwin( winSize, 125 );
name = 'Moving Spectral Average';
% Initializations
%--------------------------------------------------------------------------
if any( exist( 'idlFile' ) ~= 1 )
[ idlFile, idlPath ] = uigetfile( '*.wav', 'Ideal Audio File' );
[ strFile, strPath ] = uigetfile( '*.wav', 'Stretched Audio File' );
end
[ idlBuf, sr ] = audioread( [ idlPath, idlFile ] );
[ strBuf, sr ] = audioread( [ strPath, strFile ] );
halfWinSize = winSize / 2;
idlSize = length( idlBuf );
strSize = length( strBuf );
idlMsec = ( idlSize / sr ) * 1000;
gridMsec = 500;
gridHops = gridMsec / ( ( ( winSize / overlap ) / sr ) * 1000 );
hopSize = winSize / overlap;
hopMsec = ( hopSize / sr ) * 1000;
index = 1;
idlIndex = 1;
strIndex = 1;
winSum = sum( win );
% crop or pad stretch buffer to match ideal buffer times two
if ( idlSize * 2 ) > strSize
strBuf = [ strBuf ; zeros( ( idlSize * 2 ) - strSize, 1 ) ];
elseif strSize > ( idlSize * 2 )
strBuf = strBuf( 1 : idlSize * 2 );
end
strSize = length( strBuf );
% Normalization
%--------------------------------------------------------------------------
% get file metadata
idlInfo = audioinfo( [ idlPath, idlFile ] );
strInfo = audioinfo( [ strPath, strFile ] );
% get file normalization coefficient
idlFileNorm = str2num( idlInfo.Title );
strFileNorm = str2num( strInfo.Title );
% de-normalize normalized .wav file data
idlRaw = idlBuf / idlFileNorm;
strRaw = strBuf / strFileNorm;
% make hann windows
idlWin = hann( idlSize );
strWin = hann( strSize );
% window the files
idlNormWin = idlRaw .* idlWin;
strNormWin = strRaw .* strWin;
% calculate rms of windowed files
idlRms = rms( idlNormWin );
strRms = rms( strNormWin );
% calculate normalization scalar
strScale = 1 / ( strRms / idlRms );
% scale stretch buffer to match ideal buffer
idlNorm = idlRaw;
strNorm = strRaw * strScale;
% Analysis Loops
%--------------------------------------------------------------------------
% ideal file
hop = 1;
hopMax = length( idlBuf ) - winSize;
while hop < hopMax
% copy analysis frame from buffer
idlSamps = idlNorm( hop : hop + winSize - 1 );
% window the frame
idlWin = idlSamps .* win;
% perform fft
idlFullSpec = fft( idlWin );
% save relevant halves of spectra
idlSpec = idlFullSpec( 1 : halfWinSize );
% calculate magnitude spectra
idlMagSpec = abs( idlSpec );
% calculate spectral magnitude averages
idlAvg = sum( idlMagSpec ) / halfWinSize;
% store average values into arrays
idlMag( idlIndex ) = idlAvg;
% increment indices
hop = hop + hopSize;
idlIndex = idlIndex + 1;
end
% stretched file
hop = 1;
hopMax = length( strBuf ) - winSize;
while hop < hopMax
% copy analysis frame from buffer
strSamps = strNorm( hop : hop + winSize - 1 );
% window the frame
strWin = strSamps .* win;
% perform fft
strFullSpec = fft( strWin );
% save relevant halves of spectra
strSpec = strFullSpec( 1 : halfWinSize );
% calculate magnitude spectra
strMagSpec = abs( strSpec );
% calculate spectral magnitude averages
strAvg = sum( strMagSpec ) / halfWinSize;
% store average values into arrays
strMagLong( strIndex ) = strAvg;
% increment indices
hop = hop + hopSize;
strIndex = strIndex + 1;
end
% compress stretched array to match ideal array
while index < idlIndex;
% calculate stretched array index
strIndex = ( index * 2 - 1 );
% average adjacent pairs of values
strMag( index ) = ( strMagLong( strIndex ) + strMagLong( strIndex + 1 ) ) / 2;
%increment index
index = index + 1;
end
% calculate amplitude spectra
idlAmp = ( idlMag / winSum );
strAmp = ( strMag / winSum );
% calculate sones spectra
idlSones = idlAmp .^ 0.6;
strSones = strAmp .^ 0.6;
% calculate decibel spectra
idlDb = 20 * log10( idlAmp );
strDb = 20 * log10( strAmp );
% clip -infinity to -120 decibels
idlDb( idlDb < -120 ) = -120;
strDb( strDb < -120 ) = -120;
% copy spectra to plot arrays
idlPlot = idlSones;
strPlot = strSones;
% calculate ideal plot offset
offset = 0;
offset = abs( min( [ min( idlPlot ) min( strPlot ) ] ) );
% offset plots
idlPlot = idlPlot + offset;
strPlot = strPlot + offset;
% calculate difference between plots
difPlot = abs( idlPlot - strPlot );
% calculate mean error value
difMean = sum( difPlot ) / index;
% calculate max error value
difMax = max( difPlot );
% Plot
%--------------------------------------------------------------------------
fontName = 'Times New Roman';
fontSize = 12;
xSpc = 0.103;
ySpc = 0.64;
fig = figure( 1 );
set( fig, 'Name', name );
set( fig, 'Position', [ 0 0 800 275 ] );
set( fig, 'defaultAxesFontName', fontName );
set( fig, 'defaultTextFontName', fontName );
% curves
plot( idlPlot, 'Color', [ 0.2 0.2 0.8 ], 'LineWidth', 6 );
hold on;
plot( strPlot, 'Color', [ 0.2 0.8 0.2 ], 'LineWidth', 2 );
hold on;
plot( difPlot, 'Color', [ 0.8 0.2 0.2 ], 'LineWidth', 3 );
hold on;
% grid and labels
grid on
yMin = min( [ min( idlPlot ) min( strPlot ) min( difPlot ) ] );
yMax = max( [ max( idlPlot ) max( strPlot ) max( difPlot ) ] );
axis( [ 0 idlIndex yMin yMax ]);
set( gca, 'XTick', [ 0 : gridHops : idlIndex ] );
set( gca, 'XTickLabel', [ 0 : gridMsec : idlMsec ] );
xlabel( 'Milliseconds' );
ylabel( 'Sones' );
% legend
xPos = ( idlIndex + idlIndex * -xSpc );
yPos = yMax * ySpc;
leg = legend( 'Ideal', 'Stretch', 'Error' );
set( leg, 'Location', 'NorthEast' );
maxStr = sprintf( ' Max: %2.5f', difMax );
meanStr = sprintf( ' Avg: %2.5f', difMean );
legStr = sprintf( ' Sones Error\n%s\n%s', maxStr, meanStr );
text( xPos, yPos, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );
% tighten up figure borders
tightfig();
% Save Data to Files
%--------------------------------------------------------------------------
% get file name
[ a, fileName, b ] = fileparts( strFile );
% export graph
hgexport( fig, [ fileName, '.mov.eps' ] );
% write error data to files
csvFile = sprintf( '%s.mov.err.csv', fileName );
csvwrite( csvFile, difPlot );
% EOF