Showing results for 
Search instead for 
Did you mean: 

Help: For loop script (FFT analysis)

Go to solution


I am in need of some assistance writing a script that will reduce manual work load, as I want the same script to run on different measurement files.



- I have access to both Diadem 2012 and 2018.

- I start with 1x .tdms file with 1x group containing 20x channels.

- Each channel is 15 s long with a resolution of 10 kHz (150 000 measurement values).

- I will first run an FFT analysis manually on one of the channels, with a step size of 1000 and no overlap. This results in 150 new channels in the frequency domain, with a step size of 100 ms. They are automatically named AmplitudePeak1, AmplitudePeak2, AmplitudePeak3, etc.


My understanding is that the calculations result in an FFT analysis every 100 ms, and that the AmplitudePeak channels are created in a chronological order.


What i want to do:

I am not interested in all the frequencies in the AmplitudePeak channels, only the highest value within two intervals. Lets call them interval a-b and interval c-dI am interested in the maximum value in either one of them (not both, just the one value that is the highest). The highest value will then correspond to the time step 0.1 s. Then i want to repeat the process for the 2nd AmplitudePeak channel and add the highest value for the time step 0.2 s, and so on for all the 150 channels, resulting in one new channel with 150 measurements back in the time domain.


My thoughts on doing it (may it be the most efficient way or not (probably not)):


N=150 (Number of AmplitudePeak channels)


For (i=1 to i=N)

    Get the maximum value in AmplitudePeak(i) found in the rows a-b or c-d in the frequency column (x).

    Add the highest value to ResultChannel




When later plotting the ResultChannel it will then show amplitude on the y-axis and the time on the x-axis.


I would very much appreciate any help in writing this script. Thank you!


Regards Erik

0 Kudos
Message 1 of 4
Accepted by topic author tagetes
'-- VBS script file
'-- Created on 07/31/2019 09:50:24
'-- Author: g
'-- Comment: ---
Option Explicit  'Forces the explicit declaration of all the variables in a script.

Dim chnl, resultChnl, i, j, maxAB, maxCD, A, B, C, D
A = 3
B = 5
C = 7
D = 12
Set resultChnl = Data.Root.ChannelGroups.Add("Results").Channels.Add("Result", DataTypeChnFloat64)
i = 1

Do While True ' Loop Forever
  If Data.Root.ChannelGroups(1).Channels.Exists("AmplitudePeak" & i) Then ' If Another AmplitudePeak exists
    Set chnl = Data.Root.ChannelGroups(1).Channels("AmplitudePeak" & i)
    i = i + 1 ' Update for next AmplitudePeak
    maxAB = chnl.Values(A) ' Find maxAB
    For j = A To B Step 1
      If chnl.Values(j) > maxAB Then
        maxAB = chnl.Values(j)
      End If
    maxCD = chnl.Values(C) ' Find maxCD
    For j = C To D Step 1
      If chnl.Values(j) > maxCD Then
        maxCD = chnl.Values(j)
      End If
    ' Add to result channel
    If maxAB > maxCD Then
      resultChnl.Values(resultChnl.Size + 1) = maxAB
      resultChnl.Values(resultChnl.Size + 1) = maxCD
    End If
    Exit Do ' EXIT the while loop if no more AmplitudePeak[i] exist
  End If

 Just modify your input-output group names and the A, B, C, D ranges you want to search.  Also use recording mode to automate the FFT parameters so you can save time there as well.

Message 2 of 4

Thank you very much for the help! It worked on the first try.


One thing I don't quite understand is how the resolution is affected by the interval length. I suppose it makes sense, but I can't really figure it out.


Lets say i have the 15 s signal with 10 kHz resolution:

  • If i make an FFT of the entire signal (150 000 interval length), then i get 15 values for each integer Hz.
  • If i change the interval length to 10 000 (i.e. 1000 ms step) I get 1 value for each integer Hz.
  • However, if I keep lowering the interval step to 1000 (i.e. 100 ms step), then i only get 1 value for each 10 Hz.

It is easy to see the trend:


Values/Frequency integer = Interval length/Signal resolution

= 150 000 / 10 000 = 15

= 10 000 / 10 000 = 1

= 1000 / 10 000 = 0,1


I really feel this is something I should understand, but since I dont I would very much appreciate any explanation. Thanks!

0 Kudos
Message 3 of 4

Hi, tagetes! The frequency resolution is defined as Fs/N in FFT. Where Fs is sample frequency, N is number of data points used in the FFT. For example, if the sample frequency is 1000 Hz and the number of data points used by you in FFT is 1000. Then the frequency resolution is equal to 1000 Hz/1000 = 1 Hz

Message 4 of 4