detrending timeseries by removing the linear function fitted by samples?

I am trying to detrend a timeseries based on one sample of the timeseries in matlab.
I basically want my sample points to be all the points between 11-22, but I am getting an error saying that the t variable has to be equal to the what tac is (which is 71).
I've tried this and this does not work:
tac = 71x1 double
t = 11:22;
d_tac_ts_2 = detrend(tac,SamplePoints,t)
I get this error:
Error using detrend>checkSamplePoints (line 284)
The number of elements in the 'SamplePoints' value must equal the size of
the first argument along the first dimension.
Error in detrend>parseNV (line 266)
s = checkSamplePoints(varargin{j+1},x);
Error in detrend>parseInputs (line 161)
[continuity,s] = parseNV(1,nargin,continuity,s,x,varargin{:});
Error in detrend (line 50)
[x,polyDeg,bp,s,continuity,sizeX,N,isrowx,isNDx,lbp] = parseInputs(x,
varargin{:});
Error in plotting_ref_TAC_vs_TAC_PET (line 28)
d_tac_ts_2 = detrend(tac,'SamplePoints',t)
This works, but does not do what I want:
t = 1:71;
d_tac_ts_2 = detrend(tac,SamplePoints,t)
thanks so much for any help!

 Akzeptierte Antwort

Considering that the objective is to remove a linear trend, perhaps the easiest way would be:
t = 11:22;
p = polyfit(x(t),tac(t),1);
d_tac_ts_2 = tac - polyval(p, x);
Here, ‘x’ is the original independent variable vector that defines ‘tac’ as the dependent variable.
Plotting the result would be:
figure
plot(x, tac)
hold on
plot(x, d_tac_ts_2)
hold off
grid
To plot both the original and detrended data.

4 Kommentare

hello!
thanks so much for responding! I am now getting an error:
Error using polyfit (line 44)
X and Y vectors must be the same size.
Error in untitled (line 30)
which I tried to fix by defining x:
p = polyfit(x(t),tac(t),1);
x = (1:71);
t = 11:22;
p = polyfit(x(t),tac(t),1);
d_tac_ts_2 = tac - polyval(p, x);
but no luck -- any suggestions?
My pleasure!
My test code to explore that was:
x = 0:70;
tac = polyval([1 -5 1],x);
t = 11:22;
p = polyfit(x(t),tac(t),1);
d_tac_ts_2 = tac - polyval(p, x);
figure
plot(x, tac)
hold on
plot(x, d_tac_ts_2)
hold off
grid
It ran without error.
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by