How to speed for or while loop code?

1 view (last 30 days)
Sanjay Singh Negi
Sanjay Singh Negi on 12 Oct 2019
Commented: prasanth s on 13 Oct 2019
Here is the following code I have written to find out the optimum truncation for my time series.
But this code is really very slow. Kindly help me on this.
P=0.2; g=4; K=4; % constants
N=18000; % supposing a length of full time series
n=1500; % length of the window
const=P^g+(1-P^g)*(K/(sqrt(2)*n));
syms m l nt
omeg=const*symsum(((-pi/2)*(((l/N)-m)^2)*K^2),m,1,round((n/2)-1));
nt=0;
esw=0;
while esw<0.8
nt=nt+1;
esw=symsum(omeg,l,0,nt)/symsum(omeg,l,0,round((n-1)/2));
trunc(nt)=esw;
end
many thanks in advance.
  4 Comments
sanjay Negi
sanjay Negi on 13 Oct 2019
Yes @Kalyan Acharjya, N and n are basically deciding the truncation point. The details I would like to explain down below. Kindly follow.
@ Daniel, It could be symbolic. No, trunc is not preallocated. This will provide me the truncated point (nk).
So, here what I am try to do.
If suppose x(t) is a time series.
then
xs (l)= x[l+(s-1)(n-m+1)] ,
where l=0,1,.....n-1.
n is the length of the window and m is the amount of overlap.
so, s are the number of segmented windows which can be represented as
s = 1,2,......(n-1)+(s-1)(n-m+1) ≤ N.
N is the length of the time series x(t)
Now, the the amount of overlap m will depend upon the rate of change of P^g, which is the polarization factor calculated with the first selected window ( in cour case n=1500).
K=nW is another parameter which we decide and defined as the time-bandwidth of the prolate taper.
So the truncation point is decided by the criterion,
,
where,
The following code is trying to adapt with each segment depending upon the window length(n), and full trace length N. The code should also change the estimated value of nt upon varying values of polarisation factor P, which I suppose is not doing so !!
In above code whatever the value range I insert for P = [0 to 1], g= [2 to 6] and K [4 to 6].
The main dependency goes with the length of segmented window.
Therefore, I keenly require your help to understand the basic ingredient.
More important is the performance of this code.
Thanks
prasanth s
prasanth s on 13 Oct 2019
in your code the variable 'esw' is not in double format after inside the loop. so it make a endless loop.
notes:
split the long formulas into smallel portions.
use temporary variables to store intermediate values. so the debugging becomes easy.

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!