how to detrend data in multiple dimensions

26 views (last 30 days)
Max Baeten
Max Baeten on 22 Aug 2016
Answered: Star Strider on 23 Aug 2016
Hey all,
I am stuck on a detrending data problem. I have the following matrix Z:
[X,Y] = meshgrid(-2:.2:2);
Z = X .* exp(-X.^2 - Y.^2) + 0.1*X+0.2*Y;
surf(X,Y,Z)
Which consists of function X .* exp(-X.^2 - Y.^2) plus a plane 0.1*X+0.2*Y. Now I want to detrend matrix Z such that only the first function remains. Basically I need to fit a non curved plane trough Z(x,y) and then subtract that from Z(x,y).
In 1d that is easy using detrend(Z) but if I put a matrix in detrend() it will detrend all columns separately.
Any ideas on how to detrend in 2d?
  1 Comment
Takfarinas
Takfarinas on 23 Aug 2016
Try this file exchange function which is a 2d equivalent of the detrend function: https://uk.mathworks.com/matlabcentral/fileexchange/33192-flatten-a-data-in-2d/content/detrend_2d.m

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 23 Aug 2016
For a relatively simple solution, this comes close to completely detrending your surface:
[X,Y] = meshgrid(-2:.2:2);
Z = X .* exp(-X.^2 - Y.^2) + 0.1*X+0.2*Y;
B = [ones(size(X(:))) X(:) Y(:)]\Z(:);
Zdm = ones(size(X))*B(1) + B(2)*X + B(3)*Y; % Z-Detrending Matrix
figure(1)
surfc(X,Y,Z)
grid on
figure(2)
surfc(X,Y,Z-Zdm)
grid on

Categories

Find more on Data Preprocessing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!