%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%22pandas%3E%3D2.2%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(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%20Basic%20properties%20of%20signals%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%20Every%20measurement%20in%20biomechanics%20ends%20up%20as%20a%20signal%3A%20a%20force%20plate%2C%20a%20marker%20on%20the%20heel%2C%20an%20electrode%20on%20a%20muscle.%20Before%20anything%20can%20be%20filtered%2C%20differentiated%20or%20interpreted%2C%20it%20helps%20to%20know%20the%20vocabulary%20used%20to%20describe%20a%20signal%2C%20and%20what%20a%20computer%20actually%20stores%20when%20it%20records%20one.%0A%0A%20%20%20%20This%20notebook%20introduces%20that%20vocabulary%3A%20amplitude%2C%20frequency%2C%20period%20and%20phase%3B%20harmonics%3B%20AC%20and%20DC%20components%3B%20even%20and%20odd%20functions%3B%20continuous%20and%20discrete%2C%20analog%20and%20digital%20signals%3B%20sampling%20and%20quantization%3B%20signal%20and%20noise.%20Along%20the%20way%20it%20keeps%20returning%20to%20one%20real%20recording%2C%20the%20electrical%20activity%20of%20a%20muscle%2C%20and%20by%20the%20end%20you%20will%20have%20estimated%20its%20sampling%20interval%20and%20amplitude%20step%2C%20and%20learned%20which%20hardware%20details%20the%20file%20cannot%20establish.%0A%0A%20%20%20%20Read%20it%20in%20order%20and%20run%20each%20cell%20as%20you%20reach%20it.%20Try%20the%20distributed%20Exercises%20before%20opening%20their%20worked%20solutions.%20At%20the%20end%2C%20the%20independent%20Problems%20have%20collapsed%20final%20answers%20without%20derivations.%20Where%20you%20find%20a%20**Challenge**%20or%20a%20set%20of%20**Guiding%20questions**%2C%20stop%20and%20answer%20on%20a%20scratchpad%20before%20moving%20on.%20Several%20of%20them%20ask%20you%20to%20predict%20a%20number%20*before*%20the%20code%20prints%20it%3B%20the%20prediction%20is%20the%20point%2C%20and%20being%20wrong%20is%20the%20most%20useful%20thing%20that%20can%20happen%20to%20you%20here.%0A%0A%20%20%20%20**Challenge%200.**%20Pick%20a%20sensor%20you%20have%20used%20or%20would%20like%20to%20use%3A%20a%20force%20plate%2C%20a%20motion-capture%20camera%2C%20an%20accelerometer%2C%20an%20EMG%20electrode.%20Write%20down%20two%20guesses.%20How%20many%20numbers%20per%20second%20does%20it%20store%3F%20And%20how%20many%20*different*%20values%20can%20each%20of%20those%20numbers%20take%3F%20Keep%20the%20guesses%3B%20the%20sections%20on%20sampling%20and%20quantization%20answer%20both%20questions%20for%20one%20real%20sensor.%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%20NumPy%20does%20the%20computation%2C%20pandas%20reads%20the%20data%20file%2C%20and%20Matplotlib%20draws%20the%20plots.%20These%20dependencies%20are%20declared%20in%20the%20notebook%20metadata.%20Open%20this%20file%20in%20marimo%3B%20the%20EMG%20example%20requires%20access%20to%20the%20linked%20repository%20data.%20No%20sensor%20or%20external%20recording%20software%20is%20needed.%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_()%3A%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20matplotlib%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%0A%20%20%20%20matplotlib.rc(%22axes%22%2C%20labelsize%3D13%2C%20titlesize%3D14)%0A%20%20%20%20matplotlib.rc(%22xtick%22%2C%20labelsize%3D11)%0A%20%20%20%20matplotlib.rc(%22ytick%22%2C%20labelsize%3D11)%0A%20%20%20%20matplotlib.rc(%22legend%22%2C%20fontsize%3D11)%0A%20%20%20%20return%20np%2C%20pd%2C%20plt%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%20A%20recorded%20signal%0A%0A%20%20%20%20Here%20is%20a%20fraction%20of%20a%20second%20of%20surface%20electromyography%20(EMG)%3A%20the%20electrical%20activity%20of%20a%20muscle%2C%20picked%20up%20by%20electrodes%20on%20the%20skin.%20The%20file%20is%20in%20this%20repository's%20%5Bdata%20folder%5D(https%3A%2F%2Fgithub.com%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fdata%2Femg.csv)%3B%20its%20first%20column%20is%20time%2C%20in%20seconds%2C%20and%20the%20second%20is%20the%20EMG%20amplitude.%20The%20bottom%20panel%20zooms%20in%20on%2025%20ms%20of%20it%20and%20shows%20every%20value%20in%20the%20file%20as%20a%20dot.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20pd%2C%20plt%2C%20repo_file)%3A%0A%20%20%20%20emg_data%20%3D%20pd.read_csv(%0A%20%20%20%20%20%20%20%20repo_file('data%2Femg.csv')%2C%20header%3DNone%0A%20%20%20%20).to_numpy()%0A%20%20%20%20emg_time%2C%20emg%20%3D%20emg_data%5B%3A%2C%200%5D%2C%20emg_data%5B%3A%2C%201%5D%0A%0A%20%20%20%20_fig%2C%20_axs%20%3D%20plt.subplots(2%2C%201%2C%20figsize%3D(10%2C%206))%0A%20%20%20%20_axs%5B0%5D.plot(emg_time%2C%20emg%2C%20color%3D%22tab%3Ablue%22%2C%20linewidth%3D1)%0A%20%20%20%20_axs%5B0%5D.axvspan(0.5%2C%200.525%2C%20color%3D%22tab%3Aorange%22%2C%20alpha%3D0.3)%0A%20%20%20%20_axs%5B0%5D.set_xlabel(%22Time%20%5Bs%5D%22)%0A%20%20%20%20_axs%5B0%5D.set_ylabel(%22EMG%22)%0A%20%20%20%20_axs%5B0%5D.set_title(%22Surface%20EMG%22)%0A%20%20%20%20_zoom%20%3D%20(emg_time%20%3E%3D%200.5)%20%26%20(emg_time%20%3C%3D%200.525)%0A%20%20%20%20_axs%5B1%5D.plot(emg_time%5B_zoom%5D%2C%20emg%5B_zoom%5D%2C%20%22.-%22%2C%20color%3D%22tab%3Aorange%22%2C%20drawstyle%3D%22steps-mid%22)%0A%20%20%20%20_axs%5B1%5D.set_xlabel(%22Time%20%5Bs%5D%22)%0A%20%20%20%20_axs%5B1%5D.set_ylabel(%22EMG%22)%0A%20%20%20%20_axs%5B1%5D.set_title(%22Zoom%20on%20the%20shaded%2025%20ms%22)%0A%20%20%20%20_axs%5B1%5D.grid(True%2C%20linestyle%3D%22%3A%22)%0A%20%20%20%20for%20_ax%20in%20_axs%3A%0A%20%20%20%20%20%20%20%20_ax.axhline(np.mean(emg)%2C%20color%3D%22k%22%2C%20linewidth%3D0.8)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20emg%2C%20emg_time%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**Guiding%20questions%200.**%0A%0A%20%20%20%201.%20Count%20the%20dots%20in%20the%20zoom.%20Roughly%20how%20many%20values%20per%20second%20did%20the%20recording%20system%20store%3F%0A%20%20%20%202.%20In%20the%20zoom%20the%20signal%20moves%20in%20steps%2C%20and%20some%20values%20repeat%20exactly.%20Do%20you%20think%20the%20muscle's%20electrical%20activity%20really%20changes%20in%20steps%3F%0A%20%20%20%203.%20The%20black%20line%20is%20the%20mean%20of%20the%20whole%20signal.%20Is%20it%20zero%3F%20Should%20it%20be%3F%0A%20%20%20%204.%20Which%20parts%20of%20this%20recording%20would%20you%20call%20*signal*%2C%20and%20which%20*noise*%3F%0A%0A%20%20%20%20Keep%20your%20answers.%20The%20rest%20of%20the%20notebook%20gives%20you%20the%20words%2C%20and%20the%20code%2C%20to%20answer%20each%20of%20them%20precisely.%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%20What%20a%20signal%20is%0A%0A%20%20%20%20A%20signal%20is%20a%20set%20of%20data%20that%20conveys%20information%20about%20some%20phenomenon%20(Bendat%20and%20Piersol%2C%202010%3B%20Lathi%2C%202009%3B%20Lyons%2C%202010).%20It%20can%20be%20represented%20mathematically%20by%20a%20function%20of%20one%20or%20more%20independent%20variables%2C%20and%20we%20often%20refer%20to%20a%20signal%20simply%20as%20data.%20The%20time-dependent%20voltage%20of%20an%20electric%20circuit%2C%20the%20acceleration%20of%20a%20moving%20body%20and%20the%20EMG%20above%20are%20all%20signals.%0A%0A%20%20%20%20What%20follows%20is%20a%20brief%20description%20of%20the%20basic%20properties%20of%20signals.%20For%20more%20detail%2C%20see%20Bendat%20and%20Piersol%20(2010)%2C%20Lathi%20(2009)%2C%20Lyons%20(2010)%20and%20Smith%20(1997).%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%20Amplitude%2C%20frequency%2C%20period%2C%20and%20phase%0A%0A%20%20%20%20A%20periodic%20function%20can%20be%20characterized%20by%20its%20amplitude%2C%20frequency%2C%20period%20and%20phase.%20Let's%20see%20these%20properties%20in%20a%20periodic%20function%20composed%20of%20a%20single%20frequency%2C%20the%20sine%20wave%20or%20sinusoid%20(%5Btrigonometric%20function%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FTrigonometric_functions))%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t)%20%3D%20A%5Csin(2%20%5Cpi%20f%20t%20%2B%20%5Cphi)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%20%24A%24%20is%20the%20amplitude%2C%20%24f%24%20the%20frequency%2C%20%24%5Cphi%24%20the%20phase%2C%20and%20%24T%3D1%2Ff%24%20the%20period%20of%20the%20function%20%24x(t)%24.%0A%0A%20%20%20%20We%20can%20define%20%24%5Comega%3D2%5Cpi%20f%20%3D%202%5Cpi%2FT%24%20as%20the%20angular%20frequency%2C%20and%20then%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t)%20%3D%20A%5Csin(%5Comega%20t%20%2B%20%5Cphi)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Let's%20visualize%20two%20such%20functions%3A%20%24x_1%20%3D%20%5Csin(2%5Cpi%20t)%24%2C%20with%20unit%20amplitude%2C%20a%20frequency%20of%201%20Hz%20and%20no%20phase%2C%20and%20%24x_2%20%3D%202%5Csin(%5Cpi%20t%20%2B%20%5Cpi%2F4)%24%2C%20with%20an%20amplitude%20of%202%2C%20a%20frequency%20of%200.5%20Hz%20and%20a%20phase%20of%20%24%5Cpi%2F4%24%20rad%20(%2445%5Eo%24).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20t%20%3D%20np.linspace(-2%2C%202%2C%20101)%20%20%23%20time%20vector%20%5Bs%5D%0A%20%20%20%20A%20%3D%202%20%20%23%20amplitude%0A%20%20%20%20freq%20%3D%200.5%20%20%23%20frequency%20%5BHz%5D%0A%20%20%20%20phase%20%3D%20np.pi%20%2F%204%20%20%23%20phase%20%5Brad%5D%20(45%20degrees)%0A%20%20%20%20x1%20%3D%201%20*%20np.sin(2%20*%20np.pi%20*%201%20*%20t%20%2B%200)%20%20%23%20sinusoid%201%0A%20%20%20%20x2%20%3D%20A%20*%20np.sin(2%20*%20np.pi%20*%20freq%20*%20t%20%2B%20phase)%20%20%23%20sinusoid%202%0A%20%20%20%20return%20t%2C%20x1%2C%20x2%0A%0A%0A%40app.function%0Adef%20wave_plot(t%2C%20x1%2C%20x2%2C%20x3%3DNone)%3A%0A%20%20%20%20%22%22%22Plot%20two%20sinusoids%20annotated%20with%20amplitude%2C%20period%20and%20phase.%0A%0A%20%20%20%20If%20%60x3%60%20is%20given%2C%20it%20is%20plotted%20too%2C%20as%20the%20sum%20of%20the%20other%20two.%0A%20%20%20%20%22%22%22%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20matplotlib.ticker%20import%20MultipleLocator%0A%0A%20%20%20%20_%2C%20ax%20%3D%20plt.subplots(1%2C%201%2C%20figsize%3D(12%2C%204.5))%0A%20%20%20%20ax.plot(t%2C%20x1%2C%20color%3D%5B0%2C%200.5%2C%200%2C%200.5%5D%2C%20linewidth%3D5)%0A%20%20%20%20ax.plot(t%2C%20x2%2C%20color%3D%5B0%2C%200%2C%201%2C%200.5%5D%2C%20linewidth%3D5)%0A%20%20%20%20ax.spines%5B%22bottom%22%5D.set_position(%22zero%22)%0A%20%20%20%20ax.spines%5B%22top%22%5D.set_color(%22none%22)%0A%20%20%20%20ax.spines%5B%22left%22%5D.set_position(%22zero%22)%0A%20%20%20%20ax.spines%5B%22right%22%5D.set_color(%22none%22)%0A%20%20%20%20ax.xaxis.set_ticks_position(%22bottom%22)%0A%20%20%20%20ax.yaxis.set_ticks_position(%22left%22)%0A%20%20%20%20ax.tick_params(axis%3D%22both%22%2C%20direction%3D%22inout%22%2C%20which%3D%22both%22%2C%20length%3D5)%0A%20%20%20%20ax.set_xlim((-2.05%2C%202.05))%0A%20%20%20%20ax.set_ylim((-2.1%2C%202.1))%0A%20%20%20%20ax.locator_params(axis%3D%22both%22%2C%20nbins%3D7)%0A%20%20%20%20ax.xaxis.set_minor_locator(MultipleLocator(0.25))%0A%20%20%20%20ax.yaxis.set_minor_locator(MultipleLocator(0.5))%0A%20%20%20%20ax.grid(which%3D%22both%22)%0A%20%20%20%20ax.set_title(r%22%24A%5Csin(2%5Cpi%20ft%2B%5Cphi)%24%22%2C%20loc%3D%22left%22%2C%20size%3D18%2C%20color%3D%5B0%2C%200%2C%200%5D)%0A%20%20%20%20ax.set_title(r%22%24x_1%3D%5Csin(2%5Cpi%20t)%24%22%2C%20loc%3D%22center%22%2C%20size%3D18%2C%20color%3D%5B0%2C%200.5%2C%200%5D)%0A%20%20%20%20ax.set_title(r%22%24x_2%3D2%5Csin(%5Cpi%20t%20%2B%20%5Cpi%2F4)%24%22%2C%20loc%3D%22right%22%2C%20size%3D18%2C%20color%3D%5B0%2C%200%2C%201%5D)%0A%20%20%20%20ax.annotate(%0A%20%20%20%20%20%20%20%20%22%22%2C%20xy%3D(0.25%2C%200)%2C%20xycoords%3D%22data%22%2C%20xytext%3D(0.25%2C%202)%2C%20size%3D16%2C%0A%20%20%20%20%20%20%20%20textcoords%3D%22data%22%2C%20arrowprops%3D%7B%22arrowstyle%22%3A%20%22%3C-%3E%22%2C%20%22fc%22%3A%20%22b%22%2C%20%22ec%22%3A%20%22b%22%7D%2C%0A%20%20%20%20)%0A%20%20%20%20ax.annotate(%0A%20%20%20%20%20%20%20%20r%22%24A%3D2%24%22%2C%20xy%3D(0.25%2C%201.1)%2C%20xycoords%3D%22data%22%2C%20xytext%3D(0%2C%200)%2C%0A%20%20%20%20%20%20%20%20textcoords%3D%22offset%20points%22%2C%20size%3D18%2C%20color%3D%22b%22%2C%0A%20%20%20%20)%0A%20%20%20%20ax.annotate(%0A%20%20%20%20%20%20%20%20%22%22%2C%20xy%3D(0%2C%201.6)%2C%20xycoords%3D%22data%22%2C%20xytext%3D(-0.25%2C%201.6)%2C%20size%3D16%2C%0A%20%20%20%20%20%20%20%20textcoords%3D%22data%22%2C%20arrowprops%3D%7B%22arrowstyle%22%3A%20%22%3C-%3E%22%2C%20%22fc%22%3A%20%22b%22%2C%20%22ec%22%3A%20%22b%22%7D%2C%0A%20%20%20%20)%0A%20%20%20%20ax.annotate(%0A%20%20%20%20%20%20%20%20r%22%24t_%7B%5Cphi%7D%3D%5Cphi%2F2%5Cpi%20f%5C%2C(%5Cphi%3D%5Cpi%2F4)%24%22%2C%20xy%3D(-1.25%2C%201.6)%2C%20xycoords%3D%22data%22%2C%0A%20%20%20%20%20%20%20%20xytext%3D(0%2C%20-5)%2C%20textcoords%3D%22offset%20points%22%2C%20size%3D18%2C%20color%3D%22b%22%2C%0A%20%20%20%20)%0A%20%20%20%20ax.annotate(%0A%20%20%20%20%20%20%20%20%22%22%2C%20xy%3D(-0.25%2C%200)%2C%20xycoords%3D%22data%22%2C%20xytext%3D(-0.25%2C%202)%2C%20textcoords%3D%22data%22%2C%0A%20%20%20%20%20%20%20%20arrowprops%3D%7B%22arrowstyle%22%3A%20%22-%22%2C%20%22linestyle%22%3A%20%22dotted%22%2C%20%22fc%22%3A%20%22b%22%2C%20%22ec%22%3A%20%22b%22%7D%2C%0A%20%20%20%20%20%20%20%20size%3D16%2C%0A%20%20%20%20)%0A%20%20%20%20ax.annotate(%0A%20%20%20%20%20%20%20%20%22%22%2C%20xy%3D(-0.75%2C%20-1.8)%2C%20xycoords%3D%22data%22%2C%20xytext%3D(1.25%2C%20-1.8)%2C%0A%20%20%20%20%20%20%20%20textcoords%3D%22data%22%2C%20size%3D10%2C%0A%20%20%20%20%20%20%20%20arrowprops%3D%7B%22arrowstyle%22%3A%20%22%7C-%7C%22%2C%20%22fc%22%3A%20%22b%22%2C%20%22ec%22%3A%20%22b%22%7D%2C%0A%20%20%20%20)%0A%20%20%20%20ax.annotate(%0A%20%20%20%20%20%20%20%20r%22%24T%3D1%2Ff%5C%2C(f%3D0.5%5C%2CHz)%24%22%2C%20xy%3D(-0.16%2C%20-1.8)%2C%20xycoords%3D%22data%22%2C%0A%20%20%20%20%20%20%20%20xytext%3D(0%2C%208)%2C%20textcoords%3D%22offset%20points%22%2C%20size%3D18%2C%20color%3D%22b%22%2C%0A%20%20%20%20)%0A%20%20%20%20ax.annotate(%0A%20%20%20%20%20%20%20%20r%22%24t%5Bs%5D%24%22%2C%20xy%3D(2.05%2C%20-0.5)%2C%20xycoords%3D%22data%22%2C%20xytext%3D(0%2C%200)%2C%0A%20%20%20%20%20%20%20%20textcoords%3D%22offset%20points%22%2C%20size%3D18%2C%20color%3D%22k%22%2C%0A%20%20%20%20)%0A%20%20%20%20if%20x3%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20ax.plot(t%2C%20x3%2C%20%22r%22%2C%20linewidth%3D6)%0A%20%20%20%20%20%20%20%20ax.annotate(%0A%20%20%20%20%20%20%20%20%20%20%20%20r%22%24x_3%20%3D%20x_1%20%2B%20x_2%24%22%2C%20xy%3D(1.2%2C%202.3)%2C%20xycoords%3D%22data%22%2C%20size%3D20%2C%20color%3D%22r%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20ax.set_ylim((-3.1%2C%203.1))%0A%20%20%20%20plt.suptitle(%0A%20%20%20%20%20%20%20%20r%22Amplitude%20(%24A%24)%2C%20frequency%20(%24f%24)%2C%20period%20(%24T%24)%2C%20phase%20(%24%5Cphi%24)%22%2C%0A%20%20%20%20%20%20%20%20fontsize%3D18%2C%20y%3D1.02%2C%0A%20%20%20%20)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell%0Adef%20_(t%2C%20x1%2C%20x2)%3A%0A%20%20%20%20wave_plot(t%2C%20x1%2C%20x2)%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.**%20A%20sensor%20gives%20%24x(t)%3D3%5Csin(4%5Cpi%20t%2B%5Cpi%2F2)%24.%20Find%20the%20amplitude%2C%20period%20and%20time%20lead.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%201%0A%20%20%20%20Compare%20with%20%24A%5Csin(2%5Cpi%20ft%2B%5Cphi)%24%3A%20%24A%3D3%24%2C%20%24f%3D2%24%20Hz%20and%20%24T%3D1%2Ff%3D0.5%24%20s.%20The%20phase%20is%20%24%5Cpi%2F2%24%2C%20so%20the%20lead%20is%20%24%5Cphi%2F(2%5Cpi%20f)%3D0.125%24%20s.%20A%20positive%20phase%20shifts%20the%20graph%20left.%20The%20earlier%20Guiding%20questions%20have%20answers%20%24T_1%3D1%24%20s%2C%20%24%5Cphi_2%3D-%5Cpi%2F4%24%2C%20and%20a%20sine%E2%80%93cosine%20phase%20difference%20of%20%24%5Cpi%2F2%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%20The%20phase%20shifts%20the%20curve%20in%20time.%20A%20phase%20%24%5Cphi%24%20corresponds%20to%20a%20time%20shift%20of%20%24t_%5Cphi%20%3D%20%5Cphi%2F(2%5Cpi%20f)%24%2C%20which%20for%20%24x_2%24%20is%20%24(%5Cpi%2F4)%2F%5Cpi%20%3D%200.25%24%20s%3A%20%24x_2%24%20crosses%20zero%20going%20up%20at%20%24t%3D-0.25%24%20s%20instead%20of%20at%20%24t%3D0%24.%20A%20positive%20phase%20moves%20the%20curve%20to%20the%20*left*%2C%20earlier%20in%20time.%20We%20say%20that%20%24x_2%24%20*leads*%20a%20sinusoid%20with%20no%20phase.%0A%0A%20%20%20%20**Guiding%20questions%201.**%0A%0A%20%20%20%201.%20Read%20the%20period%20of%20%24x_1%24%20off%20the%20plot.%20Does%20it%20agree%20with%20its%20frequency%3F%0A%20%20%20%202.%20What%20phase%20would%20make%20%24x_2%24%20cross%20zero%20going%20up%20at%20%24t%20%3D%20%2B0.25%24%20s%3F%0A%20%20%20%203.%20A%20sine%20and%20a%20cosine%20of%20the%20same%20frequency%20differ%20only%20in%20phase.%20By%20how%20much%3F%0A%0A%20%20%20%20%23%23%23%20The%20sum%20of%20two%20sinusoids%0A%0A%20%20%20%20Can%20you%20guess%20the%20shape%20of%20the%20sum%20of%20the%20two%20curves%20we%20just%20plotted%3F%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x_3%20%3D%20x_1%20%2B%20x_2%20%3D%20%5Csin(2%20%5Cpi%20t)%20%2B%202%5Csin(%5Cpi%20t%20%2B%20%5Cpi%2F4)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20**Before%20you%20run%20the%20next%20cell**%2C%20sketch%20%24x_3%24%20on%20your%20scratchpad%20and%20predict%20three%20numbers%3A%20its%20period%2C%20its%20largest%20value%2C%20and%20its%20smallest%20value.%20Are%20they%20%24%5Cpm(1%20%2B%202)%20%3D%20%5Cpm%203%24%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20t%2C%20x1%2C%20x2)%3A%0A%20%20%20%20x3%20%3D%20x1%20%2B%20x2%0A%20%20%20%20wave_plot(t%2C%20x1%2C%20x2%2C%20x3)%0A%20%20%20%20print(f%22Largest%20value%20of%20x3%3A%20%20%7Bnp.max(x3)%3A5.2f%7D%2C%20at%20t%20%3D%20%7Bt%5Bnp.argmax(x3)%5D%3A.2f%7D%20s%22)%0A%20%20%20%20print(f%22Smallest%20value%20of%20x3%3A%20%7Bnp.min(x3)%3A5.2f%7D%2C%20at%20t%20%3D%20%7Bt%5Bnp.argmin(x3)%5D%3A.2f%7D%20s%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%20The%20largest%20value%20is%203%2C%20but%20the%20smallest%20is%20only%20about%20%24-1.5%24.%20Both%20sinusoids%20peak%20at%20%24t%20%3D%200.25%24%20s%2C%20so%20there%20their%20amplitudes%20add%3B%20their%20troughs%20never%20coincide%2C%20so%20the%20sum%20never%20reaches%20%24-3%24.%20Whether%20amplitudes%20add%20depends%20on%20the%20phases.%20With%20%24%5Cphi%20%3D%200%24%20for%20%24x_2%24%2C%20the%20peaks%20no%20longer%20meet%20and%20the%20sum%20stays%20within%20about%20%24%5Cpm%202.6%24.%0A%0A%20%20%20%20The%20sum%20of%20two%20sinusoids%20of%20different%20frequencies%20is%20not%20a%20sinusoid%2C%20and%20it%20need%20not%20even%20be%20symmetric%20about%20zero.%20It%20repeats%2C%20though%2C%20and%20the%20next%20section%20is%20about%20how%20often.%0A%0A%20%20%20%20%23%23%23%20Magnitude%20and%20power%0A%0A%20%20%20%20Two%20terms%20related%20to%20amplitude%20are%20magnitude%20and%20power.%20The%20magnitude%20is%20the%20absolute%20value%20of%20the%20amplitude%2C%20that%20is%2C%20the%20amplitude%20without%20its%20sign.%20The%20power%20of%20a%20signal%20is%20proportional%20to%20its%20amplitude%20(or%20magnitude)%20squared.%20Doubling%20the%20amplitude%20of%20a%20signal%20quadruples%20its%20power.%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%20Periodic%20function%2C%20fundamental%20frequency%2C%20and%20harmonics%0A%0A%20%20%20%20A%20function%20is%20said%20to%20be%20periodic%20with%20period%20%24T%24%20if%20%24x(t%2BT)%20%3D%20x(t)%24%20for%20all%20values%20of%20%24t%24%2C%20that%20is%2C%20the%20function%20repeats%20itself%20after%20a%20period.%20Two%20important%20consequences%20of%20this%20definition%20are%20that%20the%20sum%20of%20periodic%20functions%20with%20a%20common%20period%2C%20and%20a%20constant%20times%20a%20periodic%20function%2C%20are%20also%20periodic%20functions.%20It%20also%20follows%20that%20a%20periodic%20function%20repeats%20itself%20after%20%242T%2C%5C%3A%203T%2C%20%5Cdots%24%0A%0A%20%20%20%20The%20shortest%20period%20after%20which%20the%20function%20repeats%20itself%20is%20said%20to%20be%20its%20fundamental%20period%2C%20and%20its%20inverse%20is%20the%20fundamental%20frequency%20of%20the%20periodic%20function.%20A%20%5Bharmonic%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FHarmonic)%20is%20a%20component%20frequency%20of%20the%20function%20that%20is%20an%20integer%20multiple%20of%20the%20fundamental%20frequency%2C%20that%20is%2C%20the%20harmonics%20have%20frequencies%20%24f%2C%5C%3A%202f%2C%5C%3A%203f%2C%20%5Cdots%24%20(with%20periods%20%24T%2C%5C%3A%20T%2F2%2C%5C%3A%20T%2F3%2C%20%5Cdots%24)%20and%20are%20referred%20to%20as%20the%20first%2C%20second%2C%20and%20third%20harmonics%2C%20and%20so%20on.%0A%0A%20%20%20%20**Challenge%201.**%20Go%20back%20to%20%24x_3%20%3D%20x_1%20%2B%20x_2%24.%0A%0A%20%20%20%201.%20What%20is%20its%20fundamental%20period%2C%20and%20its%20fundamental%20frequency%3F%20Check%20your%20earlier%20prediction%20against%20the%20plot.%0A%20%20%20%202.%20Which%20of%20%24x_1%24%20and%20%24x_2%24%20is%20the%20first%20harmonic%20of%20%24x_3%24%2C%20and%20which%20is%20the%20second%3F%0A%20%20%20%203.%20Replace%20the%20frequency%20of%20%24x_1%24%20by%20%24%5Csqrt%7B2%7D%24%20Hz.%20Is%20the%20sum%20still%20periodic%3F%20Why%20not%3F%0A%0A%20%20%20%20This%20idea%2C%20that%20a%20periodic%20signal%20can%20be%20built%20from%20a%20fundamental%20and%20its%20harmonics%2C%20is%20the%20starting%20point%20of%20the%20%5BFourier%20series%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Ffourier_series.py).%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.**%20Find%20the%20fundamental%20and%20harmonics%20of%20%24x_3%24%20in%20Challenge%201.%20What%20changes%20if%201%20Hz%20becomes%20%24%5Csqrt2%24%20Hz%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%202%0A%20%20%20%20The%200.5%20Hz%20and%201%20Hz%20components%20complete%20one%20and%20two%20cycles%20in%202%20s.%20Thus%20%24T_0%3D2%24%20s%2C%20%24f_0%3D0.5%24%20Hz%2C%20%24x_2%24%20is%20the%20first%20harmonic%20and%20%24x_1%24%20the%20second.%20With%20%24%5Csqrt2%24%20Hz%2C%20their%20frequency%20ratio%20is%20irrational%3A%20no%20finite%20time%20contains%20whole%20numbers%20of%20both%20cycles%2C%20so%20the%20sum%20is%20not%20periodic.%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%20AC%20and%20DC%20components%0A%0A%20%20%20%20A%20signal%20can%20also%20be%20characterized%20by%20its%20%5BAC%20and%20DC%20components%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDC_bias).%20The%20terms%20come%20from%20electronics%2C%20where%20they%20mean%20alternating%20current%20and%20direct%20current.%20The%20DC%20component%2C%20also%20called%20the%20DC%20offset%20or%20DC%20bias%2C%20is%20simply%20the%20average%20(mean)%20value%20of%20the%20function%2C%20its%20constant%20part.%20The%20AC%20component%20is%20the%20oscillatory%20part%20of%20the%20function%2C%20found%20by%20subtracting%20the%20average%20value%20(the%20DC%20component)%20from%20the%20function%20itself.%20The%20next%20figure%20illustrates%20these%20components.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20ac_dc_plot()%3A%0A%20%20%20%20%22%22%22Plot%20the%20AC%20and%20DC%20components%20of%20a%20signal%2C%20and%20their%20sum.%22%22%22%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20fig%2C%20ax%20%3D%20plt.subplots(1%2C%203%2C%20figsize%3D(10%2C%203.2))%0A%20%20%20%20t%20%3D%20np.linspace(0%2C%201%2C%20101)%0A%20%20%20%20ac%20%3D%20np.sin(2%20*%204%20*%20np.pi%20*%20t)%0A%20%20%20%20dc%20%3D%202%20*%20np.ones(t.shape)%0A%20%20%20%20ax%5B0%5D.plot(t%2C%20ac%2C%20%22b%22%2C%20linewidth%3D3%2C%20label%3D%22AC%22)%0A%20%20%20%20ax%5B0%5D.set_title(r%22%24AC%3A%5C%3B%5Csin(8%5Cpi%20t)%24%22%2C%20fontsize%3D16)%0A%20%20%20%20ax%5B1%5D.plot(t%2C%20dc%2C%20%22g%22%2C%20linewidth%3D3%2C%20label%3D%22DC%22)%0A%20%20%20%20ax%5B1%5D.set_title(r%22%24DC%3A%5C%3B%202%24%22%2C%20fontsize%3D16)%0A%20%20%20%20ax%5B2%5D.plot(t%2C%20ac%20%2B%20dc%2C%20%22r%22%2C%20linewidth%3D3%2C%20label%3D%22AC%2BDC%22)%0A%20%20%20%20ax%5B2%5D.plot(t%2C%20ac%2C%20%22b%3A%22%2C%20linewidth%3D2%2C%20label%3D%22AC%22)%0A%20%20%20%20ax%5B2%5D.plot(t%2C%20dc%2C%20%22g%3A%22%2C%20linewidth%3D2%2C%20label%3D%22DC%22)%0A%20%20%20%20ax%5B2%5D.set_title(r%22%24AC%2BDC%3A%5C%3B%5Csin(8%5Cpi%20t)%2B2%24%22%2C%20fontsize%3D16)%0A%20%20%20%20for%20axi%20in%20ax%3A%0A%20%20%20%20%20%20%20%20axi.set_ylim(-1.2%2C%203.2)%0A%20%20%20%20%20%20%20%20axi.margins(0.02)%0A%20%20%20%20%20%20%20%20axi.spines%5B%22bottom%22%5D.set_position(%22zero%22)%0A%20%20%20%20%20%20%20%20axi.spines%5B%22top%22%5D.set_color(%22none%22)%0A%20%20%20%20%20%20%20%20axi.spines%5B%22left%22%5D.set_position(%22zero%22)%0A%20%20%20%20%20%20%20%20axi.spines%5B%22right%22%5D.set_color(%22none%22)%0A%20%20%20%20%20%20%20%20axi.xaxis.set_ticks_position(%22bottom%22)%0A%20%20%20%20%20%20%20%20axi.yaxis.set_ticks_position(%22left%22)%0A%20%20%20%20%20%20%20%20axi.tick_params(axis%3D%22both%22%2C%20direction%3D%22inout%22%2C%20which%3D%22both%22%2C%20length%3D5)%0A%20%20%20%20%20%20%20%20axi.locator_params(axis%3D%22both%22%2C%20nbins%3D4)%0A%20%20%20%20fig.tight_layout()%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20ac_dc_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%20Note%20that%20for%20a%20periodic%20function%20like%20%24x(t)%3DA%5Ccos(2%5Cpi%20f%20t)%24%2C%20when%20its%20frequency%20is%20zero%2C%20%24x(t)%3DA%24.%20That%20is%2C%20the%20function%20has%20only%20a%20DC%20component.%20For%20that%20reason%20we%20say%20that%20the%20DC%20component%20of%20a%20function%20has%20a%20frequency%20equal%20to%20zero%20(an%20infinite%20period).%0A%0A%20%20%20%20Let's%20separate%20the%20two%20components%20of%20the%20right-hand%20signal%20numerically.%20Besides%20the%20DC%20component%2C%20we%20will%20compute%20the%20RMS%20(root%20mean%20square)%20of%20the%20AC%20component%2C%20the%20square%20root%20of%20its%20mean%20squared%20value%2C%20a%20common%20measure%20of%20the%20size%20of%20an%20oscillation%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20RMS%20%3D%20%5Csqrt%7B%5Cfrac%7B1%7D%7BN%7D%5Csum_%7Bi%3D1%7D%5E%7BN%7D%20x_i%5E2%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20**Before%20you%20run%20the%20next%20cell**%2C%20predict%20the%20DC%20component%20and%20the%20RMS%20of%20the%20AC%20component.%20The%20AC%20component%20has%20a%20peak%20of%201%3B%20is%20its%20RMS%20larger%20or%20smaller%20than%20that%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20_t%20%3D%20np.arange(0%2C%201%2C%200.01)%20%20%23%20four%20whole%20periods%20of%20the%20AC%20component%0A%20%20%20%20_x%20%3D%20np.sin(2%20*%204%20*%20np.pi%20*%20_t)%20%2B%202%0A%0A%20%20%20%20_dc%20%3D%20np.mean(_x)%0A%20%20%20%20_ac%20%3D%20_x%20-%20_dc%0A%20%20%20%20print(f%22DC%20component%3A%20%20%20%20%20%20%20%20%20%20%7B_dc%3A.3f%7D%22)%0A%20%20%20%20print(f%22Peak%20of%20the%20AC%20part%3A%20%20%20%7Bnp.max(np.abs(_ac))%3A.3f%7D%22)%0A%20%20%20%20print(f%22RMS%20of%20the%20AC%20part%3A%20%20%20%20%7Bnp.sqrt(np.mean(_ac**2))%3A.3f%7D%22)%0A%20%20%20%20print(f%22RMS%20of%20the%20whole%20x%3A%20%20%20%20%7Bnp.sqrt(np.mean(_x**2))%3A.3f%7D%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%20The%20RMS%20of%20a%20sinusoid%20is%20its%20amplitude%20divided%20by%20%24%5Csqrt%7B2%7D%24%2C%20about%200.707%20times%20the%20peak.%20Remember%20that%20number%3A%20it%20reappears%20in%20%5BData%20filtering%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fdata_filtering.py)%20as%20the%20gain%20of%20a%20filter%20at%20its%20cutoff%20frequency.%20Note%20also%20that%20the%20RMS%20of%20the%20whole%20signal%2C%202.12%2C%20is%20dominated%20by%20the%20DC%20component%3B%20to%20describe%20the%20oscillation%20alone%2C%20remove%20the%20mean%20first.%0A%0A%20%20%20%20Now%20back%20to%20the%20EMG.%20**Before%20you%20run%20the%20next%20cell**%2C%20look%20at%20the%20black%20line%20in%20the%20first%20figure%20again%2C%20and%20predict%20whether%20the%20DC%20component%20of%20the%20EMG%20is%20large%20or%20small%20compared%20with%20the%20RMS%20of%20its%20AC%20part.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(emg%2C%20np)%3A%0A%20%20%20%20_dc%20%3D%20np.mean(emg)%0A%20%20%20%20print(f%22EMG%20DC%20component%3A%20%20%20%20%20%20%20%7B_dc%3A.5f%7D%22)%0A%20%20%20%20print(f%22EMG%20RMS%20of%20the%20AC%20part%3A%20%7Bnp.sqrt(np.mean((emg%20-%20_dc)%20**%202))%3A.5f%7D%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%20The%20DC%20component%20of%20this%20EMG%20is%20about%20one%20percent%20of%20its%20RMS%2C%20small%20but%20not%20zero.%20The%20electrical%20activity%20of%20a%20muscle%2C%20picked%20up%20between%20two%20electrodes%2C%20has%20no%20physiological%20reason%20to%20have%20a%20mean%20different%20from%20zero%3B%20a%20DC%20offset%20in%20an%20EMG%20comes%20from%20the%20electrodes%20and%20the%20amplifier.%20That%20is%20why%20the%20first%20step%20in%20processing%20EMG%20is%20usually%20to%20subtract%20the%20mean%2C%20as%20%5BData%20filtering%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fdata_filtering.py)%20does%20before%20computing%20a%20moving%20RMS.%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.**%20For%20%24x(t)%3D2%2B%5Csin(8%5Cpi%20t)%24%2C%20find%20the%20DC%2C%20AC%20power%20and%20total%20RMS%20over%20whole%20cycles.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%203%0A%20%20%20%20The%20sine%20averages%20to%20zero%2C%20leaving%20DC%20%242%24.%20Its%20mean%20square%20is%20%241%2F2%24.%20Expanding%20%24x%5E2%24%20gives%20a%20cross%20term%20whose%20average%20is%20zero%2C%20hence%20total%20power%20%244%2B1%2F2%3D4.5%24%20and%20RMS%20%24%5Csqrt%7B4.5%7D%3D2.121%24.%20Removing%20the%20mean%20changes%20RMS%20to%20%241%2F%5Csqrt2%3D0.707%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%20Even%20and%20odd%20functions%0A%0A%20%20%20%20%5BEven%20and%20odd%20functions%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FEven_and_odd_functions)%20are%20functions%20that%20satisfy%20symmetry%20relations%20with%20respect%20to%20taking%20additive%20inverses.%20The%20%5Badditive%20inverse%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAdditive_inverse)%20of%20a%20number%20%24x%24%20is%20the%20number%20that%2C%20added%20to%20%24x%24%2C%20yields%20zero.%0A%0A%20%20%20%20The%20function%20%24x(t)%24%20is%20even%20if%20%24x(t)%20%3D%20x(-t)%24%2C%20and%20odd%20if%20%24x(t)%20%3D%20-x(-t)%24.%0A%0A%20%20%20%20The%20cosine%20function%20is%20even%2C%20for%20instance%2C%20%24%5Ccos(%5Cpi)%3D%5Ccos(-%5Cpi)%24%2C%20and%20the%20sine%20function%20is%20odd%2C%20for%20instance%2C%20%24%5Csin(%5Cpi%2F2)%3D-%5Csin(-%5Cpi%2F2)%24%3B%20see%20the%20next%20figure.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20even_odd_plot()%3A%0A%20%20%20%20%22%22%22Plot%20an%20even%20function%20(cosine)%20and%20an%20odd%20function%20(sine).%22%22%22%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20fig%2C%20ax%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(10%2C%203.2))%0A%20%20%20%20t%20%3D%20np.linspace(-np.pi%2C%20np.pi%2C%20101)%0A%20%20%20%20ax%5B0%5D.plot(t%2C%20np.cos(t)%2C%20%22b%22%2C%20linewidth%3D3%2C%20label%3Dr%22%24%5Ccos(t)%24%22)%0A%20%20%20%20ax%5B0%5D.plot(%5Bnp.pi%2C%20np.pi%5D%2C%20%5B0%2C%20-1%5D%2C%20%22r%3A%22%2C%20linewidth%3D3)%0A%20%20%20%20ax%5B0%5D.plot(%5B-np.pi%2C%20-np.pi%5D%2C%20%5B0%2C%20-1%5D%2C%20%22r%3A%22%2C%20linewidth%3D3)%0A%20%20%20%20ax%5B0%5D.plot(%5B-np.pi%2C%20np.pi%5D%2C%20%5B-1%2C%20-1%5D%2C%20%22r%3A%22%2C%20linewidth%3D3)%0A%20%20%20%20ax%5B0%5D.set_title(r%22Even%20function%3A%20%24%5Ccos(t)%20%3D%20%5Ccos(-t)%24%22%2C%20fontsize%3D15)%0A%20%20%20%20ax%5B1%5D.plot(t%2C%20np.sin(t)%2C%20%22g%22%2C%20linewidth%3D3%2C%20label%3Dr%22%24%5Csin(t)%24%22)%0A%20%20%20%20ax%5B1%5D.plot(%5B-np.pi%20%2F%202%2C%20-np.pi%20%2F%202%5D%2C%20%5B0%2C%20-1%5D%2C%20%22r%3A%22%2C%20linewidth%3D3)%0A%20%20%20%20ax%5B1%5D.plot(%5B-np.pi%20%2F%202%2C%200%5D%2C%20%5B-1%2C%20-1%5D%2C%20%22r%3A%22%2C%20linewidth%3D3)%0A%20%20%20%20ax%5B1%5D.plot(%5Bnp.pi%20%2F%202%2C%20np.pi%20%2F%202%5D%2C%20%5B0%2C%201%5D%2C%20%22r%3A%22%2C%20linewidth%3D3)%0A%20%20%20%20ax%5B1%5D.plot(%5B0%2C%20np.pi%20%2F%202%5D%2C%20%5B1%2C%201%5D%2C%20%22r%3A%22%2C%20linewidth%3D3)%0A%20%20%20%20ax%5B1%5D.set_title(r%22Odd%20function%3A%20%24%5Csin(t)%20%3D%20-%5Csin(-t)%24%22%2C%20fontsize%3D15)%0A%20%20%20%20for%20axi%20in%20ax%3A%0A%20%20%20%20%20%20%20%20axi.margins(0.02)%0A%20%20%20%20%20%20%20%20axi.spines%5B%22bottom%22%5D.set_position(%22zero%22)%0A%20%20%20%20%20%20%20%20axi.spines%5B%22top%22%5D.set_color(%22none%22)%0A%20%20%20%20%20%20%20%20axi.spines%5B%22left%22%5D.set_position(%22zero%22)%0A%20%20%20%20%20%20%20%20axi.spines%5B%22right%22%5D.set_color(%22none%22)%0A%20%20%20%20%20%20%20%20axi.xaxis.set_ticks_position(%22bottom%22)%0A%20%20%20%20%20%20%20%20axi.yaxis.set_ticks_position(%22left%22)%0A%20%20%20%20%20%20%20%20axi.tick_params(axis%3D%22both%22%2C%20direction%3D%22inout%22%2C%20which%3D%22both%22%2C%20length%3D5)%0A%20%20%20%20%20%20%20%20axi.set_xlim((-np.pi%20-%200.2%2C%20np.pi%20%2B%200.2))%0A%20%20%20%20%20%20%20%20axi.set_ylim((-1.1%2C%201.1))%0A%20%20%20%20%20%20%20%20axi.set_xticks(np.linspace(-np.pi%2C%20np.pi%2C%205))%0A%20%20%20%20%20%20%20%20axi.set_xticklabels(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Br%22%24-%5Cpi%24%22%2C%20r%22%24-%5Cpi%2F2%24%22%2C%20r%22%240%24%22%2C%20r%22%24%5Cpi%2F2%24%22%2C%20r%22%24%5Cpi%24%22%5D%2C%20fontsize%3D14%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20label%20in%20axi.get_xticklabels()%20%2B%20axi.get_yticklabels()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20label.set_bbox(dict(facecolor%3D%22white%22%2C%20edgecolor%3D%22None%22%2C%20alpha%3D0.65))%0A%20%20%20%20fig.tight_layout()%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%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%20Most%20functions%20are%20neither%20even%20nor%20odd%2C%20but%20every%20function%20can%20be%20written%20as%20the%20sum%20of%20an%20even%20part%20and%20an%20odd%20part%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x(t)%20%3D%20%5Cunderbrace%7B%5Cfrac%7Bx(t)%20%2B%20x(-t)%7D%7B2%7D%7D_%7B%5Ctext%7Beven%7D%7D%20%2B%20%5Cunderbrace%7B%5Cfrac%7Bx(t)%20-%20x(-t)%7D%7B2%7D%7D_%7B%5Ctext%7Bodd%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20**Challenge%202.**%0A%0A%20%20%20%201.%20Check%20that%20the%20first%20term%20is%20indeed%20even%20and%20the%20second%20is%20odd.%0A%20%20%20%202.%20Find%20the%20even%20and%20odd%20parts%20of%20%24x(t)%20%3D%20e%5E%7Bt%7D%24.%20Do%20you%20recognize%20them%3F%0A%20%20%20%203.%20Is%20%24x_3%20%3D%20%5Csin(2%5Cpi%20t)%20%2B%202%5Csin(%5Cpi%20t%20%2B%20%5Cpi%2F4)%24%20even%2C%20odd%2C%20or%20neither%3F%20Plot%20its%20even%20and%20odd%20parts.%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.**%20Answer%20Challenge%202%3A%20split%20%24e%5Et%24%20and%20%24x_3%24%20into%20their%20even%20and%20odd%20parts.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%204%0A%20%20%20%20Apply%20the%20sum%20and%20difference%20formulas%3A%20%24e%5Et%24%20has%20even%20part%20%24%5Ccosh%20t%24%20and%20odd%20part%20%24%5Csinh%20t%24.%20For%20%24x_3%24%2C%20expand%20%242%5Csin(%5Cpi%20t%2B%5Cpi%2F4)%3D%5Csqrt2%5Csin(%5Cpi%20t)%2B%5Csqrt2%5Ccos(%5Cpi%20t)%24.%20Its%20even%20part%20is%20%24%5Csqrt2%5Ccos(%5Cpi%20t)%24%3B%20its%20odd%20part%20is%20%24%5Csin(2%5Cpi%20t)%2B%5Csqrt2%5Csin(%5Cpi%20t)%24.%20Neither%20alone%20equals%20%24x_3%24%2C%20so%20%24x_3%24%20is%20neither%20even%20nor%20odd.%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%20Continuous%20and%20discrete%20signals%0A%0A%20%20%20%20A%20%5Bcontinuous%20signal%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FContinuous_signal)%20depends%20on%20a%20continuous%20variable%2C%20that%20is%2C%20its%20independent%20variable%20varies%20continuously%20(it%20has%20a%20continuum%20domain).%20A%20%5Bdiscrete%20signal%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDiscrete-time_signal)%20depends%20on%20a%20discrete%20variable%2C%20defined%20only%20on%20a%20discrete%20set%20of%20values.%20For%20instance%2C%20the%20temperature%20throughout%20the%20day%2C%20%24T(t)%24%2C%20is%20a%20continuous%20signal%2C%20because%20it%20depends%20on%20time%2C%20which%20varies%20continuously.%20If%20we%20measure%20the%20temperature%20only%20at%20certain%20times%2C%20say%20every%20hour%2C%20the%20new%20signal%20is%20discrete%2C%20because%20its%20independent%20variable%20is%20discrete%20(%24t%24%20%3D%208%20am%2C%209%20am%2C%2010%20am%2C%20...).%0A%0A%20%20%20%20The%20following%20figure%20illustrates%20continuous%20and%20discrete%20signals%20(although%2C%20since%20we%20are%20using%20a%20digital%20computer%2C%20the%20continuous%20signal%20below%20is%20in%20fact%20not%20authentic!)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20cont_disc_plot()%3A%0A%20%20%20%20%22%22%22Plot%20a%20continuous%20and%20a%20discrete%20version%20of%20the%20same%20sinusoid.%22%22%22%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20fig%2C%20ax%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(10%2C%203.2))%0A%20%20%20%20t%20%3D%20np.linspace(0%2C%201%2C%20101)%0A%20%20%20%20x%20%3D%20np.sin(2%20*%20np.pi%20*%20t)%0A%20%20%20%20ax%5B0%5D.plot(t%2C%20x%2C%20%22r%22%2C%20linewidth%3D3)%0A%20%20%20%20ax%5B0%5D.set_title(%22Continuous%20signal%22%2C%20fontsize%3D15)%0A%20%20%20%20ax%5B1%5D.stem(t%5B%3A%3A5%5D%2C%20x%5B%3A%3A5%5D%2C%20markerfmt%3D%22ro%22%2C%20linefmt%3D%22b--%22)%0A%20%20%20%20ax%5B1%5D.set_title(%22Discrete%20signal%22%2C%20fontsize%3D15)%0A%20%20%20%20for%20axi%20in%20ax%3A%0A%20%20%20%20%20%20%20%20axi.margins(0.02)%0A%20%20%20%20%20%20%20%20axi.spines%5B%22bottom%22%5D.set_position(%22zero%22)%0A%20%20%20%20%20%20%20%20axi.spines%5B%22top%22%5D.set_color(%22none%22)%0A%20%20%20%20%20%20%20%20axi.spines%5B%22left%22%5D.set_position(%22zero%22)%0A%20%20%20%20%20%20%20%20axi.spines%5B%22right%22%5D.set_color(%22none%22)%0A%20%20%20%20%20%20%20%20axi.xaxis.set_ticks_position(%22bottom%22)%0A%20%20%20%20%20%20%20%20axi.yaxis.set_ticks_position(%22left%22)%0A%20%20%20%20%20%20%20%20axi.tick_params(axis%3D%22both%22%2C%20direction%3D%22inout%22%2C%20which%3D%22both%22%2C%20length%3D5)%0A%20%20%20%20%20%20%20%20axi.locator_params(axis%3D%22both%22%2C%20nbins%3D5)%0A%20%20%20%20%20%20%20%20axi.set_ylim((-1.1%2C%201.1))%0A%20%20%20%20%20%20%20%20axi.set_ylabel(%22Amplitude%22%2C%20fontsize%3D14)%0A%20%20%20%20%20%20%20%20axi.annotate(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22t%5Bs%5D%22%2C%20xy%3D(0.95%2C%200.1)%2C%20xycoords%3D%22data%22%2C%20color%3D%22k%22%2C%20size%3D13%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20xytext%3D(0%2C%200)%2C%20textcoords%3D%22offset%20points%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20fig.tight_layout()%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20cont_disc_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%20Sampling%0A%0A%20%20%20%20The%20reduction%20of%20a%20continuous%20signal%20to%20a%20discrete%20signal%20is%20called%20%3Ca%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSampling_(signal_processing)%22%3Esampling%3C%2Fa%3E%2C%20and%20the%20frequency%20at%20which%20it%20is%20performed%20is%20called%20the%20sampling%20frequency%20or%20sampling%20rate%2C%20in%20hertz%20(Hz).%20For%20instance%2C%20the%20discrete%20signal%20plotted%20above%20has%20a%20sampling%20frequency%20of%2020%20Hz%3A%20it%20was%20sampled%20every%200.05%20s.%0A%0A%20%20%20%20Sampling%20is%20the%20basis%20for%20using%20digital%20computers%20to%20record%20and%20store%20data%20from%20an%20observed%20phenomenon.%20Computers%20have%20a%20finite%20memory%2C%20so%20they%20cannot%20store%20a%20continuous%20signal%20(how%20many%20instants%20are%20there%20in%20one%20second%3F).%0A%0A%20%20%20%20The%20EMG%20file%20stores%20time%20in%20its%20first%20column%2C%20so%20its%20sampling%20frequency%20can%20be%20read%20off%20the%20data.%20**Before%20you%20run%20the%20next%20cell**%2C%20compare%20your%20answer%20to%20Guiding%20question%200.1%20with%20the%20guess%20you%20made%20in%20Challenge%200.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(emg_time%2C%20np)%3A%0A%20%20%20%20_dt%20%3D%20np.diff(emg_time)%0A%20%20%20%20print(f%22Sampling%20interval%3A%20%7Bnp.mean(_dt)%20*%201000%3A.3f%7D%20ms%20(from%20%7B_dt.min()%20*%201000%3A.3f%7D%20to%20%7B_dt.max()%20*%201000%3A.3f%7D%20ms)%22)%0A%20%20%20%20print(f%22Sampling%20frequency%3A%20%7B1%20%2F%20np.mean(_dt)%3A.0f%7D%20Hz%22)%0A%20%20%20%20print(f%22Number%20of%20samples%3A%20%7Bemg_time.size%7D%2C%20duration%3A%20%7Bemg_time%5B-1%5D%20-%20emg_time%5B0%5D%3A.3f%7D%20s%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%20One%20thousand%20samples%20per%20second%2C%20at%20perfectly%20regular%20intervals.%20Is%20that%20fast%20enough%3F%20The%20answer%20depends%20on%20what%20frequencies%20the%20signal%20contains%2C%20and%20that%20is%20the%20subject%20of%20the%20next%20section.%0A%0A%20%20%20%20%23%23%23%20Nyquist-Shannon%20sampling%20theorem%0A%0A%20%20%20%20Some%20requirements%20must%20be%20satisfied%20for%20the%20proper%20discretization%20of%20a%20signal%2C%20and%20an%20important%20one%20is%20expressed%20in%20the%20%5BNyquist-Shannon%20sampling%20theorem%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FNyquist%25E2%2580%2593Shannon_sampling_theorem)%2C%20in%20Shannon's%20words%3A%0A%0A%20%20%20%20%3E%20%22If%20a%20function%20x(t)%20contains%20no%20frequencies%20higher%20than%20B%20hertz%2C%20it%20is%20completely%20determined%20by%20giving%20its%20ordinates%20at%20a%20series%20of%20points%20spaced%201%2F(2B)%20seconds%20apart.%22%0A%0A%20%20%20%20For%20a%20band-limited%20signal%2C%20choose%20%24f_s%3E2f_B%24%20so%20that%20the%20highest%20component%20lies%20strictly%20below%20the%20Nyquist%20frequency%20%24f_s%2F2%24.%20The%20boundary%20needs%20care%3A%20a%20sine%20sampled%20exactly%20twice%20per%20cycle%20at%20its%20zero%20crossings%20disappears.%20Real%20acquisition%20also%20needs%20an%20analog%20anti-aliasing%20filter%20and%20a%20margin%20for%20its%20transition%20band.%20The%20EMG%20file%20has%20a%20Nyquist%20frequency%20of%20500%20Hz.%20That%20accommodates%20a%20commonly%20studied%20EMG%20band%20of%20roughly%2020%E2%80%93450%20Hz%2C%20but%20the%20file%20alone%20cannot%20prove%20that%20content%20above%20500%20Hz%20was%20suppressed%20before%20sampling.%0A%0A%20%20%20%20When%20the%20theorem%20is%20not%20satisfied%2C%20and%20a%20continuous%20signal%20is%20sampled%20at%20less%20than%20twice%20its%20highest%20frequency%2C%20an%20effect%20called%20%5Baliasing%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAliasing)%20occurs%3A%20the%20discrete%20signal%20no%20longer%20contains%20the%20same%20information%20as%20the%20continuous%20one.%20It%20does%20not%20just%20lose%20the%20high%20frequencies.%20It%20shows%20them%20disguised%20as%20low%20ones.%0A%0A%20%20%20%20**Before%20you%20run%20the%20next%20cell**%2C%20predict%20what%20you%20will%20see%20when%20a%209%20Hz%20sinusoid%20is%20sampled%20at%2010%20Hz%2C%20well%20below%20the%2018%20Hz%20the%20theorem%20demands.%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(0%2C%202%2C%202001)%20%20%23%20(almost)%20continuous%20time%0A%20%20%20%20_ts%20%3D%20np.arange(0%2C%202%2C%201%20%2F%2010)%20%20%23%20sampled%20at%2010%20Hz%0A%20%20%20%20_x9%20%3D%20np.sin(2%20*%20np.pi%20*%209%20*%20_ts)%0A%20%20%20%20_x1%20%3D%20-np.sin(2%20*%20np.pi%20*%201%20*%20_ts)%0A%0A%20%20%20%20plt.figure(figsize%3D(10%2C%203.5))%0A%20%20%20%20plt.plot(_t%2C%20np.sin(2%20*%20np.pi%20*%209%20*%20_t)%2C%20color%3D%220.7%22%2C%20linewidth%3D1%2C%20label%3D%229%20Hz%20sinusoid%22)%0A%20%20%20%20plt.plot(_t%2C%20-np.sin(2%20*%20np.pi%20*%201%20*%20_t)%2C%20%22b--%22%2C%20linewidth%3D2%2C%20label%3D%221%20Hz%20sinusoid%20(inverted)%22)%0A%20%20%20%20plt.plot(_ts%2C%20_x9%2C%20%22ro%22%2C%20markersize%3D8%2C%20label%3D%229%20Hz%20sampled%20at%2010%20Hz%22)%0A%20%20%20%20plt.xlabel(%22Time%20%5Bs%5D%22)%0A%20%20%20%20plt.ylabel(%22Amplitude%22)%0A%20%20%20%20plt.legend(loc%3D%22upper%20right%22%2C%20framealpha%3D0.9)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20plt.show()%0A%20%20%20%20print(f%22Largest%20difference%20between%20the%20samples%20of%20the%20two%20sinusoids%3A%20%7Bnp.max(np.abs(_x9%20-%20_x1))%3A.1e%7D%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%20The%20samples%20of%20the%209%20Hz%20sinusoid%20are%2C%20to%20the%20last%20decimal%20place%2C%20the%20samples%20of%20a%201%20Hz%20sinusoid.%20Nothing%20in%20the%20sampled%20data%20can%20tell%20you%20which%20one%20was%20there.%20A%20frequency%20%24f%24%20above%20the%20Nyquist%20frequency%20reappears%20at%20the%20alias%20frequency%20%24%7Cf%20-%20k%20f_s%7C%24%2C%20for%20the%20integer%20%24k%24%20that%20brings%20it%20into%20the%20range%20%24%5B0%2C%20f_s%2F2%5D%24%3B%20here%20%24%7C9%20-%2010%7C%20%3D%201%24%20Hz.%0A%0A%20%20%20%20This%20has%20a%20practical%20consequence%20that%20is%20easy%20to%20miss%3A%20**aliasing%20cannot%20be%20removed%20after%20sampling**.%20Once%20the%209%20Hz%20has%20become%201%20Hz%2C%20no%20digital%20filter%20can%20separate%20it%20from%20a%20genuine%201%20Hz.%20That%20is%20why%20acquisition%20systems%20have%20an%20analog%20*anti-aliasing*%20low-pass%20filter%20before%20the%20analog-to-digital%20converter%2C%20and%20why%20the%20sampling%20frequency%20has%20to%20be%20chosen%20before%20the%20experiment%2C%20not%20fixed%20afterwards.%0A%0A%20%20%20%20**Guiding%20questions%202.**%0A%0A%20%20%20%201.%20A%20marker%20on%20the%20heel%20during%20running%20has%20relevant%20content%20up%20to%20about%2015%20Hz.%20What%20is%20the%20minimum%20sampling%20frequency%3F%20Would%20you%20sample%20at%20exactly%20that%20rate%3F%0A%20%20%20%202.%20A%20film%20of%20a%20spinning%20wheel%20can%20show%20it%20turning%20slowly%20backwards.%20Explain%20it%20with%20aliasing.%20(See%20also%20Challenge%201%20of%20%5BAngular%20kinematics%20in%20a%20plane%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fkinematics_angular_2d.py).)%0A%20%20%20%203.%20At%20what%20frequency%20would%20an%2011%20Hz%20sinusoid%20appear%20if%20sampled%20at%2010%20Hz%3F%20And%20a%2010%20Hz%20one%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**Exercise%205.**%20A%20heel%20trajectory%20contains%20frequencies%20up%20to%2015%20Hz.%20Is%2030%20Hz%20a%20sensible%20acquisition%20rate%3F%20What%20aliases%20do%2011%20Hz%20and%2010%20Hz%20sinusoids%20produce%20at%2010%20Hz%20sampling%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%205%0A%20%20%20%20The%20Nyquist%20rate%20is%2030%20Hz%2C%20but%20choose%20a%20rate%20above%20it%20and%20allow%20an%20anti-alias%20filter%20transition%20band.%20At%2010%20Hz%20sampling%2C%20%24%7C11-10%7C%3D1%24%20Hz%3B%2010%20Hz%20maps%20to%20DC%20(its%20constant%20sampled%20value%20depends%20on%20phase%20and%20is%20zero%20for%20a%20zero-phase%20sine).%20A%20spinning%20wheel%20behaves%20similarly%20because%20frames%20sample%20its%20angular%20motion%3B%20different%20rotations%20can%20have%20the%20same%20observed%20sequence.%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%20Analog%20and%20digital%20signals%0A%0A%20%20%20%20The%20amplitude%20of%20an%20%5Banalog%20signal%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAnalog_signal)%20can%20take%20on%20any%20value%20(an%20infinite%20number%20of%20possible%20values)%20in%20a%20continuous%20range.%20The%20amplitude%20of%20a%20%5Bdigital%20signal%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDigital_signal)%20can%20take%20only%20certain%20values%20(a%20finite%20number%20of%20possible%20values).%20For%20instance%2C%20a%20binary%20signal%20can%20take%20only%20two%20values.%0A%0A%20%20%20%20The%20continuous%20and%20discrete%20properties%20refer%20to%20the%20independent%20variable%20(for%20instance%2C%20time)%3B%20the%20analog%20and%20digital%20properties%20refer%20to%20the%20amplitude%20of%20the%20dependent%20variable%20(the%20signal%20itself).%20A%20recorded%20signal%20is%20both%20discrete%20and%20digital%3A%20sampled%20in%20time%20and%20quantized%20in%20amplitude.%0A%0A%20%20%20%20%23%23%23%20Quantization%0A%0A%20%20%20%20The%20reduction%20of%20an%20analog%20signal%20to%20a%20digital%20signal%20is%20called%20%3Ca%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FQuantization_(signal_processing)%22%3Equantization%3C%2Fa%3E%2C%20and%20in%20measurement%20it%20is%20typically%20performed%20by%20an%20%5Banalog-to-digital%20(A%2FD)%20converter%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAnalog-to-digital_converter).%20The%20number%20of%20discrete%20values%20that%20the%20converter%20can%20output%20over%20its%20input%20range%20is%20its%20resolution.%20Because%20the%20output%20is%20stored%20as%20a%20binary%20number%2C%20the%20resolution%20is%20expressed%20in%20bits%20and%20the%20number%20of%20levels%20is%20a%20power%20of%20two.%20A%20resolution%20of%201%20bit%20encodes%20the%20analog%20input%20into%20one%20of%202%20(%242%5E1%24)%20levels%2C%204%20bits%20into%20one%20of%2016%20(%242%5E4%24)%20levels%2C%20and%20a%20good%20commercial%20A%2FD%20converter%2C%20with%2016%20bits%2C%20into%20one%20of%2065%2C536%20(%242%5E%7B16%7D%24)%20levels.%0A%0A%20%20%20%20If%20we%20know%20the%20voltage%20range%20of%20the%20A%2FD%20converter%20(the%20maximum%20minus%20the%20minimum%20voltage%20it%20can%20read)%2C%20we%20can%20express%20the%20resolution%20in%20volts%2C%20as%20the%20size%20of%20one%20step%3A%20the%20range%20divided%20by%20the%20number%20of%20levels.%20For%20a%20typical%20range%20of%2010%20V%2C%20from%20%24-5%24%20to%20%245%24%20V%2C%20the%20step%20is%205%20V%20for%201%20bit%2C%200.625%20V%20for%204%20bits%2C%20and%20about%200.00015%20V%20(0.15%20mV)%20for%2016%20bits.%0A%0A%20%20%20%20Let's%20write%20an%20idealized%20converter.%20It%20divides%20its%20range%20into%20%242%5E%7Bn%7D%24%20steps%20of%20equal%20size%20and%20replaces%20each%20value%20by%20the%20centre%20of%20the%20step%20it%20falls%20in%3B%20values%20outside%20the%20range%20are%20clipped%20to%20the%20first%20or%20last%20step.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20quantize(x%2C%20n_bits%2C%20v_range)%3A%0A%20%20%20%20%22%22%22Quantize%20%60x%60%20with%20an%20ideal%20%60n_bits%60%20A%2FD%20converter%20of%20range%20%60v_range%60.%0A%0A%20%20%20%20The%20range%20is%20centred%20at%20zero%2C%20%5B-v_range%2F2%2C%20v_range%2F2%5D%2C%20and%20divided%20into%0A%20%20%20%202**n_bits%20steps%20of%20equal%20size%3B%20each%20value%20is%20replaced%20by%20the%20centre%20of%20its%0A%20%20%20%20step.%20Values%20outside%20the%20range%20are%20clipped%20to%20the%20first%20or%20last%20step.%0A%20%20%20%20%22%22%22%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20step%20%3D%20v_range%20%2F%202**n_bits%0A%20%20%20%20level%20%3D%20np.clip(np.floor((np.asarray(x)%20%2B%20v_range%20%2F%202)%20%2F%20step)%2C%200%2C%202**n_bits%20-%201)%0A%20%20%20%20return%20-v_range%20%2F%202%20%2B%20(level%20%2B%200.5)%20*%20step%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**Before%20you%20run%20the%20next%20cell**%2C%20predict%20for%20a%20sinusoid%20of%20unit%20amplitude%20quantized%20with%20a%20range%20of%202%20(from%20%24-1%24%20to%20%241%24)%3A%20how%20many%20distinct%20values%20will%20the%201-bit%20and%20the%204-bit%20versions%20have%2C%20and%20what%20is%20the%20largest%20error%20each%20one%20makes%3F%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(0%2C%201%2C%20101)%0A%20%20%20%20_x%20%3D%20np.sin(2%20*%20np.pi%20*%20_t)%0A%0A%20%20%20%20_fig%2C%20_axs%20%3D%20plt.subplots(1%2C%203%2C%20figsize%3D(11%2C%203.6)%2C%20sharey%3DTrue)%0A%20%20%20%20for%20_ax%2C%20_bits%20in%20zip(_axs%2C%20(1%2C%204%2C%2016))%3A%0A%20%20%20%20%20%20%20%20_xq%20%3D%20quantize(_x%2C%20_bits%2C%202)%0A%20%20%20%20%20%20%20%20_ax.plot(_t%2C%20_x%2C%20linewidth%3D2%2C%20color%3D%5B0%2C%200%2C%201%2C%200.3%5D)%0A%20%20%20%20%20%20%20%20_ax.plot(_t%2C%20_xq%2C%20%22r.-%22%2C%20linewidth%3D1%2C%20drawstyle%3D%22steps-mid%22)%0A%20%20%20%20%20%20%20%20_ax.set_title(f%22%7B_bits%7D-bit%20resolution%22)%0A%20%20%20%20%20%20%20%20_ax.set_xlabel(%22Time%20%5Bs%5D%22)%0A%20%20%20%20%20%20%20%20_ax.set_ylim((-1.1%2C%201.1))%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B_bits%3A2d%7D%20bits%3A%20step%20%3D%20%7B2%20%2F%202**_bits%3A.6f%7D%2C%20distinct%20values%20%3D%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bnp.unique(_xq).size%3A3d%7D%2C%20largest%20error%20%3D%20%7Bnp.max(np.abs(_xq%20-%20_x))%3A.6f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20plt.show()%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%20With%201%20bit%20the%20sinusoid%20becomes%20a%20square%20wave%20with%20two%20values%2C%20%24%5Cpm%200.5%24%2C%20and%20the%20error%20reaches%20half%20a%20step%2C%200.5.%20With%204%20bits%20the%20staircase%20is%20visible%20but%20already%20follows%20the%20curve%2C%20and%20the%20error%20never%20exceeds%20half%20a%20step%2C%200.0625.%20With%2016%20bits%20the%20staircase%20is%20invisible%20at%20this%20scale.%20Note%20that%20the%2016-bit%20version%20shows%20only%20about%2050%20distinct%20values%2C%20not%2065%2C536%3A%20there%20are%20only%20101%20samples%2C%20and%20many%20fall%20on%20the%20same%20level.%20The%20resolution%20is%20how%20many%20values%20the%20converter%20*can*%20output%2C%20not%20how%20many%20a%20given%20recording%20uses.%0A%0A%20%20%20%20%23%23%23%20Back%20to%20the%20EMG%3A%20reading%20the%20converter%20off%20the%20data%0A%0A%20%20%20%20Now%20we%20can%20answer%20Guiding%20question%200.2.%20The%20steps%20in%20the%20zoomed%20EMG%20are%20quantization.%20If%20the%20smallest%20difference%20between%20two%20distinct%20values%20in%20the%20file%20is%20the%20step%20of%20the%20converter%2C%20we%20can%20work%20out%20its%20resolution.%0A%0A%20%20%20%20**Before%20you%20run%20the%20next%20cell**%2C%20predict%20the%20number%20of%20distinct%20values%20in%20the%20EMG%20file%2C%20which%20has%203360%20samples.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(emg%2C%20np)%3A%0A%20%20%20%20_levels%20%3D%20np.unique(emg)%0A%20%20%20%20_step%20%3D%20np.min(np.diff(_levels))%0A%20%20%20%20print(f%22Distinct%20values%20in%20the%20file%3A%20%7B_levels.size%7D%22)%0A%20%20%20%20print(f%22Smallest%20step%20between%20them%3A%20%20%7B_step%3A.5f%7D%22)%0A%20%20%20%20for%20_bits%20in%20(8%2C%2012%2C%2016)%3A%0A%20%20%20%20%20%20%20%20print(f%22%20%20step%20of%20a%20%7B_bits%3A2d%7D-bit%20converter%20with%20a%2010%20V%20range%3A%20%7B10%20%2F%202**_bits%3A.5f%7D%22)%0A%20%20%20%20print(f%22Range%20used%20by%20the%20EMG%3A%20from%20%7Bemg.min()%3A.4f%7D%20to%20%7Bemg.max()%3A.4f%7D%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20f%22%7B100%20*%20(emg.max()%20-%20emg.min())%20%2F%2010%3A.1f%7D%25%20of%20a%2010%20V%20range%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%20Of%203360%20samples%2C%20only%2082%20different%20values.%20The%20smallest%20step%2C%200.00244%2C%20is%20consistent%2C%20to%20the%20precision%20stored%20in%20the%20file%2C%20with%20the%20step%20of%20a%2012-bit%20converter%20with%20a%2010%20V%20range%2C%20%2410%2F2%5E%7B12%7D%24.%20(The%20file%20does%20not%20say%20so%3B%20this%20is%20the%20most%20likely%20reading%20if%20its%20values%20are%20in%20volts%20at%20the%20converter's%20input.)%20The%20other%20steps%20between%20levels%20are%20whole%20multiples%20of%20it.%0A%0A%20%20%20%20Under%20that%2010%20V%2C%2012-bit%20interpretation%2C%20this%20recording%20uses%20fewer%20than%20a%20hundred%20of%20the%204096%20possible%20levels.%20That%20suggests%20checking%20the%20amplifier%20gain.%20It%20does%20not%20identify%20the%20converter%20uniquely%3A%20scaling%2C%20rounding%2C%20or%20later%20processing%20can%20also%20create%20a%20grid%20of%20values.%20Increasing%20gain%20improves%20the%20use%20of%20the%20available%20range%20only%20if%20the%20largest%20expected%20signal%20still%20avoids%20clipping.%0A%0A%20%20%20%20**Guiding%20questions%203.**%0A%0A%20%20%20%201.%20If%20the%20amplifier%20gain%20had%20been%20ten%20times%20larger%2C%20how%20many%20levels%20would%20the%20EMG%20have%20used%3F%20What%20could%20go%20wrong%20if%20it%20had%20been%20a%20hundred%20times%20larger%3F%0A%20%20%20%202.%20With%20the%20same%20gain%2C%20how%20many%20levels%20would%20a%2016-bit%20converter%20have%20used%3F%0A%20%20%20%203.%20Go%20back%20to%20the%20guess%20you%20made%20in%20Challenge%200%20about%20the%20number%20of%20distinct%20values.%20Was%20it%20about%20the%20converter%20or%20about%20the%20recording%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**Exercise%206.**%20For%20an%208-bit%20converter%20with%20a%2010%20V%20range%2C%20compute%20the%20step%20and%20the%20largest%20quantization%20error%20without%20clipping.%20Would%20tenfold%20gain%20create%20ten%20times%20as%20many%20distinct%20values%20in%20an%20already%20saved%20file%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%206%0A%20%20%20%20There%20are%20%242%5E8%3D256%24%20levels%2C%20so%20%24%5CDelta%3D10%2F256%3D0.0390625%24%20V%20and%20the%20ideal%20mid-rise%20error%20is%20at%20most%20%24%5CDelta%2F2%3D0.01953125%24%20V.%20Amplifying%20before%20acquisition%20can%20use%20more%20levels%3B%20multiplying%20a%20saved%20file%20cannot%20recover%20discarded%20detail.%20Distinct%20occupied%20levels%20also%20depend%20on%20sample%20count.%20A%20hundredfold%20analog%20gain%20may%20clip%3B%20moving%20from%2012%20to%2016%20bits%20makes%20the%20nominal%20step%2016%20times%20finer.%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%20Deterministic%20and%20random%20signals%0A%0A%20%20%20%20A%20deterministic%20signal%20can%20be%20described%20by%20an%20explicit%20mathematical%20function%3B%20a%20random%20signal%20cannot.%20A%20sine%20wave%20and%20a%20parabola%20are%20deterministic.%20The%20outcomes%20of%20tossing%20a%20coin%20or%20throwing%20a%20die%20are%20random%3A%20although%20the%20set%20of%20possible%20values%20is%20limited%20and%20known%2C%20no%20mathematical%20function%20predicts%20the%20exact%20outcome%2C%20only%20its%20probability.%20Most%20measurements%20of%20observed%20phenomena%20in%20nature%20contain%20both%20deterministic%20and%20random%20parts.%0A%0A%20%20%20%20The%20software%20generator%20used%20here%20produces%20*pseudorandom*%20numbers%2C%20from%20a%20deterministic%20algorithm%20started%20at%20a%20seed.%20Computers%20can%20also%20obtain%20physical%20randomness%20from%20hardware%20entropy%20sources%3B%20randomness%20is%20not%20ruled%20out%20merely%20because%20a%20computer%20records%20it.%20Read%20more%20in%20%5BIntroduction%20to%20Randomness%20and%20Random%20Numbers%5D(https%3A%2F%2Fwww.random.org%2Frandomness%2F).%0A%0A%20%20%20%20The%20determinism%20is%20easy%20to%20see%2C%20and%20it%20is%20useful%3A%20**before%20you%20run%20the%20next%20cell**%2C%20predict%20whether%20two%20generators%20started%20with%20the%20same%20seed%20produce%20the%20same%20numbers.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20_rng1%20%3D%20np.random.default_rng(seed%3D42)%0A%20%20%20%20_rng2%20%3D%20np.random.default_rng(seed%3D42)%0A%20%20%20%20print(%22Generator%201%3A%22%2C%20np.round(_rng1.standard_normal(5)%2C%204))%0A%20%20%20%20print(%22Generator%202%3A%22%2C%20np.round(_rng2.standard_normal(5)%2C%204))%0A%20%20%20%20print(%22New%20seed%3A%20%20%20%22%2C%20np.round(np.random.default_rng(seed%3D7).standard_normal(5)%2C%204))%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%20Identical.%20Fixing%20the%20seed%20is%20how%20a%20simulation%20with%20%22random%22%20noise%20is%20made%20reproducible%2C%20and%20the%20notebooks%20in%20this%20collection%20do%20it%20whenever%20they%20add%20noise%20to%20a%20signal.%0A%0A%20%20%20%20%23%23%20Signal%20and%20noise%0A%0A%20%20%20%20With%20respect%20to%20what%20is%20being%20measured%2C%20data%20can%20be%20classified%20as%20signal%20or%20noise.%20%3Ca%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSignal_(electrical_engineering)%22%3ESignal%3C%2Fa%3E%20is%20the%20part%20of%20the%20data%20you%20want%2C%20the%20part%20you%20believe%20truly%20represents%20the%20phenomenon%20being%20measured%20(or%20modeled).%20%5BNoise%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FNoise)%20is%20what%20you%20don't%20want%20in%20a%20measurement%20because%2C%20in%20principle%2C%20it%20has%20no%20significant%20role%20in%20the%20understanding%20of%20the%20observed%20phenomenon.%0A%0A%20%20%20%20As%20tautological%20as%20this%20sounds%2C%20the%20distinction%20between%20noise%20and%20signal%20depends%20on%20what%20you%20think%20is%20relevant%20about%20the%20phenomenon.%20The%20power-line%20interference%20in%20an%20EMG%20is%20noise%20to%20a%20physiologist%20and%20signal%20to%20the%20engineer%20looking%20for%20a%20bad%20ground%20connection.%20Experimental%20data%20are%20contaminated%20by%20noise%2C%20and%20it%20is%20rarely%20possible%20to%20completely%20separate%20signal%20from%20noise.%0A%0A%20%20%20%20The%20%5Bsignal-to-noise%20ratio%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSignal-to-noise_ratio)%20(SNR%20or%20S%2FN)%20quantifies%20the%20level%20of%20a%20desired%20signal%20relative%20to%20the%20background%20noise.%20It%20is%20the%20ratio%20between%20the%20power%20of%20the%20signal%20and%20the%20power%20of%20the%20noise%2C%20often%20expressed%20in%20decibels%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20SNR%20%3D%20%5Cfrac%7BP_%7Bsignal%7D%7D%7BP_%7Bnoise%7D%7D%2C%20%5Cqquad%20SNR_%7BdB%7D%20%3D%2010%5Clog_%7B10%7D%5Cleft(%5Cfrac%7BP_%7Bsignal%7D%7D%7BP_%7Bnoise%7D%7D%5Cright)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20**Before%20you%20run%20the%20next%20cell**%2C%20predict%20the%20SNR%20of%20a%20sinusoid%20of%20amplitude%201%20plus%20random%20noise%20with%20a%20standard%20deviation%20of%200.1.%20The%20power%20of%20a%20signal%20with%20zero%20mean%20is%20its%20mean%20squared%20value%2C%20which%20for%20the%20sinusoid%20you%20computed%20in%20the%20section%20on%20AC%20and%20DC%20components.%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_rng%20%3D%20np.random.default_rng(seed%3D42)%0A%20%20%20%20_t%20%3D%20np.arange(0%2C%202%2C%200.001)%0A%20%20%20%20_s%20%3D%20np.sin(2%20*%20np.pi%20*%202%20*%20_t)%0A%20%20%20%20_n%20%3D%200.1%20*%20_rng.standard_normal(_t.size)%0A%0A%20%20%20%20_snr%20%3D%20np.mean(_s**2)%20%2F%20np.mean(_n**2)%0A%20%20%20%20print(f%22Signal%20power%3A%20%7Bnp.mean(_s**2)%3A.3f%7D%2C%20noise%20power%3A%20%7Bnp.mean(_n**2)%3A.4f%7D%22)%0A%20%20%20%20print(f%22SNR%20%3D%20%7B_snr%3A.0f%7D%20%3D%20%7B10%20*%20np.log10(_snr)%3A.1f%7D%20dB%22)%0A%0A%20%20%20%20plt.figure(figsize%3D(10%2C%203))%0A%20%20%20%20plt.plot(_t%2C%20_s%20%2B%20_n%2C%20color%3D%22tab%3Ared%22%2C%20linewidth%3D1%2C%20label%3D%22signal%20%2B%20noise%22)%0A%20%20%20%20plt.plot(_t%2C%20_s%2C%20%22k%22%2C%20linewidth%3D2%2C%20label%3D%22signal%22)%0A%20%20%20%20plt.xlabel(%22Time%20%5Bs%5D%22)%0A%20%20%20%20plt.ylabel(%22Amplitude%22)%0A%20%20%20%20plt.legend(loc%3D%22upper%20right%22%2C%20framealpha%3D0.9)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20plt.show()%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%20An%20SNR%20of%20about%2050%2C%20or%2017%20dB%3A%20the%20sinusoid%20has%2050%20times%20the%20power%20of%20the%20noise%2C%20although%20the%20noise%20is%20plainly%20visible.%20The%20power%20ratio%2C%20not%20the%20look%20of%20the%20plot%2C%20is%20what%20changes%20dramatically%20when%20data%20are%20differentiated%2C%20as%20you%20will%20see%20in%20%5BData%20filtering%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fdata_filtering.py).%0A%0A%20%20%20%20%23%23%20Signal%20and%20system%0A%0A%20%20%20%20In%20engineering%2C%20a%20signal%20is%20often%20associated%20with%20a%20system%2C%20an%20entity%20(a%20physical%20or%20virtual%20device)%20that%20takes%20a%20signal%20as%20input%20and%20produces%20another%20signal%20as%20output.%20In%20mathematics%2C%20if%20a%20signal%20can%20be%20represented%20as%20a%20function%2C%20a%20system%20can%20be%20represented%20by%20a%20differential%20equation.%20For%20instance%2C%20a%20mass%20attached%20to%20a%20spring%20is%20a%20system%20that%20takes%20a%20force%20(a%20signal)%20as%20input%20and%20produces%20a%20displacement%20(another%20signal)%3B%20it%20can%20be%20described%20by%20a%20second-order%20ordinary%20differential%20equation.%20A%20filter%2C%20the%20subject%20of%20the%20next%20notebook%2C%20is%20also%20a%20system.%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%207.**%20The%20signal%20power%20is%200.5%20and%20noise%20power%20is%200.01.%20What%20happens%20to%20SNR%20if%20you%20double%20the%20noise%20amplitude%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%207%0A%20%20%20%20Initially%20SNR%20is%20%240.5%2F0.01%3D50%24%2C%20or%20%2410%5Clog_%7B10%7D(50)%3D16.99%24%20dB.%20Doubling%20noise%20amplitude%20quadruples%20its%20power%3A%20SNR%20becomes%20%2412.5%24%2C%20or%20%2410.97%24%20dB%2C%20a%20decrease%20of%20%2420%5Clog_%7B10%7D(2)%3D6.02%24%20dB.%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%20Checkpoint%20questions%0A%0A%20%20%20%20Pause%20here%20before%20the%20problems.%0A%0A%20%20%20%201.%20Go%20back%20to%20the%20two%20guesses%20in%20Challenge%200.%20Which%20of%20them%20is%20set%20by%20the%20sampling%20frequency%20and%20which%20by%20the%20resolution%20of%20the%20converter%3F%0A%20%20%20%202.%20A%20sinusoid%20and%20its%20sum%20with%20a%20constant%20have%20the%20same%20AC%20component.%20Do%20they%20have%20the%20same%20RMS%3F%0A%20%20%20%203.%20Why%20can%20aliasing%20not%20be%20fixed%20after%20the%20data%20are%20recorded%2C%20while%20noise%20can%20at%20least%20be%20attenuated%3F%0A%20%20%20%204.%20A%20force%20plate%20is%20sampled%20at%201000%20Hz%20with%20a%2016-bit%20converter.%20A%20colleague%20proposes%20saving%20disk%20space%20by%20keeping%20every%20tenth%20sample.%20What%20is%20lost%2C%20and%20when%20would%20it%20not%20matter%3F%0A%20%20%20%205.%20An%20EMG%20uses%20only%202%25%20of%20the%20range%20of%20its%20converter.%20Name%20two%20ways%20to%20improve%20its%20resolution%2C%20and%20the%20risk%20of%20each.%0A%20%20%20%206.%20Give%20an%20example%20from%20your%20own%20field%20of%20a%20component%20of%20the%20data%20that%20is%20noise%20in%20one%20study%20and%20signal%20in%20another.%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%20%20%20%201.%20Sampling%20frequency%20sets%20samples%20per%20second%3B%20bit%20depth%20and%20range%20set%20possible%20amplitude%20levels.%20Occupied%20levels%20also%20depend%20on%20the%20actual%20signal.%0A%20%20%20%202.%20Generally%20no%3A%20%24%5Cmathrm%7BRMS%7D%5E2%3D%5Cmathrm%7Bvariance%7D%2B%5Cmathrm%7Bmean%7D%5E2%24.%0A%20%20%20%203.%20Different%20analog%20frequencies%20can%20generate%20the%20same%20samples.%20A%20digital%20filter%20cannot%20infer%20which%20source%20produced%20them.%0A%20%20%20%204.%20The%20new%20rate%20is%20100%20Hz%20and%20Nyquist%20is%2050%20Hz.%20Low-pass%20before%20decimation%3B%20it%20is%20acceptable%20only%20when%20the%20retained%20bandwidth%20and%20timing%20detail%20meet%20the%20study's%20needs.%0A%20%20%20%205.%20More%20analog%20gain%20risks%20clipping%3B%20more%20bits%20or%20a%20narrower%20converter%20range%20can%20improve%20quantization%2C%20but%20do%20not%20remove%20sensor%2Felectronic%20noise%20and%20a%20narrower%20range%20also%20risks%20clipping.%0A%20%20%20%206.%20One%20example%20is%2060%20Hz%20interference%3A%20unwanted%20in%20a%20muscle-activation%20study%2C%20useful%20in%20an%20electrical-grounding%20investigation.%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.%20Plot%20the%20following%20functions%20in%20the%20interval%20%24t%3D%5B-1%2C%201%5D%24%3A%3Cbr%3E%0A%20%20%20%20%20%20%20a.%20%24x(t)%20%3D%201%20%2B%200.5%5Ccos(t%20%2B%20%5Cpi%2F4)%24%3Cbr%3E%0A%20%20%20%20%20%20%20b.%20%24x(t)%20%3D%20%7Ct%7C%24%3Cbr%3E%0A%20%20%20%20%20%20%20c.%20%24x(t)%20%3D%202t%5E3%24%3Cbr%3E%0A%20%20%20%20%20%20%20d.%20%24x(t)%20%3D%20%5Csin(2%5Cpi%20t)%20%2B%20%5Ccos(2%5Cpi%20t)%24%3Cbr%3E%0A%20%20%20%20%20%20%20e.%20%24x(t)%20%3D%20%5Csin%5E2(2%5Cpi%20t)%20%2B%20%5Ccos%5E2(2%5Cpi%20t)%24%3Cbr%3E%0A%20%20%20%20%20%20%20f.%20%24x(t)%20%3D%20%5Csin(2%5Cpi%20t)%2Ft%24%2C%20with%20the%20continuous%20extension%20%24x(0)%3D2%5Cpi%24.%0A%0A%20%20%20%202.%20Plot%20the%20following%20periodic%20function%20in%20the%20interval%20%24t%3D%5B-3%5Cpi%2C%203%5Cpi%5D%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%3C%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%3C%20t%20%3C%20%2B%5Cpi%0A%20%20%20%20%5Cend%7Barray%7D%20%5Cright.%0A%20%20%20%20%5Cqquad%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%20Take%20the%20continuous%20extension%20at%20the%20corners%3A%20%24x(0)%3D1%24%20and%20%24x(%5Cpm%5Cpi)%3D0%24.%0A%0A%20%20%20%203.%20What%20are%20the%20amplitude%2C%20frequency%2C%20period%2C%20and%20phase%20of%20the%20periodic%20functions%20in%20problems%201%20and%202%3F%0A%0A%20%20%20%204.%20Calculate%20the%20AC%20and%20DC%20components%20of%20the%20functions%20in%20problems%201%20and%202.%20Average%20periodic%20functions%20over%20a%20complete%20period%3B%20for%20the%20nonperiodic%20functions%2C%20use%20the%20displayed%20interval%20%24%5B-1%2C1%5D%24.%20Use%20the%20same%20convention%20in%20problem%207.%0A%0A%20%20%20%205.%20Which%20functions%20in%20problems%201%20and%202%20are%20even%2C%20and%20which%20are%20odd%3F%0A%0A%20%20%20%206.%20The%20%5Bpower%20line%20frequency%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FUtility_frequency)%20in%20Brazil%20is%2060%20Hz%20(the%20same%20as%20in%20the%20US%3B%20in%20Europe%20it%20is%2050%20Hz%3B%20%5Bsee%20an%20online%20measurement%20of%20the%20frequency%20in%20Europe%5D(https%3A%2F%2Fwww.mainsfrequency.com%2F)).%20The%20power%20line%20voltage%20is%20usually%20specified%20as%20110%20V%20or%20127%20V%20RMS.%20For%20a%20discrete%20signal%2C%20RMS%20is%20given%20by%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20RMS%20%3D%20%5Csqrt%7B%5Cfrac%7B1%7D%7BN%7D%5Csum_%7Bi%3D1%7D%5E%7BN%7D%20x_i%5E2%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20and%20for%20a%20continuous%20periodic%20signal%20with%20period%20%24T%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20RMS%20%3D%20%5Csqrt%7B%5Cfrac%7B1%7D%7BT%7D%5Cint_%7Bt_0%7D%5E%7Bt_0%2BT%7D%20x(t)%5E2%20%5C%3A%5Cmathrm%7Bd%7Dt%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20If%20the%20power%20line%20waveform%20is%20a%20sinusoid%2C%20what%20is%20the%20amplitude%20that%20results%20in%20110%20V%20RMS%3F%0A%0A%20%20%20%207.%20Calculate%20the%20average%20power%20and%20the%20RMS%20values%20of%20the%20signals%20in%20problem%201.%0A%0A%20%20%20%208.%20Consider%20the%20continuous%20signal%20represented%20by%20the%20function%20%24x(t)%20%3D%202%5Csin(8%5Cpi%20t)%24.%3Cbr%3E%0A%20%20%20%20%20%20%20a.%20What%20is%20the%20Nyquist%20rate%2C%20%24f_N%3D2f%24%2C%20for%20this%20signal%2C%20and%20why%20should%20an%20actual%20sampling%20rate%20be%20greater%20than%20it%3F%3Cbr%3E%0A%20%20%20%20%20%20%20b.%20Plot%20discretized%20versions%20of%20this%20signal%20for%20the%20following%20sampling%20frequencies%3A%20%2410f_N%24%2C%20%242f_N%24%2C%20%24f_N%24%2C%20and%20%24f_N%2F2%24.%20In%20which%20of%20them%20do%20you%20recognize%20the%20original%20signal%3F%0A%0A%20%20%20%209.%20In%20scientific%20computing%2C%20it's%20common%20to%20use%20a%20random%20number%20generator%20to%20generate%20noise%20and%20add%20it%20to%20a%20signal.%20For%20example%2C%20the%20following%20code%20generates%20a%20sinusoid%20(signal)%20plus%20random%20noise%3A%0A%0A%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20rng%20%3D%20np.random.default_rng(seed%3D42)%0A%20%20%20%20%20%20%20t%20%3D%20np.arange(0%2C%201%2C%200.01)%0A%20%20%20%20%20%20%20x%20%3D%20np.sin(2%20*%202%20*%20np.pi%20*%20t)%20%2B%20rng.standard_normal(t.size)%20%2F%2010%0A%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20Plot%20this%20function%20and%20play%20with%20different%20levels%20of%20noise.%20For%20each%2C%20compute%20the%20SNR%20in%20decibels.%0A%0A%20%20%20%2010.%20Write%20a%20Python%20function%20that%2C%20for%20a%20given%20input%20signal%2C%20calculates%20its%20average%20value%2C%20peak-to-peak%20amplitude%2C%20average%20power%2C%20and%20RMS%20value.%20Apply%20it%20to%20the%20EMG%20above%2C%20before%20and%20after%20removing%20its%20mean.%0A%0A%20%20%20%2011.%20Quantize%20the%20EMG%20with%20%60quantize%60%2C%20using%20a%2010%20V%20range%20and%208%20bits.%20How%20many%20distinct%20values%20remain%2C%20and%20does%20the%20signal%20still%20look%20like%20an%20EMG%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%20Final%20answers%0A%20%20%20%201%E2%80%932.%20The%20collapsed%20figure%20below%20gives%20the%20requested%20graphs.%20In%201f%20the%20apparent%20singularity%20is%20removable%3A%20%24x(0)%3D2%5Cpi%24.%20The%20triangle%20reaches%201%20at%20%242k%5Cpi%24%20and%200%20at%20%24(2k%2B1)%5Cpi%24.%0A%0A%20%20%20%203.%201a%3A%20oscillation%20amplitude%20%240.5%24%2C%20%24f%3D1%2F(2%5Cpi)%24%20Hz%2C%20%24T%3D2%5Cpi%24%20s%2C%20cosine%20phase%20%24%5Cpi%2F4%24%2C%20DC%201.%201d%3A%20amplitude%20%24%5Csqrt2%24%2C%20%24f%3D1%24%20Hz%2C%20%24T%3D1%24%20s%2C%20sine%20phase%20%24%5Cpi%2F4%24.%201e%20is%20constant%3A%20no%20unique%20fundamental%20period%20or%20sinusoidal%20phase.%20The%20triangle%20has%20range%20%24%5B0%2C1%5D%24%2C%20peak-to-peak%201%2C%20AC%20peak%20amplitude%20%240.5%24%2C%20%24T%3D2%5Cpi%24%20s%20and%20%24f%3D1%2F(2%5Cpi)%24%20Hz%3B%20it%20has%20no%20single%20sinusoidal%20phase%20(its%20peak%20is%20at%20%24t%3D0%24).%201b%2C%201c%20and%201f%20are%20nonperiodic.%0A%0A%20%20%20%204.%20DC%20values%20for%201a%E2%80%93f%3A%20%241%2C%5C%201%2F2%2C%5C%200%2C%5C%200%2C%5C%201%2C%5C%20%5Cmathrm%7BSi%7D(2%5Cpi)%5Capprox1.418152%24.%20Here%20%24%5Cmathrm%7BSi%7D(z)%3D%5Cint_0%5Ez%5Csin(u)%2Fu%5C%2Cdu%24.%20The%20triangular%20wave%20has%20DC%20%241%2F2%24.%20In%20every%20case%20AC%20is%20the%20listed%20function%20minus%20its%20DC%20value%2C%20using%20the%20averaging%20intervals%20specified%20in%20the%20question.%0A%0A%20%20%20%205.%20Even%3A%201b%2C%201e%2C%201f%20and%20the%20triangle.%20Odd%3A%201c.%20Neither%3A%201a%20and%201d.%0A%0A%20%20%20%206.%20%24110%5Csqrt2%5Capprox155.56%24%20V%20peak.%0A%0A%20%20%20%207.%20Power%20%2F%20RMS%20for%201a%E2%80%93f%3A%20%241.125%2F1.060660%24%3B%20%24(1%2F3)%2F0.577350%24%3B%20%24(4%2F7)%2F0.755929%24%3B%20%241%2F1%24%3B%20%241%2F1%24%3B%20%242%5Cpi%5Cmathrm%7BSi%7D(4%5Cpi)%2F%5Csqrt%7B2%5Cpi%5Cmathrm%7BSi%7D(4%5Cpi)%7D%5Capprox9.375525%2F3.061948%24.%0A%0A%20%20%20%208.%20%24f%3D4%24%20Hz%2C%20Nyquist%20rate%20%24f_N%3D8%24%20Hz.%20The%20four%20rates%20are%2080%2C%2016%2C%208%20and%204%20Hz.%20Starting%20at%20%24t%3D0%24%2C%20the%20last%20two%20sample%20only%20zeros%3B%20the%20first%20two%20retain%20the%20oscillation%2C%20with%2020%20and%204%20samples%20per%20cycle.%0A%0A%20%20%20%209.%20For%20exactly%20the%20supplied%20seed%20and%20100%20samples%2C%20the%20SNR%20is%20%2419.2102%24%20dB.%20With%20the%20same%20noise%20draws%20scaled%20to%20standard%20deviations%200.05%20and%200.2%2C%20it%20is%20%2425.2308%24%20and%20%2413.1896%24%20dB.%20The%20nominal%20ensemble%20values%20are%20%2423.0103%24%2C%20%2416.9897%24%20and%20%2410.9691%24%20dB%20for%20standard%20deviations%200.05%2C%200.1%20and%200.2%20respectively.%0A%0A%20%20%20%2010.%20A%20final%20function%20is%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20def%20signal_summary(values)%3A%0A%20%20%20%20%20%20%20%20values%20%3D%20np.asarray(values%2C%20dtype%3Dfloat)%0A%20%20%20%20%20%20%20%20return%20np.mean(values)%2C%20np.ptp(values)%2C%20np.mean(values**2)%2C%20np.sqrt(np.mean(values**2))%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20For%20the%20supplied%20EMG%2C%20%60(mean%2C%20peak-to-peak%2C%20power%2C%20RMS)%60%20is%20approximately%20%60(0.000260131%2C%200.33447%2C%200.000445654%2C%200.0211105)%60.%20After%20mean%20removal%3A%20%60(0%2C%200.33447%2C%200.000445586%2C%200.0211089)%60.%0A%0A%20%20%20%2011.%20Nine%20levels%20remain.%20The%20broad%20bursts%20remain%20visible%2C%20but%20fine%20amplitude%20detail%20is%20lost%3B%20the%20resulting%20coarse%20staircase%20is%20unsuitable%20for%20interpreting%20small%20fluctuations.%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%20-%20%5BFourier%20series%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Ffourier_series.py)%20%E2%80%94%20building%20periodic%20signals%20from%20a%20fundamental%20and%20its%20harmonics.%0A%20%20%20%20-%20%5BFourier%20transform%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Ffourier_transform.py)%20%E2%80%94%20describing%20any%20signal%20by%20its%20frequency%20content.%0A%20%20%20%20-%20%5BData%20filtering%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fdata_filtering.py)%20%E2%80%94%20attenuating%20noise%2C%20and%20why%20differentiation%20makes%20it%20worse.%0A%20%20%20%20-%20%5BResidual%20analysis%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fresidual_analysis.py)%20%E2%80%94%20choosing%20the%20cutoff%20frequency%20of%20a%20filter%20from%20the%20data.%0A%20%20%20%20-%20%5BElectromyography%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Felectromyography.py)%20%E2%80%94%20processing%20the%20kind%20of%20signal%20used%20throughout%20this%20notebook.%0A%20%20%20%20-%20%5BThe%20Scientist%20and%20Engineer%E2%80%99s%20Guide%20to%20Digital%20Signal%20Processing%5D(https%3A%2F%2Fwww.dspguide.com%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%20Video%20lectures%20on%20the%20Internet%0A%0A%20%20%20%20-%20MIT%20OpenCourseWare%2C%20Alan%20Oppenheim%3A%20%5BLecture%2016%3A%20Sampling%5D(https%3A%2F%2Focw.mit.edu%2Fcourses%2Fres-6-007-signals-and-systems-spring-2011%2Fresources%2Flecture-16-sampling%2F).%20Predict%20the%20alias%20before%20each%20demonstration%2C%20then%20connect%20it%20to%20the%20heel-marker%20example.%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-%20Bendat%20JS%2C%20Piersol%20AG%20(2010)%20%5BRandom%20Data%3A%20Analysis%20and%20Measurement%20Procedures%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3DqYSViFRNMlwC).%204th%20Edition.%20John%20Wiley%20%26%20Sons%2C%20Inc.%0A%0A%20%20%20%20-%20Lathi%20BP%20(2009)%20%5BLinear%20Systems%20and%20Signals%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3DJC18PwAACAAJ).%20Oxford%20University%20Press.%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-%20Smith%20SW%20(1997)%20%5BThe%20Scientist%20and%20Engineer's%20Guide%20to%20Digital%20Signal%20Processing%5D(https%3A%2F%2Fwww.dspguide.com%2F).%20California%20Technical%20Pub.%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%2C%20np%2C%20plt)%3A%0A%20%20%20%20_t%20%3D%20np.linspace(-1%2C%201%2C%201001)%0A%20%20%20%20_curves%20%3D%20%5B1%20%2B%200.5*np.cos(_t%20%2B%20np.pi%2F4)%2C%20np.abs(_t)%2C%202*_t**3%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20np.sin(2*np.pi*_t)%20%2B%20np.cos(2*np.pi*_t)%2C%20np.ones_like(_t)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202*np.pi*np.sinc(2*_t)%5D%0A%20%20%20%20_fig%2C%20_axes%20%3D%20plt.subplots(2%2C%203%2C%20figsize%3D(10%2C%205))%0A%20%20%20%20for%20_ax%2C%20_y%2C%20_label%20in%20zip(_axes.flat%2C%20_curves%2C%20'abcdef')%3A%0A%20%20%20%20%20%20%20%20_ax.plot(_t%2C%20_y)%0A%20%20%20%20%20%20%20%20_ax.set(title%3Df'1%7B_label%7D'%2C%20xlabel%3D'Time%20%5Bs%5D')%0A%20%20%20%20%20%20%20%20_ax.grid(alpha%3D0.3)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_tt%20%3D%20np.linspace(-3*np.pi%2C%203*np.pi%2C%201001)%0A%20%20%20%20_triangle%20%3D%201%20-%20np.abs((_tt%20%2B%20np.pi)%20%25%20(2*np.pi)%20-%20np.pi)%2Fnp.pi%0A%20%20%20%20_fig2%2C%20_ax2%20%3D%20plt.subplots(figsize%3D(9%2C%202.5))%0A%20%20%20%20_ax2.plot(_tt%2C%20_triangle)%0A%20%20%20%20_ax2.set(title%3D'Problem%202'%2C%20xlabel%3D'Time%20%5Bs%5D'%2C%20ylabel%3D'Amplitude')%0A%20%20%20%20_fig2.tight_layout()%0A%20%20%20%20_fig3%2C%20_axes3%20%3D%20plt.subplots(2%2C%202%2C%20figsize%3D(9%2C%205))%0A%20%20%20%20_fine%20%3D%20np.linspace(0%2C%201%2C%201001)%0A%20%20%20%20for%20_ax%2C%20_fs%20in%20zip(_axes3.flat%2C%20%5B80%2C%2016%2C%208%2C%204%5D)%3A%0A%20%20%20%20%20%20%20%20_ts%20%3D%20np.arange(_fs)%2F_fs%0A%20%20%20%20%20%20%20%20_ax.plot(_fine%2C%202*np.sin(8*np.pi*_fine)%2C%20alpha%3D0.3)%0A%20%20%20%20%20%20%20%20_ax.stem(_ts%2C%202*np.sin(8*np.pi*_ts))%0A%20%20%20%20%20%20%20%20_ax.set(title%3Df'Problem%208%3A%20%7B_fs%7D%20Hz'%2C%20xlabel%3D'Time%20%5Bs%5D')%0A%20%20%20%20_fig3.tight_layout()%0A%20%20%20%20mo.accordion(%7B'Final%20plots%20%E2%80%94%20Problems%201%2C%202%20and%208'%3A%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.as_html(_fig)%2C%20mo.as_html(_fig2)%2C%20mo.as_html(_fig3)%5D)%7D%2C%20expanded%3DFalse)%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
c6e4b44b951756a69223aaf1b7504c2f