How to get the name of each file in the folder
11 次查看(过去 30 天)
显示 更早的评论
I want to extract the MFCC features of each .wav file. And I need to save the .mat file together with the name of .wav files.
e.g: hello.wav -> hello.mat
I tried using fileparts, but I found problems here:
Error using fileparts
Input must be a row vector of characters, or a string scalar, or a cellstr, or a string matrix.
Error in MFCC_extractor (line 5)
[filepath,name,ext] = fileparts(basedir);
addpath('C:\Users\DELL\Desktop\Reshape')
basedir = dir('C:\Users\DELL\Desktop\Reshape/*.wav'); % .wav directory
for i = 1:numel(basedir)
[filepath,name,ext] = fileparts(basedir);
[y,Fs] = audioread(fullfile(basedir(i).folder, basedir(i).name));% read audio
audio_data{i} = y(:,1); % make mono
[mfcc_data{i},delta,deltaDelta,loc] = mfcc(audio_data{i},Fs,NumCoeffs=20,LogEnergy="ignore");% extract mfccs
save(plus(name,'.mat'),mfcc_data);
end
0 个评论
回答(2 个)
Image Analyst
2023-1-2
Replace
[filepath,name,ext] = fileparts(basedir);
[y,Fs] = audioread(fullfile(basedir(i).folder, basedir(i).name));% read audio
with
fullFileName = fullfile(basedir(i).folder, basedir(i).name);
[filepath,name,ext] = fileparts(fullFileName);
[y,Fs] = audioread(fullFileName);% read audio
2 个评论
Image Analyst
2023-1-2
Replace:
save(plus(name,'.mat'),"mfcc_data",'-mat');
with
fillOutputFileName = fullfile(filepath, [name, '.mat']);
save(fillOutputFileName, "mfcc_data");
jibrahim
2023-1-3
If you have access to Audio Toolbox, using an audioDatastore might simplify this. Here is an example (make sure the function myCustomWriter is on path):
% First, create a datastore that points to your audio files
ads = audioDatastore(basedir,IncludeSubfolders=true);
% Apply a transformation to the datastore. The transformed datastore
% extracts mfcc coefficients from the audio data
Fs =8000;
tds = transform(ads,@(x)mfcc(x,Fs,NumCoeffs=20,LogEnergy="ignore"));
% Write mfcc coefficients for each file to a MAT file
outputLocation = fullfile(tempdir,"myFeatures");
% If you have Parallel Processing Toolbox, set UseParallel to true to
% perform wrting on multiple workers
writeall(tds,outputLocation,WriteFcn=@myCustomWriter,UseParallel=false);
function myCustomWriter(coeffs,writeInfo,~)
% myCustomWriter(spec,writeInfo,~) writes mfccs to MAT files.
filename = strrep(writeInfo.SuggestedOutputName,".wav",".mat");
save(filename,"coeffs");
end
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!