How to get the name of each file in the folder

11 visualizzazioni (ultimi 30 giorni)
Zhuoyi Ma
Zhuoyi Ma il 2 Gen 2023
Risposto: jibrahim il 3 Gen 2023
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

Risposte (2)

Image Analyst
Image Analyst il 2 Gen 2023
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 Commenti
Zhuoyi Ma
Zhuoyi Ma il 2 Gen 2023
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 il 2 Gen 2023
Replace:
save(plus(name,'.mat'),"mfcc_data",'-mat');
with
fillOutputFileName = fullfile(filepath, [name, '.mat']);
save(fillOutputFileName, "mfcc_data");

Accedi per commentare.


jibrahim
jibrahim il 3 Gen 2023
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

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by