0

I am trying to iterate through a set of samples that seems to show periodic changes. I need continuously apply the fit function to get the fourier series coefficients, the regression has to be n samples in the past (in my case, around 30). The problem is, my code is extremely slow! It will take like 1 hour to do this for a set of 50,000 samples. Is there any way to optimize this? What am I doing wrong?

Here's my code:

function[coefnames,coef] = fourier_regression(vect_waves,n)        

    j = 1;
    coef = zeros(length(vect_waves)-n,10);

    for i=n+1:length(vect_waves)

        take_fourier = vect_waves(i-n+1:i);
        x = 1:n;
        f = fit(x,take_fourier,'fourier4');
        current_coef = coeffvalues(f);
        coef(j,1:length(current_coef)) = current_coef;
        j = j + 1;



    end
    coefnames = coeffnames(f);

end

When I call [coefnames,coef] = fourier_regression(VECTOR,30); This takes forever to compute. Is there any way to fix it? What's wrong with my code?

Note: I have a intel i7 5500 U cpu, 16GB RAM, and using Matlab 2015a.

Luis Cruz
  • 1,488
  • 3
  • 22
  • 50
  • Where does the `coeffnames` come from? You do not define that one before you call it in your penultimate line. I suggest you also state what is contained within `vect_waves`, i.e. what sort of numbers and possibly post the first 10 or so lines so we can try the code ourselves. Also please post what kind of system you are on: CPU, RAM, OS, MATLAB version, that way we can see whether it is hardware/software related. – Adriaan Oct 27 '15 at 09:32
  • Coeffnames comes from "fit", it is given automatically by Matlab when you call "fit" using Fourier. vect_waves is pretty much a vector with 500k samples containing a signal, that's all (a bunch of numbers between [-1,1]). I posted more info of my hardware on the post. – Luis Cruz Oct 27 '15 at 11:47
  • So basically that's ALL the code I have. – Luis Cruz Oct 27 '15 at 11:50

1 Answers1

0

As I am not familiar with your application, I am not sure whether it is possible to vectorize the code to improve performance. However, I have a couple of other tips.

One thing you should consider is preallocation of arrays. In this case, you should preallocate at least the array coef since I believe you do know its size before starting the loop.

Another thing I suggest is to profile your code. This will provide information on what parts of your code are consuming the most time, helping you focus your effort on improving those parts' performance.

mikkola
  • 3,376
  • 1
  • 19
  • 41
  • I have tried preallocating the arrays but on this case, it did not help at all. I am not sure if it would be possible to vectorize my code, since I need to use the fit function N times and not just one time. I am guessing the "fit" function is just slow, so I might consider writing my own function for getting the fourier series coefficients, but just wanted to make sure if there was anything else I could do instead... – Luis Cruz Oct 27 '15 at 08:22