# Extracting Fourier Coefficients of an arbitrary periodic signal?

**URL:** <https://discourse.julialang.org/t/extracting-fourier-coefficients-of-an-arbitrary-periodic-signal/51036>\
**Category:** New to Julia\
**Tags:** dsp\
**Created:** [December 1, 2020, 9:48am UTC](https://discourse.julialang.org/t/extracting-fourier-coefficients-of-an-arbitrary-periodic-signal/51036 "2020-12-01T09:48:06Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 2, 2020, 1:01am UTC](https://discourse.julialang.org/t/extracting-fourier-coefficients-of-an-arbitrary-periodic-signal/51036/8 "2020-12-02T01:01:10Z")

</div>

@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_

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/9/c/9c0864d87e110d3ecaa8be55a4454431b1bb8b32.png)

---

_[View the full topic](https://discourse.julialang.org/t/extracting-fourier-coefficients-of-an-arbitrary-periodic-signal/51036)._
