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