Moving average in Objective-C

I am trying to figure out how to get the moving average from a specific value that I am getting from my microphone. I have a function frequencyChangedWithValue

that calls my measurement method. This means that I get the changing frequency value up to 10 times per second. I am now wondering how to make the average of all these changing values. How to do it?

Code

- (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 );
        }
    }

      

+3


source to share


2 answers


I have one of them:



// 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

      

+3


source


You can simply do this by holding the array and the total number of elements. Since this is a moving average, the number of elements may be greater than the total number of elements in the array, and you need to calculate the remainder of this count.

For example:

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

      



Initialize the elements and force them to hold N values ​​(NSNumber of long values). You can change the average by simply calling a simple method like this:

- (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];
}

      

0


source







All Articles