Search code examples
octavesmoothing

Octave: Smoothing a value-series (not a classical Moving average)


I'd need a function where o=f(v) returns a vector holding o(i)=(v(i)+v(i+1))/2 , so in other words:

(v(1)+v(2))/2
(v(2)+v(3))/2
(v(3)+v(4))/2
(v(4)+v(5))/2
...

So e.g. for the input vector

v = [1;3;5;3;9;9]

the result would be

[2;4;4;6;9]

What's that? A moving average? A smoothing function? In Matlab there seems to be "smooth" which could be what I'm looking for, but I don't see that in Octave. Thanks in advance for a hint.


Solution

  • There are various ways to do this. A straighforward implementation would be

    (v(1:end-1)+v(2:end))/2
    

    This gets a little bit cumbersome for more complex moving averages, in which case you could use a convolution

    conv(v, [0.5, 0.5], 'valid')
    

    here the second array [0.5, 0.5] - called the "kernel" - defines how consecutive values are weighted.

    Finally in the newer versions of octave there is also movmean.