I'm trying to analyze a sound file (.m4a) to get the amplitudes over time and make a graph. I found some code online that works great (below). However, I would like to additionally filter out all sounds that are not in a targeted frequency range. E.g. I want to only graph the sounds that are between 1900-2100 Hz. How can I do this?
var processingBuffer = [Float](repeating: 0.0, count: Int(readFile.arrayFloatValues.count))
let sampleCount = vDSP_Length(readFile.arrayFloatValues.count)
vDSP_vabs(readFile.arrayFloatValues, 1, &processingBuffer, 1, sampleCount);
let samplesPerPixel = 1
let filter = [Float](repeating: 1.0 / Float(samplesPerPixel), count: Int(samplesPerPixel))
let downSampledLength = Int(readFile.arrayFloatValues.count / samplesPerPixel)
var downSampledData = [Float](repeating:0.0, count:downSampledLength)
vDSP_desamp(processingBuffer,
vDSP_Stride(samplesPerPixel),
filter, &downSampledData,
vDSP_Length(downSampledLength),
vDSP_Length(samplesPerPixel))
readFile.points = downSampledData.map{CGFloat($0)}
Edit
The recording is actually recorded from the device's microphone at an earlier time. Is it perhaps easier to apply a filter at the recording stage?