我编写了一个 MATLAB 代码,用于从 SAC(地震)文件(通过另一个代码读取)中查找地震信号(例如 P 波)。该算法称为 STA/LTA 触发算法(实际上对我的问题并不重要)
重要的是,实际上这段代码运行良好,但由于我的地震文件太大(1GB,即两个月),执行需要将近 40 分钟才能看到结果。因此,我觉得有必要优化代码。
我听说用高级函数替换循环会有所帮助,但由于我是 MATLAB 的新手,我不知道如何去做,因为代码的目的是扫描每个时间序列。另外,我听说预分配可能会有所帮助,但我只知道如何实际执行此操作。
由于此代码是关于地震学的,因此可能难以理解,但我在顶部的注释可能会有所帮助。我希望我能在这里得到有用的建议。以下是我的代码。
function[pstime]=classic_LR(tseries,ltw,stw,thresh,dt)
% This is the code for "Classic LR" algorithm
% 'ns' is the number of measurement in STW-used to calculate STA
% 'nl' is the number of measurement in LTW-used to calculate LTA
% 'dt' is the time gap between measurements i.e. 0.008s for HHZ and 0.02s for BHZ
% 'ltw' and 'stw' are long and short time windows respectively
% 'lta' and 'sta' are long and short time windows average respectively
% 'sra' is the ratio between 'sta' and 'lta' which will be each component
% for a vector containing the ratio for each measurement point 'i'
% Index 'i' denotes each measurement point and this will be converted to actual time
nl=fix(ltw/dt);
ns=fix(stw/dt);
nt=length(tseries);
aseries=abs(detrend(tseries));
sra=zeros(1,nt);
for i=1:nt-ns
if i>nl
lta=mean(aseries(i-nl:i));
sta=mean(aseries(i:i+ns));
sra(i)=sta/lta;
else
sra(i)=0;
end
end
[k]=find(sra>thresh);
if ~isempty(k)
pstime=k*dt;
else
pstime=0;
end
return;