How to fit this signal to x axis at y=0 using polyfit/polyval?

17 visualizaciones (últimos 30 días)
I want to fit this signal to the x axis because it looks more shifted upwards, any help will be much appreciated.
coding:
Graph1=csvread('C1hv00000.csv',6,0);
x = Graph1(:, 1);
y = Graph1(:, 2);
plot(x, y);

Respuestas (1)

John D'Errico
John D'Errico el 29 de Nov. de 2020
Editada: John D'Errico el 29 de Nov. de 2020
This is just another case of someone not understanding how polynomials work or behave.
Seriously, to fit a polynomial to that data would be an unjust thing to do to a poor, unsuspecting polynomial. Why? Well, yes, you can force fit a polynomial through the origin. The data is massively noisy but you could still do so.
You would need to discard the data below x == 0 though, since it appears your function is essentially constant below zero.
As well, above 2 or 3 for x, the function again appears to settle down to a constant value. This is impossible for any polynomial function (except for the trivial case of a constant itself.) All polynomials, as x grows large in magnitude, ALWAYS MUST grow to either +inf or -inf.
As such, your data shows strongly non-polynomial behavior.
If you were willing to discard all data below zero, as well as all data above say x=2.5, could you fit what remains with a polynomial that is forced to pass through zero? Well, yes.
Polyfit does not have this capability. And you would NOT want to use a remotely high order polynomial. Your data is wildly too noisy to support anything of any significant order. (Perhaps a cubic is as high as I would go.) And, since I lack your data, I can't do too much. A picture of numbers is nice, but not terribly useful. So I;ll make up some garbage data. Actually, what I have here is a bit better than what I see in the picture.
x = rand(100,1)*2.5;
y = sin(x) + randn(size(x))/3;
plot(x,y,'.')
So, pretty noisy data. You would agree the function should pass through the origin, in this case, only because we know the underlying function does exactly that. Now, fit that with a cubic polynomial, passing through the origin. This takes only one line of code.
coef = (x.^[3:-1:1])\y;
I could also have trivially used the curve fitting toolbox, or my own polyfitn from the file exchange. But one line of code that requires no other tools is hard to beat.
The constant term in that model is implicitly zero. So that I can now use polyval to evaluate it, I'll append the constant term to the coefficients.
coef = [coef;0];
hold on
fplot(@(x) polyval(coef,x),[0,2.5])
legend('data','Cubic fit through the origin')
A higher order model would be wholly unjustified for data this noisy, but the cubic seems reasonable. Could I have added a up to maybe 5th order term? Sigh, perhaps. At some point the extra terms will just start chasing noise in the data, and given this much noise, that will happen quite quickly.
Do NOT try to use that polynomial to predict outside the support of the data. It will do obscene things, as that is just the nature of the polynomic beast.
  4 comentarios
Ebrahim Abdullah Ahmed Albabakri
Ebrahim Abdullah Ahmed Albabakri el 30 de Nov. de 2020
I don't want any change of values in the signal, I just want to draw the x-axis at y==0 instead of at the buttom of the graph. I heared it could be done by polyfit but if you have any other way it is okay.
John D'Errico
John D'Errico el 30 de Nov. de 2020
SigH. Is that all you want? How much time have I wasted trying to answer the question you asked, because you explicitly asked how to fit your signal, using polyfit. No, polyfit or polyval would seem to have nothing to do with that. I have no clue why you even thought they would.
plot(1:10,randn(1,10),'-')
set(gca,'XAxisLocation','origin')
Your alternatives there would seem to be one of: 'bottom', 'top', 'origin', with the default as 'bottom'.

Iniciar sesión para comentar.

Categorías

Más información sobre Spline Postprocessing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by