Calculating the slope in the -ve, zero, and + ve regions
Imagina is a sine wave oscillating around the zero line. My task is to calculate the slope at a few random points along the wave using a fairly rough x-axis scale. (yes it has a real app)
When the wave is at + ve terriorory (above the zero line), the slope can be calculated using the formula:
Slope = (y(n) / y(n-1)) - 1
This level yilds + ve slopes up and -ve down.
The problem is that this needs to be toggled when we are in -ve territory, and then two more expressions are required when one of the vlaues is zero for just four expressions to be selected programmatically with conditional statements.
I would like to find an ONE expression that covers all four conditions, since this is at the center of a high motions and count algorithm!
I'm sure this would be a trivial solution for a math genius, but for those weary eyes it eludes me ...
Added:
The "sine wave" is actually a MACD indicator that is derived from the (random) price action of financial markets. an example would be here:
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_average_conve
The slope (of the thick black line in the bottom graph, for example) is what I need to calculate here, just how up or down (where the heading is + ve)
The problem is that both + ve and -ve slopes can occur above and below zero. Slope calculation can also occur using increments that cross the zero line and the zero line.
It would be nice to find a solution that doesn't involve a ton of IF statements ... for example, for example shifing all y values by a fixed amount so that they become + ve and then calculate the slope in the + ve region. I would have to pick a number that historically, y has never been lower, for example by a couple of orders of magnitude (99), and then I could do the calculation on a schedule and one slope?
a source to share
what do you mean by "sinusoid"? do you mean a mathematically generated graph, y = a sin(bx)
or a smooth curve tied to some experimental points via splines that just look like a sine wave when it oscillates around the x-axis? if the former, you can make a mathematical distinction and get the exact bias at any time. if the latter, the formula you are looking for is
slope(x) = (y(x-delta) - y(x+delta))/(2 * delta)
experiment with different delta values. there are no conventions; the numerator and denominator signs will automatically ensure that you get the correct sign for the slope.
a source to share
Tilt just
slope = y(n)-y(n-1)
Or, if you have units on the x-axis, divide that by one x-axis step (i.e. x (n) -x (n-1)).
This formula is a kind of definition, and it doesn't matter which side of the x axis you are on. (I say "grade" here as the slope is the limit of this equation when you try the bins very close together, so this equation is an approximation and other approximations can be used.)
Be aware that the slope of noisy data will appear even noisier.
I suspect the first few paragraphs of the wikipedia page will help you with this.
a source to share
If you are looking for the slope of a sine wave, this is done through the standard calculus functions.
The slope of the line f(x)=sin(x)
is equal f'(x)=cos(x)
.
However, based on your formula, I'm not entirely sure what you are after. Perhaps you need to clarify this.
But if you need an approach as shown in your question, I think your concern for performance is irrelevant here. To do this calculation as a single arithmetic expression, you will have to multiply by -1 depending on whether you are above or below the x-axis (and whether you go from f (n -1) to f (n) along that axis).
It will most likely be slower (or at least not faster) than a simple sequence of if statements when compiled to machine language.
As with all optimizations, you should first take the easiest approach to develop and then see if it has performance issues. Otherwise, you could waste a lot of effort in unnecessary promotions.
a source to share