Flatten plot from two vectors

10 visualizzazioni (ultimi 30 giorni)
Lev Mihailov
Lev Mihailov il 30 Mar 2022
Modificato: Mathieu NOE il 1 Apr 2022
There are two data vectors. when plotting I get a sawtooth, would I like to smooth the graph?
How to do this?
Thank you in advance!

Risposte (2)

Mathieu NOE
Mathieu NOE il 30 Mar 2022
hello
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);
% smoothing
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);
figure(1),
plot(x,f,'-*',xx,ffs,'-',xx,ff2,'-')
legend('raw','smoothed','spline fit');
  4 Commenti
Lev Mihailov
Lev Mihailov il 1 Apr 2022
@Mathieu NOE This is my data, ladder plot appears as the graph approaches. Graph should be smooth.
Mathieu NOE
Mathieu NOE il 1 Apr 2022
Modificato: Mathieu NOE il 1 Apr 2022
ok , I didn't notice that in first place . You need a lot of zoom to see the staircase
with smoothdata you can get the desired result
clearvars
clc
A=load('DataBetaGama.csv');
x = A(:,1);
y = A(:,2);
[x,ia,ic] = unique(x);
samples = numel(x);
y = y(ia);
plot(x,y,'-r');
% resample the data (interpolation) on linearly spaced points
xx = linspace(min(x),max(x),2*samples);
yy = interp1(x,y,xx);
% smoothing
win = round(samples/25);
yys = smoothdata(yy, 'gaussian',win);
figure(1),
plot(x,y,'-*',xx,yys)
legend('raw','smoothed');

Accedi per commentare.


William Rose
William Rose il 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.
  4 Commenti
William Rose
William Rose il 31 Mar 2022
The data file you attached when plotted generates the plot below, which is very different from the plot in the original post. This data already looks very smooth at this scale.
A=load('DataBetaGama.csv');
plot(A(:,1),A(:,2),'-r');
Is this the data you want to smooth?
Lev Mihailov
Lev Mihailov il 1 Apr 2022
@William Rose Yes, this data, with a large approximation, you can see the steps (saws) that I want to smooth out

Accedi per commentare.

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by