How do i make my code faster?

Hello,
so i´ve written a code for creating random spectra. It will compute a random spectra between -pi and pi. After one fouriertransformation, a gaussianfilter and one backtransformation i got a 700 x 1 double as my spectra i need. So this basicly works fine. Now i want to have like 9000 times this 700 x 1 spectra in one structure. I made a while loop and a counter to fill every row of the struct with a new created spectra on every new loop. BUT of course it takes like, i assume, an hour to make 9000 of these 700x1 spectra. How can i make my code faster? I think its because matlab has to reread the struct wich is growing with every loop. Is there any good solution to avoid this? enclosed my Code (iam new to matlab, sorry for bad programming):
Sorry for this Big question.
% Defining Number of Spectra/Event
No_event=700;
No_spectra=10;
% Defining Guassfilter parameter
offset=350;
width=50;
Amplitude=5;
% Defining spectra y-value
randPosPi=-pi;
randNegPi= pi;
% Predefining struct
k=zeros(1,No_event);
a=1:No_spectra;
c=cell(size(a'));
field = 'spectra';
struct = struct(field,c);
[struct.spectra]=deal(k');
r = 0;
% Guassianfilter
x=1:No_event;
l(x)=Amplitude*exp((-(x-offset).^2)/((2*width)^2));
lstr=l';
% Computing Spectra
while r <= No_spectra
for spectra=1:No_spectra
% generating random numbers between -pi and pi
randomPi = randPosPi + (randNegPi-randPosPi).*rand(No_event,1); %random numbers between -pi and pi
% Fouriertransformation
fourier_of_randomPi=fft(randomPi);
Pi_gauss=lstr.*fourier_of_randomPi; %guass multiplied with fft of random Pi
%Pi_gauss_real=lstr.*realteil;
% Backtransformation
backfft_randomPi=fft(Pi_gauss);
Spektrum=lstr.*abs(real(backfft_randomPi));
% inserting double data from Spektrum into struct
struct(1+r).spectra=deal(Spektrum);
end
r=r+1;
if r==(No_spectra);
break
end
end

5 Comments

I made a slight improvement of the calculation time by deleting the struct defining and just making it global. But it still takes like 10 minutes to compute 2600 spectra. Thats only a third of the amount i need.
Using Globals here (and more generally anytime you can avoid it) is highly unadvised.
It seems the for loop is not necessary either. You could just simulate a matrix of random values:
randomPi = randPosPi + (randNegPi-randPosPi).*rand(No_event,No_Spectra); %random numbers between -pi and pi
fft will treat each column of the matrix as a different variable anyhow. But you may want to use a function like bsxfun to complete your multiplications sown the line. As an example you would want to calculate:
Pi_gauss = bsxfun(@times,lstr,fourier_of_randomPi);
Then, the while loop seems unnecessary as you are doing exactly 10 loops. So your script can avoid having to check the if statement every iteration.
Lastly, it seems as though you are storing 10 sets of 700 values here, but you are performing computations 100 times (10x in while loop and 10x in for loop). I have a feeling you are doing something not quite intended to only store the values from the last run of each for loop.
doc profile
You should always profile your code before starting trying to optimise. It will tell you where the slow areas really are.
Thanks guys. Thats really awesome. Right now i am computing 10000 of random spectra in an eyeblink. The only thing that is time wasting is writing all the data into the struct so i get a 10000x1 struct. Any quick tips on that?
thanks again.
Is there any need to have a struct? You could just store all this data in a pre-allocated matrix.

Sign in to comment.

Answers (1)

Jan
Jan on 21 Apr 2016
Edited: Jan on 21 Apr 2016
If you want to improve the speed of code, find the bottleneck at first. It is useless to improve a part of the code, which uses 0.5% of the total time only. The profiler is the tool for such investigations. I assume fft is the most demanding part, so your code does not matter. Can you reduce the number of fft calls?
Note: Replace this:
r = 0;
while r <= No_spectra
...
r = r+1;
if r == (No_spectra); % Not require, because <= No_spectra is checked also
break
end
end
by:
for r = 0:No_spectra
...
end
This will not reduce the runtime but it looks nicer and the higher the readability, the easier is the debugging.
This:
struct = struct(field,c);
is a very bad idea, because to shadow the builtin function with a variable. Be sure to use another name.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 21 Apr 2016

Commented:

on 21 Apr 2016

Community Treasure Hunt

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

Start Hunting!