comparison aux/autoalign.m @ 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 ed0a8621b96a
comparison
equal deleted inserted replaced
2:5e72201496c8 3:1f7b986dab05
1 function autoalign(folderName, masterIndex) 1 function autoalign(foldername)
2 % AUTOALIGN aligns audio in folder with selected file (index 'masterIndex') 2 % AUTOALIGN aligns audio in folder with selected file by inserting zeros
3 % by inserting zeros at the start of the other files. 3 % at the start of the files.
4 % 4 %
5 % by Brecht De Man at Centre for Digital Music on 17 November 2014 5 % by Brecht De Man at Centre for Digital Music on 17 November 2014
6 6
7 error('Work in progress, function not finished'); 7 % error('Work in progress, function not finished'); % while unfinished
8 8
9 tic; % start measuring time 9 tic; % start measuring time
10 list = dir([foldername '\*.wav']); % get names of files 10 list = dir([foldername '/*.wav']); % get names of files
11 11
12 % remove hidden files from list 12 % remove hidden files from list
13 % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220 13 % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220
14 for k = length(list):-1:1 14 for k = length(list):-1:1
15 fname = list(k).name; 15 fname = list(k).name;
16 if fname(1) == '.' 16 if fname(1) == '.'
17 list(k) = [ ]; 17 list(k) = [ ];
18 end 18 end
19 end 19 end
20 20
21 % load master file 21 shiftvec = zeros(length(list), 1);
22 masterAudioChannels = audioread([foldername '/' list(masterIndex).name]); 22
23 % first file:
24 masterAudioChannels = audioread([foldername '/' list(1).name]); % read file
23 masterAudio = sum(masterAudioChannels, 2); % sum to mono 25 masterAudio = sum(masterAudioChannels, 2); % sum to mono
24 disp(['Aligning audio files with ' list(masterIndex).name '...']);
25 26
26 % for each other file: 27 % for each other file:
27 for i = 1:length(list) 28 for i = 1:length(list)
28
29 slaveAudioChannels = audioread([foldername '/' list(i).name]); % read file 29 slaveAudioChannels = audioread([foldername '/' list(i).name]); % read file
30 slaveAudio = sum(slaveAudioChannels, 2); % sum to mono 30 slaveAudio = sum(slaveAudioChannels, 2); % sum to mono
31 31
32 % check sampling rate is the same 32 % check sampling rate is the same (or: express in terms of seconds)
33 % ... 33 % ...
34 34
35 % crosscorrelation function 35 % crosscorrelation function
36 xcorr(masterAudio, slaveAudio); 36 [xc, timeaxis] = xcorr(masterAudio, slaveAudio);
37 [value, lag] = max(abs(xc));
38 shiftvec(i) = timeaxis(lag);
37 39
38 % add zeros to beginning 40 % debugging plots
41 % figure;
42 % title(['Crosscorrelation file 1 vs ' num2str(i)]);
43 % subplot(2,1,1);
44 % plot(timeaxis, xc);
45 % xlabel('Samples');
46 % ylabel('Crosscorrelation');
47 % subplot(2,1,2);
48 % plot(timeaxis, abs(xc));
49 % xlabel('Samples');
50 % ylabel('Absolute value of crosscorrelation');
39 51
40 % monitoring/debugging 52 % monitoring/debugging
41 53
42 end 54 end
55
56 % add absolute of largest negative lag worth of zeros to all;
57 % then add 'shiftvec' value to that
58 maxlag = min(shiftvec);
59 for i = 1:length(list)
60 [audio, fs] = audioread([foldername '/' list(i).name]);
61 audiowrite([foldername '/SHIFT' list(i).name], ... % remove SHIFT
62 [zeros(-maxlag + shiftvec(i), size(audio, 2)) ; audio], ...
63 fs, 'BitsPerSample', 24);
64 end
43 65
44 elapsedTime = toc; % stop measuring time 66 elapsedTime = toc; % stop measuring time
45 disp(['autoalign took ' elapsedTime ' seconds to align the files in folder ' ... 67 disp(['autoalign took ' num2str(elapsedTime) ...
46 folderName ' with master file ' list(masterIndex).name]); 68 ' seconds to align the files in folder ' foldername]);
47 69
48 end 70 end