|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 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 "DataPathAnalyzer.h" |
| 18 | +#include <sstream> |
| 19 | +#include <iomanip> |
| 20 | +#include "fft.h" |
| 21 | + |
| 22 | +double DataPathAnalyzer::calculatePhaseError(double p1, double p2) { |
| 23 | + double diff = p1 - p2; |
| 24 | + // Wrap around the circle. |
| 25 | + while (diff > M_PI) { |
| 26 | + diff -= (2 * M_PI); |
| 27 | + } |
| 28 | + while (diff < -M_PI) { |
| 29 | + diff += (2 * M_PI); |
| 30 | + } |
| 31 | + return diff; |
| 32 | +} |
| 33 | + |
| 34 | +BaseSineAnalyzer::result_code DataPathAnalyzer::processInputFrame(const float *frameData, int channelCount) { |
| 35 | + switch (mSignalType) { |
| 36 | + case Chirp: { |
| 37 | + if (mFftBuffer.size() != mFftBufferSize) { |
| 38 | + mFftBuffer.resize(mFftBufferSize); |
| 39 | + } |
| 40 | + if (mFftBufferIndex == 0) { |
| 41 | + mFftBufferStartFrame = mFrameCounter; |
| 42 | + } |
| 43 | + float sample = frameData[getInputChannel()]; |
| 44 | + mFftBuffer[mFftBufferIndex++] = sample; |
| 45 | + if (mFftBufferIndex >= mFftBufferSize) { |
| 46 | + // Perform Spectrogram Analysis |
| 47 | + std::stringstream report; |
| 48 | + report << "Chirp analysis (peak frequency per window):\n"; |
| 49 | + |
| 50 | + std::vector<double> peakFreqs; |
| 51 | + for (int i = 0; i + mSpectrogramWindowSize <= mFftBufferSize; i += mSpectrogramHopSize) { |
| 52 | + CVector fftInput(mSpectrogramWindowSize); |
| 53 | + for (int j = 0; j < mSpectrogramWindowSize; j++) { |
| 54 | + fftInput[j] = Complex(mFftBuffer[i + j], 0); |
| 55 | + } |
| 56 | + fft(fftInput); |
| 57 | + |
| 58 | + double maxMag = 0; |
| 59 | + int peakBin = 0; |
| 60 | + long frameInChirp = mFftBufferStartFrame + i + mSpectrogramWindowSize / 2; |
| 61 | + double maxFreq = kChirpEndFrequency; |
| 62 | + if (maxFreq > getSampleRate() / 2.0) { |
| 63 | + maxFreq = getSampleRate() / 2.0; |
| 64 | + } |
| 65 | + double expectedFreq = kChirpStartFrequency + (maxFreq - kChirpStartFrequency) * frameInChirp / (getSampleRate() * kChirpDurationSeconds); |
| 66 | + int expectedBin = (int)(expectedFreq * mSpectrogramWindowSize / getSampleRate()); |
| 67 | + int searchRadius = 50; // search in a window of 100 bins |
| 68 | + int startBin = std::max(1, expectedBin - searchRadius); |
| 69 | + int endBin = std::min(mSpectrogramWindowSize / 2, expectedBin + searchRadius); |
| 70 | + |
| 71 | + for (int k = startBin; k < endBin; k++) { |
| 72 | + double mag = std::abs(fftInput[k]); |
| 73 | + if (mag > maxMag) { |
| 74 | + maxMag = mag; |
| 75 | + peakBin = k; |
| 76 | + } |
| 77 | + } |
| 78 | + double peakFreq = (double)peakBin * getSampleRate() / mSpectrogramWindowSize; |
| 79 | + peakFreqs.push_back(peakFreq); |
| 80 | + report << std::fixed << std::setprecision(0) << peakFreq << " Hz\n"; |
| 81 | + } |
| 82 | + |
| 83 | + // Check if frequencies are monotonically increasing |
| 84 | + bool passed = true; |
| 85 | + for (size_t i = 1; i < peakFreqs.size(); i++) { |
| 86 | + if (peakFreqs[i] < peakFreqs[i-1]) { |
| 87 | + passed = false; |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (passed) { |
| 93 | + mAnalysisResult = 0; // Pass |
| 94 | + report << "PASS: Frequencies are monotonically increasing.\n"; |
| 95 | + } else { |
| 96 | + mAnalysisResult = 1; // Fail |
| 97 | + report << "FAIL: Frequencies are not monotonically increasing.\n"; |
| 98 | + } |
| 99 | + |
| 100 | + mFrequencyResponse = report.str(); |
| 101 | + mFftBufferIndex = 0; |
| 102 | + } |
| 103 | + break; |
| 104 | + } |
| 105 | + case MultiTone: { |
| 106 | + if (mFftBuffer.size() != mFftBufferSize) { |
| 107 | + mFftBuffer.resize(mFftBufferSize); |
| 108 | + } |
| 109 | + float sample = frameData[getInputChannel()]; |
| 110 | + mFftBuffer[mFftBufferIndex++] = sample; |
| 111 | + if (mFftBufferIndex >= mFftBufferSize) { |
| 112 | + // Perform FFT |
| 113 | + CVector fftInput(mFftBufferSize); |
| 114 | + for (int i = 0; i < mFftBufferSize; i++) { |
| 115 | + fftInput[i] = Complex(mFftBuffer[i], 0); |
| 116 | + } |
| 117 | + fft(fftInput); |
| 118 | + |
| 119 | + // Analyze FFT output |
| 120 | + double signalPower = 0; |
| 121 | + double noisePower = 0; |
| 122 | + |
| 123 | + const int numTones = sizeof(sMultiToneFrequencies) / sizeof(sMultiToneFrequencies[0]); |
| 124 | + int bins[numTones]; |
| 125 | + for (int i = 0; i < numTones; i++) { |
| 126 | + bins[i] = (int)(sMultiToneFrequencies[i] * mFftBufferSize / getSampleRate()); |
| 127 | + } |
| 128 | + |
| 129 | + for (int i = 1; i < mFftBufferSize / 2; i++) { |
| 130 | + double power = std::norm(fftInput[i]); |
| 131 | + bool isSignal = false; |
| 132 | + for (int j = 0; j < numTones; j++) { |
| 133 | + if (i >= bins[j] - 1 && i <= bins[j] + 1) { |
| 134 | + isSignal = true; |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + if (isSignal) { |
| 139 | + signalPower += power; |
| 140 | + } else { |
| 141 | + noisePower += power; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + std::stringstream report; |
| 146 | + report << "Multi-tone analysis:\n"; |
| 147 | + if (noisePower > 0) { |
| 148 | + double sinad = 10 * log10(signalPower / noisePower); |
| 149 | + report << "SINAD = " << std::fixed << std::setprecision(2) << sinad << " dB\n"; |
| 150 | + if (sinad < mMinSinad) { |
| 151 | + mAnalysisResult = 1; // Fail |
| 152 | + report << "FAIL: SINAD is below threshold of " << mMinSinad << " dB\n"; |
| 153 | + } else { |
| 154 | + mAnalysisResult = 0; // Pass |
| 155 | + } |
| 156 | + } else { |
| 157 | + report << "SINAD = inf\n"; |
| 158 | + mAnalysisResult = 0; // Pass |
| 159 | + } |
| 160 | + mDistortionReport = report.str(); |
| 161 | + mFftBufferIndex = 0; |
| 162 | + } |
| 163 | + break; |
| 164 | + } |
| 165 | + case Sine: |
| 166 | + default: { |
| 167 | + float sample = frameData[getInputChannel()]; |
| 168 | + mInfiniteRecording.write(sample); |
| 169 | + |
| 170 | + if (transformSample(sample)) { |
| 171 | + // Analyze magnitude and phase on every period. |
| 172 | + if (mPhaseOffset != kPhaseInvalid) { |
| 173 | + double diff = fabs(calculatePhaseError(mPhaseOffset, mPreviousPhaseOffset)); |
| 174 | + if (diff < mPhaseTolerance) { |
| 175 | + mMaxMagnitude = std::max(mMagnitude, mMaxMagnitude); |
| 176 | + } |
| 177 | + mPreviousPhaseOffset = mPhaseOffset; |
| 178 | + } |
| 179 | + } |
| 180 | + break; |
| 181 | + } |
| 182 | + } |
| 183 | + return RESULT_OK; |
| 184 | +} |
| 185 | + |
| 186 | +std::string DataPathAnalyzer::analyze() { |
| 187 | + std::stringstream report; |
| 188 | + report << "DataPathAnalyzer ------------------\n"; |
| 189 | + switch (mSignalType) { |
| 190 | + case Sine: |
| 191 | + report << "LOOPBACK_RESULT_TAG " << "sine.magnitude = " << std::setw(8) |
| 192 | + << mMagnitude << "\n"; |
| 193 | + report << "LOOPBACK_RESULT_TAG " << "frames.accumulated = " << std::setw(8) |
| 194 | + << mFramesAccumulated << "\n"; |
| 195 | + report << "LOOPBACK_RESULT_TAG " << "sine.period = " << std::setw(8) |
| 196 | + << mSinePeriod << "\n"; |
| 197 | + break; |
| 198 | + case Chirp: |
| 199 | + report << "Chirp analysis not implemented yet.\n"; |
| 200 | + break; |
| 201 | + case MultiTone: |
| 202 | + report << "Multi-tone analysis not implemented yet.\n"; |
| 203 | + break; |
| 204 | + } |
| 205 | + return report.str(); |
| 206 | +} |
| 207 | + |
| 208 | +void DataPathAnalyzer::reset() { |
| 209 | + BaseSineAnalyzer::reset(); |
| 210 | + mPreviousPhaseOffset = 999.0; // Arbitrary high offset to prevent early lock. |
| 211 | + mMaxMagnitude = 0.0; |
| 212 | +} |
| 213 | + |
| 214 | +double DataPathAnalyzer::getMaxMagnitude() { |
| 215 | + return mMaxMagnitude; |
| 216 | +} |
| 217 | + |
| 218 | +std::string DataPathAnalyzer::getFrequencyResponse() { |
| 219 | + return mFrequencyResponse; |
| 220 | +} |
| 221 | + |
| 222 | +std::string DataPathAnalyzer::getDistortionReport() { |
| 223 | + return mDistortionReport; |
| 224 | +} |
| 225 | + |
| 226 | +int DataPathAnalyzer::getAnalysisResult() { |
| 227 | + return mAnalysisResult; |
| 228 | +} |
| 229 | + |
0 commit comments