Linear regression in Objective-C - objective-c

I´m trying to implement a method that fits a line to a set of points in 2D.
I wrote the following code that reads the data from two Array (X, Y coordinate)
and should calculate the parameters of the best fitting line with the least squares method.
I used the formulas given here:
mathworld.wolfram
- (void) linearRegressionOfUserAcceleration
{
double avgX = [[_accelBufferX valueForKeyPath:#"#avg.doubleValue"] doubleValue];
double avgY = [[_accelBufferY valueForKeyPath:#"#avg.doubleValue"] doubleValue];
int n = _accelBufferX.count;
double ssX, ssY, ssXY;
ssX = ssY = ssXY = 0;
int i;
// Sum of squares X, Y & X*Y
for (i = 0; i < n; i++)
{
ssX += pow([[_accelBufferX objectAtIndex:i] doubleValue],2);
ssY += pow([[_accelBufferY objectAtIndex:i] doubleValue],2);
ssXY += [[_accelBufferX objectAtIndex:i] doubleValue] * [[_accelBufferY objectAtIndex:i] doubleValue];
}
ssX = ssX - n * pow(avgX,2);
ssY = ssY - n * pow(avgY,2);
ssXY = ssXY - n * avgX * avgY;
// Best fit of line y_i = a + b * x_i
b = ssXY / ssX;
a = avgY - b * avgX;
// Correlationcoefficent gives the quality of the estimate: 1 = perfect to 0 = no fit
corCoeff = pow(ssXY,2) / ssX * ssY;
NSLog(#"n: %d, a: %f --- b: %f --- cor: %f --- avgX: %f --- avgY: %f --- ssX: %f - ssY: %f - ssXY: %f", n, a, b, corCoeff, avgX, avgY, ssX, ssY, ssXY);
}
I get outputs like this:
n: 15, a: -0.095204 --- b: 0.929245 --- cor: 3.567163 --- avgX: -0.017827 -- avgY: -0.111770 --- ssX: 2.176048 - ssY: 1.898429 - ssXY: 2.022081
The resulting line does not fit the data at all and although the corelationCoefficient is sometimes bigger than one, which IMHO should never happen if everything works correctly.
Does anyone see any errors in my implementation?
- EDIT -
This is the corrected code, following the tip from CRD.
I used this to extract the direction vector of the sampled userAcceleration in the horizontal plane between two steps, to get the step direction.
This worked for me:
- (void) linearRegressionOfUserAcceleration
{
NSUInteger n = _accelBufferX.count;
double ax, ay, sX, sY, ssX, ssY, ssXY, avgX, avgY;
// Sum of squares X, Y & X*Y
for (NSUInteger i = 0; i < n; i++)
{
#synchronized(self) {
ax = [[_accelBufferX objectAtIndex:i] doubleValue];
ay = [[_accelBufferY objectAtIndex:i] doubleValue];
}
sX += ax;
sY += ay;
ssX += ax * ax;
ssY += ay * ay;
ssXY += ax * ay;
}
avgX = sX / n;
avgY = sY / n;
radius = hypot(avgX, avgY);
ssX = ssX - n * (avgX * avgX);
ssY = ssY - n * (avgY * avgY);
ssXY = ssXY - n * avgX * avgY;
// Best fit of line y_i = a + b * x_i
b = ssXY / ssX;
a = (avgY - b * avgX);
theta = atan2(1, b);
// Correlationcoefficent gives the quality of the estimate: 1 = perfect to 0 = no fit
corCoeff = (ssXY * ssXY) / (ssX * ssY);
NSLog(#"n: %d, a: %f --- b: %f --- cor: %f --- avgX: %f -- avgY: %f --- ssX: %f - ssY: %f - ssXY: %f", n, a, b, corCoeff, avgX, avgY, ssX, ssY, ssXY);
}

Put in some known data you can check by hand, e.g. {1,1}, {2,2}, {3,3}. Are the averages correct? If so move on to the sums, etc. The error will reveal itself.
On the code itself you can make it clearer, and incidentally more efficient, by dropping the calls to #"avg.doubleValue" and producing all your sums in a single loop:
// Sum of X, Y, X^2, Y^2 & X*Y
for (NSUInteger i = 0; i < n; i++)
{
double x = [[_accelBufferX objectAtIndex:i] doubleValue];
double y = [[_accelBufferY objectAtIndex:i] doubleValue];
sX += x;
sY += y;
ssX += x * x;
ssY += y * y;
ssXY += x * y;
}

Related

Calculate an intercept velocity between two SKSpriteNodes

I created a "ship" node to move along the circular path as follows:
self.orbit = [OrbitManager getCirclePathWithCenter:centerRealPt radius:radius startingAngle:angelFromCenter isClockwise:self.isClockwise];
SKAction* orbitAction = [SKAction followPath:self.orbit asOffset:NO orientToPath:YES speed:300];
[self.ship.node runAction:orbitAction];
and I have a cannon which shoots a bullet by applying a velocity to it as follows:
bullet.node.physicsBody.velocity = [ActionHelper getVelocityFrom:bullet.node toNodeB:self.target speed:bullet.speed];
as the ship is moving along the path. But the bullet will miss every time. How can I calculate the position which the cannon should aim at, with a given speed?
This is my Objective-C (it is actually a C function) Solution to fire a projectile in to a moving target.
You can look at the derivations In this SO topic
This will give you a hit point and an angle to shoot,
you can simply translate it to velocity because you know the angle and a projectile speed, it will be something like:
`CGVector Velocity = CGVectorMake(speed * cos(theta), speed * sin(theta));`
BOOL calculateAngleToShoot(CGVector targetVelocity, CGPoint targetPosition, CGPoint selfPos,CGFloat projectileSpeed,CGFloat *theta, CGPoint * hitPoint)
{
CGFloat dx = targetPosition.x - selfPos.x;
CGFloat dy = targetPosition.y - selfPos.y;
CGVector v = targetVelocity;
CGFloat a = v.dx * v.dx + v.dy * v.dy - projectileSpeed * projectileSpeed;
CGFloat b = 2 * (v.dx * dx + v.dy * dy);
CGFloat c = v.dx * v.dx + v.dy * v.dy;
CGFloat q = b * b - 4 * a * c;
if (q < 0)
{
//Dead End;
return NO;
}
CGFloat t = ((a < 0 ? -1 : 1) * sqrt(q) - b) / (2 * a);
// Aim for where the target will be after time t
dx += t * v.dx;
dy += t * v.dy;
*theta = atan2(dy, dx);
*hitPoint = CGPointMake(targetPosition.x + v.dx * t, targetPosition.y + v.dy * t);
return YES;
}
After some investigation I got how to get the answer
first I need to get the distance(d) between the target and the center
and the time for the bullet from center to the target.
since the ship is moving along the circle, so the radius is also equals to distance(d)
CGFloat timeToArriveTarget = bullet.speed/distance;
CGFloat angularSpeed = bullet.speed/distance;
Find the angle moved within this period of time
CGFloat angle = angularSpeed * timeToArriveTarget;
CGFloat x = self.target.position.x;
CGFloat y = self.target.position.y;
CGFloat a = bullet.node.position.x;
CGFloat b = bullet.node.position.y;
and finally using this formula:
details are give by this link https://math.stackexchange.com/a/266837
CGPoint targetPt = CGPointMake((x - a) * cos(angle) - (y - b) * sin(angle) + a, (x - a) * sin(angle) + (y - b) * cos(angle) + b);
bullet.node.physicsBody.velocity = [ActionHelper getVelocityFrom:bullet.node.position toPtB:targetPt speed:bullet.speed];
the getVelocity function is given by
+(CGVector)getVelocityFrom:(CGPoint)ptA toPtB:(CGPoint)ptB speed:(CGFloat)speed{
CGPoint targetPosition = ptB;
CGPoint currentPosition = ptA;
double angle = [MathHelper getRotateAngleFrom:currentPosition toTargetPosition:targetPosition];
double velocityX = speed * cos(angle);
double velocityY = speed * sin(angle);
CGVector newVelocty = CGVectorMake(velocityX, velocityY);
return newVelocty;
}

Convolution matrix sharpen filter

i trying to implement sharpen convolution matrix filter for image.For this i create matrix 3x3. Maybe i did something wrong with formula?Also i tried other sharpen matrix but it didnt help. Color value could be larger then 255 or smaller then zero so i decide to give some limits on this(0 255).Is it correct solution?
static const int filterSmallMatrixSize = 3;
static const int sharpMatrix[3][3] = {{-1, -1, -1},{-1, 9, -1},{-1, -1, -1}};
some define
#define Mask8(x) ( (x) & 0xFF )
#define R(x) ( Mask8(x) )
#define G(x) ( Mask8(x >> 8 ) )
#define B(x) ( Mask8(x >> 16) )
#define A(x) ( Mask8(x >> 24) )
#define RGBAMake(r, g, b, a) ( Mask8(r) | Mask8(g) << 8 | Mask8(b) << 16 | Mask8(a) << 24 )
and algorithm
- (UIImage *)processSharpFilterUsingPixels:(UIImage *)inputImage
{
UInt32 *inputPixels;
CGImageRef inputCGImage = [inputImage CGImage];
NSUInteger inputWidth = CGImageGetWidth(inputCGImage);
NSUInteger inputHeight = CGImageGetHeight(inputCGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
NSUInteger bitsPerComponent = 8;
NSUInteger inputBytesPerRow = bytesPerPixel * inputWidth;
inputPixels = (UInt32 *)calloc(inputHeight * inputWidth, sizeof(UInt32));
CGContextRef context = CGBitmapContextCreate(inputPixels, inputWidth, inputHeight,
bitsPerComponent, inputBytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, inputWidth, inputHeight), inputCGImage);
for (NSUInteger j = 1; j < inputHeight - 1; j++)
{
for (NSUInteger i = 1; i < inputWidth - 1; i++)
{
Float32 newRedColor = 0;
Float32 newGreenColor = 0;
Float32 newBlueColor = 0;
Float32 newA = 0;
for (int filterMatrixI = 0 ; filterMatrixI < filterSmallMatrixSize ; filterMatrixI ++)
{
for (int filterMatrixJ = 0; filterMatrixJ < filterSmallMatrixSize; filterMatrixJ ++)
{
UInt32 * currentPixel = inputPixels + ((j + filterMatrixJ - 1) * inputWidth) + i + filterMatrixI - 1;
int color = *currentPixel;
newRedColor += (R(color) * sharpMatrix[filterMatrixI][filterMatrixJ]);
newGreenColor += (G(color) * sharpMatrix[filterMatrixI][filterMatrixJ]);
newBlueColor += (B(color)* sharpMatrix[filterMatrixI][filterMatrixJ]);
newA += (A(color) * sharpMatrix[filterMatrixI][filterMatrixJ]);
}
}
int r = MAX( MIN((int)newRedColor,255), 0);
int g = MAX( MIN((int)newGreenColor,255), 0);
int b = MAX( MIN((int)newBlueColor,255), 0);
int a = MAX( MIN((int)newA,255), 0);
UInt32 *currentMainImagePixel = inputPixels + (j * inputWidth) + i;
*currentMainImagePixel = RGBAMake(r,g,b,a);
}
}
CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage * processedImage = [UIImage imageWithCGImage:newCGImage];
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
free(inputPixels);
return processedImage;
}
As result i have this
Consider these are pixels in the middle of image:
|_|_|_|_|
|_|_|_|_|
|_|_|_|_|
|_|_|_|_|
Since you are updating image in place, this is how it looks somewhere in the middle of sharpen cycle:
|u|u|u|u|
|u|u|u|u|
|u|c|_|_|
|_|_|_|_|
Where u stands for updated pixel, c for current. So his new color depends on color of surround pixels, half of which are from already sharpened image, half from origin. To fix it we need a copy of original image's pixels:
...
CGContextDrawImage(context, CGRectMake(0, 0, inputWidth, inputHeight), inputCGImage);
UInt32 *origPixels = calloc(inputHeight * inputWidth, sizeof(UInt32));
memcpy(origPixels, inputPixels, inputHeight * inputWidth * sizeof(UInt32));
for (NSUInteger j = 1; j < inputHeight - 1; j++) {
...
And now we only need to change one line to get our current pixels from original image
//changed inputPixels -> origPixels
UInt32 * currentPixel = origPixels + ((j + filterMatrixJ - 1) * inputWidth) + i + filterMatrixI - 1;
Here are some examples of how it works compared to not updated filter (link is dropbox, sorry about that). I've tried different matrices, and as for me the best was somewhere around
const float sharpMatrix[3][3] = {{-0.3, -0.3, -0.3},{-0.3, 3.4, -0.3},{-0.3, -0.3, -0.3}}
Also, I need to notice that this way of keeping original image is not optimal. My fix basically doubles amount of memory consumed. It could be easily done via holding only two lines of pixels, and I'm sure there are even better ways.

Parametric Expression of a Bezier Curve

I have used the Parametric Expression of a Bezier Curve to locate a point along my curve and it's working as it should. The problem is I'm setting my t value as the percentage of the y axis and unfortunately (and obviously) it doesn't correlate because my curve is longer than my Y axis. So in this program if I set my Y Value to 75 I want to return the point on my line that sits at the Y value of 25 (the inverse because in iOS the (0, 0) sits at the top left instead of the bottom left as my graph reads). Currently setting my Y value retunes the point on my curve at 75% which has a Y of 15.62.
Anyone have a recommendation of how to get the point on my curve at Y instead of at 75%?
This is a follow-up question to a previous question, finding a point on a path, but I felt it was different enough to warrant its own thread.
#import "GraphView.h"
#interface GraphView ()
{
float yVal;
}
#end
#implementation GraphView
#synthesize myLabel, yValue;
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
yVal = 50;
}
return self;
}
- (IBAction)yValueTextField:(id)sender
{
yVal = yValue.text.intValue;
[self resignFirstResponder];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
float t = yVal / 100;
// Starting point
float p1x = 0;
float p1y = 100;
// Control point 1
float c1x = 50;
float c1y = 100;
// Control point 2
float c2x = 50;
float c2y = 0;
// End Point
float p2x = 100;
float p2y = 0;
CGPoint p1 = CGPointMake(p1x, p1y);
CGPoint c1 = CGPointMake(c1x, c1y);
CGPoint c2 = CGPointMake(c2x, c2y);
CGPoint p2 = CGPointMake(p2x, p2y);
// Cubic Bezier Curver Parmetic Expression
float X = pow((1 - t), 3) * p1x + 3 * pow((1 - t), 2) * t * c1x + 3 * (1 - t) * pow(t, 2) * c2x + pow(t, 3) * p2x;
float Y = pow((1 - t), 3) * p1y + 3 * pow((1 - t), 2) * t * c1y + 3 * (1 - t) * pow(t, 2) * c2y + pow(t, 3) * p2y;
myLabel.text = [NSString stringWithFormat:#"Coord = %.2f, %.2f", X, Y];
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake((X - 2), (Y - 2), 4, 4)];
[[UIColor blackColor] setFill];
[circle fill];
UIBezierPath *curve = [[UIBezierPath alloc] init];
[curve moveToPoint:p1];
[curve addCurveToPoint:p2 controlPoint1:c1 controlPoint2:c2];
[curve setLineWidth:1];
[[UIColor blueColor] setStroke];
[curve stroke];
}
#end
Here is my solution to finding my point on my bezier curve. For more background regarding this see another related post of mine --> finding a point on a path
#import "Calculation.h"
#implementation Calculation
#synthesize a, b, c, d, xy;
- (float) calc
{
float squareRootCalc =
sqrt(
6*pow(xy,2)*b*d
+4*a*pow(c,3)
-3*pow(b,2)*pow(c,2)
+9*pow(xy,2)*pow(c,2)
-6*a*c*b*d
+6*a*xy*c*b
-18*pow(xy,2)*b*c
+6*a*pow(xy,2)*c
-12*a*xy*pow(c,2)
-2*pow(a,2)*xy*d
+pow(a,2)*pow(d,2)
+4*pow(b,3)*d
+pow(xy,2)*pow(d,2)
-4*pow(b,3)*xy
-4*pow(c,3)*xy
+pow(a,2)*pow(xy,2)
+6*c*b*d*xy
+6*a*c*d*xy
+6*a*b*d*xy
-12*pow(b,2)*d*xy
+6*xy*c*pow(b,2)
+6*xy*b*pow(c,2)
-2*a*pow(xy,2)*d
-2*a*xy*pow(d,2)
-6*c*d*pow(xy,2)
+9*pow(xy,2)*pow(b,2)
-6*a*pow(xy,2)*b)
;
float aCalc = 24*c*d*xy + 24*a*pow(c,2) - 36*xy*pow(c,2) + 4 * squareRootCalc * a;
float bCalc = -12 * squareRootCalc * b;
float cCalc = 12 * squareRootCalc * c;
float dCalc = -4 * squareRootCalc * d;
float xyCalc =
24*xy*a*b
-24*xy*b*d
-12*b*a*d
-12*c*a*d
-12*c*b*d
+8*xy*a*d
+8*pow(b,3)
+8*pow(c,3)
+4*pow(a,2)*d
+24*pow(b,2)*d
-4*xy*pow(a,2)
-4*xy*pow(d,2)
+4*a*pow(d,2)
-12*c*pow(b,2)
-12*b*pow(c,2)
-12*a*b*c
-24*xy*a*c
+72*xy*c*b
-36*xy*pow(b,2)
;
float cubeRootCalc = cbrt(aCalc + bCalc + cCalc + dCalc + xyCalc);
float denomCalc = (a-3*b+3*c-d);
float secOneCalc = 0.5 * cubeRootCalc / denomCalc;
float secTwoCalc = -2 * ((a*c - a*d - pow(b,2) + c*b + b*d - pow(c,2)) / (denomCalc * cubeRootCalc));
float secThreeCalc = (a - 2*b + c) / denomCalc;
return secOneCalc + secTwoCalc + secThreeCalc;
}
- (Calculation *) initWithA:(float)p0 andB:(float)p1 andC:(float)p2 andD:(float)p3 andXy:(float)xyValue
{
self = [super init];
if (self) {
[self setA:p0];
[self setB:p1];
[self setC:p2];
[self setD:p3];
[self setXy:xyValue];
}
return self;
}
- (void) setA:(float)p0 andB:(float)p1 andC:(float)p2 andD:(float)p3 andXy:(float)xyValue
{
[self setA:p0];
[self setB:p1];
[self setC:p2];
[self setD:p3];
[self setXy:xyValue];
}
#end

Converting HSB color to RGB in Objective C

I'm trying to convert HSB values to RGB values using this algorithm, but I am not getting correct values. I have a fixed s of 29, a fixed b of 100, and am generating random integers between 0-360 for the h value, and feeding them into the function to get RGB back:
float h = (arc4random() % 360);
float s = 29;
float b = 100;
HSL2RGB(h, s, b, &red, &green, &blue);
NSLog(#"r:%f g:%f b:%f", red, green, blue);
output:
r:2971.000000 g:2971.000000 b:2971.000000
I tried this too:
float h = (arc4random() % 360)/1000.0;
float s = 0.29;
float b = 1.0;
HSL2RGB(h, s, b, &red, &green, &blue);
NSLog(#"r:%f g:%f b:%f", red, green, blue);
output:
r:1.000000 g:1.000000 b:1.000000
Am I doing something wrong, or is this algorithm messed up?
You can get the RGB components of a color constructed using HSB directly and easily using UIKit.
UIColor *color = [UIColor colorWithHue: hue saturation: saturation
brightness: brightness alpha: alpha];
if ( [color getRed: &red green: &green blue: &blue alpha: &alpha] ) {
// color converted
}
If all you care about is using the color, you can skip the if and just use it.
Per Apple's UIColor documentation:
If the color is in a compatible color space, the color is converted into RGB format and its components are returned to your application. If the color is not in a compatible color space, the parameters are unchanged.
A compatible color space in this case is RGB or HSB.
I don't know your algorithm but I should you to use this algorigthm:
typedef struct
{
double r; // percent [0 - 1]
double g; // percent [0 - 1]
double b; // percent [0 - 1]
double a; // percent [0 - 1]
} RGBA;
typedef struct
{
double h; // angle in degrees [0 - 360]
double s; // percent [0 - 1]
double v; // percent [0 - 1]
} HSV;
- (RGBA)RGBfromHSV:(HSV)value
{
double hh, p, q, t, ff;
long i;
RGBA out;
out.a = 1;
if (value.s <= 0.0) // < is bogus, just shuts up warnings
{
if (isnan(value.h)) // value.h == NAN
{
out.r = value.v;
out.g = value.v;
out.b = value.v;
return out;
}
// error - should never happen
out.r = 0.0;
out.g = 0.0;
out.b = 0.0;
return out;
}
hh = value.h;
if(hh >= 360.0) hh = 0.0;
hh /= 60.0;
i = (long)hh;
ff = hh - i;
p = value.v * (1.0 - value.s);
q = value.v * (1.0 - (value.s * ff));
t = value.v * (1.0 - (value.s * (1.0 - ff)));
switch(i)
{
case 0:
out.r = value.v;
out.g = t;
out.b = p;
break;
case 1:
out.r = q;
out.g = value.v;
out.b = p;
break;
case 2:
out.r = p;
out.g = value.v;
out.b = t;
break;
case 3:
out.r = p;
out.g = q;
out.b = value.v;
break;
case 4:
out.r = t;
out.g = p;
out.b = value.v;
break;
case 5:
default:
out.r = value.v;
out.g = p;
out.b = q;
break;
}
return out;
}
I used it into own of my project and I had no problem with it. :)

calculations in objective-c not returning the correct value

Please check out this piece of code, more specifically the hourStep calculations.
int h = [[timeArray objectAtIndex:0] intValue];
int m = [[timeArray objectAtIndex:1] intValue];
int s = [[timeArray objectAtIndex:2] intValue];
int mm = [[timeArray objectAtIndex:3] intValue];
NSLog([NSString stringWithFormat:#"time h:%d, m:%d, s:%d, mm:%d", h, m, s, mm]);
//time h:13, m:7, s:55, mm:105
float hourStep1 = m / 60;
float hourStep2 = h + hourStep1;
float hourStep3 = hourStep2 / 24;
float hourStep4 = hourStep3 * 15;
int hour1 = ceil(hourStep4);
NSLog([NSString stringWithFormat:#"hourStep1: %f, hourStep2: %f, hourStep3: %f, hourStep4: %f result: %d", hourStep1, hourStep2, hourStep3, hourStep4, hour1]);
//hourStep1: 0.000000, hourStep2: 13.000000, hourStep3: 0.541667, hourStep4: 8.125000 result: 9
float hourStep5 = ((h + (m / 60)) / 24) * 15;
NSLog([NSString stringWithFormat:#"hourStep5: %f", hourStep5]);
//hourStep5: 0.000000
I have broken down the calculation into the various steps to get the correct answer but can anyone explain why hourStep5 doesn't produce what hourStep4 produces?
It's the difference between integer division and floating-point division.
This line:
float hourStep3 = hourStep2 / 24;
evaluates to 13.0f / 24 which results in 0.541667f (floating-point division).
In the combined calculation, you are only dealing with integers (without converting to floats in between), so
(h + (m / 60)) / 24
evaluates to 13 / 24 which equals 0 (integer division). Change it to
(h + (m / 60)) / 24.0f
and you will get the same result as above.
In your line
float hourStep5 = ((h + (m / 60)) / 24) * 15;
The calculation is performed in int, not in float. Note that in C (and thus in Objective-C) the equation on the right hand side of = is performed first, without caring whatsoever the type of the left hand side (in this case, float.)
Use
float hourStep5 = ((h + (m / 60.0)) / 24) * 15;
instead.
The whole calculation of hourStep5 will be treated as integer.
Try casting both h and m into floats in that line:
float hourStep5 = (( (float) h + ( (float) m / 60)) / 24) * 15;