×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Breaking a 2d cloud of data points into straight line segments

Breaking a 2d cloud of data points into straight line segments

Breaking a 2d cloud of data points into straight line segments

(OP)
I recently found a couple of ways of doing this. They are not automatic, and i suspect they need some judgement in their use.

The first is from Matlab https://www.mathworks.com/help/signal/ref/findchan...

and the second is the very neat SLM curve fitting toolbox https://www.mathworks.com/matlabcentral/fileexchan...

When i get matlab back then I'll be using these.

Cheers

Greg Locock


New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

RE: Breaking a 2d cloud of data points into straight line segments

(OP)
Here's a rather splendid piece of code that does most of what I need. That is, for a given set of knots in x0 it generates contiguous best fit straight lines for the x and y vectors. The result, p, is the y at the defined x0. What it doesn't do is measure the error, or attempt to move the knots to reduce the overall error. That of course, is what i need.

CODE -->

function p = fitPiecewiseLinearFunction(x, y, x0)
%
% Fit a piecewise continuous function f(x) to the pairs of data points (x,y)
% such that the sum of squares of error is minimal.
%
% x0 - values of x that define ends of segments of function f(x)
%
% p - end points of the segments p = f(x0)
%  
% See also: http://golovchenko.org/docs/ContinuousPiecewiseLinearFit.pdf
%
% 4-May-2004 Nikolai Golovchenko.
%


numberOfParameters = length(x0);

% separate data in segments
j = {};
for i = 1 : numberOfParameters - 1
   j{i} = find(x > x0(i) & x <= x0(i + 1));
   
   if isempty(j{i})
      error('Insufficient amount of data points');
   end
   
end

% compute the matrices corresponding to the
% system of equations
A = zeros(numberOfParameters, numberOfParameters);
B = zeros(numberOfParameters, 1);

for i = 1:numberOfParameters
   if i ~= 1
      % first sum
      A(i, i-1) = A(i, i-1) - ...
         sum((x(j{i-1}) - x0(i-1)) .* (x(j{i-1}) - x0(i))) / (x0(i) - x0(i-1)) .^ 2;
      A(i, i) = A(i, i) + ...
         sum((x(j{i-1}) - x0(i-1)) .^ 2) / (x0(i) - x0(i-1)) .^ 2;
      
      B(i) = B(i) + ...
         (sum(x(j{i-1}) .* y(j{i-1})) - x0(i-1) * sum(y(j{i-1}))) / (x0(i) - x0(i-1));
   end
   if i ~= numberOfParameters
      % second sum
      A(i, i) = A(i, i) + ...
         sum((x(j{i}) - x0(i+1)) .^ 2) / (x0(i+1) - x0(i)) .^ 2;
      A(i, i+1) = A(i, i+1) - ...
         sum((x(j{i}) - x0(i)) .* (x(j{i}) - x0(i+1))) / (x0(i+1) - x0(i)) .^ 2;
      
      B(i) = B(i) + ...
         (-sum(x(j{i}) .* y(j{i})) + x0(i+1) * sum(y(j{i}))) / (x0(i+1) - x0(i));
   end
end


% find the parameters
p = A^-1 * B; 

The black line is an eyeballed set of knots for the blue data. The circles are 11 year moving averages and the orange line is just a linear regression.




I looked at two built in functions. 'splinefit' tries to equalise the segment lengths. 'polyfit' doesn't guarantee that the lines are contiguous at the knots.

Cheers

Greg Locock


New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

RE: Breaking a 2d cloud of data points into straight line segments

Try this. You can try several numbers of specified joints, and pick the 'Best' number based on rms error.

x=1:100;
y(1:25) = 0+rand(25,1);
y(26:50) = 1+rand(25,1);
y(51:75) = 2+rand(25,1);
y(76:100)= -1+rand(25,1);
figure
plot(x,y,'r.')
hold on

sp3=spap2(3,2,x,y) % 3 joints, 1st order( use a 2 for 1st order )
fnplt(sp3,'m')

RE: Breaking a 2d cloud of data points into straight line segments

Looks like elements derived from the Sunspot Cycle.

RE: Breaking a 2d cloud of data points into straight line segments

Settle on the number of knots the science seems to suggest, apply lsqcurvefit (Matlab) to the knot locations and turn the crank. I picked 11. Once you get the first solution, bootstrap it by perturbing the lsqcurvefit solution and send it back into the fire a few more times. I do this technique with Pacejka tire model fits and it works very well.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

eBook - Rethink Your PLM
A lot has changed since the 90s. You don't surf the Web using dial-up anymore, so why are you still using a legacy PLM solution that's blocking your ability to innovate? To develop and launch products today, you need a flexible, cloud-based PLM, not a solution that's stuck in the past. Download Now
White Paper - Using Virtualization for IVI and AUTOSAR Consolidation on an ECU
Current approaches used to tackle the complexities of a vehicle’s electrical and electronics (E/E) architecture are both cost prohibitive and lacking in performance. Utilizing virtualization in automotive software architecture provides a better approach. This can be achieved by encapsulating different heterogeneous automotive platforms inside virtual machines running on the same hardware. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close