From: Greg Burri Date: Mon, 14 Sep 2020 14:00:42 +0000 (+0200) Subject: Improve the example for DFT X-Git-Url: http://git.euphorik.ch/?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=signal_processing.git Improve the example for DFT --- diff --git a/DigitalSignalProcessing.jl b/DigitalSignalProcessing.jl index 938a69d..1648a88 100644 --- a/DigitalSignalProcessing.jl +++ b/DigitalSignalProcessing.jl @@ -67,7 +67,7 @@ let end # ╔═╡ 5cde4c70-eaa4-11ea-214c-633b65f18a18 -md"# Week 3 & 4 - Fourier analysis" +md"# DFT" # ╔═╡ 78adff50-ed5a-11ea-3c3a-cf4de58dce71 md""" @@ -84,11 +84,27 @@ $x[n] = \frac{1}{N} \sum_{k=0}^{N-1} X[k] e^{j \frac{2 \pi}{N} \cdot n \cdot k}, Produces an **N-point** signal in the frequency domain """ -# ╔═╡ 0f4abce0-ed34-11ea-2245-0590b879023f +# ╔═╡ b81dfe60-f688-11ea-06d0-7158c35b3a5e begin + # 'x' is a discrete function where values are x[0], x[1], etc. + # 'k' is the nth coefficient to compute. + # 'N' is the number of values in 'x'. 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) + # 'X' is the discret fourier transform result where values are X[0], X[1], etc. + # 'n' is the nth point to compute. + # 'N' is the number of values in 'X'. + synthesis(X, n, N) = 1 / N * sum(X(k) * exp(im * 2π / N * n * k) for k ∈ 0:N-1) +end + +# ╔═╡ 875262d0-f688-11ea-0677-07825a60c6c5 +md"## Example + +$f(n) = sin(n \cdot 2π / 100) + sin(10 \cdot n \cdot 2π / 100 + π / 4)$ +" + +# ╔═╡ 0f4abce0-ed34-11ea-2245-0590b879023f +begin # Input signal 'f'. f(n) = sin(n * 2π / 100) + 0.2 * sin(10 * n * 2π / 100 + π / 4) @@ -130,6 +146,12 @@ scatter(0:N-1, points_in_time_domain |> real, label = false, title = "Back to ti # ╔═╡ e0ae4d60-f054-11ea-249c-41c05d1b7cf7 md"## Example of a linear signal" +# ╔═╡ b186e30e-f682-11ea-1936-47067564c826 +md"# 1.4.1 DFS - Discrete Fourier series + +DFS is a DFT with periodicity explicit. +" + # ╔═╡ 1e4174f0-ed34-11ea-0c3a-69ad2f7d6561 let N = 32 @@ -137,11 +159,22 @@ let 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)) + plot_signal = scatter(x, label = false, title = "Time domain for one period", ) + plot_fft = scatter(0:N-1, points_in_freq_domain .|> norm, label = false, title = "Frequency domain for one period: F(n): Magnitude") + + points_in_freq_domain_2_periods = [analysis(f, k, 2N) for k ∈ 0:2N-1] + plot_signal_2_periods = scatter([x; x], label = false, title = "Time domain for two periods", ) + plot_fft_2_periods = scatter(0:2N-1, points_in_freq_domain_2_periods .|> norm, label = false, title = "Frequency domain for two periods: F(n): Magnitude") + + plot(plot_signal, plot_fft, plot_signal_2_periods, plot_fft_2_periods, layout = (4, 1), size = (800, 800)) end +# ╔═╡ 4acf4260-f68d-11ea-1cc5-c15a74776107 +md"Here the coefficient at position 0 is 0 because the signal is centered around 0 (mean = 0)." + +# ╔═╡ 23291580-f690-11ea-24eb-3d95ce599f42 +md"# 1.4.2 DTFT - Discrete-Time Fourier Transform" + # ╔═╡ 4d5cabe2-f06a-11ea-349f-6f940c85692e md"## Example of exponation decaying sequence" @@ -150,6 +183,7 @@ let α = 0.9 f(n) = if n < 0 0 else α^n end scatter(-5:40, f) + end # ╔═╡ Cell order: @@ -167,14 +201,19 @@ end # ╠═5bd37650-e579-11ea-354b-65f651b7fc69 # ╟─5cde4c70-eaa4-11ea-214c-633b65f18a18 # ╟─78adff50-ed5a-11ea-3c3a-cf4de58dce71 +# ╠═b81dfe60-f688-11ea-06d0-7158c35b3a5e +# ╟─875262d0-f688-11ea-0677-07825a60c6c5 # ╠═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 +# ╟─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 +# ╟─1badaab0-ed34-11ea-2709-df7b90e4e6a6 # ╟─e0ae4d60-f054-11ea-249c-41c05d1b7cf7 +# ╟─b186e30e-f682-11ea-1936-47067564c826 # ╠═1e4174f0-ed34-11ea-0c3a-69ad2f7d6561 +# ╟─4acf4260-f68d-11ea-1cc5-c15a74776107 +# ╟─23291580-f690-11ea-24eb-3d95ce599f42 # ╟─4d5cabe2-f06a-11ea-349f-6f940c85692e # ╠═3dac19b0-f06a-11ea-34ee-291908925549