annotate aux/stripzeros.m @ 15:24be5e9ce25b tip

Update README
author Brecht De Man <brecht.deman@bcu.ac.uk>
date Thu, 20 Sep 2018 12:23:20 +0200
parents 0014c50188da
children
rev   line source
b@2 1 function stripzeros(foldername)
b@2 2 % STRIPZEROS strips off an array of samples that equal zero at the
b@2 3 % beginning and end of an audio file.
b@2 4 %
b@2 5 % by Brecht De Man at Centre for Digital Music on 17 November 2014
b@2 6
b@2 7 tic; % start measuring time
b@2 8 list = dir([foldername '/*.wav']); % get names of files
b@2 9
b@2 10 % remove hidden files from list
b@2 11 % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220
b@2 12 for k = length(list):-1:1
b@2 13 fname = list(k).name;
b@2 14 if fname(1) == '.'
b@2 15 list(k) = [ ];
b@2 16 end
b@2 17 end
b@2 18
b@2 19 for i = 1:length(list) % go over each file in the folder
b@2 20 [audio, fs] = audioread([foldername '/' list(i).name]);
b@2 21
b@2 22 numSamples = size(audio,1);
b@2 23 numChannels = size(audio,2);
b@2 24 bitDepth = 24; % not automated here
b@2 25 startIndex = zeros(numChannels, 1);
b@2 26 stopIndex = zeros(numChannels, 1);
b@2 27
b@2 28 % first and last non-zero elements (for all channels)
b@2 29 for channel = 1:numChannels
b@2 30 % ? more efficient way to find first and last non-zero sample?
b@2 31 indexOfNonZeroSamples = find(audio(:,channel)); % returns indices non-zero elements
b@2 32 startIndex(channel) = min(indexOfNonZeroSamples);
b@2 33 stopIndex(channel) = max(indexOfNonZeroSamples);
b@2 34 end
b@2 35
b@2 36 start = max(startIndex);
b@2 37 stop = min(stopIndex);
b@2 38
b@2 39 % if startIndex and stopIndex are both equal to first and last sample:
b@2 40 % do nothing.
b@2 41 % else:
b@2 42 if start>1 || stop<numSamples
b@2 43 audiowrite([foldername '/' list(i).name], audio([start:stop], :), ...
b@2 44 fs, 'BitsPerSample', bitDepth);
b@11 45 % debugging/monitoring:
b@11 46 startCrop = (start-1)/fs; %
b@11 47 stopCrop = (size(audio,1)-stop)/fs;
b@11 48 disp([num2str(startCrop) 's (start) / ' num2str(stopCrop) 's (end) stripped off ' list(i).name]);
b@11 49 else
b@11 50 disp(['Nothing stripped off ' list(i).name]);
b@2 51 end
b@2 52
b@11 53
b@2 54 end
b@2 55
b@2 56 elapsedTime = toc; % stop measuring time
b@2 57 disp(['stripzeros took ' num2str(elapsedTime) ' seconds to strip zeros off the files in folder ' ...
b@2 58 foldername]);
b@2 59
b@2 60 end