1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 #ifndef GLITCH_TEST_H 18 #define GLITCH_TEST_H 19 20 class Fft; 21 class Window; 22 23 class GlitchTest { 24 public: 25 GlitchTest(void); 26 27 virtual ~GlitchTest(void) { 28 cleanup(); 29 } 30 31 /* Set up the instance to operate on input test signals sampled at 32 sample_rate that contain a stimulis tone of stim_freq frequency. 33 The signal will be considered on during the interval it exceeds 34 onset_thresh (dB re 1.0). Any frames containing a tone that have 35 a signal energy to out-of-band energy ratio less than 36 db_snr_thresh are counted as bad frames. Init must be called 37 before CheckToneSnr. */ 38 void init(float sample_rate, float stim_freq, float onset_thresh, 39 float db_snr_thresh); 40 41 /* Analyze the n_samples of the lin16 signal in pcm. This signal is 42 assumed sampled at sample_rate, and to contain a sinusoid with 43 center frequency stim_freq, embedded somewhere in time, with 44 "silence" intervals before and after. The contiguous duration of 45 the tone that exceeds onset_thresh (in dB re 1.0) is returned as 46 seconds in duration. The number of frames for which the ratio of 47 the energy at the tone frequency to the energy in the rest of the 48 spectrum is less than db_snr_thresh is returned in n_bad_frames. 49 If the test succeed, the method returns 1, else it returns a 50 negative number that reflects the cause of failure as follows: 51 -1 The instance is not initialized. 52 -2 There are not enough samples to do a reasonable check. 53 -3 The tone signal onset was not found. 54 -4 The tone signal end was not found. */ 55 int checkToneSnr(short* pcm, int n_samples, float* duration, 56 int* n_bad_frames); 57 58 private: 59 // Free memory, etc. 60 void cleanup(void); 61 62 /* Do a real FFT on the n_input samples in data, and return n_output 63 power spectral density points in output. The output points include 64 DC through the Nyquist frequency (i.e. 1 + fft_size/2). output 65 must be large enough to accommodate this size. If n_input==0 or 66 n_input > fft_size, return 0; else return 1. */ 67 int realMagSqSpectrum(float* data, int n_input, 68 float* output, int* n_output); 69 70 /* Find the largest value in data starting at start_search and ending 71 at end_search-1. The values in data are assumed to be magnitude 72 squared values from a spectrum computation based on window_size 73 sample points. Return the index where the largest value was found, 74 and return the dB (re 1.0) equivalent of the highest magnitude. */ 75 void findPeak(float* data, int start_search, int end_search, 76 int* max_loc, float* max_value); 77 78 // Real and Imaginary analysis arrays. 79 float* mRe; 80 float* mIm; 81 // Fourier transform and window. 82 Fft* mFt; 83 Window* mWind; 84 // Derived parameters and other variables. 85 float mSampleRate; 86 int mFrameStep; 87 int mWindowSize; 88 int mFftSize; 89 float mOnsetThresh; 90 float mDbSnrThresh; 91 int mLowestSpectrumBin; 92 int mLowToneBin; 93 int mHighToneBin; 94 }; 95 96 #endif // GLITCH_TEST_H 97