简体   繁体   中英

Optimizing Fourier Series Fitting Function Matlab

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM