%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.9%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22marimo%22%2C%0A%23%20%20%20%20%20%22matplotlib%22%2C%0A%23%20%20%20%20%20%22numpy%22%2C%0A%23%20%20%20%20%20%22optcutfreq%22%2C%0A%23%20%20%20%20%20%22pandas%22%2C%0A%23%20%20%20%20%20%22pyversions%22%2C%0A%23%20%20%20%20%20%22scipy%22%2C%0A%23%20%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(width%3D%22medium%22)%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%20%5Boptcutfreq%5D(https%3A%2F%2Fpypi.org%2Fproject%2Foptcutfreq)%0A%0A%20%20%20%20A%20Python%20module%20for%20automatic%20search%20of%20optimal%20filter%20cutoff%20frequency%20based%20on%20residual%20analysis%0A%0A%20%20%20%20%5Bhttps%3A%2F%2Fpypi.org%2Fproject%2Foptcutfreq%5D(https%3A%2F%2Fpypi.org%2Fproject%2Foptcutfreq)%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%20Determination%20of%20the%20optimal%20cutoff%20frequency%20for%20a%20low-pass%20filter%0A%0A%20%20%20%20A%20common%20problem%20in%20signal%20processing%20is%20to%20automatically%20determine%20the%20optimal%20cutoff%20frequency%20that%20should%20be%20employed%20in%20a%20low-pass%20filter%20to%20attenuate%20as%20much%20as%20possible%20the%20noise%20without%20compromising%20the%20signal%20content%20of%20the%20data.%0A%0A%20%20%20%20Unfortunately%2C%20there%20is%20no%20definite%20solution%20for%20this%20problem%2C%20but%20there%20are%20some%20techniques%2C%20with%20different%20degrees%20of%20success%2C%20to%20try%20to%20determine%20the%20optimal%20cutoff%20frequency.%0A%0A%20%20%20%20David%20Winter%2C%20in%20his%20classic%20book%20*Biomechanics%20and%20motor%20control%20of%20human%20movement*%2C%20proposed%20a%20method%20to%20find%20the%20optimal%20cutoff%20frequency%20based%20on%20residual%20analysis%20of%20the%20difference%20between%20filtered%20and%20unfiltered%20signals%20over%20a%20range%20of%20cutoff%20frequencies.%20The%20optimal%20cutoff%20frequency%20is%20the%20one%20where%20the%20residual%20starts%20to%20change%20very%20little%20because%20it%20is%20considered%20that%20from%20this%20point%2C%20it's%20being%20filtered%20mostly%20noise%20and%20minimally%20signal%2C%20ideally.%20This%20concept%20is%20straightforward%20to%20implement.%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%20function%20%60optcutfreq.py%60%0A%0A%20%20%20%20The%20function%20%60optcutfreq.py%60%20(%5Bsource%20code%5D(https%3A%2F%2Fgithub.com%2Fdemotu%2Foptcutfreq%2Fblob%2Fmaster%2Foptcutfreq%2Foptcutfreq.py))%20of%20the%20Python%20module%20%60optcutfreq%60%20contains%20an%20implementation%20of%20this%20method%20and%20it%20is%20divided%20in%20three%20parts%20(after%20the%20help%20section)%3A%20first%2C%20the%20residuals%20over%20a%20range%20of%20cutoff%20frequencies%20are%20calculated%3B%20second%2C%20an%20algorithm%20tries%20to%20find%20the%20noisy%20region%20(with%20a%20supposed%20linear%20behavior%20in%20the%20frequency%20domain)%20of%20the%20residuals%20versus%20cutoff%20frequencies%20plot%20and%20finds%20the%20optimal%20cutoff%20frequency%3B%20and%20third%2C%20the%20results%20are%20plotted.%20The%20code%20is%20lengthy%20relatively%20to%20the%20simplicity%20of%20the%20idea%20because%20of%20the%20long%20help%20section%2C%20the%20implementation%20of%20the%20automatic%20search%20and%20a%20rich%20plot.%20Here%20is%20the%20function%20signature%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20fc_opt%20%3D%20optcutfreq(y%2C%20freq%3D1%2C%20fclim%3DNone%2C%20show%3DFalse%2C%20ax%3DNone)%0A%20%20%20%20%60%60%60%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%20Installation%0A%0A%20%20%20%20%60%60%60bash%0A%20%20%20%20pip%20install%20%22optcutfreq%5Bplot%5D%22%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Or%0A%0A%20%20%20%20%60%60%60bash%0A%20%20%20%20conda%20install%20-c%20duartexyz%20optcutfreq%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20To%20run%20this%20%5Bmarimo%5D(https%3A%2F%2Fmarimo.io)%20notebook%20in%20an%20isolated%20environment%20with%20all%20its%20dependencies%3A%0A%0A%20%20%20%20%60%60%60bash%0A%20%20%20%20uvx%20marimo%20edit%20--sandbox%20optcutfreq_notebook.py%0A%20%20%20%20%60%60%60%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%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20from%20scipy.signal%20import%20butter%2C%20filtfilt%0A%20%20%20%20import%20pyversions%0A%0A%20%20%20%20from%20optcutfreq%20import%20optcutfreq%0A%0A%20%20%20%20return%20butter%2C%20filtfilt%2C%20mo%2C%20np%2C%20optcutfreq%2C%20pd%2C%20plt%2C%20pyversions%0A%0A%0A%40app.cell%0Adef%20_(pyversions)%3A%0A%20%20%20%20_info%20%3D%20pyversions.versions()%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%20Test%20with%20benchmark%20data%0A%20%20%20%20Let's%20test%20this%20function%20with%20benchmark%20data.%0A%0A%20%20%20%20In%201977%2C%20Pezzack%2C%20Norman%20and%20Winter%20published%20a%20paper%20where%20they%20investigated%20the%20effects%20of%20differentiation%20and%20filtering%20processes%20on%20experimental%20data%20(the%20angle%20of%20a%20bar%20manipulated%20in%20space).%20Since%20then%2C%20these%20data%20have%20became%20a%20benchmark%20to%20test%20new%20algorithms.%20Let's%20work%20with%20these%20data%20(available%20at%20%5Bhttp%3A%2F%2Fisbweb.org%2Fdata%2Fpezzack%2Findex.html%5D(http%3A%2F%2Fisbweb.org%2Fdata%2Fpezzack%2Findex.html)).%20The%20data%20have%20the%20angular%20displacement%20measured%20by%20video%20and%20the%20angular%20acceleration%20directly%20measured%20by%20an%20accelerometer%2C%20which%20we%20will%20consider%20as%20the%20true%20acceleration.%0A%20%20%20%20Part%20of%20these%20data%20are%20showing%20next%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20np%2C%20pd)%3A%0A%20%20%20%20%23%20the%20data%20file%20is%20next%20to%20this%20notebook%3B%20in%20the%20browser%2C%20read%20it%20from%20GitHub%0A%20%20%20%20_url%20%3D%20'https%3A%2F%2Fraw.githubusercontent.com%2Fdemotu%2Foptcutfreq%2Fmaster%2Fdocs%2Fpezzack.txt'%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_path%20%3D%20mo.notebook_location()%20%2F%20'pezzack.txt'%0A%20%20%20%20%20%20%20%20_source%20%3D%20_path%20if%20_path.is_file()%20else%20_url%0A%20%20%20%20except%20(AttributeError%2C%20TypeError)%3A%20%20%23%20in%20the%20browser%2C%20the%20location%20is%20a%20URL%0A%20%20%20%20%20%20%20%20_source%20%3D%20_url%0A%20%20%20%20_data%20%3D%20pd.read_csv(_source%2C%20sep%3D'%5Ct'%2C%20header%3DNone%2C%20skiprows%3D6).to_numpy()%0A%20%20%20%20time%2C%20disp%2C%20disp2%2C%20aacc%20%3D%20_data.T%0A%20%20%20%20freq%20%3D%20np.mean(1%2Fnp.diff(time))%0A%20%20%20%20return%20aacc%2C%20disp%2C%20freq%2C%20time%0A%0A%0A%40app.cell%0Adef%20_(aacc%2C%20disp%2C%20plt%2C%20time)%3A%0A%20%20%20%20_fig%2C%20(_ax1%2C%20_ax2)%20%3D%20plt.subplots(1%2C%202%2C%20sharex%3DTrue%2C%20figsize%3D(11%2C%204))%0A%20%20%20%20_fig.suptitle(%22Pezzack's%20benchmark%20data%22%2C%20fontsize%3D20)%0A%20%20%20%20_ax1.plot(time%2C%20disp%2C%20'b')%0A%20%20%20%20_ax1.set_xlabel('Time%20%5Bs%5D')%0A%20%20%20%20_ax1.set_ylabel('Angular%20displacement%20%5Brad%5D')%0A%20%20%20%20_ax2.plot(time%2C%20aacc%2C%20'g')%0A%20%20%20%20_ax2.set_xlabel('Time%20%5Bs%5D')%0A%20%20%20%20_ax2.set_ylabel('Angular%20acceleration%20%5Brad%2Fs%24%5E2%24%5D')%0A%20%20%20%20_fig.subplots_adjust(wspace%3D0.3)%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%20And%20using%20the%20residual%20analysis%20code%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(disp%2C%20freq%2C%20optcutfreq)%3A%0A%20%20%20%20fc_opt%20%3D%20optcutfreq(disp%2C%20freq%3Dfreq%2C%20show%3DTrue)%0A%20%20%20%20return%20(fc_opt%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(fc_opt%2C%20mo)%3A%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20The%20optimal%20cutoff%20frequency%20found%20is%20%7Bfc_opt%3A.1f%7D%20Hz.%20Note%20that%20the%20filtering%20process%20is%20relevant%20only%20for%20the%20derivative%20of%20the%20data%3B%20we%20cannot%20distinguish%20the%20unfiltered%20and%20filtered%20displacements%20(see%20that%20the%20RMSE%20residual%20is%20very%20small).%0A%20%20%20%20Let's%20employ%20this%20filter%2C%20differentiate%20the%20data%20twice%20and%20compare%20with%20the%20true%20acceleration%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(aacc%2C%20butter%2C%20disp%2C%20fc_opt%2C%20filtfilt%2C%20freq%2C%20np%2C%20plt%2C%20time)%3A%0A%20%20%20%20%23%20Butterworth%20filter%0A%20%20%20%20%23%20Correct%20the%20cutoff%20frequency%20for%20the%20number%20of%20passes%20in%20the%20filter%0A%20%20%20%20C%20%3D%200.802%20%20%23%20for%20dual%20pass%3B%20C%20%3D%20(2**(1%2Fnpasses)%20-%201)**0.25%0A%20%20%20%20_b%2C%20_a%20%3D%20butter(2%2C%20(fc_opt%2FC)%2F(freq%2F2))%0A%20%20%20%20_dispf%20%3D%20filtfilt(_b%2C%20_a%2C%20disp)%0A%20%20%20%20_aaccBW%20%3D%20np.diff(_dispf%2C%202)*freq*freq%0A%20%20%20%20%23%20RMSE%3A%0A%20%20%20%20_rmseBW%20%3D%20np.sqrt(np.mean((_aaccBW%20-%20aacc%5B1%3A-1%5D)**2))%0A%20%20%20%20%23%20plot%20data%0A%20%20%20%20_fig%2C%20_ax1%20%3D%20plt.subplots(1%2C%201%2C%20figsize%3D(11%2C%204))%0A%20%20%20%20_fig.suptitle(%22Pezzack's%20benchmark%20data%22%2C%20fontsize%3D20)%0A%20%20%20%20_ax1.plot(time%5B1%3A-1%5D%2C%20aacc%5B1%3A-1%5D%2C%20'g'%2C%20label%3D'Analog%20acceleration%3A%20%20%20%20%20(True%20value)')%0A%20%20%20%20_ax1.plot(time%5B1%3A-1%5D%2C%20_aaccBW%2C%20'r'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20label%3D'Butterworth%20%25.3g%20Hz%3A%20%20%20%20%20RMSE%20%3D%20%250.2f'%20%25%20(fc_opt%2C%20_rmseBW))%0A%20%20%20%20_ax1.set_xlabel('Time%20%5Bs%5D')%0A%20%20%20%20_ax1.set_ylabel('Angular%20acceleration%20%5Brad%2Fs%24%5E2%24%5D')%0A%20%20%20%20_ax1.legend(frameon%3DFalse%2C%20fontsize%3D12%2C%20loc%3D'upper%20left')%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%20The%20performance%20seems%20satisfactory%20(see%20%5Bthis%20Jupyter%20notebook%5D(https%3A%2F%2Fnbviewer.jupyter.org%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2FDataFiltering.ipynb)%20for%20a%20comparison%20using%20other%20filters)%2C%20but%20it%20is%20known%20that%20this%20residual%20analysis%20algorithm%20results%20in%20oversmoothing%20the%20kinematic%20data%20(see%20%5Bhttp%3A%2F%2Fwww.clinicalgaitanalysis.com%2Ffaq%2Fcutoff.html%5D(http%3A%2F%2Fwww.clinicalgaitanalysis.com%2Ffaq%2Fcutoff.html)).%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%20Interactive%20example%0A%0A%20%20%20%20If%20the%20automatic%20search%20fails%20or%20you%20disagree%20with%20the%20noisy%20region%20of%20the%20residuals%20it%20found%2C%20choose%20the%20limit%20frequencies%20of%20the%20noisy%20part%20of%20the%20residuals%20curve%20(%60fclim%60)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(freq%2C%20mo)%3A%0A%20%20%20%20automatic%20%3D%20mo.ui.checkbox(value%3DFalse%2C%20label%3D'automatic%20search')%0A%20%20%20%20fclim%20%3D%20mo.ui.range_slider(0%2C%20round(0.8*freq%2F2%2C%201)%2C%20step%3D0.1%2C%20value%3D%5B8%2C%2018%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20label%3D'fclim%20%5BHz%5D'%2C%20show_value%3DTrue%2C%20full_width%3DTrue)%0A%20%20%20%20mo.vstack(%5Bautomatic%2C%20fclim%5D)%0A%20%20%20%20return%20automatic%2C%20fclim%0A%0A%0A%40app.cell%0Adef%20_(automatic%2C%20disp%2C%20fclim%2C%20freq%2C%20mo%2C%20optcutfreq)%3A%0A%20%20%20%20_fc%20%3D%20optcutfreq(disp%2C%20freq%3Dfreq%2C%20fclim%3DNone%20if%20automatic.value%20else%20fclim.value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20show%3DTrue)%0A%20%20%20%20mo.md('Optimal%20cutoff%20frequency%3A%20**%25s**'%20%25%20('%25.2f%20Hz'%20%25%20_fc%20if%20_fc%20else%20'not%20found'))%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%20To%20read%20more%20about%20the%20determination%20of%20the%20optimal%20cutoff%20frequency%2C%20see%20the%20following%20papers%3A%0A%0A%20%20%20%20-%20Pezzack%2C%20Norman%2C%20%26%20Winter%20(1977).%20%5BAn%20assessment%20of%20derivative%20determining%20techniques%20used%20for%20motion%20analysis%5D(https%3A%2F%2Fdoi.org%2F10.1016%2F0021-9290(77)90010-0).%20Journal%20of%20Biomechanics%2C%2010%2C%20377-382.%0A%20%20%20%20-%20Giakas%20%26%20Baltzopoulos%20(1997)%20%5BA%20comparison%20of%20automatic%20filtering%20techniques%20applied%20to%20biomechanical%20walking%20data%5D(https%3A%2F%2Fdoi.org%2F10.1016%2FS0021-9290(97)00042-0).%20J.%20Biomech.%2030%2C%20847-850.%0A%20%20%20%20-%20Alonso%2C%20Salgado%2C%20Cuadrado%20%26%20Pintado%20(2009)%20%5BAutomatic%20smoothing%20of%20raw%20kinematic%20signals%20using%20SSA%20and%20cluster%20analysis%5D(http%3A%2F%2Flim.ii.udc.es%2Fdocs%2Fproceedings%2F2009_09_EUROMECH_Automatic.pdf).%207th%20EUROMECH%20Solid%20Mechanics%20Conference.%0A%20%20%20%20-%20Kristianslund%2C%20Krosshaug%20%26%20van%20den%20Bogert%20(2012)%20%5BEffect%20of%20low%20pass%20filtering%20on%20joint%20moments%20from%20inverse%20dynamics%3A%20Implications%20for%20injury%20prevention%5D(https%3A%2F%2Fdoi.org%2F10.1016%2Fj.jbiomech.2011.12.011).%20J.%20Biomech.%2045%2C%20666-671.%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-%20Pezzack%20JC%2C%20Norman%20RW%2C%20%26%20Winter%20DA%20(1977).%20%5BAn%20assessment%20of%20derivative%20determining%20techniques%20used%20for%20motion%20analysis%5D(https%3A%2F%2Fdoi.org%2F10.1016%2F0021-9290(77)90010-0).%20Journal%20of%20Biomechanics%2C%2010%2C%20377-382.%20%5BPubMed%5D(https%3A%2F%2Fpubmed.ncbi.nlm.nih.gov%2F893476%2F).%0A%20%20%20%20-%20Winter%20DA%20(2009)%20%5BBiomechanics%20and%20motor%20control%20of%20human%20movement%5D(http%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3D_bFHL08IWfwC%26printsec%3Dfrontcover%26source%3Dgbs_ge_summary_r%26cad%3D0%23v%3Donepage%26q%26f%3Dfalse).%204%20ed.%20Hoboken%2C%20EUA%3A%20Wiley.%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
850f7e775337030e9ecfcf40e82f94b1