Flatten plot from two vectors
9 views (last 30 days)
Mathieu NOE on 30 Mar 2022
a small demo to compare smoothing and spline fitting (using this FEX submission : SPLINEFIT - File Exchange - MATLAB Central (mathworks.com))
n = 25;
x = sort(4*rand(n,1))
f = sin(x) + 0.05*randn(n,1);
% resample the data (interpolation) on linearly spaced points
xx = linspace(min(x),max(x),100);
ff = interp1(x,f,xx);
ffs = smoothdata(ff, 'gaussian',10);
% or spline fit ?
% Breaks interpolated from data
breaks = linspace(min(x),max(x),10); %
p = splinefit(xx,ff,breaks); %
ff2 = ppval(p,xx);
William Rose on 30 Mar 2022
Can you attach the vectors please?
I cannot tell from the plot if the samlpling is uniform along x or not. I cannot tell if this is a path in two dimensions, or a function of the horizontal axis variable. Thie difference matters, because the approach to smoothing will be different.
If it is a path in 2 dimensions, with non-uniform sampling along x and along y:
Conisder defining an arbitray time variable, so that x and y both become funcitons of time. I would assign times to each point pair so that the distance travelled per unit time (where ) is constant. Use interp1() to resample the x-vector to uniform spacing in time. Do the same with the y vector. Choose a time spacing that is sufficiently fine to accurately capture the sharp corners in your data. The plot of resampled x versus resampled y should look like the original.
Then apply a flat moving average filter to the x versus time data and to the y versus time data, independently. Experiment with different widths for the moving average window. Think about how to handle the moving average window when it gets up to the edges of the signal.
If, instead of being a path in two dimensions, this is a plot of a dependent variable (y) versus an independent variable (x), then smooth the y values, and don't smooth the x values. If x-values are non-uniformly spaced, then interpolate the y values to to a uniform sampling rate along the x axis, using interp1(). Make sure the sampling interval along x is dense enough to capture accurately the sharp corners in the original data. Then smooth the interpolated y values with a flat moving average filter. Try different window widths. As usual, take care with the moving average near the edges.