Audio data loading and splitting

조회 수: 6 (최근 30일)
Divya
Divya 2025년 3월 6일
댓글: Mathieu NOE 2025년 4월 2일 14:53
How to load the audio dataset with subfolders in pwd, create ads and spilt them into train and testing set
  댓글 수: 1
Walter Roberson
Walter Roberson 2025년 3월 6일

I am not sure what you mean by "create ads"?

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

답변 (1개)

Mathieu NOE
Mathieu NOE 2025년 3월 6일
hello
see example below - adapt to you own needs
%% Important Notice :
% NB : wav files are not searched inside the main folder (yourpath) but only in subfolders (dirnames)
% in other words : this code works for wav files that are only located in
% subfolders , not in the main "yourpath" folder
% if S is empty that means no data file exist (matching the file filter in the searched subfolder)
%% define path
yourpath = pwd; % or your specific path
list=dir(yourpath); %get info of files/subfolders in current directory
isfile=~[list.isdir]; %determine index of files vs folders
dirnames={list([list.isdir]).name}; % directories names (including . and ..)
dirnames=dirnames(~(strcmp('.',dirnames)|strcmp('..',dirnames))); % remove . and .. directories names from list
%% FFT options
% if you are dealing with acoustics, you may wish to have A weighted
% spectrums / spectrograms
% option_w = 0 : linear spectrum (no weighting dB (L) )
% option_w = 1 : A weighted spectrum (dB (A) )
option_w = 0;
% spectrogram dB scale
spectrogram_dB_scale = 80; % the lowest displayed level is XX dB below the max level
NFFT = 512; % fft buffer size
OVERLAP = 0.75; % percentage of overlap (1 = 100%)
%% Loop on each folder
for ci = 1:length(dirnames) %
fileDir = char(dirnames(ci)); % current directory name
S = dir(fullfile(fileDir,'*.wav')); % get list of data files in directory according to name structure 'Sheeta*.xlsx'
S = natsortfiles(S); % sort file names into natural order (what matlab dir does not well) , see FEX :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
%% Loop inside folder
for k = 1:length(S) % read data in specified sheet
[signal,Fs] = audioread(fullfile(fileDir, S(k).name)); % or use a structure (S(k).data ) to store the full data structure
dt = 1/Fs;
[samples,channels] = size(signal);
time = (0:samples-1)*dt; % time vector
title_str = [fileDir ' / ' S(k).name];
title_str = strrep(title_str,'_',' '); % replace underscores with blanks for better rendering
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% spectrogram demo
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for ck = 1:channels
[sg,fsg,tsg] = specgram(signal(:,ck),NFFT,Fs,hanning(NFFT),floor(NFFT*OVERLAP));
tsg = tsg + NFFT/(2*Fs); % small time correction so that a fft buffer is time referenced at the buffer middle point
% FFT normalisation and conversion amplitude from linear to dB (peak)
sg_dBpeak = 20*log10(abs(sg))+20*log10(2/length(fsg)); % NB : X=fft(x.*hanning(N))*4/N; % hanning only
% apply A weigthing if needed
if option_w == 1
pondA_dB = pondA_function(fsg);
sg_dBpeak = sg_dBpeak+(pondA_dB*ones(1,size(sg_dBpeak,2)));
my_title = ('Spectrogram (dB (A))');
else
my_title = ('Spectrogram (dB (L))');
end
% saturation of the dB range : the lowest displayed level is XX dB below the max level
min_disp_dB = round(max(max(sg_dBpeak))) - spectrogram_dB_scale;
sg_dBpeak(sg_dBpeak<min_disp_dB) = min_disp_dB;
% plots spectrogram
figure();
ah1 = subplot(3,1,1);
plot(time,signal(:,ck));
xlim([min(time) max(time)]); % make sure time and spectrogram have same x axis
title(title_str); % time domain plot (one channel)
xlabel('Time (s)');
ylabel('Amplitude');
ah2 = subplot(3,1,2:3);
imagesc(tsg,fsg,sg_dBpeak);colormap('jet'); % spectrogram plot (one channel)
xlim([min(time) max(time)]); % make sure time and spectrogram have same x axis
axis('xy');grid on
hcb = colorbar('vert');
set(get(hcb,'Title'),'String','dB')
%# find current position [x,y,width,height]
pos2 = get(ah2,'Position');
pos1 = get(ah1,'Position');
%# set width of second axes equal to first
pos2(3) = pos1(3);
set(ah2,'Position',pos2)
df = fsg(2)-fsg(1); % freq resolution
title([my_title ' / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(df,3) ' Hz / Channel : ' num2str(ck)]);
xlabel('Time (s)');
ylabel('Frequency (Hz)');
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A weighting for acoustic FFT Analysis
function pondA_dB = pondA_function(f)
% dB (A) weighting curve
n = ((12200^2*f.^4)./((f.^2+20.6^2).*(f.^2+12200^2).*sqrt(f.^2+107.7^2).*sqrt(f.^2+737.9^2)));
r = ((12200^2*1000.^4)./((1000.^2+20.6^2).*(1000.^2+12200^2).*sqrt(1000.^2+107.7^2).*sqrt(1000.^2+737.9^2))) * ones(size(f));
pondA = n./r;
pondA_dB = 20*log10(pondA(:));
end
  댓글 수: 1
Mathieu NOE
Mathieu NOE 2025년 4월 2일 14:53
hello again
problem solved ?

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

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by