Task: Discrete Fourier transform




Go: package main

import (
    "fmt"
    "math"
    "math/cmplx"
)

func dft(x []complex128) []complex128 {
    N := len(x)
    y := make([]complex128, N)
    for k := 0; k < N; k++ {
        for n := 0; n < N; n++ {
            t := -1i * 2 * complex(math.Pi*float64(k)*float64(n)/float64(N), 0)
            y[k] += x[n] * cmplx.Exp(t)
        }
    }
    return y
}

func idft(y []complex128) []float64 {
    N := len(y)
    x := make([]complex128, N)
    for n := 0; n < N; n++ {
        for k := 0; k < N; k++ {
            t := 1i * 2 * complex(math.Pi*float64(k)*float64(n)/float64(N), 0)
            x[n] += y[k] * cmplx.Exp(t)
        }
        x[n] /= complex(float64(N), 0)
        
        if math.Abs(imag(x[n])) < 1e-14 {
            x[n] = complex(real(x[n]), 0)
        }
    }
    z := make([]float64, N)
    for i, c := range x {
        z[i] = float64(real(c))
    }
    return z
}

func main() {
    z := []float64{2, 3, 5, 7, 11}
    x := make([]complex128, len(z))
    fmt.Println("Original sequence:", z)
    for i, n := range z {
        x[i] = complex(n, 0)
    }
    y := dft(x)
    fmt.Println("\nAfter applying the Discrete Fourier Transform:")
    fmt.Printf("%0.14g", y)
    fmt.Println("\n\nAfter applying the Inverse Discrete Fourier Transform to the above transform:")
    z = idft(y)
    fmt.Printf("%0.14g", z)
    fmt.Println()
}




Python: 
import cmath


def dft( x ):
    
    N                           = len( x )
    result                      = []
    for k in range( N ):
        r                       = 0
        for n in range( N ):
            t                   = -2j * cmath.pi * k * n / N
            r                  += x[n] * cmath.exp( t )
        result.append( r )
    return result


def idft( y ):
    
    N                           = len( y )
    result                      = []
    for n in range( N ):
        r                       = 0
        for k in range( N ):
            t                   = 2j * cmath.pi * k * n / N
            r                  += y[k] * cmath.exp( t )
        r                      /= N+0j
        result.append( r )
    return result


if __name__ == "__main__":
    x                           = [ 2, 3, 5, 7, 11 ]
    print( "vals:   " + ' '.join( f"{f:11.2f}" for f in x ))
    y                           = dft( x )
    print( "DFT:    " + ' '.join( f"{f:11.2f}" for f in y ))
    z                           = idft( y )
    print( "inverse:" + ' '.join( f"{f:11.2f}" for f in z ))
    print( " - real:" + ' '.join( f"{f.real:11.2f}" for f in z ))

    N                           = 8
    print( f"Complex signals, 1-4 cycles in {N} samples; energy into successive DFT bins" )
    for rot in (0, 1, 2, 3, -4, -3, -2, -1):    
        if rot > N/2:
            print( "Signal change frequency exceeds sample rate and will result in artifacts")
        sig                     = [
            
            cmath.rect(
                1, cmath.pi*2*rot/N*i
            )
            for i in range( N )
        ]
        print( f"{rot:2} cycle" + ' '.join( f"{f:11.2f}" for f in sig ))
        dft_sig                 = dft( sig )
        print( f"  DFT:  " + ' '.join( f"{f:11.2f}" for f in dft_sig ))
        print( f"   ABS: " + ' '.join( f"{abs(f):11.2f}" for f in dft_sig ))