How to read multiple .wav files and plot them as separate signals?

5 views (last 30 days)
I would like to read from 10 files which they contain 10 wav files in each, so approximately 100wav files in total. I am trying to read them one by one and then apply some filttering and store the result in a new location but it should have the same name files and wav names as it was in the begging.
so practically take the voices apply some filterring and store them in a different location but in the same way same file names and wav.names audio signal but filltered. I do not care about the filltering I am only affter the read and write.
for example:
file 1:
has inside 10 wav files audio signal, eg1.wav, kle.wav.....lp.wav ( they do not have the same name or similar)
file 2:
has inside 10 wav files audio signal, kx1.wav, kle.wav.....lp.wav ( they do not have the same name or similar)
.
.
file 10
has inside 10 wav files audio signal, eg1.wav, kle.wav.....lp.wav ( they do not have the same name or similar)
I am trying to use the below code:
names = {'Voice1.wav', 'Voice2.wav', 'Voice3.wav', 'Voice4.wav'};
wave = cell(size(names));
fs = cell(size(names));
subplot_cols = 3;
subplot_rows = ceil(numel(names)/subplot_cols);
for i=1:numel(names)
[wave{i},fs{i}]=audioread('sample.wav');
t = linspace(0, (numel(wave{i})-1)/fs{i}, numel(wave{i}));
subplot(subplot_rows,subplot_cols,i)
plot(t, wave);
title(['File: ' names{i}]);
end
but it shows me an error
Error in Assigment (line 60)
plot(t,wave);
Many thanks in advance.
https://uk.mathworks.com/matlabcentral/answers/516853-how-can-i-read-multiple-wav-files-and-plot-them-as-separate-signals#comment_1034872

Answers (2)

Ameer Hamza
Ameer Hamza on 3 Oct 2020
Edited: Ameer Hamza on 3 Oct 2020
Check this code
files = dir('file*');
f = figure();
destination_folder = ''; % put location of destimation folder
for i = 1:numel(files)
sub_files = dir([files(i).name '/*.wav']);
mkdir(destination_folder, files(i).name);
for j = 1:numel(sub_files)
nexttile();
[y, Fs] = audioread(sub_files(j).name);
Ts = 1/Fs;
n = numel(y);
t = (0:1:n-1)*Ts;
plot(t, y);
title(sprintf('%s %s', files(i).name, sub_files(j).name));
% process the signal
new_filename = fullfile(destination_folder, files(i).name, sub_files(j).name);
audiowrite(new_filename, y, Fs);
end
end
  7 Comments
GreyHunter
GreyHunter on 4 Oct 2020
Yes, I realised that earlier this morning. The problem now is that it does not read all the wav audio signals and also, it does not entering the second for loop.
for j = 1:numel(sub_files)
nexttile();
[y, Fs] = audioread(sub_files(j).name);
Ts = 1/Fs;
n = numel(y);
t = (0:1:n-1)*Ts;
plot(t, y);
title(sprintf('%s %s', files(i).name, sub_files(j).name));
The sub_files are equal to this: (which sounds about right)
val =
name
folder
date
bytes
isdir
datenum
J =[]
GreyHunter
GreyHunter on 4 Oct 2020
I have tried to amend the code you provided as per below:
d = dir('E:\*****\current test\Signal_processing\experement\Matlab\2020\file');
subdirList = fullfile({d.folder}', {d.name}');
subdirList(~[d.isdir]) = []; %remove non-directories
%Loop through subdirectories
y = cell(1, 100*10); %100 files x 10 folders
c = 0; %This is just a counter
for i = length(subdirList)
filelist= dir(fullfile(subdirList{i}, '*.wav'));
% Loop through files
for j = 1:size(filelist,1)
nexttile()
c = c+1;
[y, Fs] = audioread(fullfile(filelist(j).folder, filelist(j).name));
Ts = 1/Fs;
n = numel(y);
t = (0:1:n-1)*Ts;
plot(t, y);
% title(sprintf('%s %s', subdirList(i).name, filelist(j).folder));
sound(y);
end
end
It seems it works I can read all the wav files but in the second for loop reads only the last 10 wav. audio signals and ignore the previous 90 wav audio signals which are in the 9 other files.

Sign in to comment.


GreyHunter
GreyHunter on 5 Oct 2020
Edited: GreyHunter on 5 Oct 2020
Anyone who can advise I would appriaciate that, currently I have managed to read all the files from all the subfiles I want, then I am making some folder and files same as the initial ones and I would like to take the output of each signal which has been filtered and stored in the the new files which has been created but unfortunetely it does not work. It does not store all the files but only the last 10 from the 100 and in a different location. Please see below my code
files = dir('File');
destination_folder = 'XXXx';
for i = 1:numel(files)
sub_files = dir([files(i).name '/*.wav']);
mkdir(destination_folder, files(i).name);
end
wav_folder = dir('XXXXx');
subdirList = fullfile({wav_folder.folder}', {wav_folder.name}');
for i=1:length(subdirList)
filelist = dir(fullfile(subdirList{i}, '*.wav'));
for j=1:length(filelist)
acq_fn = fullfile(subdirList{i},filelist(j).name);
[p, name, ext] = fileparts(acq_fn);
[signal, Fs] = audioread(acq_fn);
Ts = 1/Fs;
n = numel(signal);
t = (0:1:n-1)*Ts;
% title(sprintf('%s %s', subdirList(i).name, filelist(j).folder));
%filtering the signals
% the output signal should be store in a specific folder and file.
file1=sprintf('file48000_%d.wav',i);
FOLDER_NAME = 'XXXXXX';
userpath(FOLDER_NAME);
filelist1 = dir(fullfile(subdirList{i}, '*.wav'));
audiowrite(file1,outSignal,Fs);
end
end

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!