What is the problem with the size of the moving window (movfun) in octave?

Viewed 65

I'm trying to use movfun (moving window) in octave

x = -1000:0.1:1000
y = sin(x)
movfun(@(arg) printf("%d\n", size(arg)), y(1:100), 4)

I expect to see all 4s

However, surprisingly (to me) I get:

4
1
4
97
2
1
3
1
3
1
ans =

 Columns 1 through 29:

   4   4   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5

 Columns 30 through 58:

   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5

 Columns 59 through 87:

   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5

 Columns 88 through 100:

   5   5   5   5   5   5   5   5   5   5   5   5   4

What is wrong with my expectations?

1 Answers

This sheds a bit more light (I've increased window size to 8 for more clarity):

movfun( @(x) size(x, 1), y(1:100), 8 )
movfun( @(x) size(x, 2), y(1:100), 8 )

These produce the following outputs respectively:

4   5   6   7   8   8   8   8   [...]  8   8   8   8   7   6   5
1   1   1   1   93  93  93  93  [...]  93  93  93  93  1   1   1

This tells us that, presumably, under the hood, movfun makes sure to pass each 8-element vector in vertical form for processing, and that it treats the edges specially as single inputs, but then grabs all the 'non-edge' elements and performs some form of vectorised calculation involving all 93 columns at the same time.

We can confirm the 'edge' behaviour from the documentation:

"Endpoints"

This property controls how results are calculated at the boundaries (endpoints) of the window. Possible values are:

"shrink" (default)
The window is truncated at the beginning and end of the array to exclude elements for which there is no source data. For example, with a window of length 3, 'Y(1) = FCN (X(1:2))', and 'Y(end) = FCN (X(end-1:end))'.

Related