# FFT: How to rescale x-axis

**URL:** <https://root-forum.cern.ch/t/fft-how-to-rescale-x-axis/61649>\
**Category:** Newbie\
**Created:** [September 12, 2024, 10:05pm UTC](https://root-forum.cern.ch/t/fft-how-to-rescale-x-axis/61649 "2024-09-12T22:05:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![atha.stam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/atha.stam/32/14293_2.png) [@atha.stam](https://root-forum.cern.ch/u/atha.stam)\
**Post date:** [September 12, 2024, 10:05pm UTC](https://root-forum.cern.ch/t/fft-how-to-rescale-x-axis/61649/1 "2024-09-12T22:05:48Z")

</div>

* * *

_ROOT Version:_ 6.16/00  
_Platform:_ Ubuntu 20.04  
_Compiler:_ gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2)

* * *

Dear co-rooters,

I am trying to perform a FFT on a simple sinusodial function, copying the example [FFT.C](https://root.cern/doc/master/FFT_8C.html).

I am creating a `TF1` object and then a `TH1D` histogram that samples from that function. Here’s how it looks

 ![Screenshot from 2024-09-12 15-55-11](https://root-forum.cern.ch/uploads/default/original/3X/2/d/2d74f4465f00665f0bee7646c80783ee301b31cb.png)

This function has a period of 2Pi seconds (assume it’s a signal) and therefore a frequency of 1/2Pi=0.16 Hz. If I do a Fourier transform, I would expect a peak at 0.16 Hz.

I understand that `TH1::FFT` won’t give me the actual frequency and I will need to scale the x-axis. The unscaled FFT looks like this:

 ![Screenshot from 2024-09-12 15-59-49](https://root-forum.cern.ch/uploads/default/original/3X/2/e/2e73e022c2044bdea2fdb7bd20a25f4e61d53779.png)

There’s a peak at 0 (no idea why) and a peak around 250 which I think corresponds to the number of bins of my input histogram. If I change the number of bins to 25, then the peak appears at 25. So the units are bins or indices. If that’s correct, I would expect the frequency to be ta 1/125, since one period is completed at bin# 125 (there are 2 periods or cycles over the range of the input histogram).

I (think) I’m properly rescaling the x-axis, but I get the following:

 ![Screenshot from 2024-09-12 16-03-59](https://root-forum.cern.ch/uploads/default/original/3X/5/5/55625592140b610c8ab470564c406c2e750d25be.png)

Instead of a peak at 0.16, I get a peak at 20 and a peak at 0.2.  
What am I doing wrong?

Here’s my code

```auto
#include "TF1.h"
#include "TH1D.h"
#include "TMath.h"
#include "TVirtualFFT.h"

void FFT() {

   // Define sin(x) function   
   TF1 *fsin = new TF1("fsin", "sin(x)", 0, 4*TMath::Pi());
   fsin->Draw();

   // Define histogram and fill with sin(x) values
   int n = 250; // number of bins
   TH1D *hsin = new TH1D("hsin", "hsin", n+1, 0, 4*TMath::Pi());
   
   // Fill the histogram with function values
    double x;
   for (int i=0; i<n; ++i) {
      x = (Double_t(i)/n)*(4*TMath::Pi());
      hsin->SetBinContent(i+1, fsin->Eval(x));
   }
   hsin->Draw("same");

   // Compute the Fourier transform and look at the magnitude of the output
   TH1 *hm = nullptr;
   TVirtualFFT::SetTransform(nullptr);
   hm = hsin->FFT(hm, "MAG");
   hm->SetTitle("Magnitude of the Fourier Transform");
   //hm->Scale(1./sqrt(n)); // Rescale by sqrt(n)

   // Now we need to rescale the x-axis to show real frequencies
   double low_end_sin = hsin->GetBinLowEdge(1);
   double upper_end_sin = hsin->GetBinCenter(n)+hsin->GetBinWidth(n)/2.;
   double range_sin = upper_end_sin - low_end_sin;
   
   int bins_FFT = hm->GetNbinsX();
   double low_end_FFT = hm->GetBinLowEdge(1)/range_sin;
   double upper_end_FFT = ( hm->GetBinCenter(bins_FFT)+hm->GetBinWidth(bins_FFT)/2. )/range_sin;
   
   TH1D *h_FFT = new TH1D("h_FFT", "Real Frequencies", bins_FFT, low_end_FFT, upper_end_FFT);

   // Set the new x-axis (real frequencies) and fill the histogram with magnitudes
   for (int i=0; i <bins_FFT; ++i) {
   		h_FFT->SetBinContent(i+1, hm->GetBinContent(i+1));
   }

   hm->Draw("histo");
   h_FFT->Draw("histo");
}

```

Any help will be much appreciated!

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 13, 2024, 7:07am UTC](https://root-forum.cern.ch/t/fft-how-to-rescale-x-axis/61649/2 "2024-09-13T07:07:05Z")

</div>

When i run you macro I get the follwing error mesage:

```auto
 warning: null passed to a callee that requires a non-null argument [-Wnonnull]
   hm->SetTitle("Magnitude of the Fourier Transform");
   ^~

```

Do you get it also ?

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [September 13, 2024, 10:01am UTC](https://root-forum.cern.ch/t/fft-how-to-rescale-x-axis/61649/3 "2024-09-13T10:01:12Z")

</div>

Side note:  
An easier way to rescale your x axis is to just call h-\>GetXaxis()-\>SetLimits(min, max);

Side note 2:  
See TH1::TransformHisto helper function

@couet that’s maybe because of missing fftw3 package installation

---

<div class="post-metadata">

**Author:** ![atha.stam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/atha.stam/32/14293_2.png) [@atha.stam](https://root-forum.cern.ch/u/atha.stam)\
**Post date:** [September 13, 2024, 11:14am UTC](https://root-forum.cern.ch/t/fft-how-to-rescale-x-axis/61649/4 "2024-09-13T11:14:59Z")

</div>

No, I don’t get this error. I’m no compiling it though; I’m just loading .L macro.C

---

<div class="post-metadata">

**Author:** ![atha.stam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/atha.stam/32/14293_2.png) [@atha.stam](https://root-forum.cern.ch/u/atha.stam)\
**Post date:** [September 13, 2024, 1:53pm UTC](https://root-forum.cern.ch/t/fft-how-to-rescale-x-axis/61649/5 "2024-09-13T13:53:33Z")

</div>

Thanks for the tip! Much easier!  
The thing I’m not sure about is if the x-axis rescaling I’m doing, is the correct one.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [September 13, 2024, 3:41pm UTC](https://root-forum.cern.ch/t/fft-how-to-rescale-x-axis/61649/6 "2024-09-13T15:41:35Z")

</div>

Find attached this example where you can see how the x axis rescaling should work.

```auto
#include "TF1.h"
#include "TH1D.h"
#include "TMath.h"
#include "TVirtualFFT.h"
#include "TCanvas.h"

void fft() {
   // Define histogram and fill with sin(2pi*f*x) values
   const int n = 2500; // number of bins
   TH1D *hsin = new TH1D("hsin", "hsin", n, 0, 10);
   hsin->GetXaxis()->SetTitle("Time / s");
   // Fill the histogram with function values
   const double freq = 20; // Hz
   for (int i=0; i<n; ++i) {
      hsin->SetBinContent(i+1, TMath::Sin(hsin->GetXaxis()->GetBinCenter(i+1)*freq*2.*TMath::Pi()));
   }
   auto c0 = new TCanvas();
   hsin->Draw();
   // fsin->Draw("same");

   // Compute the Fourier transform and look at the magnitude of the output
   TH1 *hm = nullptr;
   TVirtualFFT::SetTransform(nullptr);
   hm = hsin->FFT(hm, "MAG");
   hm->SetTitle("Magnitude of the Fourier Transform");
   //hm->Scale(1./sqrt(n)); // Rescale by sqrt(n)

   auto c = new TCanvas();
   c->SetLogy();
   hm->GetXaxis()->SetTitle("f / Hz");
   hm->GetXaxis()->SetLimits(0, n/hsin->GetXaxis()->GetBinUpEdge(n+1)); // Rescale x axis to Hz
   hm->Draw("histo");
}

```

The “peak” at the end is related to the FFT mirroring, see [discrete signals - Why is the FFT "mirrored"? - Signal Processing Stack Exchange](https://dsp.stackexchange.com/questions/4825/why-is-the-fft-mirrored) (actually, everything between 125 to 250 Hz is the mirror of 0 to 125 Hz). [matlab - Why can the FFT always be mirrored in the middle of the x axis? - Signal Processing Stack Exchange](https://dsp.stackexchange.com/questions/8567/why-can-the-fft-always-be-mirrored-in-the-middle-of-the-x-axis)

---

<div class="post-metadata">

**Author:** ![atha.stam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/atha.stam/32/14293_2.png) [@atha.stam](https://root-forum.cern.ch/u/atha.stam)\
**Post date:** [September 13, 2024, 6:20pm UTC](https://root-forum.cern.ch/t/fft-how-to-rescale-x-axis/61649/7 "2024-09-13T18:20:57Z")

</div>

I was missing the 2Pi\*f…  
Thanks for the help!
