Replicating a Julia example

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.

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

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
and
image

3 Likes