Fourier Coeffs From FFT

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Fourier Series and Transforms From FFTs

(e.g., with Matlab)


Jaijeet Roychowdhury
Abstract
These operations are required all the time; and it is a pain to keep deriving the
correct procedures repeatedly.

1 Fourier Coefficients, FFTs and Matlab


Matlabs help fft says:
>> help fft
FFT Discrete Fourier transform.
FFT(X) is the discrete Fourier transform (DFT) of vector X. For
matrices, the FFT operation is applied to each column. For N-D
arrays, the FFT operation operates on the first non-singleton
dimension.
FFT(X,N) is the N-point FFT, padded with zeros if X has less
than N points and truncated if it has more.
FFT(X,[],DIM) or FFT(X,N,DIM) applies the FFT operation across the
dimension DIM.
For length N input vector x, the DFT is a length N vector X,
with elements
N
X(k) =
sum x(n)*exp(-j*2*pi*(k-1)*(n-1)/N), 1 <= k <= N.
n=1
The inverse DFT (computed by IFFT) is given by
N
x(n) = (1/N) sum X(k)*exp( j*2*pi*(k-1)*(n-1)/N), 1 <= n <= N.
k=1
The relationship between the DFT and the Fourier coefficients a and b in
N/2

x(n) = a0 + sum a(k)*cos(2*pi*k*t(n)/(N*dt))+b(k)*sin(2*pi*k*t(n)/(N*dt))


k=1
is
a0 = X(1)/N, a(k) = 2*real(X(k+1))/N, b(k) = -2*imag(X(k+1))/N,
where x is a length N discrete signal sampled at times t with spacing dt.
See also IFFT, FFT2, IFFT2, FFTSHIFT.
The relationship mentioned above between the DFT and the Fourier coefficients does
not match up in terms of the indices. So we derive it ourselves: First we have a
truncated Fourier series
M

x t 
Let N



Xi e ji T t 
2

(1)

 2M  1. If we sample x t  uniformly at intervals of NT , then we get


M

xn

Xi e jin N

Xi e jin N

 M
 1


N 1

l M 1

Xl 

Now, for i M, define Xi

Ne

(2)
jln 2
N

2
 Xie jin N

i 0

defining l

 i  N

 Xi  N . Then we have
xn

2
 Xi e jin N 
i 0

(3)

From the Matlab description of the IDFT and DFT, we have xn 


N  IDFT  Xi  , hence we have Xi  N1 DFT  xn  .
The matlab code for this looks like:

M = 8; % example
N = 2*M+1;
T = 1; % example; signal should be periodic with this T
tpts = (0:(N-1))/N*T;
% samples = evalx(tpts); % get the time samples
samples = cos(2*pi/T*tpts) + 3*sin(3*2*pi/T*tpts); % example
tmp = fft(samples)/N;
% tmp(1) is X_0; tmp(2:M+1) are X_1 to X_M;
% tmp(N:-1:M+2) are X_{-1} to X{-M}
% now reorder tmp
% for loops are inefficient
%for j = 1:M
%
Xarray(j) = tmp(M+1+j);
2

%
Xarray(j+M+1) = tmp(j+1);
%end
Xarray(1:M) = tmp(M+2:N);
Xarray((M+2):N) = tmp(2:(M+1));
Xarray(M+1) = tmp(1);
% now Xarray(1:M) are X_{-M} to X{-1};
% Xarray(M+1:N) are X(0) to X(N)
stem(-M:M, abs(Xarray));
oof = sprintf(Fourier Coeffiecient index (multiples of f0=%g), 1/T);
xlabel(oof);
ylabel(magnitude of Fourier Coefficient);

2 Fourier Transforms from FFTs


We would like to compute the Fourier Transform of a time-limited signal. This is useful, for example, in numerically calculating power spectral densities from data. From
the old book of Papoulis, page 343, the PSD S f  of a random process s t  obeying
certain fourth-moment conditions is given by
S f 

lim ST f 

ST f 

1
T



 

T
0

s t  e

j2 f t

 2

dt 

Note: setting s t  to a deterministic periodic signal will result in delta functions for the
above at its harmonics. Also, the above might not hold for white noise (infinite power)
waveforms.
We assume that x t  is zero outside the interval  0
T  , and also, for simplicity, that
it is continuous, so its values at t  0 and t  T are exactly zero. If x t  is bounded
then it has finite energy, so its Fourier Transform X f  exists.
Now, we form the periodic function
xT t 



x t  mT 

which is identical to x t  over the interval  0


T  , and consists of shifted copies of it
outside.
Since xT t  is periodic, it has a Fourier series, i.e., x t  i Xi e j2 f0t , where f0  T1 .
You can easily show (this is a step in the proof of the Poisson Sum Formula, see, e.g.,
the web Shannon course notes downloaded from korea) that its Fourier Coefficients Xn
are given by
Xn  f0 X n f0 

or X n f0  T Xn 
Xi can be obtained using the DFT.
The above may work well enough for deterministic signals, but it does not work
well at all for estimating the PSD of random signals - see the code in the appendix.
There is a considerable body of literature on this, see Numerical Recipes for a good
overview.
3

Matlab has convenient functions for PSD estimation using the Welch periodogram
- see the attached file PSDs.m. There are some issues with it that are not clear particularly when interpolation is used. See the notes inside PSD.m.

Generating flicker noise


Orthogonal to the issue of interpolation affecting the PSD, it is easy to generate approximate flicker noise see the files runtdflicker.m, tdflicker.m, flicker.m, runflicker.m
and flickerpsd.m.

Fourier transform of a time-limited signal

Here is some code to calculate the FT of a time-limited sequence. The time sequence
is not periodic (but could be made so by extending the boundaries). We ignore the lack
of periodicity of the end points, they probably wont make much difference for long
sequences.
Note that the code as shown below is not very useful for random inputs. For that,
one should use the Welch periodogram - see Numerical Recipes and Matlabs psd and
pwelch functions.
% we assume that time samples of length N = 2*M+1 are in samples
% we assume, M, N, T samples are set up
% example 1 - deterministic periodic - this wont converge because
% it leads to a delta function
T = 1; % this should exactly divide Twindow
Twindow = 200000;
M = 215; N = 2*M+1;
tpts = (0:(N-1))/N*Twindow;
% samples = cos(2*pi/T*tpts).2;
% example 2 - finite-BW coloured noise. How to generate finite BW
% coloured noise requires some justification - this has been already
% done in a separate document previously. Here we hack it.
NoiseBW = 1e3; % Hz
Tsamp = 1/NoiseBW;
Twindow = 1000; % sec
Nsamp = round(Twindow/Tsamp);
Tsamp = Twindow/Nsamp;
tsamps = (0:(Nsamp-1))/Nsamp*Twindow;
whitenoisesamps = randn(Nsamp,1);
M = 219; N = 2*M+1;
tpts = (0:(N-1))/N*Twindow;
samples = interp1(tsamps,whitenoisesamps,tpts,linear,0);

% Get the Fourier Coeffs


tmp = fft(samples)/N;
Xarray(1:M) = tmp(M+2:N);
Xarray((M+2):N) = tmp(2:(M+1));
Xarray(M+1) = tmp(1);
% scale by Twindow to get samples of the Fourier transform
XtrArray = Twindow*Xarray;
% compute PSD from it
Xpsd = abs(XtrArray) / Twindow;
% plot it
plot((-M:M)/Twindow, 10*log10(Xpsd + 1e-30), b.);
oof = sprintf(Frequency);
xlabel(oof);
ylabel(Power Spectrum (dB));

You might also like