Randomly select one file, every 10 files, and randomise where you take that file from
3 次查看(过去 30 天)
显示 更早的评论
I have a database of thousands of .wav files which I am looking to subset. For every 10 files, I would like to select one file, but since these files are recording at specific time intervals, I want to avoid the scenario that every 10th file occurs at the same time interval. I want to randomise where I select every 10th file from, between 1:10. So far I have come up with the following but it means that at the end of the loop, it crashes as (I think) the range of random numbers doesn't match the range of available files to draw from. I would like to apply this loop to several files of folders so I can leave it running for days without interferring.
%19 June 2020, Louise Wilson
%Plot spectrograms for a folder of .wav files, save each .png in the same
%folder as the .wav files.
%For speed-just do every 10th file, but randomise which file is every 10th
%file.
directory={'Y:\wav files\'}; %folder where wav files are
tlo=4.0;
thi=119.0;
fileend='_2min'
for a=1:length(directory) %for 1:number of folders
folder=char(directory(a)); %select a'th folder
b=dir(fullfile(folder,'*.wav')); %list all .wavs in folder
files=length(b); %number of .wavs in folder
%every 10th file, but randomise where it's taken from between 1-10
rand_num=randperm(10); %generate random number between 1:10
f=0; %first lot of random numbers is between 1-10
for c=1:10:files %for every bunch of 10 files
d=rand_num(1); %select first of those random numbers to choose random file
disp(b(d).name); %display filename
%commands to .wav file go here
f=f+10; %iterate range of random numbers to correspond with file loop
rand_num=randperm(10)+f;
end
end
0 个评论
采纳的回答
Image Analyst
2020-7-14
Use fileDatastore() and randperm():
topLevelFolder = 'D:\One Drive\WaveFiles'; % Wherever you want.
fds = fileDatastore(topLevelFolder, 'ReadFcn', @audioread, 'IncludeSubfolders', true, 'FileExtensions', '.wav')
% Get the total number of files.
numFilesTotal = length(fds.Files)
% Get 1/10th of that as the number we want to use.
nunFilesToUse = round(numFilesTotal / 10)
% Get the indexes randomly from within the whole set.
indexes = sort(randperm(numFilesTotal, nunFilesToUse), 'ascend');
% Loop over all the chosen files, processing them.
for k = 1 : length(indexes)
thisFileName = fds.Files{indexes(k)};
fprintf('Now processing "%s".\n', thisFileName);
end
Adapt as needed.
0 个评论
更多回答(1 个)
Nipun Katyal
2020-7-14
Hi,
I see that you need to select a file randomly at intervals of 10, hope this logic might help you,
b = 1000;
for i = 1:10:b
fprintf('Choose %dth file\n',i+randi([0,9],1));
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Events 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!