# \[DSP.jl\] What resampling parameters should I use to get the same output as the decimate function in MATLAB?

**URL:** <https://discourse.julialang.org/t/dsp-jl-what-resampling-parameters-should-i-use-to-get-the-same-output-as-the-decimate-function-in-matlab/28357>\
**Category:** Signal and Image Processing\
**Tags:** question, dsp\
**Created:** [September 4, 2019, 2:07am UTC](https://discourse.julialang.org/t/dsp-jl-what-resampling-parameters-should-i-use-to-get-the-same-output-as-the-decimate-function-in-matlab/28357 "2019-09-04T02:07:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Losses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/losses/32/9175_2.png) [@Losses](https://discourse.julialang.org/u/Losses)\
**Post date:** [September 4, 2019, 2:07am UTC](https://discourse.julialang.org/t/dsp-jl-what-resampling-parameters-should-i-use-to-get-the-same-output-as-the-decimate-function-in-matlab/28357/1 "2019-09-04T02:07:51Z")

</div>

Hello everyone, I am a cognitive neuroscience researcher, have little knowledge about the detail of signal processing, We are trying to resampling our signal with DSP.jl, and expect to get identical(or similar) result as [`decimate` in MATLAB](https://ww2.mathworks.cn/help/signal/ref/decimate.html?requestedDomain=cn) did, based on the document it’s possible, but how to implement confused us a lot.  
I’m very appreciated for your help. 😃

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [September 4, 2019, 4:29am UTC](https://discourse.julialang.org/t/dsp-jl-what-resampling-parameters-should-i-use-to-get-the-same-output-as-the-decimate-function-in-matlab/28357/2 "2019-09-04T04:29:13Z")

</div>

Hi @Losses, Can you clarify what you’ve tried and the way that it’s not doing what you expect? The best thing would be a code chunk that folks could copy-paste to see what you’re doing, and then provide suggestions as to what you should change.

I’m guessing that the main places where DSP.jl’s `resample` might differ have to do with delays introduced by filtering and also with edge effects, but it’s not clear to me how MATLAB’s `decimate` is implemented, so an example of what you’re seeing would be helpful.

---

<div class="post-metadata">

**Author:** ![Losses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/losses/32/9175_2.png) [@Losses](https://discourse.julialang.org/u/Losses)\
**Post date:** [September 5, 2019, 10:39am UTC](https://discourse.julialang.org/t/dsp-jl-what-resampling-parameters-should-i-use-to-get-the-same-output-as-the-decimate-function-in-matlab/28357/3 "2019-09-05T10:39:28Z")

</div>

@ssfrr Thank you for the reply, What we are trying to down sampling waveform brain activity data (very similar to sound wave) from 55.5Hz to 1Hz, and trying to avoid the impact of high frequency signal, we used to use `decimate` function in MATLAB to achieve this, what they did is:

- a lowpass Chebyshev Type I infinite impulse response (IIR) filter of order 8

But what DSP.jl provided is a FIR based method, I don’t know whether using `DSP.resample` will cause some additional effects…

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [September 5, 2019, 2:40pm UTC](https://discourse.julialang.org/t/dsp-jl-what-resampling-parameters-should-i-use-to-get-the-same-output-as-the-decimate-function-in-matlab/28357/4 "2019-09-05T14:40:35Z")

</div>

Both `DSP.resample` and MATLAB’s `decimate` apply a lowpass filter and then throw away samples to get to the new sample rate (fn1). From what I can tell from the `decimate` documentation it does some other fancy things to improve how well the signal lines up at the very beginning and end, and also to detect some possible numerical issues when designing the IIR filter (TBH I don’t really understand what they’re doing there).

There shouldn’t be much practical difference whether you use an IIR or FIR filter. Probably the bigger question is whether the edge effects mess you up (they show up as “ringing” or “ripple”). This is mostly a problem if the beginning or end of your signal is far from zero, which also crops up if your signal has a substantial DC component (constant offset). (fn2).

For instance, here’s a modified version of one of the examples on the `decimate` doc:

```julia
using Plots
using DSP

N = 200
t = 0:N-1
x = sin.(0.04t) + sin.(0.08t);
plt = sticks(t, x)
rs = resample(x, 1//4)
scatter!(plt, 0:4:N-1, rs, marker=true)

```

Which gives:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/5/25f89bb3177201ce5615274aeffc3dd9e2e6d8c9.png)

Note the lowpass filtering isn’t actually having much effect here because there isn’t any high-frequency content in the signal.

To illustrate the possible edge effects, heres a version with a DC offset (`x = sin.(0.04t) + sin.(0.08t) + 5`). You can see the ripple at the beginning and the end.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/c/ac2bb5743855cbc0e1d16b5b7310d04e6db2e437.png)

Hope this is helpful. In some ways the short answer is "DSP.jl’s `resample` has some small differences from MATLAB’s `decimate`, but for many use-cases those differences won’t matter. If your goal is to reproduce exactly some previous data then you might be in a little trouble, but if your goal is just to lowpass and downsample your data then this should be what you’re looking for.

### Footnotes

`fn1`: This is a little more complicated if the original samplerate isn’t an integer multiple of the new one, but is a reasonable mental model of what’s going on.

`fn2`: This would actually be a good place where DSP.jl could improve, it would be nice for users not to have to worry about this. There’s [an issue for it](https://github.com/JuliaDSP/DSP.jl/issues/241).

---

<div class="post-metadata">

**Author:** ![Losses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/losses/32/9175_2.png) [@Losses](https://discourse.julialang.org/u/Losses)\
**Post date:** [September 6, 2019, 10:58am UTC](https://discourse.julialang.org/t/dsp-jl-what-resampling-parameters-should-i-use-to-get-the-same-output-as-the-decimate-function-in-matlab/28357/5 "2019-09-06T10:58:16Z")

</div>

Thank you for your in-depth explanation! I think my question is solved!
