faster "log10" command

10 views (last 30 days)
Ricky
Ricky on 8 Dec 2011
Hi all, Is anyone know how to use the "log10" more wisely? currently, I convert from dB to decimal using log10 but it seems to slow down the execution process. Any help would be appreciated!
Ta
[EDITED, 09-12-2011 08:13 UTC, Jan Simon] Code copied from Answer section:
Sorry guys for the late reply, and thanks for the comment. Anyway, my code look like this:
for trial = 1:1000
for a = 1:N
for b=1:N
interfere(b,a) = dBtodec(Pr) * power(dstsr(b,a), -(dBtodec(gamma))))) * ...
Li(b) * Ri(b);
end
%
sinrSUr(a) = dBtodec(Pr) * power(ds(a), -(dBtodec(gamma))))) * ...
LSUr(a) * RSUr(a)) / (dBtodec(No) + sum(interfere(:,a)));
snr(a) = (dBtodec(Pr)*(power(ds(a), (-(dBtodec(gamma))))) * ...
LSUr(a) * RSUr(a)) / (dBtodec(No));
end
end
where dBtodec is a function which is:
function [decimal] = dBtodec(x)
%converting dB to decimal
decimal = 10*log10(x);
end
hope that make sense
  6 Comments
Ricky
Ricky on 11 Dec 2011
sorry i missed out 10 in the code, suppose to be log1o instead of just log. my bad.
Jan
Jan on 12 Dec 2011
@Rak: You can simply edit your question to fix this.

Sign in to comment.

Accepted Answer

Jan
Jan on 9 Dec 2011
Calculating -(dBtodec(gamma)) and the other constants repeatedly wastes time. Better do it once and store the value in a temporary variable. All calculations can be performed without loops. E.g.:
snr = dBtodec(Pr) .* power(ds, -dBtodec(gamma)) .* LSUr .* RSUr ./ dBtodec(No);
  2 Comments
Ricky
Ricky on 11 Dec 2011
how would i do that with 2D array?
Jan
Jan on 12 Dec 2011
If all arrays have the same size ".*" multiplies them elementwise. If some are vectors and each element should be multiplied with all elements of a subvector fo the 2D-array, use BSXFUN, which "inflates" the vector "virtually":
x = rand(3, 3); b = rand(1, 3);
y = bsxfun(@times, x, b); % or: y = x .* b(ones(1,3), :)

Sign in to comment.

More Answers (4)

Daniel Shub
Daniel Shub on 8 Dec 2011
I am guessing you are not preallocating ...
Does you code look something like:
x = randn(1e7,1);
for ii = 1:length(x)
y(ii) = log10(x(ii));
end
You could replace it with
y = log10(x);
  4 Comments
Jan
Jan on 8 Dec 2011
Then I guess, that we will get a speedup of >55% if we apply our experiences on Rak's code. But even then this will *not* be an advantage: Currently Rak has waited 16 hours for the answer! It will be hard to recover this delay even with the fastest code...
Jan
Jan on 9 Dec 2011
Damn English. Sometimes I'm too confused. While "I guess" is nonsense here, I meant "I bet". And if I had written this, I'd won. What a pitty.

Sign in to comment.


Daniel Shub
Daniel Shub on 9 Dec 2011
You may want to look at
doc db2mag
doc mag2db
doc pow2db
doc db2pow
they are not going to speed up your code, but they do the transformations in the correct direction and use the correct log base and scale factors ...
  1 Comment
Jan
Jan on 9 Dec 2011
See: http://en.wikipedia.org/wiki/Anti-pattern , Do not re-invent the square wheel. +1

Sign in to comment.


Sean de Wolski
Sean de Wolski on 8 Dec 2011
On my system:
A = magic(10000); %Don't do this!
tic,log10(A);toc
Elapsed time is 1.388229 seconds.
1.39 seconds to calculate the log10 of 10000^2 elements seems pretty good, so you probably have something else slowing you down. How much memory are you using?
b = whos;
sum(b(:).bytes)
If you're using more memory than you have RAM available that's quite possibly your issue.

Ricky
Ricky on 11 Dec 2011
Thx people, I manage to speed up my code now, thanks to you all esp. Jan
  2 Comments
Ricky
Ricky on 12 Dec 2011
and fixed everything else
Daniel Shub
Daniel Shub on 12 Dec 2011
the best way to thank people is to accept the best answer and vote for the other answers that helped you. This lets future people with similar questions and problems learn what you learned.

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!