Inverse of DFT transform

Andrew007

Member
Joined
Nov 26, 2009
Messages
5
Programming Experience
10+
Hi guys. There is a bug in my code somewhere. It has a form with 5 input fields and 5 output fields. The algorithm is supposed to do a forward DFT and then immediately do a reverse DFT on the same data - so the output values should be the same. However it returns garbage! Please help, as I cannot see where the bug is. :confused:
Thanks


VB.NET:
Public Class Form1

    Const dftcount As Integer = 5
    Dim dft_rvals(dftcount) As Double
    Dim dft_ivals(dftcount) As Double
    Dim twopi As Double = Math.PI * 2


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim counter As Integer
        For counter = 0 To dftcount - 1
            Func(counter)
        Next
        For counter = 0 To dftcount - 1
            FuncInv(counter)
        Next

    End Sub


    Private Sub Func(ByVal counter As Integer)

        Dim input(5) As Double
        input(0) = Double.Parse(in1.Text)
        input(1) = Double.Parse(in2.Text)
        input(2) = Double.Parse(in3.Text)
        input(3) = Double.Parse(in4.Text)
        input(4) = Double.Parse(in5.Text)

        dft_rvals(counter) = 0
        dft_ivals(counter) = 0

        Dim n As Integer
        For n = 0 To dftcount - 1

            dft_rvals(counter) += (input(n) * Math.Cos(1 * twopi * counter * (n / dftcount)))
            dft_ivals(counter) -= (input(n) * Math.Sin(1 * twopi * counter * (n / dftcount)))

        Next

    End Sub


    Private Sub FuncInv(ByVal n As Integer)

        Dim rval As Double = 0
        Dim ival As Double = 0

        Dim counter As Integer
        For counter = 0 To dftcount - 1

            rval += (dft_rvals(counter) * Math.Cos(-1 * twopi * counter * (n / dftcount)))
            ival -= (dft_ivals(counter) * Math.Sin(-1 * twopi * counter * (n / dftcount)))

        Next

        rval /= dftcount
        ival /= dftcount

        Select Case n
            Case 1 : out1.Text = rval.ToString
            Case 2 : out2.Text = rval.ToString
            Case 3 : out3.Text = rval.ToString
            Case 4 : out4.Text = rval.ToString
            Case 5 : out5.Text = rval.ToString
        End Select

    End Sub


End Class
 
Attachment removed, use only valid upload types.
 
Back
Top