# 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:** 3

<div class="post-metadata">

**Author:** ![Geoffrey](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@Geoffrey](https://discourse.julialang.org/u/Geoffrey)\
**Post date:** [December 1, 2020, 10:48am UTC](https://discourse.julialang.org/t/extracting-fourier-coefficients-of-an-arbitrary-periodic-signal/51036/3 "2020-12-01T10:48:21Z")

</div>

May I profits from this question to get better understanding of the `fft`. Following the wikipedia formula for fourier series ([Fourier series - Wikipedia](https://en.wikipedia.org/wiki/Fourier_series)) I though that this simple code will reconstruct a real signal from its harmonics:

```julia
using FFTW, Plots
fs = 256
Δt = 1 / fs
P = 1.0
x = 0.0:Δt:P
y = [sin(2 * pi * 7 * t) + sin(2 * pi * 15 * t) + sin(2 * pi * 30 * t) for t in x] # mixture of simple wave signal
plot(x, y, legend = false)
Fy = rfft(y)
freq = 0.0:(fs / length(y)):(fs / 2)
N = length(Fy)
ak = real.(Fy ./ N)
bk = imag.(Fy ./ N)
yr = [ak[1] / 2 for _ in x]
for i in 2:N
    yr .+= ak[i] .* cos.(2 * pi * freq[i] .* x) .+ bk[i] .* sin.(2 * pi *freq[i] .* x)
end
plot!(x, yr)

```

But actually this give me a signal that is flip from the x axis and shift in time by `Δt`, to get the correct reconstruction I found that I have to do:

```julia
yr = [ak[1] / 2 for _ in x]
for i in 2:N
    yr .+= ak[i] .* cos.(2 * pi * freq[i] .* (x .+ Δt)) .+ bk[i] .* sin.(2 * pi *freq[i] .* (x .+ Δt))
end
yr .= -yr

```

May I ask why ?

---

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