|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "FrequencyAnalyzer.h" |
| 18 | +#include <math.h> |
| 19 | +#include "fft.h" |
| 20 | + |
| 21 | +FrequencyAnalyzer::FrequencyAnalyzer() : LoopbackProcessor() { |
| 22 | + mWindow.resize(WINDOW_SIZE); |
| 23 | + mIncoherentPower = 0.0; |
| 24 | + mWindowSum = 0.0; |
| 25 | + for (int i = 0; i < WINDOW_SIZE; i++) { |
| 26 | + mWindow[i] = 0.5 * (1.0 - cos(2.0 * M_PI * i / (WINDOW_SIZE - 1))); |
| 27 | + mIncoherentPower += mWindow[i] * mWindow[i]; |
| 28 | + mWindowSum += mWindow[i]; |
| 29 | + } |
| 30 | + mInputBuffer.resize(WINDOW_SIZE); |
| 31 | + mAverageBuffer.resize(WINDOW_SIZE / 2); |
| 32 | + |
| 33 | + mInputBufferIndex = 0; |
| 34 | + mFramesAccumulated = 0; |
| 35 | + mOutputPhase = 0.0f; |
| 36 | + |
| 37 | + std::lock_guard<std::mutex> lock(mFftBufferLock); |
| 38 | + mFftMagnitudeBuffer.clear(); |
| 39 | +} |
| 40 | + |
| 41 | +void FrequencyAnalyzer::reset() { |
| 42 | + LoopbackProcessor::reset(); |
| 43 | + mInputBufferIndex = 0; |
| 44 | + mOutputPhase = 0.0f; |
| 45 | + |
| 46 | + mAverageBuffer.clear(); |
| 47 | + mFramesAccumulated = 0; |
| 48 | + |
| 49 | + std::lock_guard<std::mutex> lock(mFftBufferLock); |
| 50 | + mFftMagnitudeBuffer.clear(); |
| 51 | +} |
| 52 | + |
| 53 | +void FrequencyAnalyzer::prepareToTest() { |
| 54 | + LoopbackProcessor::prepareToTest(); |
| 55 | + mPhaseIncrement = 2.0 * M_PI * SINE_WAVE_FREQUENCY / getSampleRate(); |
| 56 | + mMeasurementWindowFrames = MEASUREMENT_TIME_SEC * getSampleRate(); |
| 57 | +} |
| 58 | + |
| 59 | +LoopbackProcessor::result_code FrequencyAnalyzer::processInputFrame(const float* frameData, |
| 60 | + int channelCount) { |
| 61 | + float sample = frameData[getInputChannel()]; |
| 62 | + mInputBuffer[mInputBufferIndex++] = sample; |
| 63 | + if (mInputBufferIndex >= WINDOW_SIZE) { |
| 64 | + // Perform FFT |
| 65 | + std::lock_guard<std::mutex> lock(mFftBufferLock); |
| 66 | + CVector fftInput(WINDOW_SIZE); |
| 67 | + for (int i = 0; i < WINDOW_SIZE; i++) { |
| 68 | + double windowed = mInputBuffer[i] * mWindow[i]; |
| 69 | + fftInput[i] = Complex(windowed, 0); |
| 70 | + } |
| 71 | + fft(fftInput); |
| 72 | + |
| 73 | + // Accumulate magnitude |
| 74 | + std::vector<double> currentMagnitudes(WINDOW_SIZE / 2); |
| 75 | + for (int i = 0; i < WINDOW_SIZE / 2; i++) { |
| 76 | + double mag; |
| 77 | + if (mSignalType == 1) { // Sine |
| 78 | + mag = 2.0 * std::abs(fftInput[i]) / mWindowSum; |
| 79 | + } else { |
| 80 | + mag = 4.0 * std::abs(fftInput[i]) / std::sqrt(mIncoherentPower); |
| 81 | + } |
| 82 | + if (mag < 1e-9) mag = 1e-9; // to prevent log(0) |
| 83 | + currentMagnitudes[i] = mag; |
| 84 | + } |
| 85 | + mAverageBuffer.accumulate(currentMagnitudes.data(), |
| 86 | + currentMagnitudes.size()); |
| 87 | + mInputBufferIndex = 0; |
| 88 | + } |
| 89 | + |
| 90 | + mFramesAccumulated++; |
| 91 | + if (mFramesAccumulated >= mMeasurementWindowFrames) { |
| 92 | + // End of measurement window! Compute the average and save it to mFftMagnitudeBuffer |
| 93 | + std::lock_guard<std::mutex> lock(mFftBufferLock); |
| 94 | + if (mFftMagnitudeBuffer.empty()) { |
| 95 | + // First measurement window |
| 96 | + mFftMagnitudeBuffer.resize(WINDOW_SIZE / 2); |
| 97 | + } |
| 98 | + for (int i = 0; i < WINDOW_SIZE / 2; i++) { |
| 99 | + double avgMag = mAverageBuffer.getAverageAt(i); |
| 100 | + double dbfs = 20.0 * std::log10(avgMag); |
| 101 | + mFftMagnitudeBuffer[i] = static_cast<float>(dbfs); |
| 102 | + } |
| 103 | + mAverageBuffer.clear(); |
| 104 | + mFramesAccumulated = 0; |
| 105 | + } |
| 106 | + |
| 107 | + return RESULT_OK; |
| 108 | +} |
| 109 | + |
| 110 | +void FrequencyAnalyzer::setSignalType(int signalType) { |
| 111 | + mSignalType = signalType; |
| 112 | +} |
| 113 | + |
| 114 | +int FrequencyAnalyzer::getWindowSize() { |
| 115 | + return WINDOW_SIZE; |
| 116 | +} |
| 117 | + |
| 118 | +LoopbackProcessor::result_code FrequencyAnalyzer::processOutputFrame(float* frameData, |
| 119 | + int channelCount) { |
| 120 | + float output = 0.0f; |
| 121 | + if (mSignalType == 0) { // White noise |
| 122 | + output = mWhiteNoise.nextRandomDouble() * mAmplitude; |
| 123 | + } else if (mSignalType == 1) { // Sine |
| 124 | + output = sin(mOutputPhase) * mAmplitude; |
| 125 | + mOutputPhase += mPhaseIncrement; |
| 126 | + if (mOutputPhase >= M_PI * 2) mOutputPhase -= M_PI * 2; |
| 127 | + } else if (mSignalType == 2) { // Silence |
| 128 | + output = 0.0f; |
| 129 | + } |
| 130 | + if (channelCount == 2) { |
| 131 | + float leftGain = (mBalance <= 0.5f) ? 1.0f : 2.0f * (1.0f - mBalance); |
| 132 | + float rightGain = (mBalance >= 0.5f) ? 1.0f : 2.0f * mBalance; |
| 133 | + frameData[0] = output * leftGain; |
| 134 | + frameData[1] = output * rightGain; |
| 135 | + } else { |
| 136 | + for (int i = 0; i < channelCount; i++) { |
| 137 | + frameData[i] = output; |
| 138 | + } |
| 139 | + } |
| 140 | + return RESULT_OK; |
| 141 | +} |
| 142 | + |
| 143 | +std::string FrequencyAnalyzer::analyze() { |
| 144 | + return "FrequencyAnalyzer: Analysis the frequency magnitude"; |
| 145 | +} |
| 146 | + |
| 147 | +bool FrequencyAnalyzer::isDone() { |
| 148 | + return false; |
| 149 | +} |
| 150 | + |
| 151 | +int FrequencyAnalyzer::getFftMagnitude(float* buffer, int length) { |
| 152 | + std::lock_guard<std::mutex> lock(mFftBufferLock); |
| 153 | + if (mFftMagnitudeBuffer.empty()) { |
| 154 | + return 0; |
| 155 | + } |
| 156 | + int numToCopy = std::min(length, (int) (WINDOW_SIZE / 2)); |
| 157 | + for (int i = 0; i < numToCopy; i++) { |
| 158 | + buffer[i] = mFftMagnitudeBuffer[i]; |
| 159 | + } |
| 160 | + return numToCopy; |
| 161 | +} |
| 162 | + |
| 163 | +int FrequencyAnalyzer::getFftFrequencies(float* buffer, int length) { |
| 164 | + int numToCopy = std::min(length, (int) (WINDOW_SIZE / 2)); |
| 165 | + double sampleRate = getSampleRate(); |
| 166 | + for (int i = 0; i < numToCopy; i++) { |
| 167 | + buffer[i] = (float) (i * sampleRate / WINDOW_SIZE); |
| 168 | + } |
| 169 | + return numToCopy; |
| 170 | +} |
0 commit comments