Asked by Rick Rosson
on 13 Sep 2011

What is the correct way to scale results when taking the Fast Fourier Transform (FFT) and/or the Inverse Fast Fourier Transform (IFFT)?

Answer by Rick Rosson
on 13 Sep 2011

Accepted Answer

Does scaling matter?

In most situations, scaling is really not all that important. The overall shape of the spectrum matters much more than the absolute scale.

What are the conventions?

But if you really are worried about it, there are several different conventions from which you can choose (see definitions below):

- Scale by dt for the FFT, and by Fs for the IFFT
- Scale by 1/M for the FFT, and by M for the IFFT
- Scale by 1 for the FFT, and by 1 for the IFFT
- Scale by 1/sqrt(M) for the FFT, and by sqrt(M) for the IFFT.
- and so on.

I generally use either option #1 or option #2 depending on my mood and whether it's raining outside.

Definitions

Here I am assuming that I have a discrete-time signal x represented as an M x N matrix, where M is the number of samples and N is the number of channels.

[M,N] = size(x);

Furthermore, I am assuming that the sampling rate is Fs and that I have defined the time increment as

dt = 1/Fs;

and the frequency increment as:

dF = Fs/M;

What do all these conventions have in common?

All of these conventions have one thing in common: The product of the two scaling factors is always 1. Please note that the ifft function in MATLAB includes a scaling factor of 1/M as part of the computation, so that the overall round-trip scaling is 1/M (as it should be).

HTH.

Rick

Dr. Seis
on 16 Feb 2012

Are you sure you are supposed to get 1. This reminds me of another post: http://www.mathworks.co.uk/matlabcentral/answers/25479-sdof-frf-fft-magnitude-discrepancy

Incidentally, you should probably turn this into another question.

omar thamer
on 19 Dec 2013

Jaimin Jha
on 9 Apr 2017

Sign in to comment.

Answer by Dr. Seis
on 28 Jan 2012

You are right about scaling being unimportant if only the shape of the spectrum is desired. However, if it is necessary that the amplitudes in the frequency spectrum be correct, then there is only one option for scaling - your option #1. In order for Parseval's theorem to hold, the energy in the time domain must equal the energy in the frequency domain. The example below demonstrates this:

> N = 8;

> dt = 0.1;

> df = 1/(dt*N)

df =

1.25

> a = randn(N,1)

a =

0.70154

-2.0518

-0.35385

-0.82359

-1.5771

0.50797

0.28198

0.03348

> b = fft(a)*dt

b =

-0.32813

0.10746 + 0.30519i

-0.080365 + 0.075374i

0.34826 + 0.17802i

0.13866

0.34826 - 0.17802i

-0.080365 - 0.075374i

0.10746 - 0.30519i

> energy_a = sum(a.*conj(a) * dt) % Not necessary to use conj here

energy_a =

0.83314

> energy_b = sum(b.*conj(b) * df) % Necessary to use conj here

energy_b =

0.83314

Rick Rosson
on 5 Nov 2015

In the continuous case, dt and df are infinitesimal quantities, which approach 0 in the limit that defines the Fourier integral. In the MATLAB code, dt and df are variables that take on values that are strictly non-zero (and positive). So the variable dt in the code is not in any way the same thing as the infinitesimal quantity dt in the continuous Fourier integral (and likewise for the variable df versus the infinitesimal df).

Moreover, the fft function is an implementation of the discrete Fourier transform (DFT), not the continuous time Fourier transform (CTFT).

Dr. Seis
on 12 Apr 2016

Johnathan, not sure what I am confusing here. I would suggest you look at the answer from "Chani" below in addition to my comment to their answer. Multiplying the output from Matlab's FFT function with "dt" is the only way to approximate the correct amplitudes (if you care about having physically meaningful amplitudes) AND allow for Parseval's theorem to be satisfied.

Just to follow Rick's comment and clarify my notation - when I say "dt" or "df" it is meant as a "delta-t" or "delta-f" (as in a discrete time or frequency increment), respectively.

Clay Fulcher
on 20 Sep 2018

Dr. Seiss, I want to thank you for helping me finally arrive at the correct scale factor to use for Matlab's FFT. I've been using 1/N for decades, and it usually isn't a problem since I most often go back to the time domain with N. However dt is the correct scale factor for FFT due to Parseval's Theorem as you made very clear. I would like to add this regarding the scale factor on IFFT: If Matlab did not internally scale their IFFT function by 1/N, then the correct scale factor to apply to IFFT would be df. However, Matlab does scale their IFFT by 1/N. It allows the FFT/IFFT transform pair to give the same result when transforming to frequency and back to time domains.

Since df = (fs / N) = [1 / (dt * N)], the correct scale factor to use with Matlab's "scaled" IFFT is (1 / dt). In Matlab,

X(f) = fft ( x(t) ) * dt

x(t) = ifft ( X(f) ) / dt

Here is the proof. If IFFT is the unscaled IFFT, and IFFTm is Matlab's implementation of IFFT/N,

x(t) = IFFT [X(f)] * df (for Parseval's theorem to be satisfied)

= IFFTm [X(f] * (df * N)

= IFFTm [X(f)] * [(fs / N) * N]

= IFFTm [X(f)] * (1 / dt)

QED

Sign in to comment.

Answer by Jérome
on 1 Aug 2017

Hello

I have seen so many subjects related to this issue and so don't know on which to put my answer...I hope this can help some people. I can see there are mainly 3 groups of people, those saying that is not important, those dividing by the number of points of the signal, and those dividing by the sampling frequency. first, for some applications,yes the correct amplitude is important. for example, the magnitude of an earthquake is computed from the amplitude of the spectrum.

then I agree with Dr Seis, the correct way of scaling spectrum is multiplying by dt.

People saying fft has to be divided by the number of points often take the example of the sin wave with amplitude A and want to see 2 peaks with amplitude A/2 on the spectrum. however, this is not the Fourier transform of a continuous sin wave. The Fourier transform has two Diracs. The value of each peak is infinite and the integration over the frequency domain is A/2. So the value of a correctly-scaled discrete spectrum we should have on both peaks is

A/df/2 = Npoints*A/Fs/2

Fs being the sampling frequency, df the step of the frequency vector.

the matlab fft outputs 2 pics of amplitude A*Npoints/2 and so the correct way of scaling the spectrum is multiplying the fft by dt = 1/Fs. Dividing by Npoints highlights A but is not the correct factor to approximate the spectrum of the continuous signal.

The second point is the parseval equation. I have seen many people saying the fft can not respect this relation or is not applicable in discrete mode. first, in discrete mode, if should tends to the continuous value. if can not be Npoints lager or smaller. And, if the fft is multiplied by dt, the energy of the input signal equals the energy of the spectrum.

I have seen quite often people using the Parseval equation for discrete signal like this, which is incorrect

sum(abs(xi).^2) = sum(abs(Xi).^2) with X = fft(x)

The correct discrete form of the Parseval relation is:

sum(abs(xi).^2)*dt = sum(abs(Xi).^2)*df

the relation is satisfied if the fft is multiplied dt and df is correctly defined.

Moreover, there are many simple typical Fourier transforms such as exponential decay, triangle function.. you can model the temporal signals and the known continuous transforms and check that fft*dt is the correct way of approaching the continuous transform.

Sign in to comment.

Answer by masoud Aghaei
on 7 Jul 2015

Sign in to comment.

Answer by Chani
on 16 Sep 2015

Edited by Chani
on 16 Sep 2015

Hi everyone,

I tried both options mentioned above, namely option #1 and option #2 with a simple sine curve. The results of both options confuse me and I am hoping you can help clear things up.

The code I am referring to is the following:

dt = 0.05; %Time step

t = [0:dt:10]; %Time vector

s = sin(2*pi*t); %Signal vector

N = length(s); %Number of data points

f_s = 1/dt; %Sampling frequency

df = f_s/N; %Frequency increment

NFFT = N; %FFT length used as second argument of fft()

y_option1 = fft(s,NFFT); %Compute FFT using sampling interval as scaling factor

y_option1 = y_option1*dt;

y_shiftOption1 = fftshift(y_option1);

y_option2 = fft(s); %Compute FFT using signal length as scaling factor

y_option2 = y_option2/N;

y_shiftOption2 = fftshift(y_option2);

if mod(N,2) == 0 %N is even

f = -f_s/2 : df : f_s/2-df;

else %N is odd

f = [sort(-1*(df:df:f_s/2)) (0:df:f_s/2)];

end

Plotting the sine signal and both result vectors y_shiftOption1 and y_shiftOption2 leads to the following Figure:

From my point of view the result of option #2 exhibits the correct amplitude of 0.5V, since the time signal has an amplitude of 1V and this value is split into the two impulses on the negative and the positive frequency scale.

However, regarding Parseval's Theorem, the energy is only preserved using option #1:

sum(abs(s).^2)*dt = 5

sum(abs(y_shiftOption1).^2).*df = 5

sum(abs(y_shiftOption2).^2).*df = 0.0495

Does this mean, if I am interested in the real amplitude of the signal, I have to use option #2 and if I want to preserve the energy of the signal option #1 has to applied?

Thanks in advance for your help!

Best Regards Chani

Image Analyst
on 10 Nov 2015

Shaui's flag on Rick's comment moved here to be a comment:

Fantastic!

Dr. Seis
on 10 Apr 2016

Rick, for Option #2, the graph represents spectral density in units of "volts per sample" (not simply "volts"). Until you supply the correct value used to sample the data as a "scale factor", the amplitudes are physically meaningless.

Also, if you do the actual Fourier Transform of that example SINE wave (with limits from 0 to 10) you will find that the amplitude at f=1Hz SHOULD be 5. So really, option 1 satisfies the amplitude issue and Parseval's theorem.

The point I have been trying to make in this post (and several others around "Matlab Answers") is if you are trying to approximate the Fourier Transform for a periodically sampled time series that is finite in length, then there is only one way to scale the output from the FFT function in Matlab - multiply the result by dt.

Kenny
on 14 Feb 2018

Doc Seis.....have you got an example to show this? I'm thinking of a portion of a sinusoidal signal that digitally spans 1 period..... such as N points, where point #0 is the start point, and the point #(N-1) is the LAST point (such that the following data point would theoretically be the same as point #0 again). The data set will be 'N' points.

For this case, the scaling should still be 'divide by N', right? If instead we multiplied the result by the sampling period, then the FFT magnitude amplitudes wouldn't look right..... right?

Sign in to comment.

Answer by Michele Marconi
on 24 Oct 2017

How is it possible that the fft computed with matlab (no particular scaling!) and the FFT obtained by utilizing Xilinx module for SystemGenerator, have a 2000x factor scaling difference? i.e. Xilinx one is 2000 times lower amplitude?

(I thought this topic was spot-on regarding amplitude issues, sorry for gravedigging!)

Sign in to comment.

Answer by RSK
on 19 Apr 2018

Dr. Seis,

I think here and at other places where you have mentioned the word "amplitude", you meant the amplitude of the resulting transform after multiplying by dt, correct? (and that is not going to be the amplitude of the sine wave at that frequency and having that transform). However, if people are interested in the amplitude of sine wave that generated the transform, should option#2 in Chani's example be used(although that is not the real amplitude of the transform)? If I have something, say, machine bed(for the sake of example) vibrating at 1hz with unit magnitude and I am presented with the transform generated by "option#1" as the measured vibration, I should certainly not conclude that the machine bed is vibrating with the amplitude shown by "option#1". So I guess dividing by N (option#2) would tell me the magnitude with which the machine bed is vibrating. Is this understanding correct? Thanks

Sign in to comment.

Answer by Bruno Luong
on 21 Sep 2018

Edited by Bruno Luong
on 21 Sep 2018

IMHO, if the input signal is x(t) is real, only about half of X(f) = fft(x)(f) is relevant (index 1:ceil((N+1)/2))) the other half is just conjugate of the flip of the first part, so the Parseval would be matching the integration of X(f)^2 on [0,1/(2*dt)] (up to Nyquist frequency) and one must multiply by an extra factor of abot sqrt(2) on top of dt on fft(x(t_i)) to get the Parseval equality up to Nyquist frequency (and not beyond that).

Some subtle consideration: if N is even, the frequency ceil((N-1)/2)*df count only once so it should not be multiplied by sqrt(2). The same for static term (f=0) for all the cases.

A factor of sqrt(2): it can make a bridge resists or fallen down.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Jan (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/15770-scaling-the-fft-and-the-ifft#comment_662512

Sign in to comment.