changeset 3:1f7b986dab05

Added autoalign.m: aligns audio by zero-padding before the signal to align the peak of the crosscorrelation function
author Brecht De Man <b.deman@qmul.ac.uk>
date Mon, 17 Nov 2014 23:01:09 +0000
parents 5e72201496c8
children b28ffd29e6e1
files aux/autoalign.m aux/autoaligntest.m
diffstat 2 files changed, 52 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/aux/autoalign.m	Mon Nov 17 19:43:43 2014 +0000
+++ b/aux/autoalign.m	Mon Nov 17 23:01:09 2014 +0000
@@ -1,13 +1,13 @@
-function autoalign(folderName, masterIndex)
-% AUTOALIGN aligns audio in folder with selected file (index 'masterIndex')
-% by inserting zeros at the start of the other files. 
+function autoalign(foldername)
+% AUTOALIGN aligns audio in folder with selected file by inserting zeros
+%  at the start of the files. 
 %
 % by Brecht De Man at Centre for Digital Music on 17 November 2014
 
-    error('Work in progress, function not finished');
+    % error('Work in progress, function not finished'); % while unfinished
     
     tic;                                % start measuring time
-    list = dir([foldername '\*.wav']);  % get names of files
+    list = dir([foldername '/*.wav']);  % get names of files
     
     % remove hidden files from list
     % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220
@@ -17,32 +17,54 @@
             list(k) = [ ];
         end
     end
-
-    % load master file
-    masterAudioChannels = audioread([foldername '/' list(masterIndex).name]); 
+    
+    shiftvec = zeros(length(list), 1); 
+    
+    % first file: 
+    masterAudioChannels = audioread([foldername '/' list(1).name]); % read file
     masterAudio         = sum(masterAudioChannels, 2); % sum to mono
-    disp(['Aligning audio files with ' list(masterIndex).name '...']);
     
     % for each other file:
     for i = 1:length(list)
-        
         slaveAudioChannels = audioread([foldername '/' list(i).name]); % read file
         slaveAudio         = sum(slaveAudioChannels, 2); % sum to mono
         
-        % check sampling rate is the same
+        % check sampling rate is the same (or: express in terms of seconds)
         % ... 
-
+        
         % crosscorrelation function
-        xcorr(masterAudio, slaveAudio); 
+        [xc, timeaxis] = xcorr(masterAudio, slaveAudio);
+        [value, lag] = max(abs(xc));
+        shiftvec(i) = timeaxis(lag);
         
-        % add zeros to beginning 
+        % debugging plots
+%         figure;
+%         title(['Crosscorrelation file 1 vs ' num2str(i)]);
+%         subplot(2,1,1);
+%         plot(timeaxis, xc);
+%         xlabel('Samples');
+%         ylabel('Crosscorrelation');
+%         subplot(2,1,2);
+%         plot(timeaxis, abs(xc));
+%         xlabel('Samples');
+%         ylabel('Absolute value of crosscorrelation');
         
         % monitoring/debugging
         
     end
+    
+    % add absolute of largest negative lag worth of zeros to all; 
+    % then add 'shiftvec' value to that
+    maxlag = min(shiftvec); 
+    for i = 1:length(list)
+        [audio, fs] = audioread([foldername '/' list(i).name]);
+        audiowrite([foldername '/SHIFT' list(i).name], ... % remove SHIFT
+            [zeros(-maxlag + shiftvec(i), size(audio, 2)) ; audio], ...
+            fs, 'BitsPerSample', 24);
+    end
 
     elapsedTime = toc;  % stop measuring time
-    disp(['autoalign took ' elapsedTime ' seconds to align the files in folder ' ...
-        folderName ' with master file ' list(masterIndex).name]);
+    disp(['autoalign took ' num2str(elapsedTime) ...
+        ' seconds to align the files in folder ' foldername]);
 
 end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aux/autoaligntest.m	Mon Nov 17 23:01:09 2014 +0000
@@ -0,0 +1,14 @@
+% autoalign test
+% generate files to test autoalign.m 
+
+[fragment,fs] = audioread('../../AUDIO/SR1/pro.wav', [10*96000, 20*96000]); % from 0:10 to 0:20
+
+% files with increasing leading zeros - end result should be all same file
+N = 5; 
+spacing = 45678; % samples
+
+for i = 1:N
+    audiowrite(['autoaligntest' num2str(i) '.wav'], ...         % file name
+        [zeros(i*spacing, size(fragment, 2)); fragment], ...    % audio
+        fs, 'BitsPerSample', 24);                               % format
+end
\ No newline at end of file