I have the attached code, which I use to compute a moving window average. However, it is rather slow. Is there a more efficient way to implement it? Is there a built in function for this kind of thing.

outputTabs and outputTrel are waht I want moving averages of.

I am using Octave, however as it is so similar to Matlab any suggestions for Matlab would also be useful, and I may be able to implement it in Octave as well.

Many thanks,
James

inc = 1000; windowWidth = 1000; beginning = 1;%finish-windowWidth-inc; k = 1; for start=beginning:inc:finish-windowWidth % Find window of absolute spikes window1 = find(outputTabs(numberIndices{n})<=start+windowWidth); window2 = find(outputTabs(numberIndices{n}(window1))>=start); window = outputTrel(numberIndices{n}(window1(window2))); if numel(window)>0 avg{n}(k) = mean(window); error{n}(k) = std(window); numInPat{n}(k) = (100/(numel(window)))*(numel(find(window>0))); else avg{n}(k) = 0; error{n}(k) = 0; numInPat{n}(k) = 0; end k = k + 1; end

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Are the avg{n}(k) arrays built dynamically or you you specify the size beforehand? I know MATLAB lets you just grow arrays but it slows it down a lot. If you allocate the arrays for their max size before you start the loop, it will run much faster.

MATLAB has a function that gets the mean and standard deviation at the same time. If you have a function like that it could speed it up a bit.

For standard deviation remember that stddev = sqrt(mean(x^2) - mean(x)^2) so if you use phoffric's excellent suggestion to speed up the means, also track the mean of the squares so you can get the stddev just as quickly.
In case the above formula is diffucult to read:
standard deviation = square root of (the average of the X^2 values minus the square of the mean)

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

However, this works if the signal is spaced regularly. It looks like this is not your case since you have to find outputTabs values in a current window. I don't think you can avoid loops.

One possible way to speed this up is to avoid running find operation across whole outputTabs vector all the times.

First, store temporary vectors into a variables. Indexing and other operations do take time. For example, why you have to index outputTabs(numberIndices{n}) at every loop?

Second, make sure the vector is sorted. Since your segments for moving average are not overlapping, you can take advantage removing those values that has been used already.

Something like this (I didn't test it, sorry for possible mistakes):

phorric, your suggestions is very elegant however for simplicity I think Kendor's suggestion lends itself better to Octave/Matlab. If I was doing this in C (and I may be) this this would be perfect (and I shall remember it!).

yuk99: as you point out the code I already have could be made more streamline, however it still seems to be going quite slow do to the loops.

Kendor: is there a way to do the std like this as well? I can't think how to form the kernel.

My implementation in matlab (years ago) was extremely easy. A convolution has many more operations. You should try to implement the moving average suggestion in octave, and you should see a significant performance improvement.

I would then go with the function filter... something as proposed by Scott:

% the mean using filtermean = filter(ones(1,n)/n,1,signal);% moving SD using filtersignal2 = filter(ones(1,n),1,signal.^2);variance=(signal2-n*mean.^2)/(n-1);stddev=sqrt(variance);% remember: the last n-1 values ar invalid...mean(1:(n-1))=[];stddev(1:(n-1))=[];

or with convolutions this would somehow look like:

kern = ones(N,1); meanT = conv(signal,kern); squared = signal.*signal; stddev=sqrt((conv(squared,kern)-(meanT.^2)/N)/(N-1)); mean = meanT/N; % remember here: you have done a convolution this adds padding - so the first N-1 values as well as the last N-1 values are not valid... stddev(1:(N-1))=[] % etc..

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

remains to say that the implementation you choose depends on how good/fast matlab/octave has implemented either conv or filter - I believe they must be super fast as they are often used. Otherwise you better go with phoffrics style of implementation.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for freeEdge Out The Competitionfor your dream job with proven skills and certifications.Get started todayStand Outas the employee with proven skills.Start learning today for freeMove Your Career Forwardwith certification training in the latest technologies.Start your trial today

MATLAB has a function that gets the mean and standard deviation at the same time. If you have a function like that it could speed it up a bit.