objective-c - Objective-C 中的移动平均线

标签 objective-c nsarray moving-average

<分区>

我正在尝试弄清楚如何从我从麦克风接收到的特定值中获取移动平均值。我有一个 frequencyChangedWithValue 函数调用我的测量方法。这意味着我在一秒钟内获得了高达 10 次的频率变化值。我现在感兴趣的是如何从所有这些变化的值中得出一个平均数。我该怎么做?

代码

- (void)frequencyChangedWithValue:(float)newFrequency{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    self.currentFrequency = newFrequency;

    [self performSelectorInBackground:@selector(filterFrequencyToGetBeats) withObject:nil];
    [pool drain];
    pool = nil;
}


- (void) filterFrequencyToGetBeats {

    if (self.currentFrequency > 1000 && pointInTime % 2 == 0)
    {
        tm_start = mach_absolute_time();
        pointInTime = pointInTime + 1;
    }
    else
        if (self.currentFrequency > 1000 && pointInTime % 2 == 1)
        {
            pointInTime = pointInTime + 1;

            tm_end = mach_absolute_time();
            tm_elapsed = tm_end - tm_start;
            mach_timebase_info(&info);
            tm_nanoSeconds = tm_elapsed * info.numer / info.denom;
            tm_milliSeconds = tm_nanoSeconds / (1000000);
            tm_seconds = (tm_milliSeconds/1000);

            fflush(stdout);
            printf ( "| allocateAudio: %5lld milliseconds, (%12lld nano seconds)\n",     tm_milliSeconds, tm_nanoSeconds );
        }
    }

最佳答案

我有其中之一:

// MovingAverage.h

@interface MovingAverage : NSObject

@property (readonly, nonatomic) float movingAverage;
@property (readonly, nonatomic) float cumulativeAverage;

- (id)initWithPeriod:(NSUInteger)period;
- (void)addDatum:(NSNumber *)datum;

@end


// MovingAverage.m

#import "MovingAverage.h"

@interface MovingAverage ()
@property (strong, nonatomic) NSMutableArray *queue;
@property (assign, nonatomic) NSUInteger period;
@property (assign, nonatomic) NSUInteger count;
@property (assign, nonatomic) float movingAverage;
@property (assign, nonatomic) float cumulativeAverage;
@end

@implementation MovingAverage

- (id)initWithPeriod:(NSUInteger)period {

    self = [self init];
    if (self) {
        _period = period;
        // with arc
        _queue = [NSMutableArray array];
        // without arc
        _queue = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)addDatum:(NSNumber *)datum {

    [self.queue insertObject:datum atIndex:0];

    float removed = 0;
    float datumf = [datum floatValue];

    if (self.queue.count > self.period) {
        removed = [[self.queue lastObject] floatValue];
        [self.queue removeLastObject];
    }

    self.movingAverage = self.movingAverage - (removed / self.period) + (datumf / self.period);

    // compute the cumulative average
    self.cumulativeAverage = self.cumulativeAverage + (datumf - self.cumulativeAverage) / ++self.count;
}

// if non-ARC
- (void)dealloc {
    [_queue release];
    [super dealloc];
}

@end

关于objective-c - Objective-C 中的移动平均线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14739481/

相关文章:

ios - NSArray 检索单个项目

不完整时间序列数据的运行平均值

python - 使用 numpy 计算滚动加权和

objective-c - 为什么 Objective-C 方法名称的最后一部分必须带一个参数(当有多个部分时)?

ios - 在 Objective C 中,如何使用 CATileLayer 进行分页聚类

ios - 由 NSMutableArray 支持的 NSArray 修改的 KVO 通知

iphone - NSDictionary 添加 NULL 键值

ios - 尝试定义字符数组时,我不断收到三个错误

ios - 获取 UITableViewCell 内的按钮单击