# Scipy.signal.convolve in Julia

**URL:** <https://discourse.julialang.org/t/scipy-signal-convolve-in-julia/91140>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 2, 2022, 11:17am UTC](https://discourse.julialang.org/t/scipy-signal-convolve-in-julia/91140 "2022-12-02T11:17:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Resul.Akay](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Resul.Akay](https://discourse.julialang.org/u/Resul.Akay)\
**Post date:** [December 2, 2022, 11:17am UTC](https://discourse.julialang.org/t/scipy-signal-convolve-in-julia/91140/1 "2022-12-02T11:17:42Z")

</div>

Hi

I am looking for an implementation of [scipy.signal.convolve](https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve.html).  
Is there an implementation in Julia?

Or convolution Linear Filtering similar to R stats::filter

Thanks

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [December 2, 2022, 11:51am UTC](https://discourse.julialang.org/t/scipy-signal-convolve-in-julia/91140/2 "2022-12-02T11:51:20Z")

</div>

For 1D signals you may use the convolution in `DSP.jl`. See [`Convolutions`](https://docs.juliadsp.org/stable/convolutions/).

For 2D and multi dimensional arrays you may use the convolutions in `ImageFiltering.jl` implemented by [`imfilter()`](https://juliaimages.org/ImageFiltering.jl/stable/function_reference/#ImageFiltering.imfilter).  
Pay attention that by default it applies correlation. You may change that by applying `reflect()` on the kernel.

Personally, I wish for a method which is well optimized (Wrapping Intel IPP) which works on arrays in general and not tied to `DSP` or `Images` context.  
For small kernels I found `StaticKernels.jl` which is the fastest I could find on the Julia eco system.

If one day [`NNLib.jl`](https://github.com/FluxML/NNlib.jl/issues/74) will support the `OneDNN` backend (See [Use oneDNN · Issue #74 · FluxML/NNlib.jl · GitHub](https://github.com/FluxML/NNlib.jl/issues/74)), it might become a good CPU based implementation for convolution / correlation.

---

<div class="post-metadata">

**Author:** ![Resul.Akay](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Resul.Akay](https://discourse.julialang.org/u/Resul.Akay)\
**Post date:** [December 2, 2022, 4:05pm UTC](https://discourse.julialang.org/t/scipy-signal-convolve-in-julia/91140/3 "2022-12-02T16:05:02Z")

</div>

Thanks for your answer. I knew [convolutions](https://docs.juliadsp.org/stable/convolutions/), but it is not the exact implementation in R or Scipy. Scipy implementation is not too complicated. I will rewrite it in Julia.

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [December 2, 2022, 4:08pm UTC](https://discourse.julialang.org/t/scipy-signal-convolve-in-julia/91140/4 "2022-12-02T16:08:24Z")

</div>

The DSP methods should be similar to the Scipy ones, see e.g. [Translating a 1d convolution from Python to Julia - #4 by rafael.guerra](https://discourse.julialang.org/t/translating-a-1d-convolution-from-python-to-julia/85080/4). I think it would be better if you provide some examples of what you’re trying to do and where/why you think they are not behaving the same. Certainly much easier than rewriting your own functions from scratch.

---

<div class="post-metadata">

**Author:** ![Resul.Akay](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Resul.Akay](https://discourse.julialang.org/u/Resul.Akay)\
**Post date:** [December 2, 2022, 4:30pm UTC](https://discourse.julialang.org/t/scipy-signal-convolve-in-julia/91140/5 "2022-12-02T16:30:11Z")

</div>

I would like to apply linear convolution filtering to univariate time series similar to R’s `stats::filter`. I could not find such a method in Julia; I will use it for seasonal decomposition similar to R’s `stats::decompose` or Python’s `statsmodels.tsa.seasonal.seasonal_decompose`.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 3, 2022, 5:14pm UTC](https://discourse.julialang.org/t/scipy-signal-convolve-in-julia/91140/6 "2022-12-03T17:14:48Z")

</div>

You can also write your own convolution, if (for whatever reason) you don’t like DSP.jl:

```julia
julia> using FFTW

julia> toy_conv(a,b) = begin
           newlen = length(a)+length(b)-1
           pada = [a; zeros(newlen-length(a))]
           padb = [b; zeros(newlen-length(b))]
           real(ifft(fft(pada) .* fft(padb)))
       end
toy_conv (generic function with 1 method)

julia> toy_conv([1,1],[1,1,0])
4-element Vector{Float64}:
 1.0
 2.0
 1.0
 0.0

```

You can also write your own FFT, if (for whatever reason) you don’t like FFTW…

---

<div class="post-metadata">

**Author:** ![Resul.Akay](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Resul.Akay](https://discourse.julialang.org/u/Resul.Akay)\
**Post date:** [December 4, 2022, 1:02pm UTC](https://discourse.julialang.org/t/scipy-signal-convolve-in-julia/91140/7 "2022-12-04T13:02:33Z")

</div>

Thank you for your response. I have rewritten the C++ code of R’s [`stats::filter`](https://github.com/SurajGupta/r-source/blob/master/src/library/stats/src/filter.c) in Julia. I will share Julia’s implementation of stats::decompose soon. I have [seen other Julia developers need it too](https://discourse.julialang.org/t/is-there-a-julia-package-that-can-decompose-a-time-series-data-into-trend-seasonality-and-random/9518/23)
