%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%22marimo%3E%3D0.23.9%22%2C%20%22numpy%3E%3D2.0%22%2C%20%22matplotlib%3E%3D3.9%22%2C%20%22urllib3%3E%3D2.2%22%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Fourier%20series%0A%0A%20%20%20%20%3E%20Marcos%20Duarte%2C%20Renato%20Naville%20Watanabe%3Cbr%3E%0A%20%20%20%20%3E%20%5BLaboratory%20of%20Biomechanics%20and%20Motor%20Control%5D(https%3A%2F%2Fbmclab.pesquisa.ufabc.edu.br%2F)%3Cbr%3E%0A%20%20%20%20%3E%20Federal%20University%20of%20ABC%2C%20Brazil%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20How%20to%20use%20this%20guide%0A%0A%20%20%20%20We%20will%20use%20sinusoid%20orthogonality%20to%20compute%20coefficients%2C%20then%20compare%20square%20and%20triangular%20waves%20in%20real%20and%20complex%20forms.%20Keep%20the%20DC%20convention%20explicit%3A%20in%20most%20of%20this%20guide%20a%E2%82%80%20is%20the%20mean%2C%20while%20some%20textbooks%20use%20a%E2%82%80%2F2.%20Check%20harmonic%20amplitude%2C%20phase%20and%20mean-square%20power%20separately.%0A%0A%20%20%20%20Try%20the%20distributed%20**Exercises**%20before%20opening%20their%20collapsed%20worked%20solutions.%20Then%20use%20the%20**Checkpoint%20questions**%20and%20attempt%20the%20**Problems**%20independently%3B%20their%20panels%20contain%20final%20answers%20only.%20Predict%20the%20effect%20of%20a%20parameter%20change%20before%20running%20it%2C%20and%20check%20units%20and%20limiting%20cases.%0A%0A%20%20%20%20**Opening%20challenge.**%20Can%20a%20sum%20of%20smooth%20sinusoids%20reproduce%20a%20signal%20with%20an%20abrupt%20jump%3F%20Predict%20what%20changes%20as%20you%20add%20harmonics%20and%20what%20happens%20exactly%20at%20the%20jump.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Python%20setup%0A%0A%20%20%20%20The%20dependency%20declaration%20at%20the%20top%20of%20this%20file%20lists%20the%20packages%20used%20by%20this%20guide.%20Marimo%20runs%20cells%20in%20dependency%20order.%20Run%20the%20notebook%20in%20an%20editor%20to%20use%20its%20displays%20and%20controls%3B%20the%20computational%20examples%20below%20explain%20their%20inputs%20before%20using%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Sinusoids%0A%0A%20%20%20%20The%20sine%20and%20cosine%20functions%20(with%20the%20same%20amplitude%20and%20frequency)%20are%20different%20only%20by%20a%20constant%20phase%20%24(%5E%5Cpi%2F_2)%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ccos(t)%3D%5Csin(t%2B%5E%5Cpi%2F_2)%20%5Cquad%20%2C%20%5Cquad%20%5Csin(t)%3D%5Ccos(t-%5E%5Cpi%2F_2)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20This%20means%20for%20instance%20that%20we%20can%20use%20only%20sines%20to%20represent%20both%20sines%20and%20cosines.%20Because%20of%20that%20we%20can%20refer%20to%20both%20sine%20and%20cosine%20waves%20as%20sinusoids.%20The%20relation%20between%20the%20sine%20and%20cosine%20functions%20can%20be%20verified%20using%20the%20trigonometric%20identities%2C%20for%20example%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csin(%5Calpha%20%2B%20%5E%5Cpi%2F_2)%20%3D%20%5Csin%7B%5Calpha%7D%20%5Ccos%7B%5E%5Cpi%2F_2%7D%20%2B%20%5Ccos%7B%5Calpha%7D%20%5Csin%7B%5E%5Cpi%2F_2%7D%20%3D%20%5Ccos%7B%5Calpha%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ccos(%5Calpha%20-%20%5E%5Cpi%2F_2)%20%3D%20%5Ccos%7B%5Calpha%7D%20%5Ccos%7B%5E%5Cpi%2F_2%7D%20%2B%20%5Csin%7B%5Calpha%7D%20%5Csin%7B%5E%5Cpi%2F_2%7D%20%3D%20%5Csin%7B%5Calpha%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Sinusoids%20are%20periodic%20functions%2C%20that%20is%2C%20if%20their%20period%20is%20%24T%24%2C%20then%20%24x(t%2BT)%20%3D%20x(t)%24.%0A%0A%20%20%20%20A%20cosine%20is%20an%20even%20function%20and%20the%20sine%20an%20odd%20function%2C%20as%20illustrated%20next.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20In%20the%20XIX%20century%2C%20the%20French%20mathematician%20%5BJean-Baptiste%20Joseph%20Fourier%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FJoseph_Fourier)%20made%20the%20discovery%20that%20any%20(in%20fact%2C%20almost%20any)%20complicated%20but%20periodic%20function%20could%20be%20represented%20as%20a%20sum%20of%20sines%20and%20cosines%20with%20varying%20amplitudes%20and%20frequencies%2C%20which%20are%20known%20as%20the%20%5BFourier%20series%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FFourier_series)%2C%20in%20his%20honor.%3Cbr%3E%0A%20%20%20%20Based%20on%20the%20Fourier%20series%2C%20he%20also%20proposed%20a%20mathematical%20transformation%20to%20transform%20functions%20between%20time%20(or%20spatial)%20domain%20and%20frequency%20domain%2C%20known%20as%20the%20%5BFourier%20transform%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FFourier_transform).%3Cbr%3E%0A%20%20%20%20Nowadays%2C%20the%20Fourier%20transform%20is%20typically%20implemented%20employing%20an%20algorithm%20known%20as%20the%20%5BFast%20Fourier%20Transform%20(FFT)%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FFast_Fourier_transform).%0A%0A%20%20%20%20Let's%20look%20at%20some%20basic%20concepts%20about%20sinusoids%20and%20then%20we%20will%20look%20at%20the%20Fourier%20series.%3Cbr%3E%0A%20%20%20%20You%20should%20be%20familiar%20with%20%5Bbasic%20trigonometry%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Ftrigonometry_basics.py)%20and%20the%20%5Bbasic%20properties%20of%20signals%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fsignal_basic_properties.py)%20before%20proceeding.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20from%20notebook_resources%20import%20repo_file%0A%20%20%20%20except%20ModuleNotFoundError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20if%20_error.name%20!%3D%20%22notebook_resources%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%0A%20%20%20%20%20%20%20%20raise%20ModuleNotFoundError(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22This%20notebook%20needs%20notebook_resources.py.%20Download%20it%20from%3A%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22https%3A%2F%2Fraw.githubusercontent.com%2FBMClab%2FBMC%2Fmaster%2Ffunctions%2Fnotebook_resources.py%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Save%20it%20in%20the%20same%20folder%20as%20this%20notebook%2C%20then%20rerun%20this%20cell.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22notebook_resources%22%2C%0A%20%20%20%20%20%20%20%20)%20from%20None%0A%20%20%20%20return%20(repo_file%2C)%0A%0A%0A%40app.cell%0Adef%20_(repo_file)%3A%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20%23%20'%25matplotlib%20inline'%20command%20supported%20automatically%20in%20marimo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20sys%0A%20%20%20%20_functions%20%3D%20%5Brepo_file(f'functions%2F%7B_m%7D.py')%20for%20_m%20in%20('even_odd_plot'%2C%20'sinusoid_int_plot'%2C%20'square_wave_plot'%2C%20'triangular_wave_plot')%5D%0A%20%20%20%20sys.path.insert(0%2C%20str(_functions%5B0%5D.parent))%20%20%23%20add%20to%20pythonpath%0A%20%20%20%20return%20np%2C%20plt%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20even_odd_plot%20import%20even_odd_plot%0A%20%20%20%20ax%20%3D%20even_odd_plot()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Integral%20of%20sinusoids%0A%0A%20%20%20%20The%20integral%20of%20a%20sine%20or%20cosine%20function%20over%20one%20period%20(as%20matter%20of%20fact%2C%20over%20any%20integer%20multiple%20of%20the%20period)%20is%20zero%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cint_%7Bt_0%7D%5E%7Bt_0%2BT%7D%20%5Csin(k%20%5Cfrac%7B2%20%5Cpi%7D%7BT%7D%20t)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%3D%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cint_%7Bt_0%7D%5E%7Bt_0%2BT%7D%20%5Ccos(k%20%5Cfrac%7B2%20%5Cpi%7D%7BT%7D%20t)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%3D%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24k%24%20is%20an%20integer%20and%20%24%5Csin(k2%5Cpi%20t%2FT)%24%20or%20%24%5Ccos(k2%5Cpi%20t%2FT)%24%20are%20the%20harmonics%20with%20periodicities%20%24T%2Fk%24.%0A%0A%20%20%20%20This%20means%20that%20the%20total%20area%20over%20one%20period%20under%20the%20sine%20or%20cosine%20curve%20is%20zero%3B%20in%20other%20words%2C%20the%20average%20value%20of%20these%20functions%20is%20zero%20(the%20average%20value%20is%20the%20integral%20(area)%20over%20one%20period%20divided%20by%20the%20period)%2C%20as%20illustrated%20in%20the%20next%20figure.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20sinusoid_int_plot%20import%20sinusoid_int_plot%0A%20%20%20%20ax_1%20%3D%20sinusoid_int_plot()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Orthogonality%20of%20sinusoids%0A%0A%20%20%20%20Another%20important%20property%20of%20sinusoids%20is%20that%20they%20are%20orthogonal%20at%20different%20frequencies%20and%20cosines%20and%20sines%20are%20always%20orthogonal%20to%20each%20other.%20That%20is%2C%20if%20we%20multiply%20two%20sinusoids%20and%20integrate%20over%20one%20period%20the%20value%20is%20zero%20if%20the%20frequencies%20are%20different%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cint_%7Bt_0%7D%5E%7Bt_0%2BT%7D%20%5Csin(m%20%5Cfrac%7B2%20%5Cpi%7D%7BT%7D%20%20t)%5Csin(n%20%5Cfrac%7B2%20%5Cpi%7D%7BT%7D%20%20t)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%3D%20%5Cleft%5C%7B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bl%20l%7D%0A%20%20%20%20%20%20%20%200%20%26%20%5Cquad%20%5Ctext%7Bif%20%7D%20m%20%5Cneq%20n%20%5C%5C%0A%20%20%20%20%20%20%20%20T%2F2%20%26%20%5Cquad%20%5Ctext%7Bif%20%7D%20m%20%3D%20n%20%5Cneq%200%0A%20%20%20%20%5Cend%7Barray%7D%20%5Cright.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cint_%7Bt_0%7D%5E%7Bt_0%2BT%7D%20%5Ccos(m%20%5Cfrac%7B2%20%5Cpi%7D%7BT%7D%20%20t)%5Ccos(n%20%5Cfrac%7B2%20%5Cpi%7D%7BT%7D%20%20t)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%3D%20%5Cleft%5C%7B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bl%20l%7D%0A%20%20%20%20%20%20%20%200%20%26%20%5Cquad%20%5Ctext%7Bif%20%7D%20m%20%5Cneq%20n%20%5C%5C%0A%20%20%20%20%20%20%20%20T%2F2%20%26%20%5Cquad%20%5Ctext%7Bif%20%7D%20m%20%3D%20n%20%5Cneq%200%0A%20%20%20%20%5Cend%7Barray%7D%20%5Cright.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20And%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cint_%7Bt_0%7D%5E%7Bt_0%2BT%7D%20%5Csin(m%20%5Cfrac%7B2%20%5Cpi%7D%7BT%7D%20%20t)%5Ccos(n%20%5Cfrac%7B2%20%5Cpi%7D%7BT%7D%20%20t)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%3D%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bl%20l%7D%0A%20%20%20%20%20%20%20%200%2C%20%26%20%5Cquad%20%5Cquad%20%5Ctext%7Bfor%20all%20%7D%20n%2C%20m%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20term%20orthogonal%20here%20means%20independent%20and%20has%20a%20similar%20meaning%20as%20in%20linear%20algebra.%20It's%20not%20that%20cosines%20and%20sines%20are%20geometrically%20perpendicular%20to%20each%20other%20as%20are%20two%20orthogonal%20vectors%20in%203D%20space%2C%20but%20rather%20the%20fact%20that%20one%20vector%20(or%20sinusoid)%20cannot%20be%20expressed%20in%20terms%20of%20the%20other.%20For%20example%2C%20the%20following%20set%20forms%20an%20orthogonal%20set%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%201%2C%20%5Ccos%20x%2C%20%5Csin%20x%2C%20cos%202x%2C%20%5Csin%202x%2C%20%5Cdots%0A%20%20%20%20%24%24%0A%0A%20%20%20%20From%20linear%20algebra%2C%20we%20know%20that%20three%20orthogonal%20vectors%20in%203D%20space%20can%20form%20a%20basis%2C%20a%20set%20of%20linearly%20independent%20vectors%20that%2C%20in%20a%20linear%20combination%2C%20can%20represent%20every%20vector.%20In%20this%20sense%2C%20cosines%20and%20sines%20can%20also%20form%20a%20basis%20and%20they%20can%20be%20used%20in%20a%20linear%20combination%20to%20represent%20other%20mathematical%20functions.%20This%20is%20the%20essence%20of%20the%20Fourier%20series.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%201%20%E2%80%94%20Use%20orthogonality.**%20Over%20%240%5Cle%20t%5Cle2%5Cpi%24%2C%20evaluate%20%24%5Cint%5Csin%20t%5Csin2t%5C%2Cdt%24%20and%20%24%5Cint%5Csin%5E2t%5C%2Cdt%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%201%0A%0A%20%20%20%20The%20product-to-sum%20identity%20gives%20%24%5Csin%20t%5Csin2t%3D%5B%5Ccos%20t-%5Ccos3t%5D%2F2%24%2C%20whose%20integral%20over%20the%20full%20period%20is%20zero.%20The%20square%20identity%20gives%20%24%5Csin%5E2t%3D(1-%5Ccos2t)%2F2%24%2C%20whose%20integral%20is%20%24%5Cpi%24.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Euler's%20formula%0A%0A%20%20%20%20There%20is%20an%20interesting%20relationship%20between%20the%20sine%2C%20cosine%2C%20and%20exponential%20functions%20and%20complex%20numbers%2C%20known%20as%20the%20%5BEuler's%20formula%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FEuler%2527s_formula)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20e%5E%7Bi%5Ctheta%7D%20%3D%20%5Ccos%20%5Ctheta%20%2B%20i%20%5Csin%20%5Ctheta%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24e%24%20is%20the%20%3Ca%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FE_(mathematical_constant)%22%3ENapier's%20constant%20or%20Euler's%20number%3C%2Fa%3E%20(%24e%3D2.71828...%24%2C%20the%20limit%20of%20%24(1%20%2B%201%2Fn)%5En%24%20as%20%24n%24%20approaches%20infinity%2C%20with%20%24%5Cln(e)%3D1%24%3B%20the%20exponential%20function%20%24e%5Ex%24%20is%20the%20inverse%20of%20the%20natural%20logarithm)%20and%20%24i%24%20is%20the%20%5Bimaginary%20unit%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FImaginary_number)%20(%24%5Csqrt%7B-1%7D%3Di%24).%3Cbr%3E%0A%20%20%20%20Using%20the%20Euler's%20formula%2C%20we%20can%20express%20the%20cosine%20and%20sine%20functions%20as%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ccos%20%5Ctheta%20%3D%20%5Cfrac%7Be%5E%7Bi%5Ctheta%7D%20%2B%20e%5E%7B-i%5Ctheta%7D%7D%7B2%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csin%20%5Ctheta%20%3D%20%5Cfrac%7Be%5E%7Bi%5Ctheta%7D%20-%20e%5E%7B-i%5Ctheta%7D%7D%7B2i%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Of%20note%2C%20a%20special%20case%20of%20the%20Euler's%20formula%2C%20known%20as%20the%20%5BEuler's%20identity%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FEuler's_identity)%2C%20has%20been%20referred%20as%20one%20of%20the%20most%20beautiful%20equations%20%5B%5B1%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMathematical_beauty)%5D%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20e%5E%7Bi%5Cpi%7D%20%2B%201%20%3D%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20It%20may%20sound%20strange%20why%20one%20would%20use%20exponential%20functions%20and%20complex%20numbers%20instead%20of%20the%20'simpler'%20sine%20and%20cosine%20functions%2C%20but%20it%20turns%20out%20that%20calculating%20the%20Fourier%20series%20is%20simpler%20with%20the%20exponential%20form.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Fourier%20series%0A%0A%20%20%20%20Without%20further%20ado%2C%20a%20periodic%20function%20can%20be%20represented%20as%20a%20sum%20of%20sines%20and%20cosines%2C%20known%20as%20the%20%5BFourier%20series%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FFourier_series)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t)%20%5Csim%20%5Csum_%7Bk%3D0%7D%5E%5Cinfty%20a_k%20%5Ccos%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%20%2B%20b_k%20%5Csin%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24a_k%24%20and%20%24b_k%24%20are%20the%20Fourier%20coefficients.%0A%0A%20%20%20%20When%20%24k%3D0%24%2C%20%24%5Ccos(0t)%3D1%24%20and%20%24%5Csin(0t)%3D0%24%2C%20then%20the%20Fourier%20series%20can%20be%20written%20as%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t)%20%5Csim%20a_0%20%2B%20%5Csum_%7Bk%3D1%7D%5E%5Cinfty%20a_k%20%5Ccos%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%20%2B%20b_k%20%5Csin%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%0A%20%20%20%20%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Determination%20of%20the%20Fourier%20coefficients%0A%0A%20%20%20%20Let's%20deduce%20how%20to%20calculate%20the%20Fourier%20coefficients%20%24a_0%24%2C%20%24a_k%24%2C%20and%20%24b_k%24.%3Cbr%3E%0A%20%20%20%20The%20coefficient%20%24a_0%24%20can%20be%20computed%20if%20we%20integrate%20the%20Fourier%20series%20over%20one%20period%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cint_%7Bt_0%7D%5E%7Bt_0%2BT%7D%20x(t)%20%5Cmathrm%7Bd%7Dt%20%3D%20%5Cint_%7Bt_0%7D%5E%7Bt_0%2BT%7D%20%5Cleft%5B%20a_0%20%2B%20%5Csum_%7Bk%3D1%7D%5E%5Cinfty%20a_k%20%5Ccos%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%20%2B%20b_k%20%5Csin%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%20%5Cright%5D%20%5C%3A%5Cmathrm%7Bd%7Dt%0A%20%20%20%20%24%24%0A%0A%20%20%20%20On%20the%20right%20side%2C%20the%20first%20term%20is%20the%20integral%20of%20a%20constant%20and%20the%20integral%20of%20the%20summation%20is%20zero%20because%20it's%20the%20integral%20over%20one%20period%20of%20sines%20and%20cosines.%20Then%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20a_0%20%3D%20%5Cfrac%7B1%7D%7BT%7D%20%5Cint_%7Bt_0%7D%5E%7Bt_0%2BT%7D%20x(t)%20%5C%3A%5Cmathrm%7Bd%7Dt%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Which%20is%20the%20average%20value%20of%20the%20function%20%24x(t)%24%20over%20the%20period%20%24T%24.%0A%0A%20%20%20%20The%20coefficients%20%24a_n%24%20and%20%24b_n%24%20can%20be%20computed%20if%20we%20multiply%20the%20Fourier%20series%20respectively%20by%20%24cos(%5Ccdot)%24%20and%20%24sin(%5Ccdot)%24%20and%20integrate%20over%20one%20period%20(deduction%20not%20shown)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20a_k%20%3D%20%5Cfrac%7B2%7D%7BT%7D%20%5Cint_%7Bt_0%7D%5E%7Bt_0%2BT%7D%20x(t)%20%5Ccos%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20b_k%20%3D%20%5Cfrac%7B2%7D%7BT%7D%20%5Cint_%7Bt_0%7D%5E%7Bt_0%2BT%7D%20x(t)%20%5Csin%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Note%20that%20%24a_n%24%20and%20%24b_n%24%20have%20the%20integrals%20multiplied%20by%202%20while%20%24a_0%24%20does%20not%20have%20this%20multiplication.%20Because%20of%20that%2C%20to%20make%20the%20equation%20for%20%24a_0%24%20consistent%20with%20the%20equations%20for%20%24a_n%24%20and%20%24b_n%24%2C%20it's%20common%20to%20also%20multiply%20by%202%20the%20equation%20for%20%24a_0%24%20and%20then%20express%20the%20Fourier%20series%20as%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t)%20%5Csim%20%5Cfrac%7Ba_0%7D%7B2%7D%20%2B%20%5Csum_%7Bk%3D1%7D%5E%5Cinfty%20a_k%20%5Ccos%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%20%2B%20b_k%20%5Csin%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%0A%20%20%20%20%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%202%20%E2%80%94%20Read%20coefficients%20without%20integrating.**%20For%20%24x(t)%3D3%2B2%5Ccos(4%5Cpi%20t)-%5Csin(2%5Cpi%20t)%24%2C%20use%20period%201%20s%20to%20identify%20the%20mean%20and%20nonzero%20real%20coefficients.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%202%0A%0A%20%20%20%20The%20mean%20is%203%2C%20%24a_2%3D2%24%20and%20%24b_1%3D-1%24%3B%20all%20other%20non-DC%20coefficients%20vanish.%20The%20fundamental%20grid%20is%201%20Hz%2C%20although%20the%20cosine%20component%20is%20at%202%20Hz.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Convergence%20of%20the%20Fourier%20series%0A%0A%20%20%20%20The%20Fourier%20series%20is%20an%20expansion%20of%20a%20function%20and%20the%20following%20theorem%20states%20that%20the%20Fourier%20series%20actually%20converges%20to%20the%20function%3A%0A%0A%20%20%20%20%3E%20The%20Fourier%20series%20of%20a%20piecewise%20continuous%20function%20%24f(x)%24%20converges%20for%20all%20values%20of%20%24x%24%20where%20%24f(x)%24%20is%20continuous.%20At%20the%20values%20where%20%24f(x)%24%20is%20discontinuous%2C%20the%20Fourier%20series%20is%20equal%20to%20the%20average%20of%20the%20right%20and%20left%20limits%20of%20%24f(x)%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cfrac%7B1%7D%7B2%7D%5Cleft%5Bf(x%5E%2B)%20%2B%20f(x%5E-)%5Cright%5D%0A%20%20%20%20%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Example%3A%20Fourier%20series%20for%20a%20square%20wave%20function%0A%0A%20%20%20%20Consider%20the%20following%20periodic%20function%20(a%20square%20wave%20with%20period%20%24T%3D2%5C%3As%24)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t)%20%3D%20%5Cleft%5C%7B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bl%20l%7D%0A%20%20%20%20%20%20%20%20-1%20%26%20%5Cquad%20%5Ctext%7Bif%20%7D%20-1%20%5Cleq%20t%20%3C%200%5C%5C%0A%20%20%20%20%20%20%20%20%2B1%20%26%20%5Cquad%20%5Ctext%7Bif%20%7D%200%20%5Cleq%20t%20%3C%20%2B1%0A%20%20%20%20%5Cend%7Barray%7D%20%5Cright.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t%20%2B%202)%20%3D%20x(t)%2C%20%5Cquad%20%5Ctext%7Bfor%20all%20%7D%20t%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Note%20that%20this%20function%20is%20discontinuous%20at%20%24t%3D%5Cdots%2C-3%2C-2%2C-1%2C%5C%3B0%2C%5C%3B1%2C%5C%3B2%2C%5C%3B3%2C%5Cdots%24.%0A%0A%20%20%20%20Which%20has%20the%20following%20plot%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20square_wave_plot%20import%20square_wave_plot%0A%20%20%20%20ax_2%20%3D%20square_wave_plot()%0A%20%20%20%20return%20(ax_2%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20Fourier%20coefficients%20are%20given%20by%20(%24a_0%3D0%24%20because%20the%20average%20value%20of%20%24x(t)%24%20is%20zero)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20a_k%20%3D%20%5Cfrac%7B2%7D%7B2%7D%5Cleft%5B%5Cint_%7B-1%7D%5E0%20-%5Ccos%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7B2%7Dt%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%2B%20%5Cint_%7B0%7D%5E1%20%5Ccos%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7B2%7Dt%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20b_k%20%3D%20%5Cfrac%7B2%7D%7B2%7D%5Cleft%5B%5Cint_%7B-1%7D%5E0%20-%5Csin%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7B2%7Dt%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%2B%20%5Cint_%7B0%7D%5E1%20%5Csin%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7B2%7Dt%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Which%20results%20in%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20a_k%20%3D%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20b_k%20%3D%20%5Cleft%5C%7B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bc%20l%7D%0A%20%20%20%20%20%20%20%200%20%26%20%5Cquad%20%5Ctext%7Bif%20k%20is%20even%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20%5Cfrac%7B4%7D%7Bk%5Cpi%7D%20%26%20%5Cquad%20%5Ctext%7Bif%20k%20is%20odd%7D%0A%20%20%20%20%5Cend%7Barray%7D%20%5Cright.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Then%2C%20the%20Fourier%20series%20for%20%24x(t)%24%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t)%20%3D%20%5Cfrac%7B4%7D%7B%5Cpi%7D%20%5Cleft%5B%20%5Csin(%5Cpi%20t)%20%2B%20%5Cfrac%7B%5Csin(3%5Cpi%20t)%7D%7B3%7D%20%2B%20%5Cfrac%7B%5Csin(5%5Cpi%20t)%7D%7B5%7D%20%2B%20%5Cfrac%7B%5Csin(7%5Cpi%20t)%7D%7B7%7D%20%2B%20%5Cfrac%7B%5Csin(9%5Cpi%20t)%7D%7B9%7D%20%2B%20%5Cdots%20%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20fact%20that%20the%20Fourier%20series%20of%20the%20given%20square%20wave%20has%20only%20sines%20is%20expected%20because%20the%20given%20square%20wave%20is%20an%20odd%20function%2C%20%24x(t)%20%3D%20-x(-t)%24.%20Then%2C%20its%20Fourier%20series%20should%20have%20only%20odd%20functions%20(sines).%0A%0A%20%20%20%20Let's%20plot%20it%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ax_2%2C%20np)%3A%0A%20%20%20%20_t%20%3D%20np.linspace(-3%2C%203%2C%201000)%0A%20%20%20%20_x%20%3D%204%20%2F%20np.pi%20*%20(np.sin(1%20*%20np.pi%20*%20_t)%20%2F%201%20%2B%20np.sin(3%20*%20np.pi%20*%20_t)%20%2F%203%20%2B%20np.sin(5%20*%20np.pi%20*%20_t)%20%2F%205%20%2B%20np.sin(7%20*%20np.pi%20*%20_t)%20%2F%207%20%2B%20np.sin(9%20*%20np.pi%20*%20_t)%20%2F%209)%0A%20%20%20%20ax_2.plot(_t%2C%20_x%2C%20color%3D'r'%2C%20linewidth%3D2%2C%20label%3D'Fourier%20series')%0A%20%20%20%20ax_2.set_title('Square%20wave%20function%20and%20the%20first%205%20non-zero%20terms%20of%20its%20Fourier%20series'%2C%20fontsize%3D14)%0A%20%20%20%20ax_2%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20And%20here%20are%20plots%20for%20each%20of%20the%20first%20five%20non-zero%20components%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20plt)%3A%0A%20%20%20%20_t%20%3D%20np.linspace(-3%2C%203%2C%20100)%0A%20%20%20%20(_fig%2C%20_axs)%20%3D%20plt.subplots(5%2C%202%2C%20figsize%3D(9%2C%205)%2C%20sharex%3DTrue%2C%20sharey%3DTrue%2C%20squeeze%3DTrue)%0A%20%20%20%20_axs%20%3D%20_axs.flat%0A%20%20%20%20_x2%20%3D%20_t%20*%200%0A%20%20%20%20for%20(_k%2C%20ax_3)%20in%20enumerate(_axs)%3A%0A%20%20%20%20%20%20%20%20if%20~_k%20%25%202%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_xf%20%3D%204%20%2F%20np.pi%20*%20np.sin((_k%20%2B%201)%20*%20np.pi%20*%20_t)%20%2F%20(_k%20%2B%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_3.plot(_t%2C%20_xf%2C%20'r'%2C%20linewidth%3D2)%0A%20%20%20%20%20%20%20%20%20%20%20%20_x2%20%3D%20_x2%20%2B%20_xf%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_3.plot(_t%2C%20_x2%2C%20'r'%2C%20linewidth%3D2)%0A%20%20%20%20%20%20%20%20ax_3.xaxis.set_ticks_position('bottom')%0A%20%20%20%20%20%20%20%20ax_3.set_ylim(-2%2C%202)%0A%20%20%20%20%20%20%20%20ax_3.set_yticks(%5B-1.5%2C%200%2C%201.5%5D)%0A%20%20%20%20%20%20%20%20ax_3.grid()%0A%20%20%20%20ax_3.set_xlabel('Time%20(s)'%2C%20fontsize%3D16)%0A%20%20%20%20_axs%5B-2%5D.set_xlabel('Time%20(s)'%2C%20fontsize%3D16)%0A%20%20%20%20_axs%5B4%5D.set_ylabel('Amplitude'%2C%20fontsize%3D16)%0A%20%20%20%20_axs%5B0%5D.set_title('First%20five%20harmonics')%0A%20%20%20%20_axs%5B1%5D.set_title('Sum%20of%20the%20harmonics')%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20plt.subplots_adjust(hspace%3D0)%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20This%20animation%20resumes%20the%20steps%20we%20have%20done%20to%20compute%20the%20Fourier%20series%20of%20a%20square%20wave%20function%3A%0A%20%20%20%20%3Cdiv%20class%3D'center-align'%3E%3Cfigure%20aria-describedby%3D%22figure-caption-1%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F2%2F2b%2FFourier_series_and_transform.gif%22%20alt%3D%22Fourier%20series%22%20%2F%3E%3C%2Ffigure%3E%3C%2Fdiv%3E%0A%0A%20%20%20%20%3Cspan%20id%3D%22figure-caption-1%22%20class%3D%22figure-caption%22%20style%3D%22display%3Ablock%3Btext-align%3Acenter%3Bfont-style%3Aitalic%22%3EFigure.%20Fourier%20series%20of%20a%20square%20wave%20function%20(%3Ca%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FFourier_series%22%20target%3D%22_blank%22%3Efrom%20Wikipedia%3C%2Fa%3E).%3C%2Fspan%3E%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%203%20%E2%80%94%20Predict%20a%20discontinuity%20value.**%20A%20periodic%20square%20wave%20jumps%20from%20%E2%88%921%20to%20%2B1%20at%20t%3D0.%20To%20what%20value%20does%20its%20Fourier%20series%20converge%20there%2C%20and%20does%20increasing%20harmonic%20count%20eliminate%20all%20overshoot%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%203%0A%0A%20%20%20%20At%20the%20jump%20the%20series%20converges%20to%20the%20midpoint%2C%200.%20Increasing%20terms%20narrows%20the%20oscillatory%20region%2C%20but%20the%20relative%20Gibbs%20overshoot%20near%20a%20jump%20does%20not%20vanish.%20Away%20from%20the%20jump%20the%20approximation%20improves.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Example%3A%20Fourier%20series%20for%20a%20triangular%20wave%20function%0A%0A%20%20%20%20Consider%20the%20following%20periodic%20function%20(a%20triangular%20wave%20with%20period%20%24T%3D2%5Cpi%24)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t)%20%3D%20%5Cleft%5C%7B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bl%20l%7D%0A%20%20%20%20%20%20%20%201%2Bt%2F%5Cpi%20%26%20%5Cquad%20%5Ctext%7Bif%20%7D%20-%5Cpi%20%5Cleq%20t%20%3C%200%5C%5C%0A%20%20%20%20%20%20%20%201-t%2F%5Cpi%20%26%20%5Cquad%20%5Ctext%7Bif%20%7D%200%20%5Cleq%20t%20%3C%20%2B%5Cpi%0A%20%20%20%20%5Cend%7Barray%7D%20%5Cright.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t%20%2B%202%5Cpi)%20%3D%20x(t)%2C%20%5Cquad%20%5Ctext%7Bfor%20all%20%7D%20t%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Which%20has%20the%20following%20plot%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20triangular_wave_plot%20import%20triangular_wave_plot%0A%20%20%20%20ax_4%20%3D%20triangular_wave_plot()%0A%20%20%20%20return%20(ax_4%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20Fourier%20coefficients%20are%20given%20by%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20a_0%20%3D%20%5Cfrac%7B1%7D%7B2%5Cpi%7D%20%5Cleft%5B%20%5Cint_%7B-%5Cpi%7D%5E0%20%5Cleft(1%2Bt%2F%5Cpi%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%2B%20%5Cint_%7B0%7D%5E%7B%5Cpi%7D%20%5Cleft(1-t%2F%5Cpi%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20a_k%20%3D%20%5Cfrac%7B1%7D%7B%5Cpi%7D%20%5Cleft%5B%20%5Cint_%7B-%5Cpi%7D%5E0%20%5Cleft(1%2Bt%2F%5Cpi%5Cright)%5Ccos%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%2B%20%5Cint_%7B0%7D%5E%7B%5Cpi%7D%20%5Cleft(1-t%2F%5Cpi%5Cright)%5Ccos%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20b_k%20%3D%20%5Cfrac%7B1%7D%7B%5Cpi%7D%20%5Cleft%5B%20%5Cint_%7B-pi%7D%5E0%20%5Cleft(1%2Bt%2F%5Cpi%5Cright)%5Csin%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%20%5Cmathrm%7Bd%7Dt%20%2B%20%5Cint_%7B0%7D%5E%7B%5Cpi%7D%20%5Cleft(1-t%2F%5Cpi%5Cright)%5Csin%5Cleft(k%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Which%20results%20in%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20a_0%20%3D%201%2F2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20a_k%20%3D%20%5Cleft%5C%7B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bc%20l%7D%0A%20%20%20%20%20%20%20%20%5Cfrac%7B4%7D%7Bk%5E2%5Cpi%5E2%7D%20%26%20%5Cquad%20%5Ctext%7Bif%20k%20is%20odd%7D%20%5C%5C%0A%20%20%20%20%20%20%20%200%20%26%20%5Cquad%20%5Ctext%7Bif%20k%20is%20even%7D%0A%20%20%20%20%5Cend%7Barray%7D%20%5Cright.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20b_k%20%3D%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Then%2C%20the%20Fourier%20series%20for%20%24x(t)%24%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t)%20%3D%20%5Cfrac%7B1%7D%7B2%7D%20%2B%20%5Cfrac%7B4%7D%7B%5Cpi%5E2%7D%20%5Cleft%5B%20%5Ccos(t)%20%2B%20%5Cfrac%7B%5Ccos(3%20t)%7D%7B3%5E2%7D%20%2B%20%5Cfrac%7B%5Ccos(5%20t)%7D%7B5%5E2%7D%20%2B%20%5Cfrac%7B%5Ccos(7%20t)%7D%7B7%5E2%7D%20%2B%20%5Cfrac%7B%5Ccos(9%20t)%7D%7B9%5E2%7D%20%2B%20%5Cdots%20%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20given%20triangular%20wave%20is%20an%20even%20function%2C%20%24x(t)%3Dx(-t)%24%2C%20and%20then%20its%20Fourier%20series%20has%20only%20even%20functions%20(cosines).%0A%0A%20%20%20%20Let's%20plot%20it%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ax_4%2C%20np)%3A%0A%20%20%20%20_t%20%3D%20np.linspace(-3%20*%20np.pi%2C%203%20*%20np.pi%2C%201000)%0A%20%20%20%20_x%20%3D%201%20%2F%202%20%2B%204%20%2F%20np.pi%20**%202%20*%20(np.cos(_t)%20%2B%20np.cos(3%20*%20_t)%20%2F%209%20%2B%20np.cos(5%20*%20_t)%20%2F%2025%20%2B%20np.cos(7%20*%20_t)%20%2F%2049%20%2B%20np.cos(9%20*%20_t)%20%2F%2081)%0A%20%20%20%20ax_4.plot(_t%2C%20_x%2C%20color%3D'r'%2C%20linewidth%3D2%2C%20label%3D'Fourier%20series')%0A%20%20%20%20ax_4.set_title('Triangular%20wave%20function%20and%20the%20first%206%20non-zero%20terms%20of%20its%20Fourier%20series'%2C%20fontsize%3D14)%0A%20%20%20%20ax_4%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20And%20here%20are%20plots%20for%20each%20of%20the%20first%20six%20non-zero%20components%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20plt)%3A%0A%20%20%20%20_t%20%3D%20np.linspace(-3%20*%20np.pi%2C%203%20*%20np.pi%2C%20100)%0A%20%20%20%20(_fig%2C%20_axs)%20%3D%20plt.subplots(6%2C%202%2C%20figsize%3D(9%2C%205)%2C%20sharex%3DTrue%2C%20squeeze%3DTrue)%0A%20%20%20%20_axs%20%3D%20_axs.flat%0A%20%20%20%20for%20(_k%2C%20ax_5)%20in%20enumerate(_axs)%3A%0A%20%20%20%20%20%20%20%20if%20_k%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_x2%20%3D%200.5%20*%20_t%20%2F%20_t%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_5.plot(_t%2C%20_x2%2C%20'r'%2C%20linewidth%3D2)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_5.set_ylim(-0.65%2C%200.65)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_5.set_yticks(%5B-0.5%2C%200%2C%200.5%5D)%0A%20%20%20%20%20%20%20%20elif%20~_k%20%25%202%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_xf%20%3D%204%20%2F%20((_k%20-%201)%20*%20np.pi)%20**%202%20*%20np.cos((_k%20-%201)%20*%20_t)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_5.plot(_t%2C%20_xf%2C%20'r'%2C%20linewidth%3D2)%0A%20%20%20%20%20%20%20%20%20%20%20%20_x2%20%3D%20_x2%20%2B%20_xf%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_5.set_ylim(-0.65%2C%200.65)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_5.set_yticks(%5B-0.5%2C%200%2C%200.5%5D)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_5.plot(_t%2C%20_x2%2C%20'r'%2C%20linewidth%3D2)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_5.set_ylim(-0.15%2C%201.15)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_5.set_yticks(%5B0%2C%200.5%2C%201%5D)%0A%20%20%20%20%20%20%20%20ax_5.xaxis.set_ticks_position('bottom')%0A%20%20%20%20%20%20%20%20ax_5.set_xlim((-3%20*%20np.pi%2C%203%20*%20np.pi))%0A%20%20%20%20%20%20%20%20ax_5.grid()%0A%20%20%20%20ax_5.set_xticks(np.linspace(-3%20*%20np.pi%20-%200.1%2C%203%20*%20np.pi%20%2B%200.1%2C%207))%0A%20%20%20%20ax_5.set_xticklabels(%5B'%24-3%5C%5Cpi%24'%2C%20'%24-2%5C%5Cpi%24'%2C%20'%24-%5C%5Cpi%24'%2C%20'%240%24'%2C%20'%24%5C%5Cpi%24'%2C%20'%242%5C%5Cpi%24'%2C%20'%243%5C%5Cpi%24'%5D)%0A%20%20%20%20ax_5.set_xlabel('Time%20(s)'%2C%20fontsize%3D16)%0A%20%20%20%20_axs%5B-2%5D.set_xlabel('Time%20(s)'%2C%20fontsize%3D16)%0A%20%20%20%20_axs%5B4%5D.set_ylabel('Amplitude'%2C%20fontsize%3D16)%0A%20%20%20%20_axs%5B0%5D.set_title('Average%20value%20and%20first%20five%20harmonics')%0A%20%20%20%20_axs%5B1%5D.set_title('Sum%20of%20the%20harmonics')%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20plt.subplots_adjust(hspace%3D0)%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Amplitude%20and%20phase%20of%20the%20frequency%20components%20in%20the%20Fourier%20series%0A%0A%20%20%20%20The%20coefficients%20%24a_k%24%20and%20%24b_k%24%20multiply%20the%20cosine%20and%20sine%20terms%2C%20respectively.%20For%20each%20%24k%3E0%24%2C%20combine%20them%20into%20one%20sinusoid%3A%0A%0A%20%20%20%20%24%24a_k%5Ccos(%5Comega_k%20t)%2Bb_k%5Csin(%5Comega_k%20t)%0A%20%20%20%20%20%20%3DA_k%5Ccos(%5Comega_k%20t%2B%5Cphi_k)%0A%20%20%20%20%20%20%3DA_k%5Csin(%5Comega_k%20t%2B%5Cpsi_k)%2C%24%24%0A%0A%20%20%20%20where%20%24%5Comega_k%3D2%5Cpi%20k%2FT%24%2C%20%24A_k%3D%5Csqrt%7Ba_k%5E2%2Bb_k%5E2%7D%24%2C%0A%20%20%20%20%24%5Cphi_k%3D%5Coperatorname%7Batan2%7D(-b_k%2Ca_k)%24%20and%0A%20%20%20%20%24%5Cpsi_k%3D%5Coperatorname%7Batan2%7D(a_k%2Cb_k)%24.%20The%20two%20phases%20differ%20by%20%24%5Cpi%2F2%24%0A%20%20%20%20modulo%20%242%5Cpi%24.%20When%20%24A_k%3D0%24%2C%20its%20phase%20is%20undefined.%20Use%20%60atan2%60%20to%20retain%20the%20quadrant.%0A%0A%20%20%20%20The%20mean-square%20contribution%20of%20this%20real%20harmonic%20is%0A%20%20%20%20%24%24P_k%3D%5Cfrac%7BA_k%5E2%7D%7B2%7D%3D%5Cfrac%7Ba_k%5E2%2Bb_k%5E2%7D%7B2%7D%2C%24%24%0A%20%20%20%20while%20the%20DC%20contribution%20is%20%24a_0%5E2%24.%20Their%20sum%20is%20the%20mean%20square%20of%20the%20signal%0A%20%20%20%20(Parseval's%20identity).%20Calling%20it%20physical%20power%20requires%20an%20appropriate%20physical%0A%20%20%20%20model%2C%20such%20as%20voltage%20squared%20divided%20by%20resistance.%20A%20nonzero%20periodic%20signal%0A%20%20%20%20has%20finite%20mean-square%20power%20but%20infinite%20energy%20over%20all%20time.%0A%0A%20%20%20%20The%20plot%20of%20the%20amplitude%20(or%20power)%20and%20phase%20of%20each%20harmonic%20as%20a%20function%20of%20the%20corresponding%20frequency%20is%20called%20the%20amplitude%20(or%20power)%20spectrum%20and%20phase%20spectrum%2C%20respectively.%20These%20spectra%20are%20very%20useful%20to%20visualize%20the%20frequency%20content%20of%20a%20function.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%204%20%E2%80%94%20Choose%20the%20phase%20quadrant.**%20For%20%24a%3D-1%24%20and%20%24b%3D1%24%20in%20%24a%5Ccos%5Comega%20t%2Bb%5Csin%5Comega%20t%24%2C%20find%20A%20and%20%CF%86%20in%20%24A%5Ccos(%5Comega%20t%2B%5Cphi)%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%204%0A%0A%20%20%20%20%24A%3D%5Csqrt2%24%20and%20%24%5Cphi%3D%5Coperatorname%7Batan2%7D(-b%2Ca)%3D-3%5Cpi%2F4%24%20(equivalently%205%CF%80%2F4).%20A%20one-argument%20arctangent%20loses%20the%20quadrant%20and%20would%20reconstruct%20the%20wrong%20signal.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20complex%20form%20of%20the%20Fourier%20series%0A%0A%20%20%20%20Using%20the%20Euler's%20formula%2C%20the%20complex%20form%20of%20the%20Fourier%20series%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t)%20%5Csim%20%5Csum_%7Bk%3D-%5Cinfty%7D%5E%5Cinfty%20c_k%20%5Cexp%5Cleft(ik%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20It%20can%20be%20shown%20that%20the%20relation%20between%20%24c_k%24%2C%20%24a_k%24%2C%20and%20%24b_k%24%20coefficients%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20c_0%20%3D%20a_0%20%5Cquad%20%2C%20%5Cquad%20c_k%20%3D%20%5Cfrac%7Ba_k-i%20b_k%7D%7B2%7D%20%5Cquad%20%2C%20%5Cquad%20c_%7B-k%7D%20%3D%20%5Cfrac%7Ba_k%2Bi%20b_k%7D%7B2%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Here%20%24a_0%24%20is%20the%20mean%2C%20as%20in%20our%20original%20convention.%20If%20instead%20the%20real%20series%20is%20written%20with%20%24a_0%2F2%24%2C%20then%20%24c_0%3Da_0%2F2%24.%20The%20symbol%20%24c_k%24%20now%20denotes%20a%20complex%20coefficient%2C%20not%20the%20real%20harmonic%20amplitude%20above.%20For%20%24k%3E0%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20a_k%20%3D%202%5Ctext%7B%20Real%7D%5C%7Bc_k%5C%7D%20%20%5Cquad%20%20%5Ctext%7Band%7D%20%20%5Cquad%20%20b_k%20%3D%20-2%5Ctext%7B%20Imag%7D%5C%7Bc_k%5C%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20complex%20Fourier%20coefficients%2C%20%24c_k%24%2C%20can%20be%20determined%20similarly%20to%20%24a_k%24%20and%20%24b_k%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20c_k%20%3D%20%5Cfrac%7B1%7D%7BT%7D%20%5Cint_%7Bt_0%7D%5E%7Bt_0%2BT%7D%20x(t)%5Cexp%5Cleft(-ik%5Cfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%0A%20%20%20%20%24%24%0A%0A%20%20%20%20And%20the%20amplitudes%20and%20phases%20of%20the%20Fourier%20series%20in%20complex%20form%20are%20given%20by%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%7Cc_k%7C%20%5C%2C%20%3D%20%5C%2C%20%7Cc_%7B-k%7D%7C%20%5C%2C%20%3D%20%5C%2C%20%5Cfrac%7B1%7D%7B2%7D%5Csqrt%7Ba_k%5E2%20%2B%20b_k%5E2%7D%20%5Cquad%20%2C%20%5Cquad%20%5Cphi_k%20%5C%2C%20%3D%20%5C%2C%20%5Coperatorname%7Batan2%7D(-b_k%2Ca_k)%3D%5Coperatorname%7Batan2%7D(%5Ctext%7BImag%7D%5C%7Bc_k%5C%7D%2C%5Ctext%7BReal%7D%5C%7Bc_k%5C%7D)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20complex%20form%20is%20more%20often%20used%20to%20express%20the%20Fourier%20series%20and%20in%20the%20calculation%20of%20the%20amplitude%20(or%20power)%20and%20phase%20spectra.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Example%3A%20complex%20form%20of%20the%20Fourier%20series%20for%20a%20square%20wave%20function%0A%0A%20%20%20%20Let's%20calculate%20again%20the%20Fourier%20series%20of%20the%20square%20wave%20function%20but%20now%20using%20the%20complex%20form.%0A%0A%20%20%20%20The%20%20%24c_k%24%20coefficients%20are%20given%20by%20(%24c_0%3D0%24%20because%20the%20average%20value%20of%20%24x(t)%24%20is%20zero)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20c_k%20%3D%20%5Cfrac%7B1%7D%7B2%7D%5Cleft%5B%20%5Cint_%7B-1%7D%5E0%20-%5Cexp%5Cleft(-ik%5Cfrac%7B2%5Cpi%7D%7B2%7Dt%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%20%2B%20%5Cint_%7B0%7D%5E1%20%5Cexp%5Cleft(-ik%5Cfrac%7B2%5Cpi%7D%7B2%7Dt%5Cright)%20%5C%3A%5Cmathrm%7Bd%7Dt%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%3D%20%5Cfrac%7Bi%5C%3Aexp(-ik%5Cpi)-i%7D%7Bk%5Cpi%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%3D%20%5Cfrac%7Bi%7D%7Bk%5Cpi%7D%5Cleft(%5Ccos(k%5Cpi)%20-%201%5Cright)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Which%20results%20in%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20c_k%20%3D%20%5Cleft%5C%7B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bc%20l%7D%0A%20%20%20%20%20%20%20%200%20%26%20%5Cquad%20%5Ctext%7Bif%20k%20is%20even%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20-%5Cfrac%7B2i%7D%7Bk%5Cpi%7D%20%26%20%5Cquad%20%5Ctext%7Bif%20k%20is%20odd%7D%0A%20%20%20%20%5Cend%7Barray%7D%20%5Cright.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20If%20you%20compare%20with%20the%20previous%20calculation%20you%20will%20notice%20some%20differences.%3Cbr%3E%0A%20%20%20%20First%2C%20now%20the%20expression%20is%20multiplied%20by%202%20instead%20of%20by%204.%20But%20remember%20that%20now%20%24k%24%20goes%20from%20%24-%5Cinfty%24%20to%20%24%5Cinfty%24%2C%20i.e.%2C%20we%20will%20now%20add%20terms%20for%20%24k%3D%5Cpm1%2C%5Cpm2%2C%5Cpm3%2C%5Cdots%24%2C%20meaning%20that%20the%20expression%20above%20will%20be%20counted%20twice%20for%20each%20%24%7Ck%7C%24.%3Cbr%3E%0A%20%20%20%20Second%2C%20the%20%24c_k%24%20coefficients%20are%20purely%20imaginary%3B%20the%20imaginary%20part%20is%20negative%20when%20%24k%24%20is%20positive%20and%20positive%20when%20%24k%24%20is%20negative.%0A%0A%20%20%20%20So%2C%20the%20amplitude%20%24%5Ctext%7BA%7D_k%24%20and%20phase%20%24%5Cphi_k%24%20of%20each%20Fourier%20coefficient%20for%20%24k%20%3E%200%24%20are%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BA%7D_k%20%3D%20%5Cleft%5C%7B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bc%20l%7D%0A%20%20%20%20%20%20%20%200%20%26%20%5Cquad%20%5Ctext%7Bif%20k%20is%20even%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20%5Cfrac%7B2%7D%7Bk%5Cpi%7D%20%26%20%5Cquad%20%5Ctext%7Bif%20k%20is%20odd%7D%0A%20%20%20%20%5Cend%7Barray%7D%20%5Cright.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cphi_k%20%3D%20%5Cleft%5C%7B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bc%20l%7D%0A%20%20%20%20%20%20%20%200%20%26%20%5Cquad%20%5Ctext%7Bif%20k%20is%20even%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20-%5Cfrac%7B%5Cpi%7D%7B2%7D%20%26%20%5Cquad%20%5Ctext%7Bif%20k%20is%20odd%7D%0A%20%20%20%20%5Cend%7Barray%7D%20%5Cright.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20For%20%24k%3C0%24%2C%20amplitude%20is%20unchanged%3A%20%24%7Cc_%7B-k%7D%7C%3D%7Cc_k%7C%24.%20Phase%20changes%20sign%20because%20real%20signals%20have%20conjugate-symmetric%20coefficients.%20The%20phase%20of%20an%20even%20harmonic%20is%20undefined%20(its%20coefficient%20is%20zero)%3B%20the%20displayed%20zero%20is%20only%20a%20plotting%20convention.%0A%0A%20%20%20%20Let's%20plot%20the%20corresponding%20amplitude%20and%20phase%20spectra%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20plt)%3A%0A%20%20%20%20_k%20%3D%20np.arange(-9%2C%2010)%0A%20%20%20%20fk%20%3D%20_k%20%2F%202%0A%20%20%20%20_odd%20%3D%20_k%20%25%202%20!%3D%200%0A%20%20%20%20ampk%20%3D%20np.zeros(_k.size)%0A%20%20%20%20ampk%5B_odd%5D%20%3D%202%20%2F%20(np.abs(_k%5B_odd%5D)%20*%20np.pi)%0A%20%20%20%20phik%20%3D%20np.full(_k.size%2C%20np.nan)%0A%20%20%20%20phik%5B_odd%5D%20%3D%20-90%20*%20np.sign(_k%5B_odd%5D)%0A%20%20%20%20(_fig%2C%20ax_6)%20%3D%20plt.subplots(2%2C%201%2C%20figsize%3D(9%2C%205)%2C%20sharex%3DTrue)%0A%20%20%20%20ax_6%5B0%5D.stem(fk%2C%20ampk%2C%20markerfmt%3D'ro'%2C%20linefmt%3D'r-')%0A%20%20%20%20ax_6%5B0%5D.set_ylim(0%2C%200.7)%0A%20%20%20%20ax_6%5B0%5D.annotate('Frequency%20(Hz)'%2C%20xy%3D(3%2C%200.3)%2C%20xycoords%3D'data'%2C%20color%3D'k'%2C%20size%3D14%2C%20xytext%3D(0%2C%200)%2C%20textcoords%3D'offset%20points')%0A%20%20%20%20ax_6%5B0%5D.set_title('Amplitude'%2C%20fontsize%3D16)%0A%20%20%20%20ax_6%5B1%5D.stem(fk%5B_odd%5D%2C%20phik%5B_odd%5D%2C%20markerfmt%3D'ro'%2C%20linefmt%3D'r-')%0A%20%20%20%20ax_6%5B1%5D.set_title('Phase%20(%24%5Eo%24)'%2C%20fontsize%3D16)%0A%20%20%20%20ax_6%5B1%5D.set_xlim(-4.55%2C%204.55)%0A%20%20%20%20ax_6%5B1%5D.set_ylim(-120%2C%20120)%0A%20%20%20%20ax_6%5B1%5D.set_yticks(%5B-90%2C%200%2C%2090%5D)%0A%20%20%20%20ax_6%5B1%5D.annotate('Frequency%20(Hz)'%2C%20xy%3D(3%2C%2015)%2C%20xycoords%3D'data'%2C%20color%3D'k'%2C%20size%3D14%2C%20xytext%3D(0%2C%200)%2C%20textcoords%3D'offset%20points')%0A%20%20%20%20for%20axi%20in%20ax_6%3A%0A%20%20%20%20%20%20%20%20axi.spines%5B'left'%5D.set_position('zero')%0A%20%20%20%20%20%20%20%20axi.spines%5B'right'%5D.set_color('none')%0A%20%20%20%20%20%20%20%20axi.spines%5B'bottom'%5D.set_position('zero')%0A%20%20%20%20%20%20%20%20axi.spines%5B'top'%5D.set_color('none')%0A%20%20%20%20%20%20%20%20axi.spines%5B'right'%5D.set_color('none')%0A%20%20%20%20%20%20%20%20axi.xaxis.set_ticks_position('bottom')%0A%20%20%20%20%20%20%20%20axi.yaxis.set_ticks_position('left')%0A%20%20%20%20%20%20%20%20axi.tick_params(axis%3D'both'%2C%20direction%3D'inout'%2C%20which%3D'both'%2C%20length%3D5)%0A%20%20%20%20%20%20%20%20axi.locator_params(axis%3D'y'%2C%20nbins%3D4)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20plt.subplots_adjust(hspace%3D0.2)%0A%20%20%20%20_fig%0A%20%20%20%20return%20ampk%2C%20fk%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20For%20a%20real%20signal%2C%20a%20one-sided%20mean-square%20spectrum%20combines%20the%20equal%20contributions%20at%20positive%20and%20negative%20frequencies.%20Thus%20each%20nonzero%20positive-frequency%20value%20is%20%242%7Cc_k%7C%5E2%24%2C%20and%20the%20DC%20value%20is%20%24%7Cc_0%7C%5E2%24.%20Summing%20this%20spectrum%20estimates%20the%20total%20mean%20square%3B%20truncating%20the%20square-wave%20series%20omits%20its%20higher%20harmonics.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ampk%2C%20fk%2C%20plt)%3A%0A%20%20%20%20Pk%20%3D%20ampk%5Bint(ampk.size%20%2F%202)%3A%5D%20**%202%0A%20%20%20%20Pk%5B1%3A%5D%20*%3D%202%0A%20%20%20%20(_fig%2C%20ax_7)%20%3D%20plt.subplots(1%2C%201%2C%20figsize%3D(9%2C%203))%0A%20%20%20%20ax_7.stem(fk%5Bint(fk.size%20%2F%202)%3A%5D%2C%20Pk%2C%20markerfmt%3D'ro'%2C%20linefmt%3D'r-')%0A%20%20%20%20ax_7.plot(fk%5Bint(fk.size%20%2F%202)%3A%5D%2C%20Pk%2C%20'ro'%2C%20clip_on%3DFalse)%0A%20%20%20%20ax_7.set_ylabel('Mean-square%20contribution'%2C%20fontsize%3D16)%0A%20%20%20%20ax_7.set_xlabel('Frequency%20(Hz)'%2C%20fontsize%3D16)%0A%20%20%20%20ax_7.set_xlim(0%2C%204.55)%0A%20%20%20%20ax_7.grid()%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%205%20%E2%80%94%20Account%20for%20power.**%20A%20cosine%20has%20peak%20amplitude%202%20V%20and%20zero%20mean.%20Give%20its%20RMS%20voltage%2C%20mean-square%20value%20and%20complex%20coefficient%20magnitudes%20at%20its%20two%20frequencies.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%205%0A%0A%20%20%20%20RMS%20is%20%242%2F%5Csqrt2%3D%5Csqrt2%24%20V%20and%20mean%20square%20is%202%20V%C2%B2.%20Each%20of%20the%20two%20complex%20coefficients%20has%20magnitude%201%20V%3B%20their%20squared%20magnitudes%20sum%20to%202%20V%C2%B2.%20Peak%20amplitude%20squared%20alone%20would%20double%20the%20average%20power-like%20quantity.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Checkpoint%20questions%0A%0A%20%20%20%201.%20What%20sets%20the%20harmonic%20frequency%20spacing%3F%0A%20%20%20%202.%20Which%20coefficients%20vanish%20for%20an%20even%20real%20signal%3F%0A%20%20%20%203.%20Why%20is%20phase%20undefined%20when%20amplitude%20is%20zero%3F%0A%20%20%20%204.%20What%20is%20the%20distinction%20between%20mean-square%20signal%20power%20and%20energy%20over%20an%20infinite%20interval%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Checkpoint%20answers%0A%0A%20%20%20%201.%20The%20period%3A%20spacing%20is%201%2FT.%202.%20Sine%20coefficients.%203.%20Every%20phase%20produces%20the%20same%20zero%20contribution.%204.%20A%20nonzero%20periodic%20signal%20has%20finite%20mean-square%20power%20but%20infinite%20total%20energy%20over%20an%20infinite%20time%20interval.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Problems%0A%0A%20%20%20%201.%20Using%20a%20trigonometric%20identity%2C%20verify%20that%20%24%5Ccos(x)%3D%5Csin(x%2B%5Cpi%2F2)%24.%0A%20%20%20%202.%20Using%20the%20Euler's%20formula%2C%20deduce%20the%20expressions%20for%20sine%20and%20cosine.%0A%20%20%20%203.%20Using%20the%20complex%20exponential%20forms%2C%20show%20that%20their%20integrals%20over%20a%20complete%20period%20are%20zero%20for%20nonzero%20integer%20harmonics.%0A%20%20%20%204.%20Verify%20the%20orthogonality%20integrals%20over%20a%20complete%20period%20for%20positive%20integer%20harmonics.%0A%20%20%20%205.%20Deduce%20the%20Fourier%20series%20for%20the%20triangular%20wave%20function%20using%20the%20complex%20form.%0A%20%20%20%206.%20Deduce%20the%20Fourier%20series%20for%20the%20function%20%24x(t)%20%3D%20t%24%20in%20the%20interval%20%24%5B0%2C%201%5D%24.%0A%20%20%20%207.%20Deduce%20the%20Fourier%20series%20for%20the%20saw-tooth%20function%20%24x(t)%20%3D%202t%2FT%24%20in%20the%20interval%20%240%20%5Cleq%20t%20%3C%20T%2F2%24%20and%20%24x(t)%3D2(t-T)%2FT%24%20in%20the%20interval%20%24T%2F2%20%5Cleq%20t%20%3C%20T%24.%0A%20%20%20%208.%20Calculate%20the%20power%20and%20phase%20spectra%20for%20the%20functions%20%24x(t)%20%3D%202%5Csin(4%5Cpi%20t)%24%20and%20%24x(t)%20%3D%202%5Ccos(4%5Cpi%20t)%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Final%20answers%20to%20original%20Problems%0A%0A%20%20%20%201.%20%24%5Csin(x%2B%5Cpi%2F2)%3D%5Ccos%20x%24.%0A%0A%20%20%20%202.%20%24%5Ccos%20x%3D(e%5E%7Bix%7D%2Be%5E%7B-ix%7D)%2F2%24%3B%20%24%5Csin%20x%3D(e%5E%7Bix%7D-e%5E%7B-ix%7D)%2F(2i)%24.%0A%0A%20%20%20%203.%20Over%20any%20complete%20period%2C%20both%20integrals%20are%20zero%20for%20a%20nonzero%20integer%20harmonic.%20At%20harmonic%20zero%2C%20the%20cosine%20integral%20is%20T%20and%20the%20sine%20integral%20is%20zero.%0A%0A%20%20%20%204.%20For%20nonzero%20integers%20m%2Cn%20over%20a%20complete%20period%20T%3A%20sine%E2%80%93cosine%20integrals%20vanish%3B%20sine%E2%80%93sine%20and%20cosine%E2%80%93cosine%20integrals%20are%20zero%20for%20unequal%20positive%20harmonics%20and%20T%2F2%20for%20equal%20positive%20harmonics.%20The%20DC%20cosine%20norm%20is%20T.%0A%0A%20%20%20%205.%20For%20the%20guide%E2%80%99s%20triangular%20wave%20%241-%7Ct%7C%2F%5Cpi%24%20on%20%24%5B-%5Cpi%2C%5Cpi%5D%24%2C%20%24c_0%3D1%2F2%24%2C%20%24c_k%3D(1-(-1)%5Ek)%2F(%5Cpi%5E2k%5E2)%24%20for%20nonzero%20integer%20k.%0A%0A%20%20%20%206.%20The%201-periodic%20extension%20has%20%24x(t)%5Csim1%2F2-(1%2F%5Cpi)%5Csum_%7Bk%3D1%7D%5E%7B%5Cinfty%7D%5Csin(2%5Cpi%20kt)%2Fk%24.%20At%20integer%20t%20the%20series%20value%20is%201%2F2.%0A%0A%20%20%20%207.%20%24x(t)%5Csim(2%2F%5Cpi)%5Csum_%7Bk%3D1%7D%5E%7B%5Cinfty%7D(-1)%5E%7Bk%2B1%7D%5Csin(2%5Cpi%20kt%2FT)%2Fk%24.%20At%20the%20jump%20t%3DT%2F2%20modulo%20T%20the%20series%20value%20is%200.%0A%0A%20%20%20%208.%20Both%20have%20total%20mean-square%20power%202%20and%20a%20one-sided%20power%20line%20of%202%20at%202%20Hz.%20In%20the%20cosine-with-plus-phase%20convention%2C%20the%20sine%20has%20phase%20%E2%88%92%CF%80%2F2%20and%20the%20cosine%20has%20phase%200.%20Their%20complex%20two-sided%20coefficients%20each%20have%20magnitude%201%20at%20%C2%B12%20Hz%2C%20so%20each%20two-sided%20power%20line%20is%201%3B%20the%20sine%20phases%20are%20%E2%88%92%CF%80%2F2%20at%20%2B2%20Hz%20and%20%2B%CF%80%2F2%20at%20%E2%88%922%20Hz.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Go%20deeper%0A%0A%20%20%20%20Use%20%5BKineticsFundamentalConcepts%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fkinetics_fundamental_concepts.py)%20to%20connect%20this%20guide%20with%20the%20surrounding%20mechanics%20and%20modeling%20material.%20Return%20to%20the%20assumptions%20behind%20each%20equation%20when%20applying%20it%20to%20a%20new%20experiment.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Video%20lectures%20on%20the%20Internet%0A%0A%20%20%20%20%5BMIT%20OpenCourseWare%5D(https%3A%2F%2Focw.mit.edu%2Fcourses%2Fres-6-007-signals-and-systems-spring-2011%2Fvideo_galleries%2Fvideo-lectures%2F)%20%E2%80%94%20Signals%20and%20Systems%3A%20the%20Fourier-series%2C%20filtering%20and%20sampling%20lectures%20explain%20the%20frequency%20analysis%20and%20processing%20choices%20used%20here.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20References%0A%0A%20%20%20%20-%20James%20JF%20(2011)%20%5BA%20Student%E2%80%99s%20Guide%20to%20Fourier%20Transform%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3D_T99VW0ARfkC).%203rd%20edition.%20Cambridge.%0A%0A%20%20%20%20-%20Lyons%20RG%20(2010)%20%5BUnderstanding%20Digital%20Signal%20Processing%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3DUBU7Y2tpwWUC%26hl).%203rd%20edition.%20Prentice%20Hall.%0A%0A%20%20%20%20-%20Robertson%20G%2C%20Caldwell%20G%2C%20Hamill%20J%2C%20Kamen%20G%20(2013)%20%5BResearch%20Methods%20in%20Biomechanics%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3DgRn8AAAAQBAJ).%202nd%20Edition.%20Human%20Kinetics.%0A%0A%20%20%20%20-%20Tang%20KT%20(2007)%20%5BMathematical%20Methods%20for%20Engineers%20and%20Scientists%203%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3DgG-ybR3uIGsC).%20Springer.%0A%0A%20%20%20%20-%20Tolstov%20GP%20(1976)%20%5BFourier%20Series%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%2Fabout%2FFourier_Series.html%3Fid%3DXqqNDQeLfAkC).%20Courier%20Dover%20Publications.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
18416acdfc0e7bad8e103ac767b34f95