combining multiple wav files into one multichannel matrix

Having a bunch of stereo wav files with the structure:
036a.wav, 036b.wav, 036c.wav ...
037a.wav, 037b.wav, 037c.wav ...
...
I wish to load them one by one and concatenate them into a structure so that the files starting with the same number (such as 036) would all be combined into a "multichannel" matrix where the 036a.wav would occupy the first two columns, 036b.wav the column no. 3 and 4, the 036c.wav the colums 5 and 6 etc.
The files 037a.wav ... 037b.wav ... 037c.wav would follow the same scheme, forming another branch of the struct.
How is this best done in Matlab, please?

3 Comments

Jan
Jan on 10 May 2018
Edited: Jan on 10 May 2018
What is coming after 036z? Do all signals have the same length? Do you know the list of numbers in advance?
Well, there was never a case that whole alphabet would be used. There are not so many files, therefore we do not need to solve this question, fortunately.
For the length: yes, they are supposed to be of the same length, of course, a casual error may happen from time to time. I will look into padding.
And for the numbers knowing in advance - I could eventually read it from the list of the files in advance, but that would only replicate a need for a loop, I think.

Sign in to comment.

 Accepted Answer

Jan
Jan on 10 May 2018
Edited: Jan on 10 May 2018
Perhaps by something like this:
S = struct();
iS = 0;
Folder = 'C.:\Your\Folder\';
for k = 1:1000
str = sprintf('%03d', k);
if exist(fullfile(Folder, [str, 'a.wav']), 'file')
List = dir(fullfile(Folder, [str, '*.wav']));
Data = cell(1, numel(List));
for iFile = 1:numel(List)
File = fullfile(Folder, List(iFile).name);
Data{iFile} = wavread(File);
end
iS = iS + 1;
S(is).Data = cat(2, Data{:});
S(iS).Key = str;
end
end
This assumes, that all signals have the same length. Otherwise you can pad the signals e.g. by FileExchange: padcat .

3 Comments

THank you again, Jan, you seem to be a genius. You know answer to all the questions. I will test this solution today.
Hello, I just wanted to ask, how can I use 'sound' after combining? I mean I want ho hear all files in one wav.file, is it possible?
Some audio editors support multichannel wav files, some of them split the multichannel wav onto several tracks, and then you can route various channels to various output as you like. If I am correct, even freeware like Audacity is capable of dealing with multichannels wavs, Reaper as well. In Matlab, I do not listen to the sound, I only process it.

Sign in to comment.

More Answers (0)

Categories

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

Asked:

on 10 May 2018

Commented:

on 15 Nov 2019

Community Treasure Hunt

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

Start Hunting!