# Smoothing Noisy Data using Moving Mean

**URL:** https://discourse.julialang.org/t/smoothing-noisy-data-using-moving-mean/65329
**Category:** General Usage
**Tags:** data, smoothing
**Created:** [July 26, 2021, 7:50pm UTC](https://discourse.julialang.org/t/smoothing-noisy-data-using-moving-mean/65329 "2021-07-26T19:50:39Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![sekarsum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sekarsum/32/27202_2.png) [@sekarsum](https://discourse.julialang.org/u/sekarsum)
#### Post date: [July 26, 2021, 7:50pm UTC](https://discourse.julialang.org/t/smoothing-noisy-data-using-moving-mean/65329/1 "2021-07-26T19:50:39Z")

</div>

Hi there,

Is there a function in Julia that is similar to MATLAB’s smooth() function.

MATLAB smooth function: `z = smooth(y, span, method)` where

`y` → input array;  
`span` → span of moving average;  
`method` → option between default moving average, lowess, loess etc.

THe result of this operation `z` is a vector of the same dimension as `y` which is basically a smoothening of the original array `y` .

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 26, 2021, 7:52pm UTC](https://discourse.julialang.org/t/smoothing-noisy-data-using-moving-mean/65329/2 "2021-07-26T19:52:08Z")

</div>

have you seen [RollingFunctions.jl](https://github.com/JeffreySarnoff/RollingFunctions.jl)? Seems to be close to what you want. But it doesn’t have out-of-the-box support for `loess` etc. It _does_ have a way to use a custom function, so maybe it’s easy to add on your own.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 27, 2021, 8:12am UTC](https://discourse.julialang.org/t/smoothing-noisy-data-using-moving-mean/65329/3 "2021-07-27T08:12:05Z")

</div>

For LOESS,

[https://github.com/JuliaStats/Loess.jl](https://github.com/JuliaStats/Loess.jl)

---

<div class="post-metadata">

### Author: ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)
#### Post date: [July 27, 2021, 9:17am UTC](https://discourse.julialang.org/t/smoothing-noisy-data-using-moving-mean/65329/4 "2021-07-27T09:17:00Z")

</div>

If it’s just a moving average and you want the same exact results as MATLAB then the only thing to figure out is what strategy MATLAB uses to estimate the tails (AR models, … etc.)

If you just want a smoother then they all have pros and cons, e.g. some can interpolate some cannot.

Anyway, to add options to the ones above we also have a `Henderson moving average filter` here [GitHub - viraltux/Forecast.jl: Julia package containing utilities intended for Time Series analysis.](https://github.com/viraltux/Forecast.jl)

```julia
using Plots, Forecast
x = log.( collect(1:.1:11) .+ rand(101))

scatter(x)
plot!(hma(x,21),legend=nothing)

```

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

---

<div class="post-metadata">

### Author: ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)
#### Post date: [September 3, 2021, 5:38pm UTC](https://discourse.julialang.org/t/smoothing-noisy-data-using-moving-mean/65329/5 "2021-09-03T17:38:19Z")

</div>

There’s no singular function (yet) that has parity with MATLAB’s `smoothdata()`, and I +1 for whoever would like to implement something like it!  
This works for a moving average at least:

```julia
using NaNStatistics, Plots

y = randn(1000)
window_size = length(y)/100.0
z = movmean(y, window_size)
plot(y, linestyle = :dot)
plot!(z, linewidth = 2)

```

![smooth](https://global.discourse-cdn.com/julialang/original/3X/2/8/2802aca9adcf3afd515232f620232ee5bac1497f.png)

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [September 3, 2021, 8:10pm UTC](https://discourse.julialang.org/t/smoothing-noisy-data-using-moving-mean/65329/6 "2021-09-03T20:10:52Z")

</div>

If you just want to plot the data more smoothly, there is this one:

```julia
julia> using EasyFit

julia> x = rand(100);

julia> m10 = movavg(x,10)

 ------------------- Moving Average ---------- 

 Number of points averaged: 11 (± 5 points)

 Pearson correlation coefficient, R = 0.43442913764734875

 Averaged X: x = [0.4737608452310648, 0.5121944744036446...
 residues = [-0.3269117547107911, 0.40109456416787137...

 -------------------------------------------- 

julia> m50 = movavg(x,50)

 ------------------- Moving Average ---------- 

 Number of points averaged: 51 (± 25 points)

 Pearson correlation coefficient, R = -0.014175383510694936

 Averaged X: x = [0.5225594827160708, 0.5220326497692106...
 residues = [-0.37571039219579705, 0.3912563888023054...

 -------------------------------------------- 

julia> plot([x,m10.x,m50.x],linewidth=2)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/1/9/1959441e6ebdb9929116be32bc1d86063f4c482c.png)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [September 3, 2021, 10:17pm UTC](https://discourse.julialang.org/t/smoothing-noisy-data-using-moving-mean/65329/7 "2021-09-03T22:17:52Z")

</div>

For a package-free moving average, see @tim.holy’s solution [here](https://julialang.org/blog/2016/02/iteration/#writing_multidimensional_algorithms_with_cartesianindex_iterators), adapted as follows for `m` odd integer \> 1:

```julia
function moving_average(A::AbstractArray, m::Int)
    out = similar(A)
    R = CartesianIndices(A)
    Ifirst, Ilast = first(R), last(R)
    I1 = m÷2 * oneunit(Ifirst)
    for I in R
        n, s = 0, zero(eltype(out))
        for J in max(Ifirst, I-I1):min(Ilast, I+I1)
            s += A[J]
            n += 1
        end
        out[I] = s/n
    end
    return out
end

```

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [September 4, 2021, 7:07am UTC](https://discourse.julialang.org/t/smoothing-noisy-data-using-moving-mean/65329/8 "2021-09-04T07:07:52Z")

</div>

ImageFiltering.jl has multidmensional array smoothing. There’s also a `mapwindow(f, A, window)` which allows you to apply `f` to “windows” of `A` rather than the single elements that `map(f, A)` applies `f` to.

---

<div class="post-metadata">

### Author: ![joshday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshday/32/368_2.png) [@joshday](https://discourse.julialang.org/u/joshday)
#### Post date: [September 9, 2021, 8:24pm UTC](https://discourse.julialang.org/t/smoothing-noisy-data-using-moving-mean/65329/9 "2021-09-09T20:24:33Z")

</div>

I’ll also note that an exponentially weighted mean is similar to a moving window and is easy to use via OnlineStats:

```julia
o = Mean(weight = ExponentialWeight(.1))

exp_smooth = [value(fit!(o, yi)) for yi in y]

```

---

<div class="post-metadata">

### Author: ![ayushpatnaikgit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayushpatnaikgit/32/25229_2.png) [@ayushpatnaikgit](https://discourse.julialang.org/u/ayushpatnaikgit)
#### Post date: [November 15, 2021, 9:54am UTC](https://discourse.julialang.org/t/smoothing-noisy-data-using-moving-mean/65329/10 "2021-11-15T09:54:50Z")

</div>

Hi,  
How do you choose the optimal window size?  
The package is very promising, I hope it develops quickly, I am looking forward to using it soon.  
Many thanks,  
Ayush
