필터 지우기
필터 지우기

How to do this more efficiently

조회 수: 1 (최근 30일)
S. David
S. David 2014년 5월 24일
댓글: S. David 2014년 6월 7일
Hello,
I have this piece of code in MATLAB:
for kk=0:N-1
for mm=0:N-1
for pp=1:Np
for qq=1:Np
if ((kk*Ts+tau(pp)))<=(mm*Ts+tau(qq))) && ((kk+1)*Ts+tau(pp))>(mm*Ts+tau(qq)))
thetaSR=(((kk+1)*Ts+tau(pp)))-((mm*Ts+tau(qq))));
F_SR_MR(kk+1,mm+1)=F_SR_MR(kk+1,mm+1)+conj(H(pp))*H(qq)*(thetaSR*exp(1i*pi*fc**thetaSR)*sinc(fc*thetaSR));
end
which obviously is not very efficient. How can I re-write it more efficiently?
Thanks
  댓글 수: 6
the cyclist
the cyclist 2014년 5월 25일
Do you have a reference (e.g. an image you can post) that shows the mathematical formula you are trying to replicate? It might be easier to build the code directly from that rather than optimizing yours.
S. David
S. David 2014년 5월 25일
I attached the formula. g(t) in it is a rectangular pulse of magnitude one over the period [0,Ts).

댓글을 달려면 로그인하십시오.

채택된 답변

Roger Stafford
Roger Stafford 2014년 5월 26일
It isn't necessary to do the inequality test for every possible pair of kk and mm values. Since kk and mm are integers and Ts must surely be a positive number, your pair of inequalities is logically equivalent to
kk-mm == d
where d = floor((tau(qq)-tau(pp))/Ts). Therefore you can simply add the appropriate vectors along corresponding diagonals of F. For large N, doing it this way should save quite a bit of computation time.
F=zeros(N);
for pp=1:Np
for qq=1:Np
d = floor((tau(qq)-tau(pp))/Ts);
kk = max(d,0):min(N-1,N-1+d);
mm = kk-d;
theta=(d+1)*Ts-(tau(qq)-tau(pp));
ix = d+1+(N+1)*mm;
F(ix) = F(ix)+conj(H(pp))*H(qq)*theta.*exp(1i*pi*fc*theta).*sinc(fc*theta);
end
end
  댓글 수: 12
S. David
S. David 2014년 5월 29일
편집: S. David 2014년 5월 29일
I didn't see your last response when I replied the last time.
I am glad you asked about this, because I was going to mention it next.
Actually, your are right, according to the equation attached earlier, the matrix should be circular. However, my code is a bit more complicated, and I though writing it this way would make things easier to understand. So, yes, the thetas here are meaningless.
The (k,m)th element of F is given by the equation attached below. You can notice the presence of the exponential inside the integral changes the situation, and the matrix is no longer circular. I think this answers all your questions.
In this case the code would be:
clear all;
clc
N=512;
fc=12*10^3;
B=8000;
df=B/N;
T=1/df;
Ts=T/N;
tau=[0 5 7 20].*10^-3;
h=[1 0.8 0.7 0.5];
Np=length(tau);
H=h.*exp(-1i*2*pi*fc*tau);
F=zeros(N);
a=[0.001 0.0012 0.0024 0.003];
for kk=0:N-1
for mm=0:N-1
for pp=1:Np
for qq=1:Np
if ((kk*Ts+tau(pp))/(1+a(pp)))<=(mm*Ts+tau(qq))/(1+a(qq)) && ((kk+1)*Ts+tau(pp))/(1+a(pp))>(mm*Ts+tau(qq))/(1+a(qq))
theta1=(((kk+1)*Ts+tau(pp))/(1+a(pp)))-((mm*Ts+tau(qq))/(1+a(qq)));
theta2=(((kk+1)*Ts+tau(pp))/(1+a(pp)))+((mm*Ts+tau(qq))/(1+a(qq)));
F(kk+1,mm+1)=F(kk+1,mm+1)+conj(H(pp))*H(qq)*(theta1*exp(1i*pi*fc*(a(qq)-a(pp))*theta2)*sinc(fc*(a(qq)-a(pp))*theta1));
elseif (mm*Ts+tau(qq))/(1+a(qq))<=(kk*Ts+tau(pp))/(1+a(pp)) && (((mm+1)*Ts+tau(qq))/(1+a(qq)))>((kk*Ts+tau(pp))/(1+aSR))
theta1=(((mm+1)*Ts+tau(qq))/(1+a(qq)))-((kk*Ts+tau(pp))/(1+a(pp)));
theta2=(((mm+1)*Ts+tau(qq))/(1+a(qq)))+((kk*Ts+tau(pp))/(1+a(pp)));
F(kk+1,mm+1)=F(kk+1,mm+1)+conj(H(pp))*H(qq)*(theta1*exp(1i*pi*fc*(a(qq)-a(pp))*theta2)*sinc(fc*(a(qq)-a(pp))*theta1));
end
end
end
end
end
Hope it makes more sense now.
Can we still follow the same approach you did previously in this case?
Thanks
S. David
S. David 2014년 6월 7일
Any hint?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

the cyclist
the cyclist 2014년 5월 24일
Especially if N is large, you might get a huge speedup if you preallocate the memory for F_SR_MR. Put the line
F_SR_MR = zeros(N,N);
ahead of the loops.
  댓글 수: 1
S. David
S. David 2014년 5월 24일
It is already there.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by