MATLAB Answers

How to plot readed audio files in multiple plots or subplots by MATLAB

6 views (last 30 days)
Deepu S S
Deepu S S on 10 Sep 2021
Commented: Deepu S S on 16 Sep 2021
i want to select one audio folder then i want to plot them all in different axis or subplots. Kindly help me to solve this.Kindly refer below code
guidata(hObject, handles);
disp('Entered');
guidata(hObject, handles);
folder_name = uigetdir('','Select src data Directory');
if isequal(folder_name,0)
disp('Directory Selected');
else
% set(handles.EEGFilename,'string','Wait Loading File.......');
disp(['You have selected ', fullfile(folder_name)]);
disp('CHD Audio file processing wait')
handles.srcFolder= fullfile(folder_name);
% set(handles.edDirName,'string',handles.srcFolder);
end
guidata(hObject, handles);
% Specify the folder where the files live.
k='';
myFolder = 'F:\audio';
folder ='F:\';
% AudioArray{k} = audioread(fullfile(folder));
% maxaudio(k) = max(AudioArray{k});
% myFolder = 'F:\audio';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '**/*.wav'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);

Accepted Answer

Dave B
Dave B on 10 Sep 2021
It looks like you're as far as getting the filenames, but need to read the data and make the plots.
audioread works well for loading the audio data, you have some commented out code with audioread above, I'm not sure if you ran into trouble. I think it's fine to store them all in a cell array first, but I didn't do that below for simplicity.
I'd suggest tiledlayout/nexttile over subplot. Here I use the 'flow' style of tiledlayout which means MATLAB will choose a grid shape for your multiple axes.
% tiledlayout flow means you can have as many 'tiles' (subplots) as you like
tiledlayout('flow')
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
% read the file with audioread
[y,fs]=audioread(fullFileName);
% this defines a time variable for your x axis:
t = linspace(0, height(y)/fs, height(y));
% this adds another tile (axes) to your layout
nexttile
% plot it
plot(t,y)
% label it
xlabel('Time (s)')
ylabel('Amplitude')
end
  5 Comments
Deepu S S
Deepu S S on 16 Sep 2021
please refer above picture. when i select mono viewe call back but the multi wave viewe is current present there,how to fix this problem
function demo_cdhd_Callback(hObject, eventdata, handles)
% hObject handle to demo_cdhd (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% set(handles.chdprevious, 'Visible','on')
% set(handles.chdnext,'visible','on')
% set(handles.chdtext,'visible','on')
% set(handles.chdpanel,'visible','on')
guidata(hObject, handles);
disp('Entered');
guidata(hObject, handles);
folder_name = uigetdir('','Select src data Directory');
if isequal(folder_name,0)
disp('Directory Selected');
else
% set(handles.EEGFilename,'string','Wait Loading File.......');
disp(['You have selected ', fullfile(folder_name)]);
disp('CHD Audio file processing wait')
handles.srcFolder= fullfile(folder_name);
% set(handles.edDirName,'string',handles.srcFolder);
end
guidata(hObject, handles);
% Specify the folder where the files live.
k='';
myFolder = 'F:\audio';
folder ='F:\';
% AudioArray{k} = audioread(fullfile(folder));
% maxaudio(k) = max(AudioArray{k});
% myFolder = 'F:\audio';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '**/*.wav'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
AudioArray = audioread(fullFileName);
[y,fs] = audioread(fullFileName);
subplot = 'theFiles';
tiledlayout('flow')
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
% read the file with audioread
[y,fs]=audioread(fullFileName);
% this defines a time variable for your x axis:
t = linspace(0, height(y)/fs, height(y));
% this adds another tile (axes) to your layout
nexttile
% plot it
plot(t,y)
% label it
xlabel('Time (s)')
ylabel('Amplitude')
set(gcf,'MenuBar','none')
end
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!