How to get the name of each file in the folder

7 ビュー (過去 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 Center および File ExchangeAI for Audio についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by