# Convert buffer function existing in matlab to julia language

**URL:** <https://discourse.julialang.org/t/convert-buffer-function-existing-in-matlab-to-julia-language/98926>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 16, 2023, 11:43am UTC](https://discourse.julialang.org/t/convert-buffer-function-existing-in-matlab-to-julia-language/98926 "2023-05-16T11:43:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RDuarte](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@RDuarte](https://discourse.julialang.org/u/RDuarte)\
**Post date:** [May 16, 2023, 11:43am UTC](https://discourse.julialang.org/t/convert-buffer-function-existing-in-matlab-to-julia-language/98926/1 "2023-05-16T11:43:02Z")

</div>

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](https://www.mathworks.com/help/signal/ref/buffer.html)) 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!

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [May 16, 2023, 12:22pm UTC](https://discourse.julialang.org/t/convert-buffer-function-existing-in-matlab-to-julia-language/98926/2 "2023-05-16T12:22:41Z")

</div>

I have used Matlab, but not that function.  
Perhaps the [partition](https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.partition) function in the Iterators package would work for you?

---

<div class="post-metadata">

**Author:** ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)\
**Post date:** [May 16, 2023, 5:50pm UTC](https://discourse.julialang.org/t/convert-buffer-function-existing-in-matlab-to-julia-language/98926/3 "2023-05-16T17:50:02Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![RDuarte](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@RDuarte](https://discourse.julialang.org/u/RDuarte)\
**Post date:** [May 17, 2023, 9:53am UTC](https://discourse.julialang.org/t/convert-buffer-function-existing-in-matlab-to-julia-language/98926/4 "2023-05-17T09:53:57Z")

</div>

> [@Jake](#):
>
> `@show(numfreq, numavg, indadd)`

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.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [May 17, 2023, 11:37am UTC](https://discourse.julialang.org/t/convert-buffer-function-existing-in-matlab-to-julia-language/98926/5 "2023-05-17T11:37:46Z")

</div>

> [@frylock](#):
>
> Perhaps the [partition](https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.partition) function…

The `partition` function from IterTools package is more appropriate ( [partition](https://juliacollections.github.io/IterTools.jl/latest/#partition(xs,-n,-%5Bstep%5D)-1) ). In Julia 1.9:

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 17, 2023, 1:04pm UTC](https://discourse.julialang.org/t/convert-buffer-function-existing-in-matlab-to-julia-language/98926/6 "2023-05-17T13:04:51Z")

</div>

Sounds like you want something like:

```julia
@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`](https://docs.julialang.org/en/v1/base/arrays/#Views-(SubArrays-and-other-view-types)) 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
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](https://sourceforge.net/p/octave/signal/ci/default/tree/inst/buffer.m) puts zeros at the beginning and end for some reason:

```plaintext
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](https://github.com/JuliaArrays/PaddedViews.jl)). (The explicit-loop implementation by @GunnarFarneback in [another thread](https://discourse.julialang.org/t/converting-python-code-into-julia-code/98991/7) has the zero-padding built-in, but doesn’t return views.)
