How to use Heaviside/unit step Function in Arduino?

Hi Guys!
is there any way to use a Heavisidefuntion / unit step function?
It would help me a lot, because using a "correct modelled" Heaviside function would be much more easier than writing a lot of if/else functions...
greetz and thanks for your answers,
Sibi

What will you use it for? :slight_smile:

I've never heard about that particular algorithm before. How does it differ from: Sign function - Wikipedia
Too lazy for google...

int H(int x) {
 if (x < 0) {
   return 0;
 }
 return 1;
}

Or maybe even:

#define H(x) (x>=0)

Or you could use floats, of course. (not needed in the second example!)

I'd never heard of it; all I found on wikipedia seemed to be about finding mathematical equivalents for the function in systems (ie pure math) that don't have conditional capabilities. Since a CPU DOES does have conditionals, it's a lot easier.
There have been a couple of prior postings on FFT and DFTs, if that's your ultimate goal...

@AlphaBeta:
I need it for RGB Leds. I have a time based value for the pwm and normally you would use controll structures to find the current value, but with a heaviside function you can get your values with only one structure. By summing and multiplying Heaviside functions you can create all kinds of linear gradients.

the differnce between heaviside and sign is, that sign is 1 for x<0 and heaviside is 0 for x<0. Multiplying any function with Heaviside function crates a causal version of the original function.

@westfw:
i will try your first example and report back if it works that way

greetz

ok, the H(int x) works fine, BUT:
normaly you write a Heaviside function as "H(x-a)", so you can slide it by the amount of x to the right on the x-axis.
I tried that, but it didn't work:

float y = H((x[i])-a);

but I always get 0 for the y! example: if a=2 I should get this:
x y
-1 0
0 0
1 0
2 1
but it's alway zero!
does anybody know how to fix this?

float y = H(x[i])-a);