Physics 114: Lecture 18 Least Squares Fit to 2D Data

Slides:



Advertisements
Similar presentations
Introduction: Correlation and Regression The General Linear Model is a phrase used to indicate a class of statistical models which include simple linear.
Advertisements

Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Physics 114: Lecture 19 Least Squares Fit to 2D Data Dale E. Gary NJIT Physics Department.
Kevin Kelly Mentor: Peter Revesz.  Importance of Project: Beam stability is crucial in CHESS, down to micron-level precision  The beam position is measured.
Introduction: The General Linear Model b b The General Linear Model is a phrase used to indicate a class of statistical models which include simple linear.
Uncertainty Representation. Gaussian Distribution variance Standard deviation.
Curve-Fitting Regression
Principles of the Global Positioning System Lecture 10 Prof. Thomas Herring Room A;
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
Physics 114: Lecture 17 Least Squares Fit to Polynomial
Colorado Center for Astrodynamics Research The University of Colorado STATISTICAL ORBIT DETERMINATION Project Report Unscented kalman Filter Information.
Physics 114: Lecture 15 Probability Tests & Linear Fitting Dale E. Gary NJIT Physics Department.
STATISTICS: BASICS Aswath Damodaran 1. 2 The role of statistics Aswath Damodaran 2  When you are given lots of data, and especially when that data is.
Analytical vs. Numerical Minimization Each experimental data point, l, has an error, ε l, associated with it ‣ Difference between the experimentally measured.
Physics 114: Exam 2 Review Lectures 11-16
MA/CS 3751 Fall 2002 Lecture 24. MA/CS 3752 ginput ginput is a Matlab function which takes one argument input: number of points to select in the image.
Learning to perceive how hand-written digits were drawn Geoffrey Hinton Canadian Institute for Advanced Research and University of Toronto.
Physics 114: Lecture 18 Least Squares Fit to Arbitrary Functions Dale E. Gary NJIT Physics Department.
Kylie Gorman WEEK 1-2 REVIEW. CONVERTING AN IMAGE FROM RGB TO HSV AND DISPLAY CHANNELS.
WHOLE BRAIN THINKING HERRMANN INTERNATIONAL
Physics 114: Lecture 8 Measuring Noise in Real Data Dale E. Gary NJIT Physics Department.
SalsaJ (Such a Lovely Software for Astronomy) Practical Session.
April 21, 2016Introduction to Artificial Intelligence Lecture 22: Computer Vision II 1 Canny Edge Detector The Canny edge detector is a good approximation.
List manipulation;curve fitting
Physics 114: Lecture 16 Least Squares Fit to Arbitrary Functions
John Federici NJIT Physics Department
The Maximum Likelihood Method
Physics 114: Lecture 13 Probability Tests & Linear Fitting
John Federici NJIT Physics Department
Modelling and Simulating Social Systems with MATLAB
Data manipulation II: Least Squares Fitting
Linear Algebra Review.
Chapter 7. Classification and Prediction
Statistical Data Analysis - Lecture /04/03
Vocabulary byte - The technical term for 8 bits of data.
Physics 114: Lecture 14 Linear Fitting
Physics 114: Exam 2 Review Weeks 7-9
Physics 114: Lecture 17 More Fitting to Arbitrary Functions
Matlab Training Session 4: Control, Flow and Functions
Physics 114: Final Project Hints
Line Fitting James Hayes.
Physics 114: Lecture 15 Least Squares Fit to Polynomial
The Maximum Likelihood Method
Vocabulary byte - The technical term for 8 bits of data.
OPSE 301: Lab13 Data Analysis – Fitting Data to Arbitrary Functions
User Defined Functions
A segmentation and tracking algorithm
A special case of calibration
Regression and Residual Plots
Physics 114: Lecture 10 Error Analysis/ Propagation of Errors
The Maximum Likelihood Method
Finding Lines in Images
Fitting Curve Models to Edges
Physics 114: Exam 2 Review Material from Weeks 7-11
Today’s Objectives: Students will be able to:
Physics 114: Lecture 14 Linear Fitting
Lesson 16: Functions with Return Values
Vectorized Code, Logical Indexing
Physics 114: Lecture 11 Error Analysis, Part II
John Federici NJIT Physics Department
Quadratic Functions and Models
Nonlinear regression.
Physics 114: Lecture 14-a Linear Fitting Using Matlab
Prediction and Accuracy
Programming Control Structures with JavaScript Part 2
PHYS 408 Applied Optics (Lecture 18)
Calibration and homographies
A Brief Introduction to Power
CMPT 120 Lecture 22 – Unit 4 – Computer Vision
CS5760: Computer Vision Lecture 9: RANSAC Noah Snavely
Presentation transcript:

Physics 114: Lecture 18 Least Squares Fit to 2D Data John F. Federici NJIT Physics Department

Star Wars (retro) Humor Star Wars satire movie – Mel Brooks, Spaceballs (1987) “Luke Skywalker” finds out from “Darth Vader” how they are related. https://www.youtube.com/watch?v=CZd_YyFzPD0

Nonlinear Least Squares Fitting We saw that for a function y(x) the chi-square is which can be used to fit a curve to data points yi. In just the same way, we can easily extend this idea to fitting a function z(x,y), that is, a surface, to a 2D data set of measurements zi(x,y). You know that it takes three parameters to fit a Gaussian curve To extend this to a 2D Gaussian is straightforward. It is: Note that this has 5 parameters, but the most general way to describe a 2D Gaussian actually has 6 parameters, which you will do for homework.

The Big Picture… Why 2D fit? Before we get into details of HOW to do a 2D fit to data, let’s give a few examples of WHY one would want to do a 2D fit to data. EXAMPLE : Optical laser beams typically have a Gaussian profile in cross-section

The Big Picture… Why 2D fit? Before we get into details of HOW to do a 2D fit to data, let’s give a few examples of WHY one would want to do a 2D fit to data. EXAMPLE : Optical laser beams typically have a Gaussian profile in cross-section

The Big Picture… Why 2D fit? Before we get into details of HOW to do a 2D fit to data, let’s give a few examples of WHY one would want to do a 2D fit to data. EXAMPLE : Optical laser beams typically have a Gaussian profile in cross-section

The Big Picture… Why 2D fit? EXAMPLE : Gaussian beam profile can be distorted by atmospheric propagation… applications to free-space optical (wireless) communications. Gaussian beams can be efficiently coupled into fiber optical cables…. Fiber optic communications. Higher order Gaussian modes reduce efficiency of coupling of light into optical fibers.

Plotting a 2D Gaussian in MatLAB Let’s introduce some useful MatLAB commands for creating a 2D Gaussian. One can use a brute force method such as z = zeros(101,101); a = 2.5; bx = 2.3; by = -1.2; cx = 1.5; cy = 0.8; i = 0; for x = -5:0.1:5 i = i + 1; j = 0; for y = -5:0.1:5 j = j + 1; z(i,j) = a*exp(-((x-bx)/cx)^2 – ((y-by)/cy)^2); end imagesc(-5:0.1:5,-5:0.1:5,z) Or, we can plot it as a surface to provide x and y coordinates surf(-5:0.1:5,-5:0.1:5,z)

Better Way of Creating 2D Functions That way of using for loops is a very slow, so here is another way of doing it for “round” Gaussians: x = -5:0.1:5; y = -5:0.1:5; siz = size(x); siz = siz(2); a = 2.5; bx = 2.3; by = -1.2; c = 0.8; dist = zeros(siz,siz); for i=1:siz for j=1:siz dist(i,j) = x(i)^2 + y(j)^2; % Create “distance” array once end % use the next two statements multiple times for different Gaussians sdist = circshift(dist,round([bx/0.1,by/0.1])); % Shifted “distance” array z = a*exp(-sdist/c); imagesc(x,y,z);

Best Way of Creating 2D Functions The best way, however, is to use the meshgrid() MatLAB function: (This was introduced in Lecture 2) >> [x, y] = meshgrid(-5:.1:5, -5:.1:5); >> a = 2.5; bx = 2.3; by = -1.2; cx = 1.5; cy = 0.8; >> z = a*exp( - (((x-bx)/cx).^2 + ((y-by)/cy).^2)); >> surf(x,y,z); This is completely general, and allows vector calculations which are very fast. Here is another example, the sinc() function: z = a*sinc(sqrt(((x-bx)/cx).^2 + ((y-by)/cy).^2)); You can see that the meshgrid() function is very powerful as a shortcut for 2D function calculation. To add noise, just add sig*randn(size(z)), where sig is the standard deviation of the noise.

Examples of Fitting 2D Function Clearly, fitting a Gaussian 2D function to the data will be a NONLINEAR fit. Remember, the concept (Lecture 16) is that one is searching parameter space to find values for the parameters which make Chi-Squared (or R-Squared) a MINIMUM.

Searching Parameter Space in 2D Brute Force Example Just as we did in the 1D case, searching parameter space is just a trial and error exercise. % Initial values around which to search a0 = max(max(z)); [ix iy] = find(z == a0); bx0 = x(ix,iy); by0 = y(ix,iy); cx0 = 1; cy0 = 1; astep = 0.05; bstep = 0.05; cstep = 0.05; chisqmin = 1e20; for a = a0-5*astep:astep:a0+5*astep for bx = bx0-5*bstep:bstep:bx0+5*bstep for by = by0-5*bstep:bstep:by0+5*bstep for cx = cx0-5*cstep:cstep:cx0+5*cstep for cy = cy0-5*cstep:cstep:cy0+5*cstep ztest = a*exp(-(((x-bx)/cx).^2 + ((y-by)/cy).^2)); chisq = sum(sum(((ztest-z)/sig).^2)); if (chisq < chisqmin); chisqmin = chisq; params = [a,bx,by,cx,cy]; end end params = 2.1465 2.3000 -1.2000 1.2500 0.8000

USE THE FORCE LUKE Rather than writing code to search parameter space ourselves, we will follow our example in 1-D fitting and use the FORCE…. ie. built-in functions of Matlab to help us search parameter space. Best fit 2D data

USE THE FORCE LUKE Not so good ‘Best fit’ >> a = 2.5; bx = 2.3; by = -1.2; cx = 1.5; cy = 0.8;

Why are the fit parameters off? We need to adjust FIT options and limit the UPPER and LOWER Limits as well as INITIAL Guess…. We want a GLOBAL minimum not a LOCAL minimum The extracted parameters must make PHYSICAL sense. Example: If we were fitting data for the speed of a car on the Garden State Parkway, what MINIMUM, MAXIMUM, and INITIAL GUESSES would we use for the speed?

Change in FIT OPTIONS

Class Exercise Import Lecture_18_Laser_mode.jpg into matlab. Save the JPG data as the variable ‘laser’. Use the following code to convert to grayscale for fitting. >> grayL=rgb2gray(laser); % convert from RGB to grayscale >> colormap(gray) % use grayscale color map >> imagesc(grayL) % Plot image of laser beam mode Create the “x” and “y” limits for this 400 by 548 pixel image using the following code >> y=1:1:548; >> x=1:1:400; Using the curve fitting tool, fit the image of the laser beam mode to a GAUSSIAN beam profile z = a*exp( - (((x-bx)/cx).^2 + ((y-by)/cy).^2)) You will need to change the fit options for the fitting parameters a, bx, by, cx, and cy to get a ‘good’ best fit. Once you have a reasonable fit, comment on whether or not the laser mode appears to be well represented by a Gaussian profile. HINT: In Curve fitting tool, plot the residuals.