From 56fa8b8a06a317704d3199e3ed18ede01ed254c4 Mon Sep 17 00:00:00 2001 From: Josh Ott Date: Mon, 16 May 2022 23:22:36 -0400 Subject: Added main scripts and example graphic --- fourier.jl | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 fourier.jl (limited to 'fourier.jl') diff --git a/fourier.jl b/fourier.jl new file mode 100644 index 0000000..911c984 --- /dev/null +++ b/fourier.jl @@ -0,0 +1,70 @@ +""" +Simpson's rule integration on function `f` with bounds `a` to `b` with `n` intervals. +""" +function integrate(f, a, b, n) + h = (b-a)/2n + x(k) = a + k*h + y(k) = f(x(k)) + + s = y(0) + y(2n) + for i in 1:2n-1 + i % 2 == 1 ? s += 4y(i) : s += 2y(i) + end + + I = h/3*s + return I +end + +""" +Calculate Fourier coefficient for term with frequency `n` with function `f`. +""" +function c(f, n) + g(t)=f(t)*exp(-2π*im*n*t) + integrate(g,0,1,500) +end + +""" +Calculate `n` coefficients for the terms in the fourier series approximation of a given function `f(t)` over domain [0,1]. + +Corresponding frequencies for the coefficients are 0, 1, -1, 2, -2, and so on. +""" +function calculateCoefficients(f, n) + coefficients = zeros(Complex{Float64}, n) + coefficients[1] = c(f, 0) + + Threads.@threads for i in 2:n + coefficients[i] = c(f, (i÷2)*(-1)^i) + end + + return coefficients +end + +""" +Calculate Fourier series at time `t` with `coefficients` as given by `calculateCoefficients`. +""" +function fourierSeries(t, coefficients) + z = coefficients[1] + n = length(coefficients) + + for i in 2:n + z += coefficients[i]*exp(2π*im*(i÷2)*(-1)^i*t) + end + + return z +end + +""" +Similar to `fourierSeries` but returns a vector of all the intermediate sums. For use in animations. +""" +function fourierArm(t, coefficients) + n = length(coefficients) + arm = zeros(Complex{Float64}, n) + z = 0 + + for i in 1:n + z += coefficients[i] * exp(2π*im*(i÷2)*(-1)^i*t) + arm[i] = z + end + + return arm +end \ No newline at end of file -- cgit v1.3