Can someone provide a way to produce a random float between -Pi/2 and Pi/2 please?
I've tried...
float angleR = M_PI / arc4random_uniform(1000) - M_PI * 0.5;
But that doesn't work, lol.
float angle = (rand()/(float)RAND_MAX)*PI - PI/2;
You can easily adapt it to use arc4rand function (mind that its maximum value should be 0x100000000).
Something like this should work. If you want your result to be (roughly) uniformly random, you certainly don't want to divide by a uniformly random number (as that will bias you severely toward angles near, in your case, -π/2).
float angleR = ((float)arc4random_uniform(1000) - 500) * M_PI;
Related
I have question probably more in audio processing, than programming at all.
Just for fun, for understand little bit more I made my own plugin to measure impulse response of the filters. Something that allows me to see various equalisers curves. Similar like it happens in Waves QClone plugin - but qClone can also implement those curves to other signals, like regular EQ, but my plugin just measure those curves - as I know VST Plugin Analyser can do similar things.
But with my plugin the problem is accuracy of low frequences, somewhere below 150 Hz it starts to show crazy curves, inappropriate to real EQ changes. But above 150 Hz everything is OK (almost OK - it shows almost perfectly the EQ curves, but has problem to show curves for very narrow Q parameters).
And I was wondering almost whole week, what I do wrong, I tried to change resolution o measured frequencies range, also tried to change buffersize for one impulse. Don’t know what to do and it is annoying hardly :slight_smile: please help me.
My code for measure impulse response is mainly like that:
float freqResolution = 1000.0f; // it’s for set range of measured freq: float minFreqIndex = log10(20.0f)*freqResolution / log10(wSampleRate); float maxFreqIndex = log10(20000.0f)*freqResolution / log10(wSampleRate); for(int sample=(int)minFreqIndex; sample < maxFreqIndex; sample++) {
logScaleFreq = pow(10.0f, log10(wSampleRate) * (float)sample / (freqResolution-1.0f));
_Re = processor.filteredImpulse[0];
_Im = 0.0f;
for (int i=1; i<buffersize; ++i) {
_Re += processor.filteredImpulse[i] * cosf(-(float)i * 2 * double_Pi * logScaleFreq / wSampleRate);
_Im += processor.filteredImpulse[i] * sinf(-(float)i * 2 * double_Pi * logScaleFreq / wSampleRate);
}
float _Re_2 = pow(_Re, 2.0f);
float _Im_2 = pow(_Im, 2.0f);
float _Hf = pow(_Re_2 + _Im_2, 0.5f);
logScale_dB = 20*log10(_Hf);
Mainly it’s something like that, and then I print it as a logScale_dB in the function of logScaleFreq.
For any help, great thanks in advance.
Of course
processor.filteredImpulse[i]
It’s an filtered data from array of one impulse, something like [1, 0, 0, 0, 0, 0, 0, 0, 0…]
with length dependent on buffersize. But there is always only one 1, and many of zeros, like I think impulse should be :slight_smile:
While I write code like below
NSLog(#"%f",acos(cos(1)));
it returns 1 as result but if I tried to obtain result from degree instead of radian then facing problem as below
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
NSLog(#"%f",acos(cos(DEGREES_TO_RADIANS(1))));
then it returns 0.017453 instead of 1.If I write like below
NSLog(#"%f",acos(cos(DEGREES_TO_RADIANS(1)))*180/M_PI);
then I'm getting proper 1 as result. But if I'll write like below
NSString *a1 = [NSString stringWithFormat:#"%f",(cos(DEGREES_TO_RADIANS(1)))];
NSString *a2 = [NSString stringWithFormat:#"%f",(acos([a1 doubleValue])*180/M_PI)];
NSLog(#"%f",[a2 doubleValue]);
then I'm getting 0.998999 as result.
Then where the problem is?
Please help me
0.017453 is about how many radians are in one degree.
If you want your logging line to output 1, you'll need to convert back to degrees.
#define RADIANS_TO_DEGREES(angle) ((angle) * 180.0 / M_PI)
NSLog(#"%f", RADIANS_TO_DEGREES(acos(cos(DEGREES_TO_RADIANS(1)))));
Note that you may get a result like 0.9999, though, due to the limited accuracy of trigonometric functions, and of floats in general.
Arc Cosine is the inverse of cosine.
Cosine takes an angle as input, and returns a value ranging from -1 to 1.
Arc Cosine (acos) takes a value from -1 to 1 as input, and returns an angle.
The input to acos() is not an angle, so you should not try to convert it between degrees and radians. The result is an angle. That's what you convert.
So you would write
x = RADIANS_TO_DEGREES(acos(1)).
That would give you the angle at which the cosine is 1, expressed in degrees. (The value of acos(1) is 0, which is the same in degrees or radians.)
Better to use acos(0) as a test value. The cosine function = 0 at pi/2 (or 90 degrees.)
So
acos(0) == pi/2. (~1.5707)
RADIANS_TO_DEGREES(acos(0)) == 90.
EDIT:
I see now that in your question you were taking acos(cos(DEGREES_TO_RADIANS(1)), which is correct.
However, you still need to convert the result of acos() to degrees if you want your answer in degrees, as described above. (And Kevin's answer is also correct. My mistake)
Note that cos(1) is a little strange, in both degrees and radians. That isn't going to give an even answer in either.
cos(0) = 1, cos(pi) = -1, cos(2pi) = 1.
(or in degrees)
cos(180) = -1, and cos(360) = 1)
cos(1) in degrees is going to be an strange decimal value, and so will cos(1) in radians.
I am trying to build a degrees/radians calculator. Here are the functions I have.
func degreesToRadians(degrees: Double) -> Double {
return degrees * (M_PI / 180)
}
func radiansToDegrees(radians: Double) -> Double {
return radians * (180 / M_PI)
}
degreesToRadians(90)
radiansToDegrees(M_PI/4)
degreesToRadians(90) returns 1.5707963267949
radiansToDegrees(M_PI/4) returns 45.0
What I want to happen is in the Degrees to Radians function instead of 1.5707963267949 I want the output to be π/2. Is this even possible?
Thanks.
To represent 90 degrees as π/2, what you want to do is
consider the fraction of degrees over 180 (i.e. numerator of degrees and denominator of 180);
reduce this fraction (i.e. divide both the numerator and denominator by the greatest common factor); in the case of 90/180, the greatest common factor is, in fact, 90, yielding a reduced numerator of 1 and a reduced denominator of 2;
the result as a string representation of the fraction, i.e. something like
"\(reducedNumerator)π/\(reducedDenominator)"
Obviously, if either the numerator or denominator are 1, then you can suppress that portion of the string, but hopefully you get the basic idea.
Take a crack at that.
If you want exactly: "π/2", I don't think this is possible with double. You might get this in String, but not in a number. I think your best option would be to take an optional bool in which case result is returned as multiple of pi.
degreesToRadians(90, resultAsMultipleOfPi:true)
If this bool is true then 0.5 should be returned. You may need to do some rounding off to get well rounded numbers.
If you look closely on what is really M_PI you will see that it is predefined double value in math.h that equals
#define M_PI 3.14159265358979323846264338327950288 /* pi */
If you want more precision you can declare and use long double value instead.
You can directly use M_PI constant.
If you need in float format just use this,
let pi = Float(M_PI)
I'm developing map-based game using cocos2d v3.
I have a map with size of 2^19 points. On that map I have object that should move over time in short distance. About 60-70 points.
CGPoint offset = [_trajectoryPath offsetForNextPosition];
CGFloat x = self.position.x + offset.x;
CGFloat y = self.position.y + offset.y;
self.position = CGPointMake(x, y);
At such map size map position can be something like {300000, 40000} points.
When I try to add small step, lets say about {0.002f, 0.004f}, to animate object position I end up with still the same {300000, 40000} points...
I understand that it happens because of precision of float. Values normalised by map size, to be between 0 and 1.0, don't work either.
Is it possible somehow to increase precision of float type on iOS? Or may be someone cam give a hint about possible workaround for this problem?
Thanks.
mightee.cactus, I remember we had a similar issue while adding very small numbers to very large ones with float in c.
The solution was follows: we changed types to double to preserve accuracy; in your case you can make all the arithmetic operations with doubles and translate them into CGFloat just before use in CGPointMake.
I want to calculate the angle between two lines formed by three points(one of the points is the point of intersection of the two lines) using inverse cosine function as follows:
CGFloat a = initialPosition.x - origin.x;
CGFloat b = initialPosition.y - origin.y;
CGFloat c = currentPosition.x - origin.x;
CGFloat d = currentPosition.y - origin.y;
CGFloat angle = (180/M_PI) * acosf(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));
Unfortunately, acosf returns a value between 0 and pi only. How do I find a value between 0 and 2*pi (going, say, in the anti-clockwise manner)?
i don't know what language you are using, but typically there is an atan2 function that gives you a value from the full 360 degrees. in this case you need to use it twice and then add a little additional logic.
some pseudocode will help clear things up:
initialAngle = atan2(initialPosition.y - origin.y, initialPosition.x - origin.x)
currentAngle = atan2(currentPosition.y - origin.y, currentPosition.x - origin.x)
# angle is measured from x axis anti-clock, so lets find the value starting from
# initial and rotating anti-clock to current, as a positive number
# so we want current to be larger than initial
if (currentAngle < initialAngle) {currentAngle += 2 pi}
# and then we can subtract
return currentAngle - initialAngle
i know this isn't using acos, but that is multi-valued so to do so ends up using lots of logic about signs of differences that is bug-prone. atan2 is what you want.
found a simple solution...This comes from high school maths! First make an equation of a line made from the origin and the initialPosition of the form y = mx+c. A point lying on either side of this line will satisfy y < mx+c or y > mx+c, depending on where it is. If finding angles in the clockwise or anti-clockwise sense, make the following check:
currentPosition.y < (currentPosition.x *(initialPosition.y - origin.y) + (initialPosition .x * origin.y - initialPosition.y * origin.x)) / (initialPosition.x - origin.x)
If the above condition is true, then the line formed from origin and currentPosition makes an angle less than 180 deg (in the clockwise sense) with the line formed from origin and initialPosition. Otherwise it makes an angle more than 180 deg in the clockwise sense and less than 180 deg in the anti-clockwise sense...and so on. Depending on the requirement, the final angle is either the (angle returned by acos) or (360 - (angle returned by acos)).