Convert buffer function existing in matlab to julia language

Hello everyone,
I’ve been using Matlab for years but now I would like to move to Julia, however I’m having some doubts regarding equivalent functions in Julia.
The problem is, in matlab I use the function buffer (Buffer signal vector into matrix of data frames - MATLAB buffer) and now I would like to use the same one in Julia.
Do you know the equivalent function of buffer in Julia.
Thank you in advance!

I have used Matlab, but not that function.
Perhaps the partition function in the Iterators package would work for you?

1 Like

Likewise I have not used that function. Rather for that functionality I use:

    j = 1               # start of first block
    k = j + bs - 1      # block finish location
    npoint = length(data)
    # numfreq = floor(Int, bs/2.56) + 1
    Δf = fs/bs
    numfreq = floor(Int, fmax/Δf) + 1
    # @show(npoint, bs, overlap)
    numavg = floor(Int, (npoint-bs*overlap)/(bs*(1.0-overlap)))  # works if no synchonous avg
    indadd = floor(Int, bs*(1.0 - overlap))             # indicies to add for next block
    # @show(numfreq, numavg, indadd)

where
bs is block size (frame size)
fs is sampling frequency

Thank you Jake for your help.
However, I was expecting to obtain a matrix, not a number.
Probably I’m doing something wrong…
Thank you in advance for your time.

The partition function from IterTools package is more appropriate ( partition ). In Julia 1.9:

julia> import IterTools as Itr

julia> v = [1:10...]
10-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

julia> Itr.partition(v,4,2) .|> collect |> splat(hcat)
4×4 Matrix{Int64}:
 1  3  5   7
 2  4  6   8
 3  5  7   9
 4  6  8  10

The parameters 4 and 2 of partition determine the ‘length’ and ‘step’ of the frames.

1 Like

Sounds like you want something like:

@views buffer(X, n, p=0) = [X[i:i+n-1] for i in firstindex(X):n-p:lastindex(X)-n+1]

This returns an array of views of X rather than copies.

If you want to stack these “frames” into a matrix, similar to Matlab, you can do stack(buffer(X, n, p)), but I would eventually try to re-think your code to avoid making copies of all the data like this. (In general, Matlab contorts you into a particular “vectorized” style of programming because Matlab loops are slow, and there is often a faster and more natural way to do things in Julia once you get used to it.)

e.g.

julia> stack(buffer(1:11, 3, 1))
3×5 Matrix{Int64}:
 1  3  5  7   9
 2  4  6  8  10
 3  5  7  9  11

I’m not sure if this is exactly the desired output? I don’t have Matlab handy, but the GNU Octave buffer function puts zeros at the beginning and end for some reason:

octave:1> buffer(1:11, 3, 1)
ans =

    0    2    4    6    8   10
    1    3    5    7    9   11
    2    4    6    8   10    0

You could easily tweak my implementation above to do this, e.g. by first zero-padding X (or, to avoid copies, by wrapping X in a zero-PaddedView from PaddedViews.jl). (The explicit-loop implementation by @GunnarFarneback in another thread has the zero-padding built-in, but doesn’t return views.)

1 Like