Extracting Fourier Coefficients of an arbitrary periodic signal?

@Geoffrey, I have edited your code below to be closer to the definition of the Fourier coefficients, used the standard fft and adjusted the length of input signal x to contain N samples. The minus sign required in the imaginary part of the fft, is probably due to the fft sign convention in Julia.
Edit: defined a1 and had loop starting at 1

using FFTW, Plots
N = 256;
P = 1.0;
Δt = P / N
x = 0.0:Δt:(P-Δt)   # lenght(x) == N
y = [sin(2π*7*t) + sin(2π*15*t) + sin(2π*30*t) for t in x] # mixture of simple wave signal
plot(x, y, legend = false, linewidth=2)
Fy = fft(y)[1:N÷2]
ak =  2/N * real.(Fy)
bk = -2/N * imag.(Fy)  # fft sign convention
ak[1] = ak[1]/2
yr = zeros(N,1)
for i in 1:N÷2
    yr .+= ak[i] * cos.(2π*(i-1)/P * x) .+ bk[i] * sin.(2π*(i-1)/P * x)
end
plot!(x, yr, linestyle=:dash, linewidth=2)

fourier_coefficients

6 Likes