# 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:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [December 1, 2020, 9:48am UTC](https://discourse.julialang.org/t/extracting-fourier-coefficients-of-an-arbitrary-periodic-signal/51036/1 "2020-12-01T09:48:06Z")

</div>

Hello!

Does a package exist in Julia which takes a signal and return the Fourier Coefficients needed to approximate this signal?

Kind regards

---

<div class="post-metadata">

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

</div>

If by a signal you mean a vector of floating point numbers, then FFTW.jl will transform it using fft for you.

---

<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 ?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 1, 2020, 11:26am UTC](https://discourse.julialang.org/t/extracting-fourier-coefficients-of-an-arbitrary-periodic-signal/51036/4 "2020-12-01T11:26:54Z")

</div>

You might need `fftshift` or `ifftshift`

---

<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, 1:08pm UTC](https://discourse.julialang.org/t/extracting-fourier-coefficients-of-an-arbitrary-periodic-signal/51036/5 "2020-12-01T13:08:23Z")

</div>

As I use `rfft` I already get only half the spectrum and from my understanding `fftshift` only perform a circular shit to put the 0 frequency in the middle of the spectrum. For example if I want the reconstruction using `fft` instead I will have to do this:

```julia
plot(x, y, legend = false)
Fy = fftshift(fft(y))
freq = 0.0:(fs / length(y)):(fs / 2)
freq = [freq[end:-1:2]; freq]
N = length(Fy)
ak = real.(Fy ./ N)
bk = imag.(Fy ./ N)
yr = [ak[div(N, 2) + 1] / 2 for _ in x]
for i in 1:div(N, 2) # negative frequency
    yr .+= ak[i] .* cos.(2 * pi * freq[i] .* (x .+ Δt)) .- bk[i] .* sin.(2 * pi * freq[i] .* (x .+ Δt))
end
for i in (div(N, 2) + 2):N # positive frequency
    yr .+= ak[i] .* cos.(2 * pi * freq[i] .* (x .+ Δt)) .+ bk[i] .* sin.(2 * pi * freq[i] .* (x .+ Δt))
end
yr .= -yr
plot!(x, yr)

```

But once again I have to add a time shift of `Δt` and to reverse the signal from the x axis… It seems I’m missing something there

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [December 1, 2020, 2:08pm UTC](https://discourse.julialang.org/t/extracting-fourier-coefficients-of-an-arbitrary-periodic-signal/51036/6 "2020-12-01T14:08:53Z")

</div>

I want the specific coefficients, i.e. a0, a1,b1,a2,b2 etc.

When I use that library and the function `fft` I get the Fourier transformed signal, but how do I get it to output the coefficients it used?

Kind regards

---

<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, 6:22pm UTC](https://discourse.julialang.org/t/extracting-fourier-coefficients-of-an-arbitrary-periodic-signal/51036/7 "2020-12-01T18:22:04Z")

</div>

@Ahmed_Salih the ak and bk coefficient from my code should be what your are looking for… just need to figure out why the reconstruction need to be inverted and shifted !

---

<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)

---

<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 2, 2020, 4:57pm UTC](https://discourse.julialang.org/t/extracting-fourier-coefficients-of-an-arbitrary-periodic-signal/51036/9 "2020-12-02T16:57:53Z")

</div>

@rafael.guerra thank you very much for your answer, indeed my mistake was with the sign of the `bk` coefficients !
