Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(forward) fourier transform and (backward) inverse fourier transform gives the same result #30

Open
hlclemson opened this issue Jan 11, 2024 · 1 comment

Comments

@hlclemson
Copy link

import arraymancer
import fftw3
import std/complex

let shape = @[6]

# Use dummy tensor to create plan
var
    dummy_input = newTensor[Complex64](shape)
    dummy_output = newTensor[Complex64](shape)
var FFT: fftw_plan = fftw_plan_dft(dummy_input, dummy_output, FFTW_FORWARD, FFTW_ESTIMATE)
var inverseFFT: fftw_plan = fftw_plan_dft(dummy_input, dummy_output, FFTW_BACKWARD, FFTW_ESTIMATE)

# Allocate output Tensor
var inputRe = [1.0,2.0,3.0,4.0,5.0,6.0].toTensor.reshape(1, 6)
var inputIm = [0.0,0.0,0.0,0.0,0.0,0.0].toTensor.reshape(1, 6)
var input = map2_inline(inputRe, inputIm):
    complex64(x, y)

# initialize output tensors
var output = newTensor[Complex64](shape)
var output_fft = newTensor[Complex64](shape)
var output_ifft = newTensor[Complex64](shape)

fftw_execute_dft(FFT, input, output) # Execute FFT
var outConj = conjugate(output)
# calculate the power of the signal by elementwise multiplication
var sigMag = output*.outConj

# execute inverse fourier transform
fftw_execute_dft(inverseFFT, sigMag, output_ifft) # Execute a plan on new Tensor
fftw_execute_dft(FFT, sigMag, output_fft) # Execute a plan on new Tensor
echo(output_ifft)
echo(output_fft)
Tensor[Complex[system.float64]] of shape "[6]" on backend "Cpu"
    (546.0, 0.0)    (456.0, 0.0)    (402.0, 0.0)    (384.0, 0.0)    (402.0, 0.0)    (456.0, 0.0)
Tensor[Complex[system.float64]] of shape "[6]" on backend "Cpu"
    (546.0, 0.0)    (456.0, 0.0)    (402.0, 0.0)    (384.0, 0.0)    (402.0, 0.0)    (456.0, 0.0)
@Clonkk
Copy link
Member

Clonkk commented Oct 21, 2024

This is the operation performed by FFTW : https://fftw.org/fftw3_doc/The-1d-Discrete-Fourier-Transform-_0028DFT_0029.html#The-1d-Discrete-Fourier-Transform-_0028DFT_0029

I didn't reproduce locally on UT so that's a bit weird, I'll check your code later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants