# FFT + Measurements

**URL:** https://discourse.julialang.org/t/fft-measurements/33791
**Category:** Numerics
**Created:** [January 25, 2020, 11:20pm UTC](https://discourse.julialang.org/t/fft-measurements/33791 "2020-01-25T23:20:12Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [January 25, 2020, 11:20pm UTC](https://discourse.julialang.org/t/fft-measurements/33791/1 "2020-01-25T23:20:12Z")

</div>

I thought it would be cool if I could calculate the FFT using uncertain data, so I tried using Measurements together with FFTW. I did not get it working.

Instead, I wrote a simple FT function which can be used with Measurements.

I’m not sure if it is mathematical sound as Measurements only seems to work on Floats and FFTs often produces complex numbers.

Maybe someone with more experience in FFTs can check that.  
I think this could be useful for signal/image processing.

Here is some example code:

```julia
using FFTW
using Test
using Measurements

function fft2(v::AbstractArray)
    k = similar(v,Complex{eltype(v)})
    f = fftfreq(length(v))
    for i in 1:length(k)
        k[i] = sum(v .* exp.((-1im * 2*π.*(i-1)) .*f))
    end
    k
end

a = range(0,10,length = 20)

reference = fft(a)
sample = fft2(a)
am = measurement.(a,1.0)
sample2 = fft2(am)

@test length(sample) == length(reference)
@test all(sample .≈ reference)
@test all(Measurements.value.(sample2) .≈ reference)

```

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [January 26, 2020, 12:31am UTC](https://discourse.julialang.org/t/fft-measurements/33791/3 "2020-01-26T00:31:39Z")

</div>

`FFTW.jl` only works with standard numerical types because it’s a wrapper around a C library. [`FastTransforms.jl`](https://github.com/JuliaApproximation/FastTransforms.jl) has an implementation of FFT for any custom Julia type:

```julia
 using Measurements, FastTransforms

julia> V = measurement.(randn(10), rand(10) ./ 10)
10-element Array{Measurement{Float64},1}:
    0.49 ± 0.081
   0.967 ± 0.069
    0.21 ± 0.034
   0.256 ± 0.065
   0.232 ± 0.055
   0.764 ± 0.02
  -1.065 ± 0.041
 -0.7481 ± 0.0099
  -0.653 ± 0.081
   0.835 ± 0.013

julia> ifft(fft(V))
10-element Array{Complex{Measurement{Float64}},1}:
     0.49 ± 0.081 + 0.0 ± 2.6e-17im
    0.967 ± 0.069 - 2.66e-16 ± 1.9e-17im
     0.21 ± 0.034 + 7.55e-16 ± 2.9e-17im
    0.256 ± 0.065 + 1.0e-16 ± 2.8e-17im
    0.232 ± 0.055 - 1.55e-16 ± 2.7e-17im
     0.764 ± 0.02 - 2.23e-16 ± 3.2e-17im
   -1.065 ± 0.041 - 1.33e-16 ± 2.5e-17im
 -0.7481 ± 0.0099 + 9.33e-16 ± 4.0e-17im
   -0.653 ± 0.081 - 8.9e-17 ± 3.0e-17im
    0.835 ± 0.013 + 7.55e-16 ± 3.6e-17im

```

Performance is not great, though

> [@feanor12](#):
>
> Measurements only seems to work on Floats and FFTs often produces complex numbers.

`Measurement`s work with complex numbers, if this is what you mean:

```julia
julia> using Measurements

julia> z = complex(1 ± 0.1, 2 ± 0.1)
(1.0 ± 0.1) + (2.0 ± 0.1)im

julia> z ^ 2
(-3.0 ± 0.45) + (4.0 ± 0.45)im

julia> sqrt(z ^ 2)
(1.0 ± 0.1) + (2.0 ± 0.1)im

```
