this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

after peak normalization before smoothing

alice 7e87648a 4719a26e

+53 -4
+53 -4
src/ext/fft.c
··· 18 18 float fAmplification = 1.0f; 19 19 bool bCreated = false; 20 20 kiss_fft_cpx fftBuf[FFT_SIZE + 1]; 21 + // float fFFTSmoothingFactor = 0.9f; // higher value, smoother FFT 22 + // static float fftDataSmoothed[FFT_SIZE]; 21 23 22 24 void OnReceiveFrames(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) 23 25 { ··· 191 193 } 192 194 193 195 ////////////////////////////////////////////////////////////////////////// 196 + double tic_api_fft(tic_mem* memory, s32 freq) { 197 + static const float fPeakMinValue = 0.01f; 198 + static const float fPeakSmoothing = 0.995f; 199 + static float fPeakSmoothValue = 0.0f; 194 200 195 - double tic_api_fft(tic_mem* memory, s32 freq) 196 - { 201 + bool bPeakNormalization = true; 202 + if (bPeakNormalization) { 203 + float peakValue = fPeakMinValue; 204 + for (int i = 0; i < FFT_SIZE; i++) { 205 + float val = 2.0f * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i); 206 + if (val > peakValue) { 207 + peakValue = val; 208 + } 209 + } 210 + if (peakValue > fPeakSmoothValue) { 211 + fPeakSmoothValue = peakValue; 212 + } 213 + if (peakValue < fPeakSmoothValue) { 214 + fPeakSmoothValue = fPeakSmoothValue * fPeakSmoothing + peakValue * (1 - fPeakSmoothing); 215 + } 216 + if (fPeakSmoothValue > 0.0f) { 217 + fAmplification = 1.0f / fPeakSmoothValue; 218 + if (freq < 10) { 219 + printf("freq: %d, fAmplification: %.2f\n", freq, fAmplification); 220 + } 221 + } else { 222 + fAmplification = 1.0f; 223 + } 224 + } 225 + 197 226 u32 interval = FFT_SIZE / 256 / 2; // the 2 is to discard super high frequencies, they suck 198 227 freq = freq * interval; 199 228 freq = fmin(freq, FFT_SIZE); ··· 202 231 static const float scaling = 1.0f / (float)FFT_SIZE; 203 232 float res = 0; 204 233 for (int i = freq; i < freq + interval; ++i) { 205 - res += 2.0 * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling; 234 + res += 2.0f * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling * fAmplification; 235 + } 236 + 237 + if (freq < 10) { 238 + printf("freq: %d, res: %.2f\n", freq, res); 206 239 } 207 240 208 241 return res; 209 - } 242 + } 243 + 244 + // double tic_api_fft(tic_mem* memory, s32 freq) 245 + // { 246 + // u32 interval = FFT_SIZE / 256 / 2; // the 2 is to discard super high frequencies, they suck 247 + // freq = freq * interval; 248 + // freq = fmin(freq, FFT_SIZE); 249 + // freq = fmax(freq, 0); 250 + 251 + // static const float scaling = 1.0f / (float)FFT_SIZE; 252 + // float res = 0; 253 + // for (int i = freq; i < freq + interval; ++i) { 254 + // res += 2.0 * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling; 255 + // } 256 + 257 + // return res; 258 + // }