fourierTool - fmauger1/QMol-grid GitHub Wiki

fourierTool

Miscellaneous tools for (fast) Fourier transforms

Description

fourierTool is defined as an abstract class with member static methods providing various commonly-used functionalities when dealing with (fast) Fourier transforms.

Table of Contents

Description

Usage

Frequency grid for fast Fourier algorithm

Scaled Fourier and inverse-Fourier transforms

Examples

Frequency grid for fast Fourier algorithm

Scaled Fourier and inverse-Fourier transforms

Usage

Frequency grid for fast Fourier algorithm

fftGrid

Compute frequency grid associated with MATLAB fft function (see example).

F = fourierTool.fftGrid(T)
  • The input T is assumed to be a Cartesian-grid vector (all-increasing, equally spaced components).
  • The output F is the frequency-grid discretization of Fourier-transformed signals sampled over T and computed with the MATLAB function fft.
  • The output F is a row vector with the same number of elements and in the input T.

Optionally, specify the number of points to include in the frequency grid as a second argument

F = fourierTool.fftGrid(T,N)
  • This is useful when calculating the fast Fourier transform with a set transform length
  • fourierTool.fftGrid(T,[]), with an empty second argument, is equivalent to fourierTool.fftGrid(T)

Scaled Fourier and inverse-Fourier transforms

Calculate the Fourier and inverse-Fourier transforms with the proper amplitude scaling. The scaling is calculated for the angular-frequency transforms (symmetric and unitary). For instance, in one dimension, the Fourier $\mathcal{F}$ and inverse-Fourier ${\mathcal{F}}^{-1}$ transforms, are respectively given by

$$ \mathcal{F}\left\lbrack f\right\rbrack \left(\nu \right) = \frac{1}{\sqrt{2\pi }}\int _{-\infty }^{\infty } f(t)~{{\mathrm{e}}}^{-i\nu t}dt ~~ ~~ {\mathrm{and}} ~~ ~~ {\mathcal{F}}^{-1} \left\lbrack f\right\rbrack \left(t\right) = \frac{1}{\sqrt{2\pi }}\int _{-\infty }^{\infty } f(\nu ){{\mathrm{e}}}^{i\nu t}~d\nu .~~~~~~(1) $$

Warning: The scaling implicitly assumes that the input signal starts at time $t=0$ . If not, an additional phase shift should be added to the scaling. If one only cares about the amplitude of the Fourier transform -- e.g., when looking at the spectral intensity -- the phase-shift scaling can be ignored.

fft

Compute the scaled Fourier transform of Eq. (1)

Y = fourierTool.fft(X,dt)
  • dt specifies the sampling (time) step to be used for the scaling
  • The scaled Fourier transform is performed calling MATLAB's native fast-Fourier transform: Y = fft(X)

Specify the transform length

Y = fourierTool.fft(X,dt,n)
  • See MATLAB's native fast-Fourier-transform documentation: Y = fft(X,dt,n)

Specify the dimension along which to perform the Fourier transform

Y = fourierTool.fft(X,dt,n,dim)
  • See MATLAB's native fast-Fourier-transform documentation: Y = fft(X,dt,n,dim)

ifft

Compute the scaled inverse Fourier transform of Eq. (1)

Y = fourierTool.ifft(X,df)
  • df specifies the sampling frequency step to be used for the scaling
  • The scaled inverse Fourier transform is performed calling MATLAB's native fast-Fourier transform: Y = ifft(X)

Specify the transform length

Y = fourierTool.ifft(X,dt,n)
  • See MATLAB's native inverse-fast-Fourier-transform documentation: Y = ifft(X,dt,n)

Specify the dimension along which to perform the inverse Fourier transform

Y = fourierTool.ifft(X,dt,n,dim)
  • See MATLAB's native inverse-fast-Fourier-transform documentation: Y = ifft(X,dt,n,dim)

Examples

Frequency grid for fast Fourier algorithm

Use Fourier transforms to find the frequency components of a signal buried in noise.

% Signal
T = 0:1e-3:2;
S = 0.7*sin(2*pi*50*T) + sin(2*pi*120*T) + 2*randn(size(T));

% Frequency analysis
F = fourierTool.fftGrid(T);
Y = fft(S);

Y = abs(Y(F > 0)).^2;
F = F(F > 0);

% Plot results
figure
    subplot(2,1,1)
        plot(T*1e3,S)
        xlim(T([1 end])*1e3)
    subplot(2,1,2)
        plot(F/2/pi,Y)
        xlim(F([1 end])/2/pi)

Scaled Fourier and inverse-Fourier transforms

Compare the native and scaled Fourier transform

% Signal
T = linspace(-10,10,1e3);
s = 1/3;
S = exp(-T.^2/2/s^2);

% Fourier transform
F     = fourierTool.fftGrid(T);
Y_th  = s * exp(-F.^2*s^2/2);
Y_nat = fft(S);
Y_scl = fourierTool.fft(S,T(2)-T(1));

% Plot results
f     = fftshift(F);
y_th  = fftshift(Y_th);
y_nat = fftshift(Y_nat);
y_scl = fftshift(Y_scl);
figure, set(gca,'YScale','log'), hold on
    plot(f,abs(y_th).^2)
    plot(f,abs(y_nat).^2,':')
    plot(f,abs(y_scl).^2,'--')

Check that the scaled Fourier transform satisfies Plancherel theorem $\int _{-\infty }^{\infty } {\left|f(t)\right|}^2 ~dt=\int _{-\infty }^{\infty } {\left|\mathcal{F}[f](\nu )\right|}^2~d\nu$

sum(abs(S).^2)*(T(2)-T(1)) - sum(abs(Y_scl).^2)*(F(2)-F(1))
1.110223024625157e-16

Check the inverse Fourier transforms

% Inverse Fourier transforms
S_nat = ifft(Y_nat);
S_scl = fourierTool.ifft(Y_scl,f(2)-f(1));

% Plot results
figure, set(gca,'YScale','log'), hold on
    plot(T,abs(S).^2)
    plot(T,abs(S_nat).^2,':')
    plot(T,abs(S_scl).^2,'--')

Include the phase shift for the signal not starting at zero

% Fully scaled Fourier transform
P_scl = exp(-1i*T(1)*F);
p_scl = fftshift(P_scl);

% Plot result
figure
    plot(f,abs(y_th-y_scl.*p_scl).^2)