# Replicating a Julia example

**URL:** <https://discourse.julialang.org/t/replicating-a-julia-example/78646>\
**Category:** New to Julia\
**Tags:** question, plotting\
**Created:** [March 29, 2022, 2:16am UTC](https://discourse.julialang.org/t/replicating-a-julia-example/78646 "2022-03-29T02:16:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![djturizo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djturizo/32/35005_2.png) [@djturizo](https://discourse.julialang.org/u/djturizo)\
**Post date:** [March 29, 2022, 2:16am UTC](https://discourse.julialang.org/t/replicating-a-julia-example/78646/1 "2022-03-29T02:16:41Z")

</div>

I am trying to learn Julia and I read [this article](https://arstechnica.com/science/2020/10/the-unreasonable-effectiveness-of-the-julia-programming-language/3/) about the quick success of Julia. In the last page of the article the author works a small example showing the benefits of multiple dispatch. They define a custom class `Spect` and define a `plot()` function for it. Then for an object `sqw` of type `Spect` they can call `plot(sqw)` without having to edit the original plot function. Moreover, this definition also affects similar plotting functions so that you can also call `scatter(sqw)` without problems. My issue is that author does not show the code, so I do not understand how can you achieve this. I am specially interested in the fact that just defining `plot()` for this new class is enough to also call other functions like `scatter()` without defining them for the new class.

Can someone write a small example of this like that of the article so that I can understand how all of this is achieved? Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 29, 2022, 3:50am UTC](https://discourse.julialang.org/t/replicating-a-julia-example/78646/2 "2022-03-29T03:50:19Z")

</div>

It’s a shame the article doesn’t link to the code. Here’s my rough reproduction attempt. My version uses the `dct` and `idct` so I’m not getting the nice harmonics, but I think it shows the ideas pretty well.

```julia
using RecipesBase, FFTW
struct Spect
    points :: AbstractRange
    weights :: Vector{Float64}
end
function Spect(f::Function, min, max, n)
    points = range(min, max, n)
    Spect(points, dct(f.(points)))
end

@recipe function f(S::Spect)
    S.points, idct(S.weights)
end

```

These definitions are enough for

```julia
using Plots
squarewave(x) = iseven(floor(x)) ? 1.0 : 0.0
sqw = Spect(squarewave, 0, 5, 20);

plot(sqw)
scatter(sqw)

```

The result of this is  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/1/d10810e00be06a2dc34321e1848ba4077cff6764.png)  
and  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/1/81505d066b5d187a98f26d60e09f51c22f0c55a5.png)

---

<div class="post-metadata">

**Author:** ![LeePhillips](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leephillips/32/205514_2.png) [@LeePhillips](https://discourse.julialang.org/u/LeePhillips)\
**Post date:** [March 29, 2022, 1:18pm UTC](https://discourse.julialang.org/t/replicating-a-julia-example/78646/3 "2022-03-29T13:18:29Z")

</div>

This reproduces the figures in the article:

```julia
using Plots, RecipesBase

"""Do the Fourier synthesis. a and b are vectors of coefficients for the sine and
cosine components. Return a function of the independent variable."""
function fsc(a, b, x)
     function f(x)
       ss = sum(a .* sin.(x .* (1:length(a))))
       cs = sum(b .* cos.(x .* (1:length(a))))
       ss + cs
     end
     return f(x)
end

"""A datatype that holds two vectors of Fourier components."""
struct Spect
     a::Array{Float64, 1}
     b::Array{Float64, 1}
end

"""The plotting recipe takes a Spect and plots it in physical space."""
@recipe function f(s::Spect)
           x = pi/50 .* (0:100)
           y = map(w -> fsc(s.a, s.b, w), x)
           x, y
        end

#Here is the spectrum of a square wave. It has only odd sine components that
#go as 1/n. Here we include the first 10 harmonics.
a = isodd.(1:10) ./ (1:10)
b = zeros(10)

sqw = Spect(a, b)

#Making the plots. Any function from Plots.jl that understands recipes will 
#now be able to plot a Spect.
plot(sqw)
scatter(sqw)

"""Filter a Spect by zeroing anything higher than the nth harmonic."""
function lowpass(s::Spect, n)
     t = deepcopy(s)
     t.a[n+1:end] .= 0
     t.b[n+1:end] .= 0
     return t
end

#Filter the square wave that we created above, keeping only the first three
#harmonics.
filtered_sqw = lowpass(sqw, 3)

#Take a look at the filtered square wave.
plot(filtered_sqw)

```
