How to get the name of each file in the folder

조회 수: 17 (최근 30일)
Zhuoyi Ma
Zhuoyi Ma 2023년 1월 2일
답변: jibrahim 2023년 1월 3일
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

답변 (2개)

Image Analyst
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
Zhuoyi Ma
Zhuoyi Ma 2023년 1월 2일
HI, thanks! Now I am facing to a problem about saving the data to .mat file.
Would you please take a look at it?
----------ErrMsg--------
Arrays have incompatible sizes for this operation.
Error in MFCC_extractor (line 10)
save(plus(name,'.mat'),"mfcc_data",'-mat');
addpath('C:\Users\DELL\Desktop\Reshape')
basedir = dir('C:\Users\DELL\Desktop\Reshape/*.wav'); % .wav directory
for i = 1:numel(basedir)
fullFileName = fullfile(basedir(i).folder, basedir(i).name);
[filepath,name,ext] = fileparts(fullFileName);
[y,Fs] = audioread(fullFileName);% 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",'-mat');
end
Image Analyst
Image Analyst 2023년 1월 2일
Replace:
save(plus(name,'.mat'),"mfcc_data",'-mat');
with
fillOutputFileName = fullfile(filepath, [name, '.mat']);
save(fillOutputFileName, "mfcc_data");

댓글을 달려면 로그인하십시오.


jibrahim
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

카테고리

Help CenterFile Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by