3

我试图弄清楚如何从我从麦克风收到的某个值中获得移动平均值。我有一个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 );
        }
    }
4

2 回答 2

3

我有其中一个:

// 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
于 2013-02-06T23:25:03.190 回答
0

您可以通过保存一个数组和元素总数来简单地做到这一点。由于它是移动平均值,因此元素计数可能高于数组元素的总计数,您需要计算此计数的剩余部分。

例如:

@property (nonatomic,retain) NSMutableArray* elements;   
@property (nonatomic,assign) NSUInteger lastIndex;
@property (nonatomic,assign) double average;

初始化元素并使其保存 N 个值(长值的 NSNumber)。您应该能够通过调用像这样的简单方法来更改平均值:

- (void) changeAverage: (long) newValue
{
    NSUInteger index= (++lastIndex) % elements.count;
    NSNumber* oldValue= elements[index]; // same as [elements objectAtIndex: index];
    average+= (double)newValue/elements.count -  (double)[oldValue longValue]/elements.count;   
    // The cast is to don't lose the decimal precision while dividing these numbers.
    [elements replaceObjectAtIndex: index withObject: @(newValue)];  
    // @(newValue) is the same as [NSNumber numberWithLong: newValue];
}
于 2013-02-06T23:41:44.290 回答