how do i read multiple audio file from different files name ?
3 次查看(过去 30 天)
显示 更早的评论
i have a folder about 136 subfolders which have different names
f1
f2
.
.
.
.
f136
and each subfolder has 10 audio file
these audio files have the same name for whole subfolders
f1 [ sa1 sa2 ....... sa10]
f2 [sa1 sa2 .... sa10]
.
.
f136 [sa1 sa2 ... sa10]
so i need to read them in one matrix
can anyone help me
i will be appreciate
1 个评论
Stephen23
2020-12-23
"so i need to read them in one matrix"
Does your computer have enough memory to store all of the imported data in one array? Or for that matter, in separate arrays?
采纳的回答
Walter Roberson
2020-12-23
finfo = dir();
finfo(~[finfo.isdir]) = []; %get rid of non-folders
finfo(ismember({finfo.name}, {'.', '..'})) = []; %get rid of . and ..
foldernames = {finfo.name};
numfolders = length(foldernames);
folderdata = cell(numfolders,3);
for foidx = 1 : numfolders
thisfolder = foldernames{foidx};
dinfo = dir(fullfile(thisfolder, '*.wav'));
thesenames = fullfile(thisfolder, {dinfo.name});
numfile = length(thesenames);
thisdata = cell(numfile,1);
thisfs = zeros(numfile,1);
for fiidx = 1 : numfile
[thisdata{fiidx}, thisfs(fiidx)] = audiread(thesenames{fiidx});
end
folderdata{foidx, 1} = thisdata;
folderdata{foidx, 2} = thisfs;
folderdata{foidx, 3} = thesenames;
end
Output from this is an N x 3 cell array where N is the number of folders. The first column of the cell array, folderdata{K,1} is itself a cell array with one entry per file in the folder that is the audio data. The second column of the cell array, folderdata{K,2} is a vector of sampling frequency data, one for each WAV file in the folder. The third column of the cell array, folder{K,3} is a cell array of file names that were read from in the folder.
This code does not assume that the number of samples is exactly the same for each input file. It also does not assume that the sampling frequency is the same for each file, and it does not assume that the number of channels is the same for each file.
It is not clear to me what your desired output is. You have 136 folders, each with 10 files, number of samples per file is unknown, number of channels per file is unknown. Even assuming that you had exactly the same number of samples per file and that there is only one channel per file, you would still be looking at 136 x 10 x number of samples, but you kind of implied that you expect a 2D array rather than a 3D array.
0 个评论
更多回答(2 个)
weikang zhao
2020-12-23
You need a loop body, the loop body continuously generates the path of the audio file.
Run the following script and you will understand.
for i=1:136
for j=1:10
filename=['f',num2str(i),'\sa',num2str(j),'.wav'];
disp(str);
%code to read the audio file %
end
end
have fun!
4 个评论
weikang zhao
2020-12-23
you can get all the names of the subfolder by "dir" function, for example
X=dir;
X will contain information about all subfolders and files in the current folder, X is a struct array, and you can traverse the 'name' field of X.
jibrahim
2020-12-23
If you have access to Audio Toolbox, there is no need for much custom code to accomplish this. Just use audioDatastore.
From the top folder:
ads = audioDatastore(pwd,'IncludeSubfolders',true);
data = readall(ads);
data is a cell array with the audio from all files. If all entries in data have the same dimensions, you can concatenate the signals in a matrix.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio and Video Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!