# Looking for a better algorithm for finding the slope of an input signal

I've got an input signal that while sloping up, approximately bounces off an imaginary line. I'm looking for the equation to that imaginary line. I have a method, but is very brute force and it takes too long. I'm looking for ways to optimize my approach or hopefully learn about a completely different more efficient method. I've written a script in Octave that will do what I want, but it takes 3-10 seconds (with an input signal of 1800 samples) depending on how many iterations I run.

My brute force algorithm in Octave:

  x = 0:1:1800;
for i = 1:200
p = polyfit(x, input, 1);  % p gives the slope and offset
output= polyval(p, x);
input = clip(input, output);
endfor


the clip function clamps the input to the output. Clamps the input signal to the linear approximation.

  for i = 1:length(input)
if(input(i) > output(i))
input(i) = output(i);
end
endfor


To better explain what I'm looking for, I've included the rough image below. Of course, the red signal is the input and I'm looking for the equation of the blue line. Thanks for taking a look.

I hope I understand what your'e asking correctly

To get the minimum slope of the image I would run an calculate the slope at each point (should be fast enough) and than take the minimal one.

For example :

x = 1:.05:3000;
y = 100+x + 300*sin(2*pi*x/1000);
origin = [0 0] ; % or origin = [ x(1) y(1) ];
slope = (y - origin(2)) ./ (x - origin(1));
[ minslope , minidx ] = min( slope ) ;
angle = atand( minslope );
figure;plot( x,y ,'-');
hold on;
plot([ origin(1) x(minidx)] , [origin(2) y(minidx)] ,'-r');


Will give you this slope: Best of luck!,