int a = sgn(amplitude_scale * sin(phase) * maxval);
fval.f = (float) a;
res = fval.i;
I don't know what you're trying to do with that union, but what you're doing there will give you complete garbage. Why aren't you just using 'a' directly like you do with 'b' right below it?
EDIT:
oh wait... I get it. You're taking the float's binary data and reinterpretting it as ints so you can bitshift it. Ugh.
In that case it would help to know some of the constants so we can narrow the problem down.
Is to_unsigned true?
what about is_float?
and big_endian?
Shouldn't the correct syntax be: amplitude_scale * sgn(sin(phase)) * maxval;
I mean do you not want to amplify the square wave by amplitude_scale and maxval (because you do so with the sinusoid one)?
The sgn() function looks fine to me. The only problem which comes to my mind now is somewhere an integer gets overflown but I don't think that's the case. Just have in mind that signed int has a max value of 32768 on most of the systems.
What do you have when you expect a square wave? A null signal? A garbage signal?
I tried googling to see if sgn() could be a function name already used by some other library. Google has already found your question in 3 forums in the first 2 results pages :-P Try renaming it in case it would be the problem?
Please don't use case sensitive function names. Try being more expresive. Then you can read the code without having to chase down the function to see what it does. Maybe:
int return_sign_of_argument( double d );
More expressive names really pay off in the long run.
PS, the return value is not really a square wave. Its just a negative or positive one. It becomes a square wave or sine wave only when combined with is predecessors and successors in something like a chart or on an oscope. The function does not return a square wave, just a value.
Yes thanks for all your help guys.
The solution: amplitude_scale * sgn(sin(phase)) * maxval;
I originally placed the sgn function for the entire calculation instead of the sin() function only.