Fast Fourier Transform (FFT) – Displaying the frequency-domain

Dear all,

this is a total beginner question, sorry for this one, but I am stuck. Let me explain you what I plan to do:

I have preprossed one run of one subject. My aim is to extract or calculate the power law exponent of this run. How do I plan to do this? Via the following steps:

  1. Take the run’s preprossed file (in my case “pb05.Subject_1.r01.scale+tlrc”).
  2. Calculate the fast fourier transform (FFT) to transform the time-domain of the run into its frequency-domain.
  3. Take the FFT to then calculate the power-spectrum (frequency-domain) and finally the PLE.

This is my code for the FFT (step 2):

Fast Fourier Transform (FFT)

directory=/users/philipp/desktop/fmri/dataset/subjects
for subject in subj1_exp1
do
mkdir $directory/$subject/FFT_PLE
for fMRIruns (pb05.Subject_1.r01.scale+tlrc)
do
cd $directory/$subject/preprocessing
echo “Processing $subject …”
3dPeriodogram -prefix $directory/$subject/FFT_PLE/FFT.$fMRIruns $fMRIruns
-nfft 1180
done
done

Now I have two questions: how is it possible via AFNI to transform the output file (FFT.pb05.Subject_1.r01.scale+tlrc), i.e., the four transformed file, into a .1D file so that I can nicely inspect the frequency-domain via AFNI (e.g., via the 1dplot option)? Of course, I can open the file just mentioned via the AFNI GUI and then display the file via a click on the GRAPH button, but this is a very silly way to do it, isn’t it?

My second question would be: would you recommend to use 3dfim+ to calculate the PLE, or is there a better option with AFNI today? I read that 3dfim+ is really outdated and no longer supported.

Many thanks in advance,

Philipp

An update:

I managed to display the output of step 2 (the calculated frequency domain) by adding the command “> Avg_FFT.1D”.
Next, I could then easily display the .1D file via the command “1dplot -ok_1D_text avg_fft.1d”. AFNI then plots the frequency domain and it is way better to inspect than via the AFNI GUI of course.

However, I am still stuck with the calculation of the power law exponent (PLE, step 3) based on the previously calculated frequency domain. I also searched through the board here as well as through AFNI’s program list, tried out lots of things, but I am still unable to do this.

In case you could provide help I would be thankful.

Philipp

Another update.

With massive amount of help from a friend of mine, I managed to finally calculate the PLE (for each voxel). I will show you my script here for two reasons:

  1. Maybe you have comments on the script.
  2. Maybe the script, even though it is a very basic and simple script at this point (and there is tons to improve), could help other beginners who search for how to calculate the PLE in the AFNI board.

Here it is:

FFT & PLE

FFT = Fast Fourier Transform (transformation of a time domain into its frequency domain)
PLE = Power Law Exponent (the PLE quantifies the shape/slope of the frequency domain in one value/number)

The frequency domain has different slopes everywhere, i.e., it is not linear. Consequently, three steps are required to calculate the PLE.

  1. The time-series/domain needs to be transformed into its frequency domain via the FFT.
  2. The non-linear frequency domain needs adjustments to make it linear. This is done via the logarithm of both frequency and power (log(P) and log(F)).
  3. The applied logarithm results into a line which is still not exactly linear. To create a linear line/slope, a linear regression line between log(F) and log(P) is applied.
    The slope of this perfect linear line is the power law exponent (PLE).

Fast Fourier Transform (FFT) - Transformation of the time-series into its frequency domain

directory=/users/philipp/desktop/fmri/dataset/subjects
directory_PLE=/users/philipp/desktop/fmri/dataset/Info
for subject in Subject1
do
mkdir $directory/$subject/FFT_PLE_RestingState
for fMRIruns (errts.Subject1_Rest.anaticor+tlrc)
do
cd $directory/$subject/Preprocessing_RestingState
echo "Processing $subject ..."

3dPeriodogram
-nfft 192
-prefix $directory/$subject/FFT_PLE_RestingState/FFT.$fMRIruns $fMRIruns

1deval
-a $directory_PLE/FFT_ideal.1D'{4..95}'
-expr 'log(a)' \

$directory/$subject/FFT_PLE_RestingState/FFT.1D

done
done

This is the first part of the script. What does it do? First, 3dPeriodogram adjusts the preprocessed and regressed functional dataset (e.g. "errts.Subject1_Rest.anaticor+tlrc" in this example, a resting-state run) to match its index number to the index number of our FFT_ideal.1D input file. It is the latter file that we have to provide for AFNI. Example: Lets say our that our provided "FFT_ideal.1D" file contains 96 values. Then, for the -nfft command, we will multiply this number by 2 (96*2=192). We will consequently use the value of 192: -nfft 192. The result is a transformation of the time-series into its frequency domain.

Second, 1deval calculates the logarithm for the FFT_ideal.1D input file. The output is a .1D file which contains all the numbers/values of the FFT.

Logarithm of Amplitude/Power log(P) y-axis

directory=/users/philipp/desktop/fmri/dataset/subjects
for subject in Subject1
do
for fMRIruns (FFT.errts.Subject1_Rest.anaticor+tlrc)
do
cd $directory/$subject/FFT_PLE_RestingState
echo "Processing $subject ..."

3dTcat
-prefix BP.$fMRIruns
$fMRIruns'[4..95]'

3dTsmooth
-hamming 7
-prefix Smooth.BP.$fMRIruns
BP.$fMRIruns

3dcalc
-prefix Log_Y.Smooth.BP.$fMRIruns
-a Smooth.BP.$fMRIruns
-expr '-log(a)'

done
done

In this third part above we basically do the same as for the second part, but for the y-axis. Note: 3dTsmooth smoothes the power. 3dcalc takes the logarithm of the power. We are now ready to calculate the PLE in the final step below. By opening the output file of this final script, one can inspect the PLE of every voxel via the Graph window in AFNI’s Gui.

Linear Regression Line between log(F) and log(P)

directory=/users/philipp/desktop/fmri/dataset/subjects
for subject in Subject1
do
for fMRIruns (Log_Y.Smooth.BP.FFT.errts.Subject1_Rest.anaticor+tlrc)
do
cd $directory/$subject/FFT_PLE_RestingState
echo "Processing $subject ..."
3dfim+
-input $fMRIruns
-ideal_file FFT.1D
-out 'Fit Coef'
-bucket PLE.$fMRIruns
done
done

Philipp

Thanks for this contribution to the AFNI community! You may want to also consider 3dLombScargle, written more recently by Paul Taylor. It allows for a censor list of volumes that is of importance in most FMRI acquisitions.

Thanks, Daniel. I will take a look at 3dLombScargle.

Have a good rest of the week.