Loops slowing down dramatically with increased iterations
Mostrar comentarios más antiguos
I am running a code that splits a folder of long-duration acoustic files into shorter-duration files. Time is kept track of by sample number and timestamps in the filenames. Once chunks of data per shorter time period are loaded in, these shorter files are saved with the appropriate timestamp. There are series of for loops and if statements to meet the needs of the goal. Unfortunately, the script starts out really fast and then increasingly becomes slower and slower with increasing iterations. I have read similar questions regarding this issue, but I am unable to identify why this is happening still. It appears that memory handling might be the source?
Any suggestions on how to prevent this would be greatly appreciated, as well as any suggestions to increase efficiency. I am using Matlab 2017a on a Linux OS.
clear all; close all; clc;
datadir = uigetdir('Select data folder with all files to be split');
cd(datadir)
flist = dir('*.wav');
rec_length = input('Input length of recording required in seconds: ');
fname = flist(1).name;
Ndot = find(fname == '_'); %will look for the indices of underscores
NdotPrefix = Ndot(3); %which underscore precedes the timestamp
FilePrefix = fname(1:NdotPrefix); %Gets filename part that precedes timestamp
NdotSuffix = Ndot(5); %which underscore follows the timestamp
FileSuffix = fname(NdotSuffix:end); %Gets filename part that follows timestamp
clear fname
for nf = 1:length(flist)
disp(['Input file No.: ',num2str(nf)])
fname = flist(nf).name;
if nf == 1
I = audioinfo(fname);
Ns = I.TotalSamples; %total number of samples
Fs = I.SampleRate;
NS_rec = rec_length*Fs; % section length in number of samples, e.g. 60*Fs for 1 min files
Nrec = floor(Ns/NS_rec); %This determines how many complete files will be made from the original file, e.g. how many minutes
timestep = 0;
OrigTime = fname(NdotPrefix+1:NdotSuffix-1);
start_time = datenum(OrigTime,'yymmdd_HHMMSS');
for nr = 1:Nrec+1 %breaking up recording to X min, then the +1 indicates what is left over as may not make a whole X min file
if nr <= Nrec
S = audioread(fname, [NS_rec*(nr-1)+1, NS_rec*nr]);
time = start_time + timestep*rec_length/24/60/60;
dst = datestr(time,31);
if nr == 1
newfname =['S_',FilePrefix,dst(3:4),dst(6:7),dst(9:10),'_',dst(12:13),dst(15:16),dst(18:19),FileSuffix];
else
newfname =['S_',FilePrefix,dst(3:4),dst(6:7),dst(9:10),'_',dst(12:13),dst(15:16),dst(18:19),FileSuffix];
end
audiowrite(newfname, S, Fs);
timestep = timestep + 1;
clear S time dst newfname
else % this is for the remaining data at the end of the original that is under X min and wont make a whole Xmin file
SExtra = audioread(fname, [NS_rec*(nr-1)+1, I.TotalSamples]);
end
end
clear I Ns Fs NS_rec Nrec OrigTime
else
I = audioinfo(fname);
Ns = I.TotalSamples + length(SExtra);
Fs = I.SampleRate;
NS_rec = rec_length*Fs;
Nrec = floor(Ns/NS_rec);
offset = length(SExtra);
for nr = 1:Nrec+1
if nr <=Nrec
if nr == 1
S = [SExtra;(audioread(fname, [NS_rec*(nr-1)+1, NS_rec*nr-offset]))];
time = start_time + timestep*rec_length/24/60/60;
dst = datestr(time,31);
newfname =['S_',FilePrefix,dst(3:4),dst(6:7),dst(9:10),'_',dst(12:13),dst(15:16),dst(18:19),FileSuffix];
audiowrite(newfname, S, Fs);
timestep = timestep + 1;
clear SExtra S time dst newfname
else
S = audioread(fname, [NS_rec*(nr-1)+1-offset, NS_rec*nr-offset]);
time = start_time + timestep*rec_length/24/60/60;
dst = datestr(time,31);
newfname =['S_',FilePrefix,dst(3:4),dst(6:7),dst(9:10),'_',dst(12:13),dst(15:16),dst(18:19),FileSuffix];
audiowrite(newfname, S, Fs);
timestep = timestep + 1;
clear S time dst newfname
end
else
SExtra = audioread(fname, [NS_rec*(nr-1)+1-offset, I.TotalSamples]);
if nf == length(flist)
time = start_time + timestep*rec_length/24/60/60;
dst = datestr(time,31);
newfname =['S_',FilePrefix,dst(3:4),dst(6:7),dst(9:10),'_',dst(12:13),dst(15:16),dst(18:19),FileSuffix];
audiowrite(newfname, SExtra, Fs);
timestep = timestep + 1;
end
end
end
clear I Ns Fs NS_rec Nrec offset
end
end
Respuesta aceptada
Más respuestas (1)
the cyclist
el 4 de Feb. de 2020
0 votos
I have not gone through your code, but this is a classic symptom of failing to preallocate matrices, and instead letting them grow incrementally. See this documentation for guidance.
1 comentario
Categorías
Más información sobre File Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!