Trying to convert whole folder into spectrograms

7 ビュー (過去 30 日間)
Justine Hughes
Justine Hughes 2022 年 4 月 12 日
コメント済み: Justine Hughes 2022 年 4 月 14 日
Hi, I have a local folder of .wav files that Id like to convert to spectrograms (.png) in another local folder. so I can use it to train my CNN model. This is what I have but it's not working. Also gave no errors. Help please!!
folder = uigetdir(pwd,'*insert source folder path name here*');
filelist = dir(fullfile(folder, '*.wav')); %get list of all wav files in the folder
for fileidx = 1:numel(filelist)
[audioIn,fs] = audioread({filelist.name});
S = melSpectrogram(audioIn,fs);
save('*insert destination folder path name here*','Cough1.png','-png');
end

採用された回答

jibrahim
jibrahim 2022 年 4 月 12 日
Hi Justine, consider using an audioDatastore to do this. then you don't have to write the for loop. for example:
% This audioDatastore will point to all audio files in folder and its
% subfolders
ads = audioDatastore(folder,IncludeSubfolders=true);
% Write my images here
outputLocation = fullfile(tempdir,"mySpectrograms");
% Create PNGs for all audio files.
writeall(ads,outputLocation,WriteFcn=@myCustomWriter);
function myCustomWriter(audioIn,writeInfo,~)
% Get the sample rate of the audio file
fs = writeInfo.ReadInfo.SampleRate;
% Extract Mel spectrogram
S = melSpectrogram(audioIn,fs);
% Form image name
imagename = strrep(writeInfo.SuggestedOutputName,".wav",".png");
% Save image
imwrite(S(:,:,1),imagename);
end
  3 件のコメント
jibrahim
jibrahim 2022 年 4 月 14 日
Hi Justine,
How about this?
% This audioDatastore will point to all audio files in folder and its
% subfolders
ads = audioDatastore(folder,IncludeSubfolders=true);
% Write my images here
outputLocation = fullfile(tempdir,"mySpectrograms");
% Create PNGs for all audio files.
writeall(ads,outputLocation,WriteFcn=@myCustomWriter);
function myCustomWriter(audioIn,writeInfo,~)
% Get the sample rate of the audio file
fs = writeInfo.ReadInfo.SampleRate;
% Extract Mel spectrogram. This generates a plot.
melSpectrogram(audioIn,fs);
% Form image name
imagename = strrep(writeInfo.SuggestedOutputName,".wav",".png");
% Save the figure as an image
saveas(gcf,imagename);
end
Justine Hughes
Justine Hughes 2022 年 4 月 14 日
Okay so when I used imagename as a variable it didnt work but when I put the strrep function to replace imagename in saveas, it worked :) thank you very much!

サインインしてコメントする。

その他の回答 (1 件)

Rik
Rik 2022 年 4 月 12 日
You are inserting all files at once, instead of indexing into the filelist array.
If you want want to store an image you need to use imwrite. Look up the documentation to see several examples. I would suggest generating the file name of the png with fullfile, using filelist(n).folder.
  2 件のコメント
Justine Hughes
Justine Hughes 2022 年 4 月 12 日
I updated it with imwrite and tried to index filelist but perhaps I misunderstood.
Im kind of inexperienced with matlab, could you explain this please?
for fileidx = 1:numel(filelist)
[audioIn,fs] = audioread(filelist.name(fileidx)); %this line gives the error
S = melSpectrogram(audioIn,fs);
filename = sprintf('Cough%d.png',fileidx);
imagename = [savefolder,'\Cough_',num2str(fileidx),'.png'];
imwrite(S,imagename); %save image as png
end
Rik
Rik 2022 年 4 月 12 日
You're indexing the field, not the struct.
for fileidx = 1:numel(filelist)
[audioIn,fs] = audioread(filelist(fileidx).name);
S = melSpectrogram(audioIn,fs);
filename = sprintf('Cough%d.png',fileidx);
imagename = fullfile(savefolder,sprintf('Cough_%d.png',fileidx));
imwrite(S,imagename); %save image as png
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFeature Extraction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by