Flatten plot from two vectors

조회 수: 7(최근 30일)
Lev Mihailov
Lev Mihailov 2022년 3월 30일
편집: Mathieu NOE 2022년 4월 1일
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!

답변(2개)

Mathieu NOE
Mathieu NOE 2022년 3월 30일
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
Mathieu NOE
Mathieu NOE 2022년 4월 1일
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');

댓글을 달려면 로그인하십시오.


William Rose
William Rose 2022년 3월 30일
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
Lev Mihailov
Lev Mihailov 2022년 4월 1일
@William Rose Yes, this data, with a large approximation, you can see the steps (saws) that I want to smooth out

댓글을 달려면 로그인하십시오.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by