# Rescale X axis (for FFT)

Hi,

I am following along with the FFT tutorial located here: https://root.cern.ch/root/html/tutorials/fft/FFT.C.html

The comments mention that the x axis must be manually re-scaled (line 98, line 139). I want to accomplish this so that the x-axis reflects units of frequency. How do I go about doing this?

Hi,

This is needed due to the discreteness of the FFT. You should do as shown in the tutorial and transform the bin axis in frequency values

Lorenzo

Yes, this was my original question: how to â€śtransform the bin axis in frequency values.â€ť I followed the tutorial for FFT, but the last part of your â€śreplyâ€ť isnâ€™t illustrated in the tutorial.

I originally did not want to copy the histogram due to memory issues but I realized there is no other way to accomplish this simple task. So I copied the histogram, and modified the xMin, xMax of the new histogram, then copied the bin contents of the FFT histogram into the new one. See below for sample code, for anyone else who needs to do this.

Note: in the xMax range of the new histogram, hFFT, I divide by 2.0 as well, because the R2C FFT gives two peaks, one at the positive frequency and one at the negative frequency. I didnâ€™t want to display the second frequency, so that explains the 2.0. â€śtotalâ€ť is the signal (aka data values) in a vector earlier in the code. â€śpaddingâ€ť is an int that pads some number of 0â€™s at the end of the vector â€śtotalâ€ť.

// do the FFT and get the values we care about.
int fftSize = total.size();
TVirtualFFT* fft_total = TVirtualFFT::FFT(1, &fftSize, "R2C P K");
fft_total->SetPoints(&(total[0]));
fft_total->Transform();

// everything up to this part is in the tutorial
TH1* fft_total_hist = 0;
fft_total_hist = TH1::TransformHisto(fft_total, fft_total_hist, "Re");

// here is where I "rescale" the x-axis
// the fftSize-1 is for an overflow/underflow bin offset that biases the frequency
TH1D* hFFT = new TH1D("Hist FFT", "Histogram of FFT SQUID + noise"
, (fftSize - 1) / 2
, fft_total_hist->GetXaxis()->GetXmin() / ((fftSize-1)*timeStep)
, fft_total_hist->GetXaxis()->GetXmax() / (2.0*(fftSize-1)*timeStep));

// copy FFT histogram to new, scaled x-axis histogram
for(int j = 0; j < hFFT->GetNbinsX(); j++)
{
hFFT->SetBinContent(j, fft_total_hist->GetBinContent(j));
}
delete fft_total_hist;
delete fft_total;
total.clear();

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.