1
votes

I have a two dimensional data set, of some fixed dimensions (xLen and yLen), which contains a sine curve.

I've already determined the frequency of the sine curve, and I've generated my own sine data with the frequency using

SineData = math.sin((2*math.pi*freqX)/xLen + (2*math.pi*freqY)/yLen)

where freqX and freqY and the oscillation frequencies in the X and Y directions for the curve.

But now I'd like to do a linear least squares fit (or something similar), so that I can fit the right amplitude. As far as I know, a linear least squares is the right way to go, but if there's another way that's fine as well.

The leastsq function is SciPy doesn't do a multidimensional fit. Is there a python implementation for a 2/multidimensional least square fitting algorithm

Edit: I found the 2 dimensional frequency of the sine wave from a 2D FFT. The data contains a 2D sine + noise, so I only picked the largest peak of the 2D FFT and took an inverse of that. Now I have a sine curve, but with an amplitude that's off. Is there a way to do a 2 dimensional least squares (or similar), and fit the amplitude?

2

2 Answers

0
votes

You might also consider a 2D Finite/Discrete Fourier Transform (FFT/DFT) if your data is well served by using trig functions.

NumPy has an DFT solution built in.

There are lots of places to help you get started; Google found this one.

Start with your original data. The transform will tell you if your frequency solution is correct and if there are other frequencies that are also significant.

0
votes

In least squares fitting , one minimizes a residual function, perhaps chisquare. Since this involves summing estimates corresponding to the difference squared at each of the points of model minus data, the number of dimensions is "forgotten" in making the residual. Thus all the values in the 2D difference function array can be copied to a 1D array as the result of the residual function supplied to, for example, leastsq. An example for complex to real rather than 2D to 1D is given in my answer to this question: Least Squares Minimization Complex Numbers