0

It appeared "Compile error: Sub or Function not defined" when I run the code. I have been finding the solutions but they all did not work. Anyone know please help me. I don't know about coding this is my homework. The professor give this code for us to process the data from our experiment. Here it my code

Function FFTRec(N As Integer, theta As Double, ar() As Double, ai() As Double, tmpr() As Double, tmpi() As Double)
    Dim nh As Integer, j As Integer
    Dim xr, xi, wr, wi, tmp2r(512) As Double, tmp2i(512) As Double

    If N > 1 Then
    nh = N / 2
    For j = 0 To nh - 1
        tmpr(j) = ar(j) + ar(nh + j)
        tmpi(j) = ai(j) + ai(nh + j)
        xr = ar(j) - ar(nh + j)
        xi = ai(j) - ai(nh + j)
        wr = Cos(theta * j)
        wi = Sin(theta * j)
        tmp2r(j) = xr * wr - xi * wi
        tmp2i(j) = xi * wr + xr * wi
    Next j
    Call FFTRec(nh, 2 * theta, tmpr, tmpi, ar, ai)
    Call FFTRec(nh, 2 * theta, tmp2r, tmp2i, ar, ai)
    For j = 0 To nh - 1
        ar(2 * j) = tmpr(j)
        ai(2 * j) = tmpi(j)
        ar(2 * j + 1) = tmp2r(j)
        ai(2 * j + 1) = tmp2i(j)
    Next j
    End If
 End Function
 Public Sub FFT()
    Dim xr(512) As Double, xi(512) As Double, tmpr(512) As Double, tmpi(512) As Double
    Dim pi, wm, theta As Double
    Dim i, Tr, N As Integer
    Dim curStartTime, curEndTime, curFreq As Currency

    i = 0: N = 512: Tr = 6
    pi = WorksheetFunction.pi
    theta = 2 * pi / N

    For i = 1 To N
        xr(i - 1) = Cells(i + Tr - 1, 2): xi(i - 1) = 0
    Next i

    Call QueryPerformanceFreQuency(curFreq)
    Call QueryPerformanceCounter(curStartTime)

    Call FFTRec(N, theta, xr, xi, tmpr, tmpi)

    Call QueryPerformanceCounter(curEndTime)
    Cells(1, 9) = "Processing Time " & CStr((curEndTime - curStartTime) / curFreq) & " Second"

    Cells(Tr - 1, 9) = "xr(i)_FFT": Cells(Tr - 1, 10) = "xi(i)_FFT": Cells(Tr - 1, 11) = "P_FFT"
    For i = 0 To N - 1
        Cells(i + Tr, 9) = xr(i)
     Cells(i + Tr, 10) = xi(i)
    Cells(i + Tr, 11) = Sqr(xr(i) ^ 2 + xi(i) ^ 2)
    Next i
 End Sub

Code

Error

By the way I am not a native speaker.

Thank you.

Raunak Thomas
  • 1,393
  • 1
  • 12
  • 28
  • If you keep calling FFTRec from within FFTRec with no conditional exit, aren't you going into an infinite loop? You also don't call functions; they return a value. –  May 27 '18 at 06:17

1 Answers1

0

The function QueryPerformanceFrequency is not defined. You need to define it somewhere. I think this should fix it. There is a code present here that you can try.

Else simply ask your professor to provide the code for definition of QueryPerformanceFrequency

Raunak Thomas
  • 1,393
  • 1
  • 12
  • 28
  • Yes. After I ran the program it marked blue at "QueryperformanceFrequency" .It still doesn't work with the new solution.It appeared "Code error: The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them with the PtrSafe attribute.".But Thank you so much. – benkyoua May 27 '18 at 06:33
  • @benkyoua There is nothing in your code above that is 64 v 32 dependant as far as I can see. If your code was formatted correctly it would be a lot easier to tell. I am guessing you are referring to an API call that is made somewhere else in the code. Probably in one of your not show calls. – QHarr May 27 '18 at 07:12
  • Yes, I tried to define QueryPerformanceFrequency and added some codes (https://stackoverflow.com/questions/31383177/vba-queryperformancecounter-not-working/31387007#31387007) into Class modules. – benkyoua May 27 '18 at 07:25
  • Private Declare PtrSafe Function for 64 bit. I am guessing you already did that! – QHarr May 27 '18 at 10:31
  • Yeah, I did but still.Thank you guys. – benkyoua May 27 '18 at 11:27