1
$\begingroup$

I have a list of the form dataTosmooth={{a,b,c},....} where a and b are coordinates and c is the data to be smoothed. In other words, smooth dataTosmooth[[All,3]] along dataTosmooth[[All,{1,2}]]. What I want is a GaussianFilter[] that can specify the spread of the smoothing in each coordinate direction (i believe this is the standard deviation of the smoothing kernel?).

My data is below:

dataTosmooth = {{-50.85000000000002`, 10.270358052733272`, 
35.71056426325409`}, {-41.10000000000002`, 2.873303230466569`, 
38.81568368585049`}, {-50.85000000000002`, 26.632846724285706`, 
32.673746341673144`}, {-41.10000000000002`, 19.236823144577595`, 
62.82335234608225`}, {-31.340000000000032`, 11.842619292783251`, 
68.53798691236852`}, {-21.590000000000032`, 4.449371441148591`, 
59.67967828331516`}, {-50.85000000000002`, 42.998118304091385`, 
24.63168690934504`}, {-41.10000000000002`, 35.60127021988911`, 
39.14245981028786`}, {-31.340000000000032`, 28.205878010311572`, 
108.66125712506734`}, {-21.590000000000032`, 20.813838889904257`, 
71.86692911207489`}, {-11.830000000000041`, 13.420766840507909`, 
56.90601725652569`}, {-2.07000000000005`, 6.025392936376173`, 
45.86143750472721`}, {-50.85000000000002`, 59.3621659426611`, 
15.762228586085156`}, {-41.10000000000002`, 51.96435329871178`, 
21.391649726002893`}, {-31.340000000000032`, 44.57034147474533`, 
93.0932476069022`}, {-21.590000000000032`, 37.178070307403786`, 
103.45958341304103`}, {-11.830000000000041`, 29.784020977809462`, 
71.78875677987466`}, {-2.07000000000005`, 22.389236995571`, 
56.71646586309835`}, {7.67999999999995`, 14.992388529677152`, 
47.14707785218505`}, {17.440000000000055`, 7.597788181370409`, 
46.556240183245045`}, {-50.85000000000002`, 75.72467877215065`, 
17.284429409153184`}, {-41.10000000000002`, 68.32743607405777`, 
41.167517535066395`}, {-31.340000000000032`, 60.932774236589516`, 
97.62585210895732`}, {-21.590000000000032`, 53.54074691792057`, 
100.79005357672948`}, {-11.830000000000041`, 46.14684985504994`, 
87.71485621033645`}, {-2.07000000000005`, 38.751590761310176`, 
61.202148655092174`}, {7.67999999999995`, 31.355932184885482`, 
53.28526015700777`}, {17.440000000000055`, 23.960184532899337`, 
51.37840013079427`}, {-41.10000000000002`, 84.69127060783131`, 
48.50554092059999`}, {-31.340000000000032`, 77.29765442653235`, 
96.52191875423944`}, {-21.590000000000032`, 69.90470174293358`, 
102.16980131134638`}, {-11.830000000000041`, 62.51153713633315`, 
87.28252975252263`}, {-2.07000000000005`, 55.1171707113319`, 
71.13287039055854`}, {7.67999999999995`, 47.719660637139974`, 
56.039176921150435`}, {17.440000000000055`, 40.32255868764509`, 
51.62321764477686`}, {-21.590000000000032`, 86.26760264399773`, 
99.89893442875152`}, {-11.830000000000041`, 78.87493853044776`, 
84.38244870824737`}, {-2.07000000000005`, 71.47982757170443`, 
66.47546126203261`}, {7.67999999999995`, 64.08276415269069`, 
56.678848788591324`}, {17.440000000000055`, 56.68762104179337`, 
49.11240169244424`}, {-2.07000000000005`, 87.84359593999416`, 
60.45632142299099`}, {7.67999999999995`, 80.44678282702743`, 
46.25808469878537`}, {17.440000000000055`, 73.05145942047734`, 
43.000967430977646`}, {17.440000000000055`, 89.41549423365234`, 
35.91954899494147`}, {-50.85000000000002`, 92.08824379203644`, 
8.605283260881098`}, {-50.85000000000002`, 108.45069667426264`, 
11.52966608362967`}, {-41.10000000000002`, 101.05393883219705`, 
44.11489034328432`}, {-31.340000000000032`, 93.66005797761179`, 
73.27571323433162`}, {-50.85000000000002`, 124.81532739053824`, 
13.822080107567034`}, {-41.10000000000002`, 117.41888025571974`, 
36.794739433783214`}, {-31.340000000000032`, 110.0246073374448`, 
71.61101451592485`}, {-21.590000000000032`, 102.6314654421303`, 
78.16186826068069`}, {-11.830000000000041`, 95.23887349513711`, 
74.51831620480968`}, {-50.85000000000002`, 141.18006853527996`, 
13.310659262410699`}, {-41.10000000000002`, 133.78046387959196`, 
57.95955788306187`}, {-31.340000000000032`, 126.38715905181873`, 
67.83749405749008`}, {-21.590000000000032`, 118.99470606408583`, 
60.94596541199428`}, {-11.830000000000041`, 111.60257187866787`, 
61.15398760506133`}, {-2.07000000000005`, 104.20728733201076`, 
48.67468541244001`}, {7.67999999999995`, 96.81027117502538`, 
45.52254935743384`}, {-50.85000000000002`, 157.54204574955395`, 
32.85902615720679`}, {-41.10000000000002`, 150.14434755173272`, 
58.74089291115688`}, {-31.340000000000032`, 142.75176035586526`, 
40.68660344634335`}, {-21.590000000000032`, 135.35972757157379`, 
43.96553536578396`}, {-11.830000000000041`, 127.96571938604754`, 
47.731975131980526`}, {-2.07000000000005`, 120.57125958422365`, 
46.88224024084176`}, {7.67999999999995`, 113.17334849523019`, 
33.146201139798805`}, {17.440000000000055`, 105.778455903297`, 
28.744622975436638`}, {-50.85000000000002`, 173.90647150665006`, 
13.951939506749587`}, {-41.10000000000002`, 166.50903777139433`, 
39.42513474929413`}, {-31.340000000000032`, 159.11491853678754`, 
36.678015213476314`}, {-21.590000000000032`, 151.72299087080495`, 
29.002943816102533`}, {-11.830000000000041`, 144.33048474012006`, 
45.740992555912044`}, {-2.07000000000005`, 136.93710041334336`, 
27.876090113213525`}, {7.67999999999995`, 129.53618856327074`, 
37.304938681091556`}, {17.440000000000055`, 122.14189062612374`, 
22.23726489476616`}, {-31.340000000000032`, 175.47796435907952`, 
52.748557326243535`}, {-21.590000000000032`, 168.08570710991364`, 
64.81976781815867`}, {-11.830000000000041`, 160.6931957243165`, 
44.599820627441986`}, {-2.07000000000005`, 153.2983792825559`, 
37.64146649640527`}, {7.67999999999995`, 145.9001470791707`, 
22.779087997547222`}, {17.440000000000055`, 138.504480760232`, 
18.657687959658883`}, {-11.830000000000041`, 177.05682588856274`, 
41.93606204688277`}, {-2.07000000000005`, 169.66138640423281`, 
45.416159018569594`}, {7.67999999999995`, 162.26467463897`, 
32.96873518957013`}, {17.440000000000055`, 154.8692135101764`, 
22.87621472184591`}, {7.67999999999995`, 178.62832066247012`, 
32.728904656282026`}, {17.440000000000055`, 171.23374350652944`, 
25.148891476564128`}, {-50.85000000000002`, 239.3621659426611`, 
12.480879376069625`}, {-41.10000000000002`, 231.96435329871179`, 
8.025642964896925`}, {-31.340000000000032`, 224.57034147474533`, 
33.438439706421754`}, {-21.590000000000032`, 217.1780703074038`, 
105.20882021009456`}, {-11.830000000000041`, 209.78402097780946`, 
46.7701363051253`}, {-2.07000000000005`, 202.389236995571`, 
48.438182769381434`}, {7.67999999999995`, 194.99238852967716`, 
36.58827851238699`}, {17.440000000000055`, 187.59778818137042`, 
29.716782076799635`}, {-50.85000000000002`, 255.72503363985203`, 
6.583537460970347`}, {-41.10000000000002`, 248.32777634141468`, 
11.033084337573124`}, {-31.340000000000032`, 240.93309418426773`, 
16.048551336491414`}, {-21.590000000000032`, 233.54104127754883`, 
73.47984281420318`}, {-11.830000000000041`, 226.1493860608138`, 
56.288509484618636`}, {-2.07000000000005`, 218.75216187654576`, 
38.96555155518782`}, {7.67999999999995`, 211.35655756554726`, 
34.24591847943343`}, {17.440000000000055`, 203.96085357284127`, 
29.93264438702334`}, {-41.10000000000002`, 264.6912706078313`, 
8.917437300031883`}, {-31.340000000000032`, 257.29765442653235`, 
22.77318159590351`}, {-21.590000000000032`, 249.90470174293358`, 
94.24297188650199`}, {-11.830000000000041`, 242.51186189307893`, 
66.90333885539648`}, {-2.07000000000005`, 235.1171707113319`, 
32.60697773483461`}, {7.67999999999995`, 227.71966063713998`, 
27.04036061150073`}, {17.440000000000055`, 220.32255868764508`, 
27.352197516835826`}, {-21.590000000000032`, 266.2683330630546`, 
87.00032327526148`}, {-11.830000000000041`, 258.87565693618865`, 
93.94211834954542`}, {-2.07000000000005`, 251.48052198668333`, 
24.320164473128056`}, {7.67999999999995`, 244.08342282394418`, 
22.173894786437497`}, {17.440000000000055`, 236.6882328805953`, 
25.82559735998376`}, {-2.07000000000005`, 267.843998447932`, 
24.36438969069408`}, {7.67999999999995`, 260.44714390335577`, 
24.329576907541977`}, {17.440000000000055`, 253.05180958679978`, 
21.350541234357504`}, {17.440000000000055`, 269.41549423365234`, 
22.553025074255565`}, {-50.85000000000002`, 190.26963741670468`, 
12.249710853730392`}, {-41.10000000000002`, 182.8723158359478`, 
19.09584681285435`}, {-50.85000000000002`, 206.6328467242857`, 
18.15002617078005`}, {-41.10000000000002`, 199.23682314457758`, 
22.89167058123981`}, {-31.340000000000032`, 191.84261929278324`, 
41.178883544846144`}, {-21.590000000000032`, 184.4493714411486`, 
95.6362818181468`}, {-50.85000000000002`, 222.99811830409138`, 
7.866947311378158`}, {-41.10000000000002`, 215.6012702198891`, 
8.608068017853943`}, {-31.340000000000032`, 208.20587801031158`, 
54.017223179278695`}, {-21.590000000000032`, 200.81383888990425`, 
84.16516084461551`}, {-11.830000000000041`, 193.4207668405079`, 
47.98167202172095`}, {-2.07000000000005`, 186.02539293637616`, 
46.21709910844685`}, {-50.85000000000002`, 272.0884633470491`, 
4.787857140725892`}, {-50.85000000000002`, 288.4525503585659`, 
10.812641790977825`}, {-41.10000000000002`, 281.0550168884835`, 
7.30768355910409`}, {-31.340000000000032`, 273.6608250905488`, 
40.51857351881985`}, {-50.85000000000002`, 304.81562802291336`, 
17.58421826525137`}, {-41.10000000000002`, 297.4192052801607`, 
12.388432104184938`}, {-31.340000000000032`, 290.02495126176467`, 
36.474884509755384`}, {-21.590000000000032`, 282.6318225761067`, 
68.86386570618869`}, {-11.830000000000041`, 275.2392380406287`, 
93.5971826499067`}, {-50.85000000000002`, 321.18006853527993`, 
8.22371841079204`}, {-41.10000000000002`, 313.78046387959193`, 
10.071113270140483`}, {-31.340000000000032`, 306.38715905181874`, 
16.99704533146858`}, {-21.590000000000032`, 298.99470606408585`, 
67.83629559461517`}, {-11.830000000000041`, 291.60257187866785`, 
125.48029964898875`}, {-2.07000000000005`, 284.20728733201076`, 
35.34755260976352`}, {7.67999999999995`, 276.8102711750254`, 
23.263237844289865`}, {-50.85000000000002`, 337.5423841647362`, 
11.36600510293745`}, {-41.10000000000002`, 330.146487811147`, 
18.917134746044393`}, {-31.340000000000032`, 322.75205173260684`, 
41.586135910901795`}, {-21.590000000000032`, 315.3597275715738`, 
92.72125969808648`}, {-11.830000000000041`, 307.9662966033093`, 
163.53064896220525`}, {-2.07000000000005`, 300.57157486151215`, 
40.001162639353375`}, {7.67999999999995`, 293.1740217393397`, 
21.27936243170834`}, {17.440000000000055`, 285.77880817562993`, 
20.515038910516356`}, {-50.85000000000002`, 353.90647150665006`, 
18.3776858173166`}, {-41.10000000000002`, 346.50903777139433`, 
27.376215041528297`}, {-31.340000000000032`, 339.1149185367875`, 
30.257997289972742`}, {-21.590000000000032`, 331.72299087080495`, 
99.86756530525814`}, {-11.830000000000041`, 324.3304847401201`, 
91.83169605316021`}, {-2.07000000000005`, 316.9371004133434`, 
18.836631864534585`}, {7.67999999999995`, 309.53618856327074`, 
19.95064660606268`}, {17.440000000000055`, 302.14189062612377`, 
16.002839201216762`}, {-31.340000000000032`, 355.4790590727488`, 
52.64555536795103`}, {-21.590000000000032`, 348.08678141464077`, 
67.83523568176052`}, {-11.830000000000041`, 340.69388671032993`, 
38.75331985778767`}, {-2.07000000000005`, 333.299033512983`, 
26.485557385110862`}, {7.67999999999995`, 325.90075350369267`, 
22.29703567741686`}, {17.440000000000055`, 318.50690611599794`, 
21.108851224071856`}, {-11.830000000000041`, 357.05682588856274`, 
46.125344443158355`}, {-2.07000000000005`, 349.6613864042328`, 
37.48141472783543`}, {7.67999999999995`, 342.26467463897`, 
31.004461775686405`}, {17.440000000000055`, 334.8692135101764`, 
26.973876250920995`}, {7.67999999999995`, 358.6283206624701`, 
42.92870834301914`}, {17.440000000000055`, 351.23374350652944`, 
40.47545552554042`}};

The noisy data looks like this:

enter image description here

The result I want looks somewhat like below.

enter image description here

This was generated using the squared exponential kernel in Predict[] as

sqExp = Predict[
   Thread[dataTosmooth[[All, {1, 2}]] -> dataTosmooth[[All, 3]]] , 
   Method -> {"GaussianProcess", 
     "CovarianceType" -> "SquaredExponential"}];
ContourPlot[
 sqExp[{x, y}], {x, -50, 17.5}, {y, 2.84, 358}, PlotRange -> Full, 
 PlotLegends -> 
  Placed[BarLegend[Automatic, LegendMargins -> {{0, 0}, {10, 5}}, 
    LegendLabel -> "contours", 
    LabelStyle -> {25, Italic, FontFamily -> "Times New Roman"}], 
   Below], Frame -> True, 
 FrameLabel -> {Style["x direction", 20, Italic], 
   Style["y direction, [\[Theta]]", 20, Italic]}, 
 LabelStyle -> {20, GrayLevel[0], FontFamily -> "Times New Roman"}, 
 LabelStyle -> {20, GrayLevel[0], FontFamily -> "Times New Roman"}, 
 ImageSize -> Large, GridLines -> Automatic
 ]
$\endgroup$

2 Answers 2

1
$\begingroup$

To apply e.g. a gaussian filter we need the data in an array. Toward this aim, we first sort the data;

d = Sort[dataTosmooth, First[#1] < First[#2] &]

Then we split the data according to the x values. This gives an array, from which we only take the third value (z or function value):

datz= Split[d = Sort[dat, First[#1] < First[#2] &], First[#1] === First[#2] &][[All, All, 3]]

These values can now be filtered. We can adjust the strength of the filter by: smthfac, I choose 5 for an example.:

smthfac = 5;
smth = GaussianFilter[datz, smthfac] ;

To draw a ContourPlot, we need to reassemble the data in the original format: {{x,y,z},..}:

datsmth=Transpose[Append[Transpose@d[[All, 1 ;; 2]], Flatten@smth]]

With this we can draw the plot:

ListContourPlot[datsmth]

enter image description here

For convenience, the code in compact form:

smthfac = 5;
smth = GaussianFilter[
   Split[d = Sort[dataTosmooth, First[#1] < First[#2] &], 
     First[#1] === First[#2] &][[All, All, 3]], smthfac] ;
ListContourPlot[
 Transpose[Append[Transpose@d[[All, 1 ;; 2]], Flatten@smth]]]
$\endgroup$
3
  • $\begingroup$ Hello Daniel, Thank you for your answer but I don't think it is what I want. Since the data which is to be smoothed varies in a 2D space, my understanding is that in general the Gaussian Kernel that GaussianFilter is suppose to use is 2 dimensional gaussian distribution so that the strength and standard deviation can be controlled in each direction. $\endgroup$
    – Tom6639
    Mar 17, 2021 at 17:39
  • $\begingroup$ GaussianFilter[dat, r ] uses a kernel of radius, that is STD of r/2. This smoothes all direction equally. If this is not what you want, you can read the help of GaussianFilter. If GaussianFilter is not what you want, you may replace it e.g. by MeanFilteror any other filter. $\endgroup$ Mar 17, 2021 at 17:58
  • $\begingroup$ I guess, the original amount of a data is too few for good smoothing. It is needed in up-sampling first. $\endgroup$
    – Rom38
    Mar 18, 2021 at 5:05
1
$\begingroup$

As I said in comment for Daniel's answer, for good smoothing you need more data: The original data:

 ListContourPlot[dataTosmooth]

enter image description here

It is impossible to improve it with any dances around.

Let's make the interpolation function and make the up-sampling to 201*201 matrix:

fun = Interpolation[dataTosmooth, InterpolationOrder -> 1];
{xmin, xmax} = MinMax[dataTosmooth[[All, 1]]];
{ymin, ymax} = MinMax[dataTosmooth[[All, 2]]];
newdata = 
  Table[fun[x, y], {y, ymin, ymax, (ymax - ymin)/200}, {x, xmin, 
    xmax, (xmax - xmin)/200}];

ListContourPlot[newdata[[4 ;; -4, 4 ;; -4]], InterpolationOrder -> 3]

enter image description here

As one can see, the result looks not much better. I've cropped the matrix for 4 lines at all sides to remove the artifacts of interpolation at the edges of x,y-domain.

This data is fully appropriate for smoothing with, let's say, GaussianFilter:

fd = GaussianFilter[newdata[[4 ;; -4, 4 ;; -4]], 10];
ListContourPlot[fd, InterpolationOrder -> 3]

enter image description here

Of course, you can choose the desired level of up-sampling and filtering.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.