Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Hi *,

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

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
```

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"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.

```
avg_i = (x_i + x_i+1 + ... + x_n+i-1)/n
```

Then, the next window moving one element forward is:
```
avg_i+1 = ( x_i+1 + ... + x_n+i-1 + x_n+i)/n
```

Notice that:
```
avg_i+1 = avg_i + (x_n+i - x_i)/n
```

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)

http://www.mathworks.com/help/techdoc/ref/filter.html

See the Example section.

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

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):

```
tmpTabs = outputTabs(numberIndices{n});
tmpTrel = outputTrel(numberIndices{n});
lastfound = 0;
for start=beginning:inc:finish-windowWidth
winidx = find(tmpTabs(lastfound=1:end)>=start && tmpTabs(lastfound=1:end)<=start+windowWidth);
if numel(winidx)>0
window = tmpTrel(winidx+lastfound);
lastfound = winidx(end);
...
else
...
end
end
```

If you want more help please make your code runnable defining all variables and providing small data to test.

conv() and kernel with (ones(windowsize)/windowsi

I've tested it out and its quite fast:

```
len = [10000,300000,2000000];
windowsize = 2000;
% convolution
for turn=1:numel(len)
randomsignal = rand(len(turn),1);
tic
kernel = ones(windowsize,1)/windowsize;
movingavg_c = conv(randomsignal,kernel);
toc
end
```

```
Elapsed time is 0.012252 seconds.
Elapsed time is 0.330076 seconds.
Elapsed time is 2.189201 seconds.
```

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.

Many thanks,

James

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.

Paul

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

```
% the mean using filter
mean = filter(ones(1,n)/n,1,signal);
% moving SD using filter
signal2 = 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..
```

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trialyou find some pseudo codeand nice explanation here:

http://www.dspguide.com/ch2/2.htm

Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

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.