Moving average

Hi all,

Is there a Scilab function to compute a running average over a specified number of column entries within an array:
i.e:
 Values in array 3 cell running average 6 5.3 3 4.7 7 7.0 4 5.3 10 4.0 2 0
Andy

Scilab 26-10-18, 8:20 p.m. Noiser1234
Not as such. However, a routine for calculating a simple moving average can be easily written. Here is one. Please test it extensively before relying on it.
Please feel free to improve this code and report fail cases.
```//Function to compute simple moving average.
//vec is a simple row or column vector
//windw is the number of data points over which the average has to be computed
//avg is a column vector consisting of the computed moving averages
//Example:
//-->sma([6,3,7,4,10,2,0],3)
// ans  =
//
//    5.3333333
//    4.6666667
//    7.
//    5.3333333
//    4.
//FOSSEE, IIT Bombay

function avg=sma(vec, windw)
avg=[]
for i=1:length(vec)-windw+1
av=sum(vec(i:i+(windw-1)))/windw
avg=[avg;av]
end
endfunction```

This seems to work perfectly with stings of numbers (as in your example above) - my only question is, can this be modified to work with a vector (i.e. 1x50 number array?).

Maybe I'm missing something in my code, but I get an error when I replace ([6,3,7,4,10,2,0],3)with ([x],3)where "x" represents my array of values.

Andy

29-10-18, 8:20 p.m. Noiser1234

I tried to execute the same sma function, as proposed by Noiser1234, through two approaches, and the results are as follows. The first approach:
--> sma([6,3,7,4,10,2,0],3)
ans =
5.3333333
4.6666667
7.
5.3333333
4.
And, the second approach:
--> iput = [6,3,7,4,10,2,0]; --> wndw = 3; --> movmean = sma(input,wndw);
at line 3 of function sma ( D:\Scilab\MovAvg.sce line 19 ) length: Wrong type for input argument(s).
It seems we need to work a bit on the implementation!
17-11-20, 9:01 p.m. abbas14