Divide .wav file into equal segments

22 vues (au cours des 30 derniers jours)
Christine
Christine le 27 Mai 2020
Commenté : Walter Roberson le 29 Juin 2021
I've been having some trouble dividing my 159567 ms audio file into 3000 ms segments. I've seen on previous threads about using the 'bufer' function, but I'm not quite sure how to implement it here. What ends up happening here is that the correct number of segments are created, but the audio file doesn't get cut into its 3000 ms chunks (instead I have 53 audio files that are exactly the same). Any help would be appreciated.
%epoch into 3 sec nonoverlapping segments
for i = 1
[y, Fs] = audioread('Myfile.wav');
stims{i} = y;
stiml(i) = length(stims{i});
stiml(i) = stiml(i)/44.1; %put lengths into milliseconds in time (not samples). Length: 2 min, 39 s
end
t1 = 1;
t2 = 3000; %ms
k = 1;
while t2 <= stiml %equal to 159567 ms
new_seg = [t1,t2];
File = sprintf('Part%02d.wav', k);
audiowrite(File, y, Fs);
t1=t1+3000;
t2=t2+3000;
k=k+1;
end
  2 commentaires
Walter Roberson
Walter Roberson le 27 Mai 2020
You are proceeding by 3000 samples, not by 3000 ms. To get 3000 milliseconds in terms of samples you need to multiply Fs by 3000 (milliseconds) / 1000 (milliseconds per second)
Christine
Christine le 27 Mai 2020
Thanks, that makes sense. I made that change, but the same thing is happening as before. I have a feeling I didn't code new_seg right (my experience with matlab is minimal). I based this code off of another thread where they were trying to divide a signal with a .csv file:
Folder = 'E:\data_Lab\MITBIH\edited\';
X = xlsread('E:\data_Lab\MITBIH\edited\100.csv');
X1 = X(:,1);
t1 = 1;
t2 = 3600;
k = 1;
while t2 <= 651600
new_seg = X1(t1:min(t2,length(X1)));
File = sprintf('Part%04d.csv', k);
csvwrite(fullfile(Folder, File), new_seg);
t1 = t1 + 3600;
t2 = t2 + 3600;
k = k + 1;
end

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 28 Mai 2020
Modifié(e) : Walter Roberson le 28 Mai 2020
The below will split all .wav in a given directory into 3 second buffers. A directory named after the file will be created inside outdir for each file, and the split files will be named by Part inside the relevant directory.
This code does not assume that the sample frequency is the same for all files.
This code does not assume mono source, and does not assume that the number of channels is the same for each file.
%epoch into 3 sec nonoverlapping segments
projectdir = 'location of files to split';
outdir = 'location of directory to split into'; %can be the same as projectdir
dinfo = dir( fullfile(projectdir, '*.wav') );
filenames = fullfile( projectdir, {dinfo.name} );
nfiles = length(dinfo);
stims = cell(nfiles,1);
stiml = zeros(nfiles,1);
Fss = zeros(nfiles,1);
for K = 1 : nfiles
[y, Fss(K)] = audioread(filenames{K});
stims{K} = y;
stiml(K) = size(y,1) / Fss(K) * 1000;
end
for K = 1 : nfiles
[~, basename, ext] = fileparts( filenames{K} );
thisoutdir = fullfile(outdir, basename);
if ~exist(thisoutdir, 'dir');
try
mkdir(thisoutdir);
catch ME
fprintf('count not make the output directory "%s", skipping file\n', thisoutdir);
continue;
end
end
nchan = size(stims{K},2);
Fs = Fss(K);
buffered_chans = arrayfun(@(Cidx) buffer(stims{K}(:,Cidx), floor(3*Fss{K})), 1:nchan, 'uniform', 0);
nparts = size(buffered_chans{1},2);
for part = 1 : nparts
File = fullfile(thisoutdir, sprintf('Part%02d.wav', part));
this_segment = cell2mat(cellfun(@(C) C(:,part), buffered_chans, 'uniform', 0));
audiowrite(File, this_segment, Fs);
end
end
  5 commentaires
awais khan
awais khan le 29 Juin 2021
Brace indexing is not supported for variables of this type.
I got this error.
Walter Roberson
Walter Roberson le 29 Juin 2021
You are right, it needed a minor fix.
%epoch into 3 sec nonoverlapping segments
projectdir = fullfile(matlabroot, 'toolbox', 'audio', 'samples');
outdir = 'junk_out'; %can be the same as projectdir
dinfo = dir( fullfile(projectdir, '*.wav') );
filenames = fullfile( projectdir, {dinfo.name} );
nfiles = length(dinfo);
stims = cell(nfiles,1);
stiml = zeros(nfiles,1);
Fss = zeros(nfiles,1);
for K = 1 : nfiles
[y, Fss(K)] = audioread(filenames{K});
stims{K} = y;
stiml(K) = size(y,1) / Fss(K) * 1000;
end
for K = 1 : nfiles
[~, basename, ext] = fileparts( filenames{K} );
thisoutdir = fullfile(outdir, basename);
if ~exist(thisoutdir, 'dir');
try
mkdir(thisoutdir);
catch ME
fprintf('count not make the output directory "%s", skipping file\n', thisoutdir);
continue;
end
end
nchan = size(stims{K},2);
Fs = Fss(K);
buffered_chans = arrayfun(@(Cidx) buffer(stims{K}(:,Cidx), floor(3*Fss(K))), 1:nchan, 'uniform', 0);
nparts = size(buffered_chans{1},2);
for part = 1 : nparts
File = fullfile(thisoutdir, sprintf('Part%02d.wav', part));
this_segment = cell2mat(cellfun(@(C) C(:,part), buffered_chans, 'uniform', 0));
audiowrite(File, this_segment, Fs);
fprintf('wrote "%s"\n', File)
end
end
wrote "junk_out/Ambiance-16-44p1-mono-12secs/Part01.wav" wrote "junk_out/Ambiance-16-44p1-mono-12secs/Part02.wav" wrote "junk_out/Ambiance-16-44p1-mono-12secs/Part03.wav" wrote "junk_out/Ambiance-16-44p1-mono-12secs/Part04.wav" wrote "junk_out/Ambiance-16-44p1-mono-12secs/Part05.wav" wrote "junk_out/AudioArray-16-16-4channels-20secs/Part01.wav" wrote "junk_out/AudioArray-16-16-4channels-20secs/Part02.wav" wrote "junk_out/AudioArray-16-16-4channels-20secs/Part03.wav" wrote "junk_out/AudioArray-16-16-4channels-20secs/Part04.wav" wrote "junk_out/AudioArray-16-16-4channels-20secs/Part05.wav" wrote "junk_out/AudioArray-16-16-4channels-20secs/Part06.wav" wrote "junk_out/AudioArray-16-16-4channels-20secs/Part07.wav" wrote "junk_out/ChurchImpulseResponse-16-44p1-mono-5secs/Part01.wav" wrote "junk_out/ChurchImpulseResponse-16-44p1-mono-5secs/Part02.wav" wrote "junk_out/Click-16-44p1-mono-0.2secs/Part01.wav" wrote "junk_out/Counting-16-44p1-mono-15secs/Part01.wav" wrote "junk_out/Counting-16-44p1-mono-15secs/Part02.wav" wrote "junk_out/Counting-16-44p1-mono-15secs/Part03.wav" wrote "junk_out/Counting-16-44p1-mono-15secs/Part04.wav" wrote "junk_out/Counting-16-44p1-mono-15secs/Part05.wav" wrote "junk_out/Counting-16-44p1-mono-15secs/Part06.wav" wrote "junk_out/Engine-16-44p1-stereo-20sec/Part01.wav" wrote "junk_out/Engine-16-44p1-stereo-20sec/Part02.wav" wrote "junk_out/Engine-16-44p1-stereo-20sec/Part03.wav" wrote "junk_out/Engine-16-44p1-stereo-20sec/Part04.wav" wrote "junk_out/Engine-16-44p1-stereo-20sec/Part05.wav" wrote "junk_out/Engine-16-44p1-stereo-20sec/Part06.wav" wrote "junk_out/Engine-16-44p1-stereo-20sec/Part07.wav" wrote "junk_out/FemaleSpeech-16-8-mono-3secs/Part01.wav" wrote "junk_out/Heli_16ch_ACN_SN3D/Part01.wav" wrote "junk_out/Heli_16ch_ACN_SN3D/Part02.wav" wrote "junk_out/JetAirplane-16-11p025-mono-16secs/Part01.wav" wrote "junk_out/JetAirplane-16-11p025-mono-16secs/Part02.wav" wrote "junk_out/JetAirplane-16-11p025-mono-16secs/Part03.wav" wrote "junk_out/JetAirplane-16-11p025-mono-16secs/Part04.wav" wrote "junk_out/JetAirplane-16-11p025-mono-16secs/Part05.wav" wrote "junk_out/JetAirplane-16-11p025-mono-16secs/Part06.wav" wrote "junk_out/Laughter-16-8-mono-4secs/Part01.wav" wrote "junk_out/Laughter-16-8-mono-4secs/Part02.wav" wrote "junk_out/MainStreetOne-16-16-mono-12secs/Part01.wav" wrote "junk_out/MainStreetOne-16-16-mono-12secs/Part02.wav" wrote "junk_out/MainStreetOne-16-16-mono-12secs/Part03.wav" wrote "junk_out/MainStreetOne-16-16-mono-12secs/Part04.wav" wrote "junk_out/MainStreetOne-16-16-mono-12secs/Part05.wav" wrote "junk_out/NoisySpeech-16-22p5-mono-5secs/Part01.wav" wrote "junk_out/NoisySpeech-16-22p5-mono-5secs/Part02.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part01.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part02.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part03.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part04.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part05.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part06.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part07.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part08.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part09.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part10.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part11.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part12.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part13.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part14.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part15.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part16.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part17.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part18.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part19.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part20.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part21.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part22.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part23.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part24.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part25.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part26.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part27.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part28.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part29.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part30.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part31.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part32.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part33.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part34.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part35.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part36.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part37.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part38.wav" wrote "junk_out/Rainbow-16-8-mono-114secs/Part39.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part01.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part02.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part03.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part04.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part05.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part06.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part07.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part08.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part09.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part10.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part11.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part12.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part13.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part14.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part15.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part16.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part17.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part18.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part19.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part20.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part21.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part22.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part23.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part24.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part25.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part26.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part27.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part28.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part29.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part30.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part31.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part32.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part33.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part34.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part35.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part36.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part37.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part38.wav" wrote "junk_out/RainbowNoisy-16-8-mono-114secs/Part39.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part01.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part02.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part03.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part04.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part05.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part06.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part07.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part08.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part09.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part10.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part11.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part12.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part13.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part14.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part15.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part16.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part17.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part18.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part19.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part20.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part21.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part22.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part23.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part24.wav" wrote "junk_out/RockGuitar-16-44p1-stereo-72secs/Part25.wav" wrote "junk_out/SpeechDFT-16-8-mono-5secs/Part01.wav" wrote "junk_out/SpeechDFT-16-8-mono-5secs/Part02.wav" wrote "junk_out/TrainWhistle-16-44p1-mono-9secs/Part01.wav" wrote "junk_out/TrainWhistle-16-44p1-mono-9secs/Part02.wav" wrote "junk_out/TrainWhistle-16-44p1-mono-9secs/Part03.wav" wrote "junk_out/TrainWhistle-16-44p1-mono-9secs/Part04.wav" wrote "junk_out/Turbine-16-44p1-mono-22secs/Part01.wav" wrote "junk_out/Turbine-16-44p1-mono-22secs/Part02.wav" wrote "junk_out/Turbine-16-44p1-mono-22secs/Part03.wav" wrote "junk_out/Turbine-16-44p1-mono-22secs/Part04.wav" wrote "junk_out/Turbine-16-44p1-mono-22secs/Part05.wav" wrote "junk_out/Turbine-16-44p1-mono-22secs/Part06.wav" wrote "junk_out/Turbine-16-44p1-mono-22secs/Part07.wav" wrote "junk_out/Turbine-16-44p1-mono-22secs/Part08.wav" wrote "junk_out/WashingMachine-16-44p1-stereo-10secs/Part01.wav" wrote "junk_out/WashingMachine-16-44p1-stereo-10secs/Part02.wav" wrote "junk_out/WashingMachine-16-44p1-stereo-10secs/Part03.wav" wrote "junk_out/WashingMachine-16-44p1-stereo-10secs/Part04.wav" wrote "junk_out/WashingMachine-16-44p1-stereo-10secs/Part05.wav" wrote "junk_out/WashingMachine-16-44p1-stereo-10secs/Part06.wav" wrote "junk_out/WashingMachine-16-44p1-stereo-10secs/Part07.wav" wrote "junk_out/multipleSounds-16-16-mono-18secs/Part01.wav" wrote "junk_out/multipleSounds-16-16-mono-18secs/Part02.wav" wrote "junk_out/multipleSounds-16-16-mono-18secs/Part03.wav" wrote "junk_out/multipleSounds-16-16-mono-18secs/Part04.wav" wrote "junk_out/multipleSounds-16-16-mono-18secs/Part05.wav" wrote "junk_out/multipleSounds-16-16-mono-18secs/Part06.wav" wrote "junk_out/multipleSounds-16-16-mono-18secs/Part07.wav"

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Audio I/O and Waveform Generation dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by