import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Compare%20%60linear%60%20and%20%60circular%60%20options%20for%20finding%20PIV%20correlations.%0A%0A%20%20%20%20This%20tutorial%20is%20inspired%20by%20the%20discussion%20in%20OpenPIV%20Google%20groups%20dated%20by%205th%20of%20July%202024%20(here%20is%20the%20link%3A%20https%3A%2F%2Fgroups.google.com%2Fg%2Fopenpiv-users%2Fc%2FqWvjyzEY-JA%2Fm%2F1FcgN9MeAwAJ).%0A%0A%20%20%20%20The%20underline%20OpenPIV%20function%20-%20the%20one%20that%20actually%20calculates%20PIV%20correlations%20-%20is%20%60pyprocess.fft_correlate_images()%60.%20Among%20others%2C%20it%20has%20two%20arguments%3A%20%60correlation_method%60%20that%20assumes%20the%20values%20of%20either%20%60circular%60%20or%20%60linear%60%20and%20%60normalized_correlation%60%20that%20assumes%20the%20values%20of%20either%20%60True%60%20or%20%60False%60.%20This%20notebook%20explores%20the%20effect%20of%20these%20two%20arguments.%20Note%2C%20that%20there%20already%20exists%20an%20OpenPIV%20tutorial%20notebook%20going%20into%20the%20depth%20of%20the%20difference%20between%20%60linear%60%20and%20%60circular%60%20fft%20methods%20-%20that%20notebook%20is%20called%20%60studying_correlations.ipynb%60.%20In%20turn%2C%20the%20current%20notebook%20aims%20to%20compare%20the%20two%20methods%20on%20a%20real%20dataset.%0A%0A%20%20%20%20Here%20is%20the%20quote%20from%20Dr.%20Liberzon's%20answer%20in%20OpenPIV%20Google%20groups%20describing%20the%20difference%20between%20%60circular%60%20and%20%60linear%60%20fft%20methods%20of%20finding%20PIV%20correlations.%0A%0A%20%20%20%20%3E%20Cross-correlation%20basic%20definition%20we%20use%20is%20called%20'direct%20cross-correlation%2C'%20and%20it%20is%20defined%20in%20the%20space%20domain.%20We%20take%20two%20finite%20signals%2C%20A%20and%20B%2C%20each%20of%20length%20N%20(let's%20say%20a%20single%20row%20of%20intensity%20values%20in%20the%20image)%20and%20shift%20B%20over%20A%20from%20left%20to%20right%20such%20that%20the%20first%20B%20touches%20that%20A%20from%20the%20left%201%20pixel%2C%20so%20the%20shift%20is%20-N%20and%20then%20shifts%20and%20overlaps%20completely%20(zero%20shift)%20and%20to%20the%20right%20to%20the%20last%20pixel%20touches%20A%20-%20%2BN.%20At%20each%20shift%2C%20we%20multiply%20the%20intensity%20of%20overlapped%20regions%20between%20part%20of%20A%20and%20part%20of%20B%20and%20get%20a%20value%20of%20cross-correlation.%20The%20result%20is%20a%20signal%20of%202N-1%20length.%20if%20we%20do%20it%20A%20with%20A%20we%20get%20correlation%20and%20obviously%20the%20highest%20value%20will%20be%20at%20the%20shift%20%3D%200%2C%20i.e.%20complete%20overlap.%20This%20animation%20is%20great%2C%20thanks%20to%20Zhigunov%0A%20%20%20%20https%3A%2F%2Fcommons.wikimedia.org%2Fwiki%2FFile%3ACross_Correlation_Animation.gif%0A%0A%20%20%20%20%3EYou%20can%20find%20this%20algorithm%20in%20OpenPIV%20here%3A%0A%20%20%20%20%60%60%60%0A%20%20%20%20%20%20%20%20elif%20correlation_method%20%3D%3D%20'direct'%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20convolve2d(normalize_intensity(window_a)%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%20normalize_intensity(window_b%5B%3A%3A-1%2C%20%3A%3A-1%2C%20%3A%3A-1%5D)%2C%20'full')%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(%22method%20is%20not%20implemented%22)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%3EAs%20you%20see%2C%20we%20eventually%20dropped%20it%20out%20because%20it%20is%20slower%20than%20the%20FFT-based%20ones.%20Its%20definition%20is%20rather%20clear%20in%20Scipy%20correlate%0A%0A%20%20%20%20%3Ehttps%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.signal.correlate.html%0A%0A%20%20%20%20%3EMaybe%20we%20need%20to%20learn%20from%20Scipy%20how%20to%20choose%20automatically%20which%20method%20is%20faster.%20But%20the%20general%20agreement%20that%20FFT-based%20ones%20are%20faster.%0A%0A%20%20%20%20%3EWhen%20the%20PIV%20community%20moved%20to%20FFT-based%20correlations%2C%20it%20becomes%20clear%20that%20the%20FFT%20based%20cross-correlation%20is%20a%20sort%20of%20%22periodic%22%20or%20%22circular%22%20assumption%2C%20i.e.%20the%20finite%20signal%20of%20length%20N%20is%20presumably%20periodic%20and%20will%20repeat%20itself%20again%20and%20again.%20Sort%20of%20imagine%20one%20interrogation%20window%20repeats%20itself%20over%2032%20pixels%20again%20and%20again.%20In%20this%20case%20the%20circular%20method%2C%20i.e.%20using%20the%20original%20signal%20of%20length%20N%20and%20creating%20a%20result%20of%202N-1%20will%20give%20the%20correct%20result.%20The%20proof%20of%20this%20is%20out%20of%20the%20scope%20of%20this%20answer%2C%20I%20recommend%20the%20book%2C%20but%20you%20can%20find%20it%20here%20also%3A%0A%0A%20%20%20%20%3Ehttp%3A%2F%2Fwww.learnpiv.org%2Ffft%2F%0A%0A%20%20%20%20%3ESo%2C%20to%20adjust%20the%20FFT-based%20correlation%20algorithm%20to%20the%20finite%20length%20non-periodic%20interrogation%20windows%2C%20the%20signal%20processing%20community%20suggests%20to%20pad%20zeros%20of%20at%20least%20length%20N%20on%20each%20side%20of%20the%20window%20-%20this%20means%20that%20the%20basic%20%22period%22%20is%20much%20longer%20and%20the%20inner%20(middle)%20part%20is%20our%20signal%20and%20then%20the%20FFT-based%20will%20correctly%20represent%20the%20shift%20close%20to%20the%20center%20of%20the%20window%2C%20less%20than%201%2F2%20of%20window%20%20size%20and%20will%20tame%20down%20the%20high%20frequencies%20coming%20from%20the%20edges%20of%20the%20window%20(due%20to%20non-periodicity%20of%20these%20finite%20windows).%20So%20linear%20one%20is%20the%20same%20formula%20as%20the%20circular%20one%2C%20but%20with%20zero-padded%20windows%0A%0A%20%20%20%20%3EErich%20Zimmer%20asked%20to%20add%20both%20algorithms%20to%20the%20OpenPIV%20-%20he%20claimed%20that%20in%20some%20cases%20circular%20is%20faster%20(smaller%20windows)%20and%20if%20the%20shift%20is%20not%20too%20large%2C%20then%20maybe%20circular%20can%20be%20even%20more%20accurate.%20In%20the%20original%20OpenPIV%20(in%20Matlab%20and%20Python)%20we%20had%20only%20zero-padded%20version%20-%20as%20Raffel's%20book%20recommends.%0A%0A%20%20%20%20%3EYou%20can%20find%20the%20discussion%20here%3A%0A%20%20%20%20%3Ehttps%3A%2F%2Fgithub.com%2FOpenPIV%2Fopenpiv-python%2Fpull%2F208%0A%0A%20%20%20%20In%20the%20current%20notebook%2C%20we%20are%20going%20to%20use%20a%20difficult%20PIV%20case%20of%20an%20upward%20cap-bubbly%20air-water%20two-phase%20flow%20and%20see%20what%20velocity%20field%20we%20get%20with%20different%20methods.%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%20pathlib%0A%20%20%20%20import%20cv2%20%23%20conda%20install%20opencv%3B%20alternatively%20use%20imread()%20from%20openpiv%20tools%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20openpiv%20import%20tools%2C%20pyprocess%2C%20preprocess%2C%20validation%2C%20filters%2C%20scaling%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20cv2%2C%0A%20%20%20%20%20%20%20%20filters%2C%0A%20%20%20%20%20%20%20%20np%2C%0A%20%20%20%20%20%20%20%20plt%2C%0A%20%20%20%20%20%20%20%20preprocess%2C%0A%20%20%20%20%20%20%20%20pyprocess%2C%0A%20%20%20%20%20%20%20%20scaling%2C%0A%20%20%20%20%20%20%20%20tools%2C%0A%20%20%20%20%20%20%20%20validation%2C%0A%20%20%20%20)%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%20Now%20let's%20write%20a%20function%20that%20does%20basic%20PIV%20processing%20on%20a%20pair%20of%20PIV%20frames.%20The%20function%20will%20calculate%20correlations%20with%20%60pyprocess.fft_correlate_images()%60%2C%20then%20it%20will%20convert%20correlations%20to%20displacements%20with%20%60pyprocess.correlation_to_displacement%60%20and%2C%20finally%2C%20it%20will%20validate%20the%20velocity%20field%20and%20replace%20the%20outliers.%20You%20will%20see%20that%20different%20correlation%20methods%20result%20in%201)%20different%20signal%20to%20noise%20threshold%20values%2C%202)%20the%20range%20of%20values%20in%20which%20one%20can%20change%20the%20treshold%20value%20and%2C%20still%2C%20obtain%20reasonably%20good%20velocity%20field.%0A%0A%20%20%20%20Note%20that%20different%20magnitudes%20of%20signal%20to%20noise%20ratio%20thresholds%20for%20different%20methods%20are%20expected.%20Here%20is%20the%20quote%20from%20Dr.%20Liberzon.%0A%0A%20%20%20%20%3EThere%20is%20no%20single%20value%20that%20can%20be%20used%20to%20threshold%20invalid%20vectors.%20This%20is%20because%20different%20correlation%20methods%20provide%20different%20values%20of%20correlation%20maps%20(the%20result%20of%20cross-correlation%20as%20explained%20in%20the%20following%20answer)%20and%20different%20definitions%3A%20peak%20to%20peak%20or%20peak%20to%20mean.%20Therefore%2C%20changing%20the%20correlation%20method%20or%20the%20S%2FN%20ratio%20definition%20will%20create%20a%20new%20distribution%20of%20values%20for%20a%20given%20image%20pair%20or%20set.%20I%20suggest%20always%20running%20the%20small%20subset%20of%20images%2C%20creating%20a%20histogram%20of%20S2N%20values%2C%20and%20then%20choosing%20a%20threshold%20that%20separates%20less%20and%20more%20suspicious%20vectors.%20BTW%2C%20these%20vectors%20LOOK%20GOOD%20most%20of%20the%20time%2C%20so%20the%20high%20or%20low%20S2N%20is%20not%20yet%20a%20definition%20of%20an%20invalid%20vector.%0A%0A%20%20%20%20%3EI%20recommend%20the%20book%20by%20Raffel.%20It%20contains%20everything%20one%20needs%20to%20know%20about%20PIV%2C%20at%20least%20in%20the%20context%20of%20OpenPIV.%0A%0A%20%20%20%20%3EThis%20quick%20source%20that%20I%20found%20is%20reasonably%20correct%20https%3A%2F%2Farxiv.org%2Fpdf%2F1405.3023%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20cv2%2C%0A%20%20%20%20filters%2C%0A%20%20%20%20np%2C%0A%20%20%20%20plt%2C%0A%20%20%20%20preprocess%2C%0A%20%20%20%20pyprocess%2C%0A%20%20%20%20scaling%2C%0A%20%20%20%20tools%2C%0A%20%20%20%20validation%2C%0A)%3A%0A%20%20%20%20def%20PIVanalysis(dataArray%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20imageShape%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20searchAreaSize%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20overlap%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scalingFactor%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20deltaT%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maskBubble%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nameTXT%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20normalizedCorrelation%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20correlationMethod%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20onImg%20%3D%20None%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s2nThresh%20%3D%200.8%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%3A%0A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20The%20procedure%20is%20adapted%20from%20ensemble_correlation.ipynb%20OpenPIV%20tutorial.%0A%20%20%20%20%20%20%20%20Parameters%3A%20dataArray%20(numpy%20array)%20-%20an%20array%20of%20PIV%20images%20that%20contains%20only%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20one%20PIV%20pair%2C%20its%20structure%20is%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dataArray%20%3D%20np.asarray(%5BframeA%2C%20frameB%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20onImg%20(cv2.imread)%20-%20an%20image%20to%20plot%20the%20velocity%20fields%20on%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20searchAreaSize%20(int)%20-%20is%20the%20size%20of%20the%20interrogation%20window%20on%20frameB%3B%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%20%20%20%20%20%20%20%20%20%20%20%20%20it%20can%20be%20a%20bit%20bigger%2C%20which%2C%20reportedly%2C%20is%20better%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%20%20%20%20%20%20%20%20%20%20%20%20%20for%20PIV%20pair%20with%20high%20dynamic%20range%20(like%20in%20our%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%20%20%20%20%20%20%20%20%20%20%20%20%20case%2C%20where%20velocities%20in%20the%20bubble%20wake%20are%20much%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%20%20%20%20%20%20%20%20%20%20%20%20%20bigger%20than%20velocities%20closer%20to%20the%20walls%20of%20the%20test%20section%20-%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%20%20%20%20%20%20%20%20%20%20%20%20%20the%20range%20of%20velocities%20(i.e.%2C%20dynamic%20range)%20is%20big)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maskBubble%20(numpy%20array)%20-%20a%20binary%20image%20where%20bubbles%20are%20white%20and%20background%20is%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20black%3B%20is%20used%20to%20mask%20out%20bubbles%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20deltaT%20(float)%20-%20s%2C%20the%20time%20between%20the%20two%20PIV%20frames%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scalingFactor%20(float)%20-%20pix%2Fmm%2C%20scaling%20factor%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nameTXT%20(str)%20-%20.txt%2C%20the%20name%20of%20the%20file%20to%20save%20the%20resultant%20OpenPIV%20velocity%20field%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20normalizedCorrelation%20(bool)%20-%20can%20be%20True%20or%20False%2C%20this%20is%20a%20parameter%20in%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pyprocess.fft_correlate_images()%20function%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20correlationMethod%20(str)%20-%20can%20be%20%22circular%22%20or%20%22linear%22%2C%20this%20is%20a%20parameter%20in%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pyprocess.fft_correlate_images()%20function%0A%20%20%20%20%20%20%20%20Returns%3A%20None%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%20Mask%20out%20the%20bubbles.%0A%20%20%20%20%20%20%20%20dataArray%5B0%5D%20%3D%20cv2.bitwise_and(dataArray%5B0%5D%2C%20dataArray%5B0%5D%2C%20mask%3Dcv2.bitwise_not(maskBubble))%0A%20%20%20%20%20%20%20%20dataArray%5B1%5D%20%3D%20cv2.bitwise_and(dataArray%5B1%5D%2C%20dataArray%5B1%5D%2C%20mask%3Dcv2.bitwise_not(maskBubble))%0A%0A%20%20%20%20%20%20%20%20%23%20Find%20the%20correlations%20map.%0A%20%20%20%20%20%20%20%20%23%20%22linear%22%20correlation_method%20together%20with%20normalized_correlation%3DTrue%0A%20%20%20%20%20%20%20%20%23%20helps%20to%20boost%20the%20s2n%20threshold%20from%201.003%20to%202%20for%2095%25%20valid%20vectors.%0A%20%20%20%20%20%20%20%20corrs%20%3D%20pyprocess.fft_correlate_images(%0A%20%20%20%20%20%20%20%20%20%20%20%20pyprocess.moving_window_array(dataArray%5B0%5D%2CsearchAreaSize%2Coverlap)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20pyprocess.moving_window_array(dataArray%5B1%5D%2CsearchAreaSize%2Coverlap)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20normalized_correlation%3DnormalizedCorrelation%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20correlation_method%3DcorrelationMethod%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20mesh%20%3D%20pyprocess.get_field_shape(imageShape%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%20%20%20%20%20%20%20%20%20%20%20search_area_size%20%3D%20searchAreaSize%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%20%20%20%20%20%20%20%20%20%20%20overlap%20%3D%20overlap%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20nrows%2C%20ncols%20%3D%20mesh%5B0%5D%2C%20mesh%5B1%5D%0A%0A%20%20%20%20%20%20%20%20%23%20Having%20found%20correlations%2C%20we%20can%20find%20corresponding%20displacements%20in%20pix.%0A%20%20%20%20%20%20%20%20%23%20IMPOTANT%3A%20OpenPIV%20uses%20u%20and%20v%20for%20displacements%20as%20well%20as%20for%20velocities%20in%0A%20%20%20%20%20%20%20%20%23%20Since%20that's%20confusing%2C%20we%20are%20going%20to%20use%20xDisp%20and%20yDisp%20for%20%0A%20%20%20%20%20%20%20%20%23%20displacements%20and%20u%20and%20v%20for%20velocities.%0A%20%20%20%20%20%20%20%20xDisp%2C%20yDisp%20%3D%20pyprocess.correlation_to_displacement(corrs%2C%20nrows%2C%20ncols)%0A%0A%20%20%20%20%20%20%20%20%23%20Having%20found%20displacements%20in%20pix%2C%20we%20can%20find%20corresponding%20velocities%20in%0A%20%20%20%20%20%20%20%20%23%20pix%2Ftime%0A%20%20%20%20%20%20%20%20u%20%3D%20xDisp%2FdeltaT%20%23%20pix%2Fs%0A%20%20%20%20%20%20%20%20v%20%3D%20yDisp%2FdeltaT%20%23%20pix%2Fs%0A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20pyprocess.get_coordinates(imageShape%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%20%20%20%20%20%20%20%20%20%20%20search_area_size%20%3D%20searchAreaSize%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%20%20%20%20%20%20%20%20%20%20%20overlap%20%3D%20overlap%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20%23%20Mask%20out%20the%20areas%20without%20the%20flow%20right%20now%20(bubbles).%0A%20%20%20%20%20%20%20%20grid_mask%20%3D%20preprocess.prepare_mask_on_grid(x%2C%20y%2C%20maskBubble%5B%3A%2C%3A%3A-1%5D)%0A%20%20%20%20%20%20%20%20masked_u%20%3D%20np.ma.masked_array(u%2C%20mask%3Dgrid_mask)%0A%20%20%20%20%20%20%20%20masked_v%20%3D%20np.ma.masked_array(v%2C%20mask%3Dgrid_mask)%0A%0A%20%20%20%20%20%20%20%20%23%20Now%20let's%20do%20validation.%20Pay%20attention%2C%20that%20validation%20doesn't%20mean%20automatic%0A%20%20%20%20%20%20%20%20%23%20replacement%20of%20the%20outliers%2C%20it%20means%20that%20we%20are%20going%20to%20detect%20the%20outliers%0A%20%20%20%20%20%20%20%20%23%20and%20mark%20them%20as%20outliers.%20Their%20replacement%20is%20a%20separate%20procedure%20and%20will%0A%20%20%20%20%20%20%20%20%23%20be%20done%20next.%0A%0A%20%20%20%20%20%20%20%20%23%20Do%20the%20validation%20of%20the%20correlation%20peaks.%0A%20%20%20%20%20%20%20%20corrs%20%3D%20corrs.astype('float64')%0A%20%20%20%20%20%20%20%20s2n%20%3D%20pyprocess.sig2noise_ratio(corrs%2C%20%22peak2mean%22)%0A%20%20%20%20%20%20%20%20invalid_mask_s2n%20%3D%20validation.sig2noise_val(s2n%2C%20threshold%20%3D%20s2nThresh)%0A%0A%20%20%20%20%20%20%20%20%23%20Now%20we%20can%20replace%20outliers%20flagged%20by%20the%20invalid_mask_s2n.%0A%20%20%20%20%20%20%20%20masked_u%2C%20masked_v%20%3D%20filters.replace_outliers(masked_u.flatten()%2C%20masked_v.flatten()%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20invalid_mask_s2n%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20method%20%3D%20'localmean'%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_iter%20%3D%203%2C%20%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20kernel_size%20%3D%2017%20%0A%20%20%20%20%20%20%20%20)%20%23%20IMPORTANT%20to%20flatten%20u%20and%20v%0A%0A%20%20%20%20%20%20%20%20x%2C%20y%2C%20masked_u%2C%20masked_v%20%3D%20scaling.uniform(x%2C%20y%2C%20masked_u%2C%20masked_v%2C%20scaling_factor%20%3D%20scalingFactor)%0A%0A%20%20%20%20%20%20%20%20%23%20MASK%20OUT%20VELOCITY%20FIELD.%0A%20%20%20%20%20%20%20%20%23%20Before%20saving%20the%20field%20to%20a%20.txt%20file%2C%20give%20zeros%20to%20those%20vectors%20that%20lie%20in%20the%20masked%20regions.%0A%20%20%20%20%20%20%20%20%23%20Right%20now%2C%20x%20and%20y%20are%20in%20the%20image%20system%20of%20coordinates%3A%20x%20is%20to%20the%20right%2C%20y%20is%20downwards%2C%20(0%2C0)%0A%20%20%20%20%20%20%20%20%23%20is%20in%20the%20top%20left%20corner.%20It%20can%20be%20learnt%20from%20the%20GitHub%20code%20of%20tools.transform_coordinates.%0A%20%20%20%20%20%20%20%20%23%20Since%20our%20x%20and%20y%20are%20in%20mm%2C%20we're%20going%20to%20use%20scaling%20factor%20to%20convert%20them%20to%20pix.%20Then%20we're%20going%0A%20%20%20%20%20%20%20%20%23%20to%20use%20them%20to%20identify%20whether%20or%20not%20their%20place%20on%20an%20example%20masked%20image%20is%20masked.%0A%20%20%20%20%20%20%20%20xFlat%20%3D%20x.flatten()%0A%20%20%20%20%20%20%20%20yFlat%20%3D%20y.flatten()%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(xFlat.size)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20maskBubble%5Bint(yFlat%5Bi%5D*scalingFactor)%2C%20int(xFlat%5Bi%5D*scalingFactor)%5D%20%3D%3D%20255%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20masked_u%5Bi%5D%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20masked_v%5Bi%5D%20%3D%200%0A%0A%20%20%20%20%20%20%20%20x%2C%20y%2C%20masked_u%2C%20masked_v%20%3D%20tools.transform_coordinates(x%2C%20y%2C%20masked_u%2C%20masked_v)%0A%0A%20%20%20%20%20%20%20%20tools.save(nameTXT%2C%20x%2C%20y%2C%20masked_u%2C%20masked_v%2C%20invalid_mask_s2n)%0A%0A%0A%20%20%20%20%20%20%20%20%23%20Calculate%20the%20percentage%20of%20the%20invalid%20vectors%20due%20to%20sig2noise%20validation.%0A%20%20%20%20%20%20%20%20invSnrOutPer%20%3D%20(np.count_nonzero(invalid_mask_s2n)%20%2F%20invalid_mask_s2n.size)%20*%20100%20%23%20percent%20of%20the%20invalid%20vectors%0A%20%20%20%20%20%20%20%20print(%22%5CnPercentage%20of%20invalid%20vectors%20due%20to%20signal%20to%20noise%20ratio%20validation%3A%20%7B%3A.2f%7D.%22.format(invSnrOutPer))%0A%20%20%20%20%20%20%20%20print(%22%5CnThe%20resultant%20velocity%20field%20(red%20are%20vectors%20that%20were%20validated%20as%20outliers%5Cnand%20replaced%20with%20the%20average%20of%20their%20neighbouring%20vectors)%3A%22)%0A%20%20%20%20%20%20%20%20figure%2C%20_%20%3D%20tools.display_vector_field(filename%20%3D%20str(nameTXT)%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scaling_factor%20%3D%20scalingFactor%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scale%20%3D%203500%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20on_img%20%3D%20True%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20show_invalid%20%3D%20True%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20image_name%20%3D%20onImg%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20plt.show()%0A%0A%20%20%20%20return%20(PIVanalysis%2C)%0A%0A%0A%40app.cell%0Adef%20_(cv2%2C%20np)%3A%0A%20%20%20%20%23%20Global%20variables%0A%20%20%20%20tPulse%20%3D%200.0015%20%23%20s%20-%20time%20between%20frame%20A%20and%20frame%20B%0A%20%20%20%20ol%20%3D%2016%20%23%20overlap%3A%20give%20it%20in%20pix%2C%20not%20%25%0A%20%20%20%20interW%20%3D%2032%20%23%20the%20size%20of%20the%20interrogation%20window%0A%20%20%20%20searchAS%20%3D%20np.round(1.1*interW).astype(np.uint32)%20%23%20the%20size%20of%20the%20interrogation%20window%20in%20frameB%20%0A%20%20%20%20sf%20%3D%201%2F0.052%20%23%20pix%2Fmm%20scaling%20factor%0A%0A%20%20%20%20%23%20The%20name%20of%20the%20OpenPIV%20txt%20file%20to%20save%20the%20results%20to.%0A%20%20%20%20fileTXT%20%3D%20'..%2Ftest21%2FbubblesLinearCircular.txt'%0A%0A%20%20%20%20%23%20The%20file%20names%20of%20PIV%20frames%20and%20bubbles%20mask.%0A%20%20%20%20frameA%20%3D%20'..%2Ftest21%2FbubblesFrameA.png'%0A%20%20%20%20frameB%20%3D%20'..%2Ftest21%2FbubblesFrameB.png'%0A%20%20%20%20bubblesMaskFile%20%3D%20'..%2Ftest21%2FbubblesMask.png'%0A%0A%20%20%20%20pairPIV%20%3D%20np.asarray(%5Bcv2.imread(frameA%2C0)%2C%20cv2.imread(frameB%2C0)%5D)%20%23%200%20flag%20means%20read%20as%20gray%20scale%0A%20%20%20%20bubblesMask%20%3D%20cv2.imread(bubblesMaskFile%2C0)%20%23%200%20flag%20means%20read%20as%20gray%20scale%0A%20%20%20%20imageShape%20%3D%20pairPIV%5B0%5D.shape%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20bubblesMask%2C%0A%20%20%20%20%20%20%20%20bubblesMaskFile%2C%0A%20%20%20%20%20%20%20%20fileTXT%2C%0A%20%20%20%20%20%20%20%20imageShape%2C%0A%20%20%20%20%20%20%20%20ol%2C%0A%20%20%20%20%20%20%20%20pairPIV%2C%0A%20%20%20%20%20%20%20%20searchAS%2C%0A%20%20%20%20%20%20%20%20sf%2C%0A%20%20%20%20%20%20%20%20tPulse%2C%0A%20%20%20%20)%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%20Let's%20start%20with%20**linear%20normalized%20correlation**.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20PIVanalysis%2C%0A%20%20%20%20bubblesMask%2C%0A%20%20%20%20bubblesMaskFile%2C%0A%20%20%20%20fileTXT%2C%0A%20%20%20%20imageShape%2C%0A%20%20%20%20ol%2C%0A%20%20%20%20pairPIV%2C%0A%20%20%20%20searchAS%2C%0A%20%20%20%20sf%2C%0A%20%20%20%20tPulse%2C%0A)%3A%0A%20%20%20%20%23%20According%20to%20the%20comments%20given%20in%20the%20body%20of%20pyprocess.fft_correlate_images()%20function%2C%0A%20%20%20%20%23%20linear%20correlation%20must%20be%20normalized.%20So%2C%20we%20are%20going%20to%20explore%20only%20linear%20normalized%20option.%0A%20%20%20%20%23%20I.e.%2C%20we%20are%20not%20going%20to%20do%20linear%20not%20normalized%20option.%0A%20%20%20%20print(%22linear%20correlation%2C%20normalized%3A%5Cn%22)%0A%20%20%20%20PIVanalysis(dataArray%20%3D%20pairPIV%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20imageShape%20%3D%20imageShape%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20searchAreaSize%20%3D%20searchAS%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20overlap%20%3D%20ol%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scalingFactor%20%3D%20sf%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20deltaT%20%3D%20tPulse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maskBubble%20%3D%20bubblesMask%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nameTXT%20%3D%20fileTXT%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20normalizedCorrelation%20%3D%20True%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20correlationMethod%20%3D%20'linear'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s2nThresh%20%3D%202%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20onImg%20%3D%20bubblesMaskFile%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20)%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%20You%20can%20notice%20that%2C%20in%20the%20case%20of%20linear%20normalized%20correlation%20method%2C%20we%20are%20dealing%20with%20relatively%20high%20values%20of%20signal%20to%20noice%20ratios%20because%20the%20signal%20to%20noise%20threshold%20we%20used%20was%20s2nThresh%20%3D%202.%20That's%20a%20pretty%20good%20threshold.%20I.e.%2C%20linear%20correlation%20method%20produces%20nice%20correlations%20and%20we%20can%20have%20a%20lot%20of%20correlations%20peak%20at%20least%202%20times%20bigger%20than%20their%20respective%20averages.%20Go%20ahead%20and%20play%20with%20the%20s2nThresh%20parameter%20above.%20You%20will%20see%20that%20you%20can%20go%20up%20to%20s2nThresh%20%3D%205%20and%2C%20still%2C%20get%20the%20visually%20correct%20velocity%20field.%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%20Now%2C%20let's%20do%20**circular%20not%20normalized%20correlation**.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20PIVanalysis%2C%0A%20%20%20%20bubblesMask%2C%0A%20%20%20%20bubblesMaskFile%2C%0A%20%20%20%20fileTXT%2C%0A%20%20%20%20imageShape%2C%0A%20%20%20%20ol%2C%0A%20%20%20%20pairPIV%2C%0A%20%20%20%20searchAS%2C%0A%20%20%20%20sf%2C%0A%20%20%20%20tPulse%2C%0A)%3A%0A%20%20%20%20print(%22circular%20correlation%2C%20not%20normalized%3A%5Cn%22)%0A%20%20%20%20PIVanalysis(dataArray%20%3D%20pairPIV%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20imageShape%20%3D%20imageShape%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20searchAreaSize%20%3D%20searchAS%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20overlap%20%3D%20ol%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scalingFactor%20%3D%20sf%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20deltaT%20%3D%20tPulse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maskBubble%20%3D%20bubblesMask%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nameTXT%20%3D%20fileTXT%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20normalizedCorrelation%20%3D%20False%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20correlationMethod%20%3D%20'circular'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s2nThresh%20%3D%201.05%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20onImg%20%3D%20bubblesMaskFile%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20)%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%20You%20can%20notice%20that%2C%20in%20the%20case%20of%20circular%20not%20normalized%20correlation%20method%2C%20we%20are%20dealing%20with%20relatively%20low%20values%20of%20signal%20to%20noice%20ratios%20because%20the%20signal%20to%20noise%20threshold%20we%20used%20was%20s2nThresh%20%3D%201.05.%20That's%20a%20rather%20bad%20threshold.%20I.e.%2C%20we%20keep%20the%20correlation%20peaks%20which%20are%20only%205%25%20larger%20than%20their%20respective%20averages.%20That's%20not%20a%20good%20threshold%20to%20distinguish%20valid%20signal%20from%20noise.%20Go%20ahead%20play%20with%20the%20parameter%20s2nThresh%20and%20you%20will%20see%20that%20you%20can't%20go%20above%20s2nThresh%20%3D%201.11%20(where%20the%20velocity%20field%20starts%20looking%20unphysical).%20In%20other%20words%2C%20circular%20not%20normalized%20method%20produces%20correlations%20with%20peaks%20barealy%20distingyishable%20from%20the%20noise%20of%20the%20background.%20I.e.%2C%20this%20method%20doesn't%20give%20us%20a%20good%20signal.%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%20Now%20let's%20try%20**circular%20normalized%20method**.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20PIVanalysis%2C%0A%20%20%20%20bubblesMask%2C%0A%20%20%20%20bubblesMaskFile%2C%0A%20%20%20%20fileTXT%2C%0A%20%20%20%20imageShape%2C%0A%20%20%20%20ol%2C%0A%20%20%20%20pairPIV%2C%0A%20%20%20%20searchAS%2C%0A%20%20%20%20sf%2C%0A%20%20%20%20tPulse%2C%0A)%3A%0A%20%20%20%20print(%22circular%20correlation%2C%20normalized%3A%5Cn%22)%0A%20%20%20%20PIVanalysis(dataArray%20%3D%20pairPIV%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20imageShape%20%3D%20imageShape%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20searchAreaSize%20%3D%20searchAS%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20overlap%20%3D%20ol%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scalingFactor%20%3D%20sf%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20deltaT%20%3D%20tPulse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maskBubble%20%3D%20bubblesMask%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nameTXT%20%3D%20fileTXT%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20normalizedCorrelation%20%3D%20True%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20correlationMethod%20%3D%20'circular'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s2nThresh%20%3D%202%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20onImg%20%3D%20bubblesMaskFile%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20)%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%20You%20can%20notice%20that%2C%20in%20the%20case%20of%20circular%20normalized%20correlation%20method%2C%20we%20are%20dealing%20with%20large%20values%20of%20signal%20to%20noice%20ratios%20because%20the%20signal%20to%20noise%20threshold%20we%20used%20was%20s2nThresh%20%3D%202.%20That's%20a%20good%20threshold.%20I.e.%2C%20we%20keep%20the%20correlation%20peaks%20which%20are%2020%25%20larger%20than%20their%20respective%20averages.%20This%20is%20very%20nice%3A%20if%20the%20peak%20is%2020%25%20larger%20than%20the%20background%20noise%2C%20we%20have%20a%20valid%20signal%20indeed!%20Go%20ahead%20play%20with%20the%20parameter%20s2nThresh%20and%20you%20will%20see%20that%20you%20can't%20go%20above%20s2nThresh%20%3D%203%20(where%20the%20velocity%20field%20starts%20looking%20unphysical).%20Compare%20that%20to%20linear%20normalized%20method%20above%20where%20we%20could%20go%20up%20to%20s2nThresh%20%3D%205.%20In%20other%20words%2C%20circular%20normalized%20method%20produces%20correlations%20with%20peaks%20easily%20distingyishable%20from%20the%20noise%20of%20the%20background.%20I.e.%2C%20this%20method%20gives%20us%20a%20good%20signal.%20But%20not%20as%20good%20as%20the%20signal%20we%20could%20obtain%20from%20linear%20normalized%20method%20where%20we%20could%20assign%20a%20threshold%20of%20s2nThresh%20%3D%205%20and%2C%20still%2C%20get%20a%20legible%20velocity%20field.%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**Conclusion**%3A%20linear%20normalized%20method%20is%20the%20winner.%20It%20gives%20the%20best%20PIV%20correlation%20signal%20which%20can%20be%20easily%20thresholded%20to%20get%20rid%20of%20outliers.%20It%20makes%20sense%2C%20because%20-%20as%20was%20discussed%20at%20the%20beginning%20of%20this%20notebook%20-%20linear%20normalized%20method%20was%20specifically%20developed%20for%20general%20PIV%20applications.%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%20Final%20note.%0A%0A%20%20%20%20As%20it%20was%20discussed%20at%20the%20beginning%20of%20this%20notebook%2C%20the%20only%20difference%20between%20the%20formulas%20for%20%60linear%60%20and%20%60circular%60%20fft%20correlation%20methods%20is%20that%20the%20former%20has%20zero%20padding.%0A%0A%20%20%20%20Dr.%20Liberzon%20asked%20ChatGPT%20to%20explain%20the%20importance%20of%20zero%20padding%20and%20create%20a%20little%20python%20example%20using%20OpenPIV.%20Below%20is%20what%20Dr.%20Liberzon%20received%20in%20response.%0A%0A%20%20%20%20*Note%20that%20ChatGPT%20confirms%20our%20conclusions%3A%20linear%20(and%20normalized)%20method%20produces%20the%20best%20signal%20where%20the%20peaks%20can%20be%20identified%20in%20the%20easiest%20manner.*%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%20Zero-padding%20is%20a%20common%20technique%20used%20before%20performing%20FFT-based%20cross-correlation%20to%20improve%20the%20accuracy%20and%20interpretability%20of%20the%20results.%20Here's%20an%20explanation%20of%20why%20zero-padding%20is%20important%3A%0A%0A%20%20%20%20%23%23%23%20Purpose%20of%20Zero-Padding%0A%0A%20%20%20%201.%20**Increase%20Resolution%20in%20the%20Frequency%20Domain**%3A%0A%20%20%20%20%20%20%20-%20**Interpolation**%3A%20Zero-padding%20the%20signal%20before%20taking%20the%20FFT%20increases%20the%20number%20of%20points%20in%20the%20FFT%20result%2C%20effectively%20interpolating%20the%20frequency%20spectrum.%20This%20helps%20achieve%20finer%20resolution%20in%20the%20frequency%20domain%2C%20making%20it%20easier%20to%20identify%20the%20peak%20correlation%20more%20accurately.%0A%20%20%20%20%20%20%20-%20**More%20Accurate%20Peak%20Detection**%3A%20With%20higher%20resolution%2C%20the%20position%20of%20the%20peak%20in%20the%20cross-correlation%20function%20can%20be%20determined%20more%20precisely%2C%20which%20is%20particularly%20important%20for%20accurate%20time%20delay%20estimation.%0A%0A%20%20%20%202.%20**Avoid%20Circular%20Convolution%20Artifacts**%3A%0A%20%20%20%20%20%20%20-%20**Linear%20Convolution**%3A%20In%20FFT-based%20cross-correlation%2C%20the%20use%20of%20FFT%20inherently%20assumes%20circular%20convolution.%20Zero-padding%20helps%20to%20mitigate%20the%20circular%20convolution%20artifacts%2C%20making%20the%20result%20more%20representative%20of%20the%20true%20linear%20convolution.%20This%20is%20because%20zero-padding%20effectively%20separates%20the%20instances%20of%20the%20signals%2C%20avoiding%20overlap%20that%20can%20cause%20spurious%20correlations.%0A%20%20%20%20%20%20%20-%20**Signal%20Separation**%3A%20By%20padding%20with%20zeros%2C%20the%20effective%20length%20of%20the%20signals%20is%20increased%2C%20ensuring%20that%20the%20end%20of%20one%20signal%20does%20not%20interfere%20with%20the%20start%20of%20the%20other%2C%20which%20can%20distort%20the%20cross-correlation%20result.%0A%0A%20%20%20%203.%20**Handling%20Boundary%20Effects**%3A%0A%20%20%20%20%20%20%20-%20**Reducing%20Edge%20Effects**%3A%20Zero-padding%20helps%20to%20reduce%20edge%20effects%20by%20adding%20extra%20space%20around%20the%20signal.%20This%20ensures%20that%20the%20FFT%20does%20not%20wrap%20around%20the%20edges%20of%20the%20signal%2C%20which%20can%20introduce%20artifacts%20and%20distortions%20in%20the%20cross-correlation%20computation.%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%20Demonstration%20with%20OpenPIV-Python%0A%0A%20%20%20%20Let's%20demonstrate%20the%20effect%20of%20zero-padding%20in%20FFT-based%20cross-correlation%20using%20OpenPIV-Python.%20We'll%20perform%20cross-correlation%20on%20a%20pair%20of%20signals%20with%20and%20without%20zero-padding.%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%20from%20scipy.signal%20import%20correlate%0A%20%20%20%20N%20%3D%20100%0A%20%20%20%20t%20%3D%20np.linspace(0%2C%201%2C%20N%2C%20endpoint%3DFalse)%0A%20%20%20%20f%20%3D%20np.sin(2%20*%20np.pi%20*%20t)%0A%20%20%20%20%23%20Create%20example%20signals%0A%20%20%20%20g%20%3D%20np.sin(2%20*%20np.pi%20*%20t%20%2B%20np.pi%20%2F%204)%20%20%23%20Number%20of%20samples%0A%20%20%20%20linear_corr%20%3D%20correlate(f%2C%20g%2C%20mode%3D'full')%0A%20%20%20%20padding_length%20%3D%20N%20%20%23%20Sine%20wave%0A%20%20%20%20f_padded%20%3D%20np.pad(f%2C%20(0%2C%20padding_length)%2C%20'constant')%20%20%23%20Phase-shifted%20sine%20wave%0A%20%20%20%20g_padded%20%3D%20np.pad(g%2C%20(0%2C%20padding_length)%2C%20'constant')%0A%20%20%20%20%23%20Compute%20cross-correlation%20without%20zero-padding%20the%20%22old-school%20way%22.%0A%20%20%20%20%23%20The%20function%20correlate()%20below%20doesn't%20use%20fft%20to%20calculate%20correlations.%20It%20was%20the%0A%20%20%20%20%23%20original%20function%20used%20in%20OpenPIV.%20It%20was%20deemed%20slow%20and%20was%20abandoned%20and%20replaced%20with%0A%20%20%20%20%23%20fft-based%20function.%20Still%2C%20it%20doesn't%20do%20any%20padding%20and%20is%20a%20good%20example%20of%20how%20%0A%20%20%20%20%23%20to%20work%20with%20it.%0A%20%20%20%20f_fft%20%3D%20np.fft.fft(f_padded)%0A%20%20%20%20g_fft%20%3D%20np.fft.fft(g_padded)%0A%20%20%20%20%23%20Zero-pad%20the%20signals%0A%20%20%20%20circular_corr%20%3D%20np.fft.ifft(f_fft%20*%20np.conj(g_fft)).real%0A%20%20%20%20lags_linear%20%3D%20np.arange(-N%20%2B%201%2C%20N)%0A%20%20%20%20lags_circular%20%3D%20np.arange(-N%2C%20N)%0A%20%20%20%20plt.figure(figsize%3D(12%2C%208))%0A%20%20%20%20%23%20Compute%20FFT-based%20cross-correlation%20with%20zero-padding.%20Note%20that%20we%20are%20not%20using%20%0A%20%20%20%20%23%20OpenPIV%20native%20fft%20function.%20But%20np.fft.ifft()%20function%20used%20below%20is%20at%20the%20core%0A%20%20%20%20%23%20of%20OpenPIV%20implementation%20of%20fft%20correlations.%20OpenPIV%20implementation%20is%20much%20more%0A%20%20%20%20%23%20difficult.%20So%2C%20this%20is%20a%20nice%20example%20convaying%20the%20core%20idea%20without%20additional%0A%20%20%20%20%23%20complications.%0A%20%20%20%20plt.subplot(2%2C%201%2C%201)%0A%20%20%20%20plt.plot(lags_linear%2C%20linear_corr)%0A%20%20%20%20plt.title('Cross-correlation%20without%20zero-padding')%0A%20%20%20%20plt.xlabel('Lag')%0A%20%20%20%20%23%20Generate%20lag%20arrays%0A%20%20%20%20plt.ylabel('Correlation')%0A%20%20%20%20plt.grid()%0A%20%20%20%20plt.subplot(2%2C%201%2C%202)%0A%20%20%20%20%23%20Plot%20the%20results%0A%20%20%20%20plt.plot(lags_circular%2C%20circular_corr)%0A%20%20%20%20plt.title('FFT-based%20cross-correlation%20with%20zero-padding%20(i.e.%2C%20linear%20cross-correlation)')%0A%20%20%20%20plt.xlabel('Lag')%0A%20%20%20%20plt.ylabel('Correlation')%0A%20%20%20%20plt.grid()%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%20%23%23%23%20Observations%0A%20%20%20%20-%20**A%20note%20to%20ChatGPT's%20observastions%20from%20the%20author%20of%20this%20notebook**%3A%20Look%20at%20the%20plots%20above%3A%20the%20signal%20at%20zero%20is%20much%20better%20when%20classical%20correlation()%20function%20was%20used.%20FFT%20has%20inferior%20signal%20at%20zero%20and%20%22artifacts%22%20at%20the%20edges.%20So%20in%20this%20example%2C%20the%20classical%20correlation()%20function%20is%20superior.%20But%20since%20FFT%20is%20faster%2C%20we%20use%20it%20nonetheless.%20Also%20pay%20attention%20that%20OpenPIV's%20implementation%20of%20fft%20correlations%20is%20much%20more%20intricate%20and%20better%20tested%20than%20this%20simplistic%20little%20ChatGPT%20example.%20The%20following%20is%20what%20we%20were%20supposed%20to%20conclude...%0A%20%20%20%20-%20**Without%20Zero-Padding**%3A%20The%20linear%20cross-correlation%20without%20zero-padding%20might%20have%20lower%20resolution%20and%20could%20suffer%20from%20edge%20effects%2C%20making%20it%20harder%20to%20accurately%20determine%20the%20peak%20correlation.%0A%20%20%20%20-%20**With%20Zero-Padding**%3A%20The%20FFT-based%20cross-correlation%20with%20zero-padding%20provides%20higher%20resolution%20and%20mitigates%20circular%20convolution%20artifacts%2C%20leading%20to%20more%20accurate%20and%20interpretable%20results.%0A%0A%20%20%20%20%23%23%23%20Conclusion%0A%20%20%20%20Zero-padding%20before%20FFT-based%20cross-correlation%20is%20crucial%20for%20achieving%20higher%20resolution%20in%20the%20frequency%20domain%2C%20avoiding%20artifacts%20from%20circular%20convolution%2C%20and%20reducing%20boundary%20effects.%20This%20practice%20ensures%20more%20accurate%20and%20reliable%20cross-correlation%20results%2C%20which%20is%20particularly%20important%20in%20applications%20requiring%20precise%20time%20delay%20estimation%20and%20signal%20analysis.%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
af34273fe5ccf236a33a22bfb60d9655