Skip to content

Instantly share code, notes, and snippets.

@marcpabst
Last active March 18, 2024 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcpabst/00985a07bcf61a52e2e79355624dc967 to your computer and use it in GitHub Desktop.
Save marcpabst/00985a07bcf61a52e2e79355624dc967 to your computer and use it in GitHub Desktop.
fftfun
### A Pluto.jl notebook ###
# v0.19.40
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
el
end
end
# ╔═╡ 1341a7cc-6b15-4a2c-9111-6d9e27f61f36
begin
using PlutoUI
using FFTW
using DSP
using GLMakie
end
# ╔═╡ 1347aba1-d5df-4e38-a8b3-972503d301c4
md"𝑓₂ = $(@bind 𝑓₂ PlutoUI.Slider(14:0.1:16)), ϕ₂ = $(@bind ϕ₂ PlutoUI.Slider(0:0.1:2π))"
# ╔═╡ 1a296569-b173-4c8d-9855-a921e2daf35c
begin
duration = 2.0
samplingrate = 1000
# generate two sine waves
𝑓₁ = 15
ϕ₁ = 0
a₁ = 1
a₂ = 1
t = collect(0:1/samplingrate:duration)[1:end-1]
sine_wave_1 = sin.(2 * π * 𝑓₁ * t .+ ϕ₁) * a₁
sine_wave_2 = sin.(2 * π * 𝑓₂ * t .+ ϕ₂) * a₂
# plot the sine waves
fig = Figure(size=(800, 400))
ax = Axis(fig[1, 1:3])
lines!(ax, t, sine_wave_1, color=:black, label="𝑓₁ = $𝑓₁, ϕ₁ = $(round(ϕ₁,digits=2))")
lines!(ax, t, sine_wave_2, color=:red, label="𝑓₂ = $𝑓₂, ϕ₂ = $(round(ϕ₂,digits=2))")
lines!(ax, t, sine_wave_1 + sine_wave_2, color=:blue, label="Sum of sine waves")
# add legend with frequency and phase
Legend(fig[3, 1:3], ax, orientation=:horizontal)
# compute the power spectrum of the sine waves and of the sum of the sine waves
sine_wave_1_fft = fft(sine_wave_1) / length(sine_wave_1)
sine_wave_2_fft = fft(sine_wave_2) / length(sine_wave_2)
sine_wave_sum_fft = fft(sine_wave_1 + sine_wave_2) / length(sine_wave_1 + sine_wave_2)
sine_wave_1_power_spectrum = abs2.(sine_wave_1_fft) .^ 2
sine_wave_2_power_spectrum = abs2.(sine_wave_2_fft) .^ 2
sine_wave_sum_power_spectrum = abs2.(sine_wave_sum_fft) .^ 2
freqs = fftfreq(length(sine_wave_1), samplingrate)
# plot the power spectrum
ax1 = Axis(fig[2, 1], title="Sine wave 1")
lines!(ax1, freqs, sine_wave_1_power_spectrum, color=:black)
ax2 = Axis(fig[2, 2], title="Sine wave 2")
lines!(ax2, freqs, sine_wave_2_power_spectrum, color=:red)
ax3 = Axis(fig[2, 3], title="Sum of sine waves")
lines!(ax3, freqs, sine_wave_sum_power_spectrum, color=:blue)
xlims!.([ax1, ax2, ax3], 0, 30)
# plot a vertical line at the frequency of the first sine wave
vlines!.([ax1, ax2, ax3], [𝑓₁], color=:black)
# link the y-axes
linkyaxes!(ax1, ax2, ax3)
fig
end
# ╔═╡ Cell order:
# ╠═1341a7cc-6b15-4a2c-9111-6d9e27f61f36
# ╠═1347aba1-d5df-4e38-a8b3-972503d301c4
# ╟─1a296569-b173-4c8d-9855-a921e2daf35c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment