Ios4 iPhone加速计运动不平稳

Ios4 iPhone加速计运动不平稳,ios4,Ios4,我必须实现一个iPhone加速计应用程序,其中我必须根据加速计坐标移动图像。我的应用程序运行良好,但有时我的ImageView会移到顶部(视图y),然后消失 我使用了以下代码 UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer]; accel.delegate = self; accel.updateInterval = 1.0f/30.f; #define kFilteringFactor 0.1 - (void)a

我必须实现一个iPhone加速计应用程序,其中我必须根据加速计坐标移动图像。我的应用程序运行良好,但有时我的ImageView会移到顶部(视图y),然后消失

我使用了以下代码

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self; 
accel.updateInterval = 1.0f/30.f;

#define kFilteringFactor 0.1

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    static UIAccelerationValue rollingX = 0.0;
    static UIAccelerationValue rollingY = 0.0;

    // Subtract the low-pass value from the current value to get a simplified high-pass filter
    rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
    rollingY = (acceleration.y * kFilteringFactor) + (rollingY * (1.0 - kFilteringFactor));

    double accelX = acceleration.x - rollingX;
    double accelY = acceleration.y -  rollingY;

    // Use the acceleration data.
    float newX = containerView.center.x + ((float)accelX * 30.0f);
    float newY = containerView.center.y + ((float)accelY * 30.0f);
    containerView.center = CGPointMake(newX, newY);

}

我觉得这也有点令人沮丧,因为您使用的这个过滤器(这是很常见的)似乎没有像您预期的那样好。 最后,我决定计算加速度计最后8个样本的加权平均值,并将其作为最终值

问题是:我对旧样本的权重越大,最终的移动将越平稳,但延迟会感觉更清楚。另一方面,我对新样品的重量越重,最终的运动会越脏,但更精确(感觉到的延迟会越来越少)

我的解决方案是对中间的样本进行称重,使其比新的或旧的样本更重,并创建一个金字塔形的权重。我发现(不知道为什么,有人能解释吗?)牛顿的二项式权重是最好的

简单地说,在任何时候,我都会根据这个数组向最后8个样本中的每一个添加一个复制因子:1;7.21; 35; 35; 21; 7.1(使用Pascal三角形很容易找到这些值:)

代码如下所示:

  if ([d count]<8) {
    [d addObject:[NSNumber numberWithFloat:acceleration.x]];
}
else{
    [d removeObjectAtIndex:0];
    [d addObject:[NSNumber numberWithFloat:acceleration.x]];
}

NSMutableArray*binom=[[NSMutableArray alloc] init];
[binom addObject:[NSNumber numberWithInt:1]];
[binom addObject:[NSNumber numberWithInt:7]];
[binom addObject:[NSNumber numberWithInt:21]];
[binom addObject:[NSNumber numberWithInt:35]];
[binom addObject:[NSNumber numberWithInt:35]];
[binom addObject:[NSNumber numberWithInt:21]];
[binom addObject:[NSNumber numberWithInt:7]];
[binom addObject:[NSNumber numberWithInt:1]];

float s=0;
int j=0;
for (NSNumber* n in d){
    s+=[n floatValue]*[[binom objectAtIndex:j] intValue];
    j++;
}
s=s/128;

if([d count]我也觉得有点令人沮丧,因为您使用的这个过滤器(非常常见)似乎并没有像您预期的那样好。
最后,我决定计算加速度计最后8个样本的加权平均值,并将其作为最终值

问题是:我对旧样品称重越多,最终的运动会更平稳,但延迟会感觉更清楚。另一方面,我对新样品称重越多,最终的运动会更脏,但更精确(延迟会感觉越来越少)

我的解决方案是给中间的样本加上比新旧样本多得多的权重,然后创建一个权重金字塔。我发现(不知道为什么,有人能解释吗?)牛顿的二项式权重是最好的

简单地说,在任何时候,我都会根据这个数组向最后8个样本中的每一个添加一个复制因子:1;7;21;35;35;21;7;1(使用Pascal三角形很容易找到这些值:)

代码如下所示:

  if ([d count]<8) {
    [d addObject:[NSNumber numberWithFloat:acceleration.x]];
}
else{
    [d removeObjectAtIndex:0];
    [d addObject:[NSNumber numberWithFloat:acceleration.x]];
}

NSMutableArray*binom=[[NSMutableArray alloc] init];
[binom addObject:[NSNumber numberWithInt:1]];
[binom addObject:[NSNumber numberWithInt:7]];
[binom addObject:[NSNumber numberWithInt:21]];
[binom addObject:[NSNumber numberWithInt:35]];
[binom addObject:[NSNumber numberWithInt:35]];
[binom addObject:[NSNumber numberWithInt:21]];
[binom addObject:[NSNumber numberWithInt:7]];
[binom addObject:[NSNumber numberWithInt:1]];

float s=0;
int j=0;
for (NSNumber* n in d){
    s+=[n floatValue]*[[binom objectAtIndex:j] intValue];
    j++;
}
s=s/128;
if([d计数]