Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Math 157 Group 7 - Elain Ng (eln009) Presentation
Math 157: Intro to Mathematical Software
UC San Diego, Winter 2018
Elain Ng
The Discrete Fast Fourier Transform
The Fourier Transform evolved from Fourier series, which is a representation of a function f(t) with period 2l (f(t+2l)=f(t)) in terms of sines and cosines with the same period:
This equation can be written as:
where for all n with identical to and identical to such that . The can be obtained from f(t) with:
The two preceding equations can be written as:
where
respectively. The Fourier transform is obtained by taking the limit of the Fourier series as the period length 2l tends to infinity. In doing this, let and the let the successive differences in f be df. This provides the equations of the Fourier transform (see reference 1):
and
with
The integral yielding is the Fourier transform of f(t), and that yielding f(t) is the inverse Fourier tansform. The Fourier integral is utilized to analyze non-periodic functions of t from . Fourier transforms, or their discrete form, are useful for signal processing and solving certain ordinary and partial differential equations. This is true because of the very many simple properties of Fourer transforms (see reference 2 below).
Parseval's theorem:
is easy to derive using the integral representation of the delta function in terms of complex exponentials.
To implement an FFT in python, a discrete Fourier transform over sampled data must be used.
Let
where is the sampling interval and k = 0, 1, 2, ... , N-1 (N consecutive points).
The Nyquist critical frequency is:
The Sampling theorem: If a continuous function f(t) is sampled at a time interval and f(t) is bandwidth limited to frequencies < , then f(t) is completely determined by its samples given above. If f(t) is not bandwith limited, then all of the power components at frequencies outside of the range are aliased or falsely translated into that range. Discrete Fourier transforms (DFTs) are designed to be limited to within the range defined by the Nyquist critical frequency. In particular, the python algorithms in scipy assume a sampling interval of 1 Hz, giving Hz.
For N samples, the DFT esitmates at the following discrete frequencies: The DFT results for the extreme values of frequency are identical, and in scipy the output of the frequencies are actually -N/2,...,N/2-1, but not necessarily in that particular order.
The integral form of the Fourier transform can be approximated by a discrete sum as follows:
where and from above have been used. Let
be the discrete Fourier transform (DFT) of . The inverse discrete Fourier transform (IDFT) is:
and Parseval's theorem becomes:
The power spectral density of a tansformed signal is plotted using the input as a function of frequency.
References:
Fourier Analysis and Generalized Functions, Lighthill, 1970.
Numerical Recpes in C++, Press et. al., 2007.
Advanced Engineerin Mathematics, Kreyzig, 1999.
Note
The scipy.fftpack's fft and ifft are sufficient for the exercises below. Although all signals will be real in the exercises, it is better to become accustomed to using fft and ifft, since most literature discusses fft and ifft. Also, please utilize only powers of 2 for the N of the fft and ifft. With that, the best results will be obtained for the problems.
Example:
Observations
- When choosing a time interval for a signal obtained from a mathematical formula, the total time sampled should contain at least one full period of the lowest frequency specified by the formula. Always use an N value which is an integer power of 2 in order to avoid zero padding of the input signal.
- The ability of the DFT to match an input's signal accurately depends significantly on the sampling rate. Increasing the sampling rate always increases the Nyquist frequency, which narrows the nonzero or high amplitude portion of the PSD plot.
- The sampling frequency or number of samples per second must be determined to obtain the correct DFT frequencies. The python DFT default frequencies should be multiplied by the sampling frequency. In the example above, the default DFT frequencies have been corrected.
- For a real signal, the imaginary part of the DFT is zero.
- N=8. Note that the DFT for the first frequency is the same as that of the fifth, which is the negative of the Nyquist frequency. Also, the DFT for the same positive and negative frequency is identical.
- The PSD, when plotted, should use only positive frequencies. It also should include both 0 Hz and the Nyquist frequency. The contributions for positive frequencies other that 0 Hz or the Nyquist frequency should be doubled.
- The inverse DFT returns the real, initial signal exactly along with a zero imaginary part.
- It is possible to filter a signal using the DFT and then the IDFT. The unwanted frequencies may be removed from the real, initial signal by nulling the DFT contributions corresponding to the these frequencies, and then performing the inverse DFT with the modified DFT data. Scipy.fftpack will issue a warning if this is done, but the warning can be ignored since the final result provided is correct. (See reference 2 above about filtering with the DFT.)
Exercises
Please use scipy.fftpack's fft and ifft for these problems.
Problem 1:
Using ecg.txt, do the following: a) First plot the signal in ecg.txt. Next, demean the signal. b) Find the DFT of the signal and check to see if Parseval's theorem is satisfied. c) What is the Nyquist critical frequency? Find the power spectral density (PSD) of the signal and plot it. d) Find the frequency of the maximum of the PSD.
Problem 2:
Using the DFT of the signal in problem 1, filter the signal to remove some of the very low frequency components and the very high frequency components beyond the non-zero portion of the PSD. Replot the filtered ECG signal. You should at least see the removal of baseline wandering. Try to find roughly optimal low and high frequency cutoffs that preserve the basic shape of the ecg but remove unwanted characteristics.
Problem 3:
Let f(t) = sin(7t)+sin(17t) Sample the signal and plot the PSD. Interpret the result.