# How can I obtain an analytic function from a set of points from an image?

So I have this image:

and I want to extract some piecewise-function of this nonperiodic wave-train, that will later be expanded to a Fourier series.

Step 1: I use the Coordinate tool to get the point set:

points = {{{209.31751662971172, 300.5625}, {204.74101995565408,
262.53214285714284}, {201.02261640798224,
296.88214285714287}, {197.0181818181818,
358.2214285714286}, {194.72993348115298,
300.5625}, {191.29756097560974,
269.8928571428571}, {188.7232815964523,
320.19107142857143}, {185.86297117516628,
296.88214285714287}, {183.00266075388024,
250.26428571428573}, {181.28647450110861,
301.7892857142857}, {177.8541019955654,
347.18035714285713}, {172.13348115299334,
316.5107142857143}, {169.2731707317073,
225.7285714285714}, {166.12682926829265,
295.65535714285716}, {162.4084257206208,
396.2517857142857}, {159.5481152993348,
295.65535714285716}, {156.68780487804875,
176.65714285714284}, {153.82749445676274,
294.4285714285714}, {149.8230598669623,
407.29285714285714}, {144.96053215077603,
311.6035714285714}, {145.53259423503323,
305.46964285714284}, {145.81862527716186,
293.2017857142857}, {144.67450110864743,
180.33749999999998}, {140.3840354767184,
289.52142857142854}, {135.80753880266073,
655.1035714285715}, {130.9450110864745,
185.65357142857147}, {126.36851441241683,
378.2589285714286}, {122.65011086474499,
293.6107142857143}, {116.64345898004433,
336.5482142857143}, {112.92505543237249,
259.2607142857143}, {110.35077605321507,
297.29107142857146}, {105.7742793791574,
325.5071428571429}, {102.62793791574279,
296.06428571428575}, {98.90953436807094,
255.58035714285717}, {96.62128603104212,
289.9303571428572}, {93.47494456762747,
320.6}, {91.18669623059866,
286.25000000000006}, {87.46829268292682,
296.06428571428575}, {84.32195121951219,
259.2607142857143}, {82.60576496674057,
297.29107142857146}, {78.88736141906872,
340.22857142857146}, {76.3130820399113,
326.73392857142863}, {73.73880266075386,
286.25000000000006}, {72.59467849223945,
226.13750000000005}, {69.44833702882482,
245.76607142857148}, {66.87405764966739,
293.6107142857143}, {65.15787139689577,
330.41428571428577}, {62.58359201773834,
352.4964285714286}, {61.43946784922393,
315.6928571428572}, {58.8651884700665, 291.1571428571429}}};


Step 2: I prepare a piecewise function of the points, where the deepest through is aligned with $$y=0$$ and the tallest wave is aligned at $$x=0$$ :

points2 = {#[[1]], #[[2]] - 176.65714285714284} & /@ points;

Clear[t];
f[t_] = Piecewise[
Partition[Sort[points2], 2,
1] /. {{a_?NumericQ, b_}, {c_, d_}} :> {b, a <= t < c}];

f1[t_] = f[(135.80753880266073 (t + Pi)/Pi)];
Plot[f1[t], {t, -Pi, Pi}]


But I get a blank plot, which is strange.

Usually I get a plot which is a step-wise function of the wave-train, that can be later transformed into a Fourier series using:

FD[t_] = FourierSeries[f1[t], t, 15]


I cannot see any error in the codes here. Why is the plot blank?

Are there better methods to get an analytic function for Fourier series expansion?

points = {{461.25, 156.}, {457.5, 144.75}, {447.75,
158.25}, {445.5, 167.25}, {440.25, 161.25}, {433.5,
144.75}, {429., 154.5}, {425.25, 159.}, {418.5,
157.5}, {412.5, 150.}, {409.5, 161.25}, {405.75,
166.5}, {402.75, 152.25}, {396.75, 146.25}, {390.,
173.25}, {385.5, 162.75}, {381., 138.}, {375.,
156.75}, {369.75, 151.5}, {363., 163.5}, {359.25,
150.75}, {355.5, 138.}, {351.75, 150.}, {345.,
174.}, {339., 154.5}, {336.75, 128.25}, {331.5,
141.}, {327., 164.25}, {321., 180.}, {318.,
162.75}, {309., 132.}, {308.25, 139.5}, {303.,
191.25}, {300.75, 261.}, {295.5, 184.5}, {288.,
134.25}, {280.5, 153.75}, {275.25, 167.25}, {269.25,
171.}, {264., 150.75}, {260.25, 138.75}, {258.,
154.5}, {253.5, 168.}, {245.25, 153.75}, {241.5,
153.75}, {232.5, 144.}, {230.25, 153.}, {224.25,
169.5}, {221.25, 153.75}, {212.25, 147.}, {206.25,
161.25}, {201., 167.25}, {197.25, 144.}, {192.,
153.}, {180., 158.25}, {174., 162.75}};

points2 = {#[[1]], #[[2]] - 128} & /@ points;

Clear[t];
f[t_] = Piecewise[
Partition[Sort[points2], 2,
1] /. {{a_?NumericQ, b_}, {c_, d_}} :> {b, a <= t < c}];

f1[t_] = f[(300.75 (t + Pi)/Pi)];
Plot[f1[t], {t, -Pi, Pi}, PlotRange -> Full]


this gives:

which then by:

FD[t_] = FourierSeries[f1[t], t, 15]


gives:

The upper example does not work however, still the procedures are exactly the same.

• There is are two points {1111.4627274071884, 337.8744638367107}, {1111.4627274071884, \ 323.29877237095116} with same x-value. Mar 6, 2023 at 10:36
• Instead of Piecewise you might use ip=Interpolation[points,InterpolationOrder->1], which makes skaling easier I think Mar 6, 2023 at 10:44
• Yes ip[x] may be used like a biuld in function Mar 6, 2023 at 10:47
• This sounds like an X,Y problem within a bad idea. @Vangsnes please edit your question to explain what is your goal, and your starting data. Share the code that creates your error. All this looks like a bad idea, extracting data from a pixelated image to build an interpolating function so it can be Fourier transformed analytically... Why? What are you trying to achieve? Mar 6, 2023 at 10:57
• Thanks for the edit. What is "the point extraction tool"? What kind of analytical function do you expect? Do you already have a model to fit? What is the core goal here? The coefficients of a Flourier series? How many coefficients do you expect to know ? Is it important "Why is the plot blank?" in that context? Any chance of getting better quality data? Mar 6, 2023 at 11:15

First we scale the data:

ma = PositionLargest[points[[All, 2]]][[1]]
mi = PositionSmallest[points[[All, 2]]][[1]]
points2 = {#[[1]] - points[[ma, 1]], #[[2]] - points[[mi, 2]]} & /@
points;
ListPlot[points2]


Then we define the first piecewise function:

f[t_?NumericQ] =
Piecewise[
Partition[Sort[points2], 2,
1] /. {{a_?NumericQ, b_}, {c_, d_}} :> {b, a <= t < c}];
Plot[f[t], {t, Min[points2[[All, 1]]], Max[points2[[All, 1]]]}]


Finally we scale the x range:

f1[t_] =
f[(t + Pi) (Max[points2[[All, 1]]] -
Min[points2[[All, 1]]])/(2 Pi) + Min[points2[[All, 1]]] ];
Plot[f1[t], {t, -Pi, Pi}, PlotRange -> Full]
`

Now the Fourier transform works as expected.

• which version do you use? I have Mathematica 13.0.1 and I get at your first command: \$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of {-{{{99.4975,139.53},{99.4975,158.795},{97.2799,125.518},{93.7317,99.2472},{91.514,155.293},{89.2964,183.315},{86.1917,165.801},{83.5306,143.033},{82.6435,72.9759},{79.9824,100.999},<<28>>}}[[ma,1]]+{{99.4975,139.53},{99.4975,158.795},{97.2799,125.518},<<6>>,{79.9824,100.999},<<28>>}[1],-{{<<1>>}}[[mi,2]]+{<<1>>}[2]}. ListPlot::lpn: points2 is not a list of numbers or pairs of numbers. Mar 6, 2023 at 12:33
• I forgot to convert the first command from text to code. It is fixed and should work now. I am using version 13.2, but I do not think this is the issue. Mar 6, 2023 at 12:47
• You're right, it was a miscode in my sheet. Thanks for the correction Mar 6, 2023 at 13:24