Audiokit strange amplitude values on small frequency
Asked Answered
D

1

9

I'm trying to build simple spectrum analyser using AudioKit library for iOS:

Yellow line is max, red is current - changes 10 times per second.

enter image description here

The problem is, that amplitude values for first few frequency points are too high, that i think is wrong.

The code (i removed parts, that not relevant to AudioKit):

AppDelegate init:

mic = AKMicrophone()

fftTap = AKFFTTap.init(mic!)
tracker = AKFrequencyTracker.init(mic)
let silence = AKBooster(tracker, gain: 0)

AudioKit.output = silence
try! AudioKit.start()

ViewController:

let micSampleRate = 44100

var tracker: AKFrequencyTracker!
var fftTap: AKFFTTap?

var maxValues = [Double](repeating: -400, count: 255)

let timeInterval = 0.1

var isPaused = true

let FFT_SIZE = 510

override func viewDidLoad() {
    super.viewDidLoad()

    tracker = (UIApplication.shared.delegate as! AppDelegate).tracker
    fftTap = (UIApplication.shared.delegate as! AppDelegate).fftTap

    let freqPreparedValue =  self.micSampleRate * 0.5 / self.FFT_SIZE

    Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true) { [unowned self] (timer) in
        if (!self.isPaused) {

            for i in stride(from: 0, to: self.FFT_SIZE - 2, by: 2) {

                let re = self.fftTap!.fftData[i]
                let im = self.fftTap!.fftData[i + 1]
                let normBinMag = 2.0 * sqrt(re * re + im * im)/self.FFT_SIZE
                //let freq = self.micSampleRate * 0.5 * i / self.FFT_SIZE
                let freq = freqPreparedValue * i
                let amplitude = 20.0 * log10(normBinMag)

                let i2 = i / 2
                if (self.maxValues[i2] < amplitude) {
                    self.maxValues[i2] = amplitude
                }

                //to understand, what is X and Y on image
                tickDataSeries.appendX(SCIGeneric(freq), y: SCIGeneric(amplitude))
                tickMaxDataSeries.appendX(SCIGeneric(freq), y: SCIGeneric(self.maxValues[i2]))

                print("bin: \(i/2) \t freq: \(freq)\t ampl.: \(amplitude)\t maxVal: \(self.maxValues[i2])\t re: \(re)\t im: \(im)" )
            }
        }
    }
}

Part of the output (not any real sounds, mostly silence around microphone):

bin: 0   freq: 0.0   ampl.: -118.073654770687    maxVal: -110.92564348456614     re: 3.5231216315878555e-05  im: 0.0003163595392834395
bin: 1   freq: 86.47058823529412     ampl.: -133.15079565501773  maxVal: -132.1323399190405  re: 5.5011274525895715e-05  im: 1.1023327715520281e-05
bin: 2   freq: 172.94117647058823    ampl.: -156.47641201587314  maxVal: -144.73820841794645     re: 3.040101546503138e-06   im: 2.3225734366860706e-06
bin: 3   freq: 259.4117647058823     ampl.: -166.16880958269164  maxVal: -152.1284594880522  re: 4.182010684417037e-07   im: 1.1816056257885066e-06
bin: 4   freq: 345.88235294117646    ampl.: -160.81829961464794  maxVal: -156.8105240841191  re: 2.272412530146539e-06   im: 4.711087910891365e-07
bin: 5   freq: 432.3529411764706     ampl.: -172.891584678714    maxVal: -162.2467662380227  re: 5.55981898742175e-07    im: 1.5817417420294078e-07

See, how fast amplitude is falling from -118 to -172 and then its bouncing around -170 - -200 values.

Isn't it wrong?

Dichlorodifluoromethane answered 28/6, 2019 at 10:16 Comment(0)
C
1

You are looking at the DC offset in bin 0. There is information on how to eliminate that here: http://blog.originlab.com/data-handling/how-to-remove-dc-offset-before-performing-fft

As for the rest of the bins, if this is an audio signal, 86 Hz and above are not difficult sounds to produce. It's above low E on a standard guitar, and it's above the 60 cycle hum (50 in the EU) that is produced by mains and is picked up in plenty of electronics. Given you describe this as "silence around a microphone", the signal you are reporting does not look atypical to me. It is likely dominated by noise sources (eg room noise, you shuffling in your chair, mains hum picked up in the electronics) that don't have lots of high frequency content.

So no, it's not wrong.

Edit: To add further clarification, you have a pretty reasonable frequency spectrum for a typical room and recording set up without special treatment. Your noise floor is more that 100db below clipping for your signal, so there is a good chance you aren't actually looking at a problem here.

Chairman answered 7/7, 2019 at 3:46 Comment(3)
Thanks for answer. At least it's clear now in which direction to research.Dichlorodifluoromethane
Sure. You may be better off with an actual sound source to analyze (sine wave, pink noise, music, speech), rather than trying to interpret the signal of "nothing."Chairman
I compared chart of song with professional sound analysis app and patterns looks mostly the same, but they just on different frequences. E.g. big spikes on 1k-2k Hz in my app and on 0.5k-1.5k in professional app. May be it happens because of different FFT size - mine is 510 (made also 1k and 2k, but on 2k phone is hot, while 1k is acceptable), and app FFT size was 2, 4, 8k.Dichlorodifluoromethane

© 2022 - 2024 — McMap. All rights reserved.