From fb1ba68822a0af73f0f8f259d3e19faf37b8a973 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sun, 13 Sep 2020 23:41:49 +0200 Subject: [PATCH] Add julia notebook --- DigitalSignalProcessing.jl | 180 +++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 DigitalSignalProcessing.jl diff --git a/DigitalSignalProcessing.jl b/DigitalSignalProcessing.jl new file mode 100644 index 0000000..938a69d --- /dev/null +++ b/DigitalSignalProcessing.jl @@ -0,0 +1,180 @@ +### A Pluto.jl notebook ### +# v0.11.14 + +using Markdown +using InteractiveUtils + +# ╔═╡ c8101fc0-e4a8-11ea-2924-f78341e62d7a +begin + using Plots, LinearAlgebra + default(label = false, minorgrid = true) +end + +# ╔═╡ b33ad050-e489-11ea-3b17-e598a406a091 +md"# Week 2 - Vector space" + +# ╔═╡ e0f11590-e489-11ea-0147-0928a6e40330 +x = [1, 2, 4] + +# ╔═╡ 35be38a0-e48a-11ea-3616-3f314782c3ad +let + α = 10 + α * x # Multiply by a scala. +end + +# ╔═╡ 3db39320-e48a-11ea-02a9-6dc184ea3fdf +let + y = [3, 4, 5] + x + y +end + +# ╔═╡ 502b22a0-e48c-11ea-3a62-c7de5e345f02 +md"Addition in $L2[-1, 1]$" + +# ╔═╡ 79c736a0-e48a-11ea-2886-cf90c9c874db +let + t = -1:1/200:1 + f1(t) = sin(π * t) + f2(t) = 0.3 * sin(25 * π * t) + f3(t) = f1(t) + f2(t) + plot(t, f3, label = false) +end + +# ╔═╡ 5c99778e-e48b-11ea-23cf-7b1537bf57d9 +md"## Approximation of a square function" + +# ╔═╡ 294b51f0-e55e-11ea-068d-2f6663453bbc +md"## Inner product" + +# ╔═╡ f0112030-e48c-11ea-3d07-d383bd861e81 +let + v1 = [1, 2, 3] + v2 = [2, 3, 4] + + # Same two expressions for inner product. + dot(v1, v2), + transpose(v1) * v2 +end + +# ╔═╡ 5bd37650-e579-11ea-354b-65f651b7fc69 +let + A = [ 0 0 0 1 + 1 0 0 0 + 0 1 0 0 + 0 0 1 0 ] + + A^4 +end + +# ╔═╡ 5cde4c70-eaa4-11ea-214c-633b65f18a18 +md"# Week 3 & 4 - Fourier analysis" + +# ╔═╡ 78adff50-ed5a-11ea-3c3a-cf4de58dce71 +md""" +### Analysis + +$X[k] = \sum_{n=0}^{N-1} x[n] e^{-j \frac{2 \pi}{N} \cdot n \cdot k}, k \in \mathbb{Z}$ + +Produces an **N-periodic** signal in the time domain + +### Synthesis + +$x[n] = \frac{1}{N} \sum_{k=0}^{N-1} X[k] e^{j \frac{2 \pi}{N} \cdot n \cdot k}, n \in \mathbb{Z}$ + +Produces an **N-point** signal in the frequency domain +""" + +# ╔═╡ 0f4abce0-ed34-11ea-2245-0590b879023f +begin + analysis(x, k, N) = sum(x(n) * exp(-im * 2π / N * n * k) for n ∈ 0:N-1) + synthesis(X, n, N) = 1 / N * sum(X(k) * exp(im * 2π / N * n * k) for k ∈ 0:N-1) + + # Input signal 'f'. + f(n) = sin(n * 2π / 100) + 0.2 * sin(10 * n * 2π / 100 + π / 4) + + # Analysis of 'f'. + N = 100 + points_in_freq_domain = [analysis(f, k, N) for k ∈ 0:N-1] + F(n) = points_in_freq_domain[n % length(points_in_freq_domain) + 1] + + # Synthesis of 'F'. + points_in_time_domain = [synthesis(F, n, N) for n ∈ 0:N-1] .|> real +end + +# ╔═╡ c48cd7e0-e515-11ea-02ba-5999ac4f4c9a +let + t = -1:1/200:1 + f(t, k) = sin((2k + 1) * π * t) / (2k + 1) + g(N) = t -> (f(t, k) for k ∈ 0:N) |> sum + plot(t, g(20), label = false) +end + +# ╔═╡ 10a85110-ed34-11ea-36ff-175bceac4be1 +scatter(0:N-1, f, label = false, title = "Input: f(n)") + +# ╔═╡ 1609ba40-ed34-11ea-29d2-41e5742df891 +scatter(0:N-1, points_in_freq_domain |> imag, label = false, title = "Frequency domain: F(n): Imaginary") + +# ╔═╡ 1915e8d0-ed34-11ea-37b7-2f4938bcf27e +scatter(0:N-1, points_in_freq_domain |> real, label = false, title = "Frequency domain: F(n): Real") + +# ╔═╡ 20bb8390-ed5e-11ea-2415-cbc53e60cc0a +scatter(points_in_freq_domain, label = false, title = "Frequency domain: F(n)") + +# ╔═╡ 0f318a60-efb7-11ea-0416-3ded1c40b27a +scatter(0:N-1, points_in_freq_domain .|> norm, label = false, title = "Frequency domain: F(n): Magnitude") + +# ╔═╡ 1badaab0-ed34-11ea-2709-df7b90e4e6a6 +scatter(0:N-1, points_in_time_domain |> real, label = false, title = "Back to time domain") + +# ╔═╡ e0ae4d60-f054-11ea-249c-41c05d1b7cf7 +md"## Example of a linear signal" + +# ╔═╡ 1e4174f0-ed34-11ea-0c3a-69ad2f7d6561 +let + N = 32 + x = [ 2n / (N - 1) - 1 for n ∈ 0:N-1] + f(n) = x[(n % N) + 1] + points_in_freq_domain = [analysis(f, k, N) for k ∈ 0:N-1] + + plot_signal = scatter(x, label = false, title = "Time domain", ) + plot_fft = scatter(0:N-1, points_in_freq_domain .|> norm, label = false, title = "Frequency domain: F(n): Magnitude") + plot(plot_signal, plot_fft, layout = (2, 1)) +end + +# ╔═╡ 4d5cabe2-f06a-11ea-349f-6f940c85692e +md"## Example of exponation decaying sequence" + +# ╔═╡ 3dac19b0-f06a-11ea-34ee-291908925549 +let + α = 0.9 + f(n) = if n < 0 0 else α^n end + scatter(-5:40, f) +end + +# ╔═╡ Cell order: +# ╟─b33ad050-e489-11ea-3b17-e598a406a091 +# ╠═c8101fc0-e4a8-11ea-2924-f78341e62d7a +# ╠═e0f11590-e489-11ea-0147-0928a6e40330 +# ╠═35be38a0-e48a-11ea-3616-3f314782c3ad +# ╠═3db39320-e48a-11ea-02a9-6dc184ea3fdf +# ╟─502b22a0-e48c-11ea-3a62-c7de5e345f02 +# ╠═79c736a0-e48a-11ea-2886-cf90c9c874db +# ╟─5c99778e-e48b-11ea-23cf-7b1537bf57d9 +# ╠═c48cd7e0-e515-11ea-02ba-5999ac4f4c9a +# ╟─294b51f0-e55e-11ea-068d-2f6663453bbc +# ╠═f0112030-e48c-11ea-3d07-d383bd861e81 +# ╠═5bd37650-e579-11ea-354b-65f651b7fc69 +# ╟─5cde4c70-eaa4-11ea-214c-633b65f18a18 +# ╟─78adff50-ed5a-11ea-3c3a-cf4de58dce71 +# ╠═0f4abce0-ed34-11ea-2245-0590b879023f +# ╠═10a85110-ed34-11ea-36ff-175bceac4be1 +# ╠═1609ba40-ed34-11ea-29d2-41e5742df891 +# ╠═1915e8d0-ed34-11ea-37b7-2f4938bcf27e +# ╠═20bb8390-ed5e-11ea-2415-cbc53e60cc0a +# ╟─0f318a60-efb7-11ea-0416-3ded1c40b27a +# ╠═1badaab0-ed34-11ea-2709-df7b90e4e6a6 +# ╟─e0ae4d60-f054-11ea-249c-41c05d1b7cf7 +# ╠═1e4174f0-ed34-11ea-0c3a-69ad2f7d6561 +# ╟─4d5cabe2-f06a-11ea-349f-6f940c85692e +# ╠═3dac19b0-f06a-11ea-34ee-291908925549 -- 2.45.2