1.5: Fourier Series
- Page ID
- 96177
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)By the end of this lab, students will be able to:
- Use MATLAB to approximate Fourier series coefficients of a sampled signal.
- Use MATLAB to synthesize a signal from its Fourier series coefficients.
- Describe the difference in sound made by two musical instruments in terms of their Fourier series coefficients.
- Create your own synthesized instrument using Fourier series (bonus).
Overview
In this lab you will experience the power of Fourier series analysis and synthesis.
Pre-Lab Reading
Fourier Analysis
Begin by reviewing the Fourier analysis formula. Given a periodic signal \(x(t)\) with period \(T\), \(x(t)\) can be represented by \[x(t)=\sum_{k=-\infty}^\infty a_k e^{j k \omega_0 t} \nonumber \] where \(\omega_0=\frac{2\pi}T\) and \[a_k=\frac{1}{T}\int_{0}^T x(t) e^{-jk \omega_0 t} dt\qquad \mbox{ for all } k \nonumber \] Although an infinite number of harmonics may be required for a general signal, in most situations, a finite number of them provides a practically good approximation.
If the signal \(x(t)\) is not given as a mathematical function but we have a sampled recorded version of it, the integrals to compute the coefficients (\(a_k\)) cannot be evaluated precisely. Although there are more efficient methods to perform the Fourier analysis directly on the sampled signals (e.g., the Fast Fourier Transform (FFT)), we will use a simple method to approximately evaluate those integrals: the Riemann approximation. Assuming that the signal \(x(t)\) is reasonably smooth (Riemann integrable) and that the sampling time \(T_s\) is an integer fraction of the period \(T\), i.e., \(T = N T_S\) of samples in a period (this is not really necessary but simplifies our code), then: \[\label{eq1.eq} a_k=\frac{1}{T}\int_{0}^T x(t) e^{-jk \omega_0 t} dt\simeq \frac{T_s}{T}\sum_{n=0}^{T/T_s} x[T_s n]e^{-j k \omega_0 T_s n}. \] The approximation gets better and better as the number of samples goes to infinity (meaning \(T_s\) goes to zero). In other words, fixing the sampling time limits the quality of the approximation, especially for larger values of \(k\). This is because \(T_s\) becomes too large with respect to \(k\omega_0\), the frequency of the \(k\)th harmonic.
Find the Fourier series coefficients \(a_0\), \(a_1\), \(a_2\), and \(a_3\) for the square wave given below (assume the signal repeats with period \(T=0.04\)). Use the integral in [eq1.eq] and include your answer in your report. \[\begin{aligned} x(t) = \begin{cases} 1 & 0 \leq t < 0.02 \\ 0 & 0.02 \leq t < 0.04 \end{cases}\end{aligned} \nonumber \]
Fourier Synthesis
The synthesis formula allows one to generate periodic signals from the linear combination of harmonic complex exponentials. Here we assume that the number of non-zero coefficients \(a_k\) is finite. Then \[\tilde{x}(t)=\sum_{k}a_ke^{j k \omega_0 t} \nonumber \] In particular, \[\tilde{x}{}[T_s n]=\sum_{k}a_ke^{j k \omega_0 T_s n} \nonumber \]
Lab Exercises
Fourier Series Analysis Function
Write a Matlab function “findFS" which takes as inputs \(x_T\), a vector of samples of \(x(t)\) covering one period of \(x(t)\); \(k\), the index of the desired coefficient; the period \(T\); and the sampling time \(T_s\); and produces as output the approximate \(a_k\) coefficient according to (\ref{eq1.eq}). Note that you can easily take advantage of vectorization by considering the product of the row vector \(x_T\) and the column vector \(e^{-j k \omega_0 T_s n}\). A template for the function is provided.
Fourier Series Coefficients of a Square Wave
Let’s construct a test signal. Define in Matlab
x_T=[ones(1000,1);zeros(1000,1)];
This is one period of a square wave signal. Let the fundamental period be \(T=0.04\) sec.
- Calculate the sampling time \(T_s\) and explain your answer. (Hint: \(T_s=T/2000\))
- Use the function you wrote in the previous exercise to compute \(a_0\), \(a_1\), \(a_2\), and \(a_3\).
- Find \(a_{-1}\), \(a_{-2}\), and \(a_{-3}\). How do these relate to \(a_1\), \(a_2\), and \(a_3\)?
- Verify the coefficients you have computed approximate reasonably well the actual coefficients you obtained for the prelab.
Fourier Series Synthesis
Write a Matlab function “synthFS” which has the following inputs: a column vector \(C=[a_0; a_1; \ldots; a_m]\) of coefficients where \(m\) is the largest integer corresponding to a non-zero coefficient, the sampling time \(T_s\), and the fundamental period \(T\). The output should be \(\tilde{x}_T(T_s n)\), the vector of \(N = T/T_s\) (\(N\) an integer) samples corresponding to one period of \(\tilde{x}\). Note that your program should verify that \(T/T_s\) is an integer. You will also need to extend \(C\) to include the complex conjugate coefficients corresponding to the negative \(k\)s. Finally, you may want to generate an array F
whose columns are the vectors \(e^{j k \omega_0 T_s n}\). \(\tilde{x}_T\) is then computed as F*CC
, where CC
is a vector containing all the coefficients from \(-m\) to \(m\). A template for this function is provided.
Spectra of a Trumpet and Whistle
Load the data file trumpet_whistle.mat
, which can be found on Canvas. It contains vectors trumpet
and whistle
. Both signals are sampled at \(f_0=44100\) Hz, and they represent one period of synthetic trumpet and whistle tones generated by a toy electric keyboard.
- Compute the period of the two signals.
- For each signal, compute and report the first 9 harmonic coefficients, i.e., \(a_k\)s with \(k=-9,\ldots,9\) using the function “findFS” you have developed.
- For each signal, plot the magnitude and phase spectra of the frequency components from part b. Briefly comment on their main differences. You may use the Matlab function
stem
to plot the spectrum. - For each signal, synthesize an approximation by using the periods in part a, the coefficients in part b, the sampling time \(T_s=1/f_0\), and your function “synthFS.”
- For each signal, plot on the same plot the given signal and its synthesized approximation. (You may use
spectraplot.m
found on Canvas. Make sure the axis labels are accurate.) Comment on the quality of the approximation in both cases. - For each signal and its approximation, generate a new signal by repeating them for 1000 periods (e.g., using the built in function
repmat
). Use the functionsound
in Matlab and the sampling frequency \(f_0\) to hear the sound you have generated. Can you hear the difference between the originals and the approximations? Comment. (Note you may need to scale the signals to have magnitude 1, seehelp sound
.)
Note that the trumpet sound has a much richer spectrum and corresponds to a more complex sound. While the whistle sound essentially does not have any harmonic components above the ninth and does not have any even harmonic components, the trumpet sound has important harmonics above the ninth; this can be argued from the error in the approximation.
Bonus
Synthesize your own “instrument” by repeating steps d-f of the last subsection. Instead of using the \(a_k\) that you calculated for the trumpet and whistle, come up with your own coefficients any way you want. (You don’t have to limit yourself to nine coefficients.) Upload your sound and describe it in your report.
Report Checklist
Be sure the following are included in your report.