Consulting

Results 1 to 6 of 6

Thread: Run several Fast Fourier Transformations in a row

  1. #1
    VBAX Newbie
    Joined
    Oct 2018
    Posts
    2
    Location

    Run several Fast Fourier Transformations in a row

    Hi guys,

    Recently I had to calculate some data with Fast Fourier Transformation (FFT).
    At the beginning I used data analysis toolpak in the worksheet directly which worked.
    since I have to do it quite often in thenext time I tried to do it in VBA.
    The Macro works just fine basically.
    I always get a set of 30 test runs which I analyze.The macro I have works and is also fast enough.
    But I know it is just super complicatedwhat I did for no reason
    Can you help me to find a more clever wayto write the macro and also to make it flexible in case I have 28 or 32datasets and not exactly 30?
    What I have is that (I cut it after 5thrun as it is super long otherwise):

    Sub CalcFFT512()
    '
    ' CalcFFT with 512 samples
    On Error GoTo ExitNow
    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
    End With
    '1
    Application.Run "ATPVBAEN.XLAM!Fourier", ActiveSheet.Range("$D$8:$D$519") _
    , ActiveSheet.Range("$AI$8"), False, False
    '2
    Application.Run "ATPVBAEN.XLAM!Fourier", ActiveSheet.Range("$E$8:$E$519") _
    , ActiveSheet.Range("$AL$8"), False, False
    '3
    Application.Run "ATPVBAEN.XLAM!Fourier", ActiveSheet.Range("$F$8:$F$519") _
    , ActiveSheet.Range("$AO$8"), False, False
    '4
    Application.Run "ATPVBAEN.XLAM!Fourier", ActiveSheet.Range("$G$8:$G$519") _
    , ActiveSheet.Range("$AO$8"), False, False
    '5 and so on....

    ExitNow:
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
    End With
    End Sub


    Any good idea?

    Best regards
    Steffen

  2. #2
    SHouldn't that be:

    '1
    Application.Run "ATPVBAEN.XLAM!Fourier", ActiveSheet.Range("$D$8:$D$519") _
    , ActiveSheet.Range("$AI$8"), False, False
    '2
    Application.Run "ATPVBAEN.XLAM!Fourier", ActiveSheet.Range("$E$8:$E$519") _
    , ActiveSheet.Range("$AL$8"), False, False
    '3
    Application.Run "ATPVBAEN.XLAM!Fourier", ActiveSheet.Range("$F$8:$F$519") _
    , ActiveSheet.Range("$AO$8"), False, False
    '4
    Application.Run "ATPVBAEN.XLAM!Fourier", ActiveSheet.Range("$G$8:$G$519") _
    , ActiveSheet.Range("$AR$8"), False, False
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  3. #3
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    Assuming the layout of the rest is consistent and you can do it in a loop like this:
    For i = 0 To 29
    Application.Run "ATPVBAEN.XLAM!Fourier", ActiveSheet.Range(Cells(8, 4 + i), Cells(519, 4 + i)) _
    , ActiveSheet.Range(Cells(8, 35 + (i * 3)), Cells(8, 35 + (i * 3))), False, False
    Next i
    you can the set the loop using a value in a cell or user input

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,639
    Sub M_snb()
      sn = Sheet1.Cells(8, 4).CurrentRegion
        
      For j = 0 To UBound(sn, 2) - 1
        Application.Run "ATPVBAEN.XLAM!Fourier", Sheet1.Range("$D$8:$D$519").Offset(, j), Sheet1.Cells(8, 35 + j)
      Next
    End Sub

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Since I know how much trouble I have going back to a macro after a time, I opt for the wordy, self documenting approach



    Option Explicit
    
    Sub CalcFFT512()
        Dim rData As Range, rData1 As Range, rCol As Range, rAnswer As Range
        
        'On Error GoTo ExitNow
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
            .Calculation = xlCalculationManual
        End With
        
        Set rData = ActiveSheet.Range("$D$8")
        Set rData1 = rData.End(xlDown)
        Set rData = Range(rData, rData1.End(xlToRight))
        
        Set rAnswer = ActiveSheet.Range("$AI$8")
        
        For Each rCol In rData.Columns
            Application.Run "ATPVBAEN.XLAM!Fourier", rCol, rAnswer, False, False
            Set rAnswer = rAnswer.Offset(0, 3)
        Next
    
    ExitNow:
        With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        End With
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Newbie
    Joined
    Oct 2018
    Posts
    2
    Location
    Guys you are amazing!

    The solutions work perfect in is just so much more convenient now
    I added a function to clear the result are prior to FFT calculation and that’s it.

    Thanks a lot!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •