this repo has no description
0
fork

Configure Feed

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

spectral whitening

alice dacc07b7 96e35f0f

+83 -2
+14
src/cqtdata.c
··· 17 17 // Array of kernels, one per CQT bin 18 18 CqtKernel cqtKernels[CQT_BINS]; 19 19 20 + // Spectral whitening data 21 + float cqtBinAverages[CQT_BINS]; 22 + float cqtWhitenedData[CQT_BINS]; 23 + 20 24 void CQT_Init(void) 21 25 { 22 26 // Zero all data arrays ··· 30 34 31 35 // Zero kernel pointers 32 36 memset(cqtKernels, 0, sizeof(cqtKernels)); 37 + 38 + // Initialize spectral whitening arrays 39 + memset(cqtBinAverages, 0, sizeof(cqtBinAverages)); 40 + memset(cqtWhitenedData, 0, sizeof(cqtWhitenedData)); 41 + 42 + // Start with small initial averages to avoid divide-by-zero 43 + for (int i = 0; i < CQT_BINS; i++) 44 + { 45 + cqtBinAverages[i] = CQT_WHITENING_FLOOR; 46 + } 33 47 } 34 48 35 49 void CQT_Cleanup(void)
+11
src/cqtdata.h
··· 21 21 #define CQT_BASS_Q_MAX 17.0f // Full Q achieved at 80+ Hz 22 22 #define CQT_TREBLE_Q_FACTOR 11.0f // Smoother for high frequencies 23 23 24 + // Spectral whitening toggle (set to 0 to disable) 25 + #define CQT_SPECTRAL_WHITENING_ENABLED 1 26 + 27 + // Spectral whitening parameters 28 + #define CQT_WHITENING_DECAY 0.99f // Running average decay (0.98-0.995 for 1-2 second adaptation) 29 + #define CQT_WHITENING_FLOOR 0.001f // Minimum average to prevent divide-by-zero 30 + 24 31 // Raw CQT magnitude data 25 32 extern float cqtData[CQT_BINS]; 26 33 ··· 36 43 37 44 // Enable flag (tied to fftEnabled initially) 38 45 extern bool cqtEnabled; 46 + 47 + // Spectral whitening data 48 + extern float cqtBinAverages[CQT_BINS]; // Long-term running averages per bin 49 + extern float cqtWhitenedData[CQT_BINS]; // Whitened CQT data 39 50 40 51 // Sparse kernel storage structures 41 52 typedef struct {
+58 -2
src/ext/cqt.c
··· 343 343 } 344 344 #endif 345 345 346 - // Apply smoothing 346 + // Apply spectral whitening if enabled 347 + #if CQT_SPECTRAL_WHITENING_ENABLED 348 + for (int i = 0; i < CQT_BINS; i++) 349 + { 350 + // Update running average for this bin 351 + cqtBinAverages[i] = cqtBinAverages[i] * CQT_WHITENING_DECAY + 352 + cqtData[i] * (1.0f - CQT_WHITENING_DECAY); 353 + 354 + // Ensure average doesn't go below floor 355 + if (cqtBinAverages[i] < CQT_WHITENING_FLOOR) 356 + cqtBinAverages[i] = CQT_WHITENING_FLOOR; 357 + 358 + // Apply whitening by dividing by the average 359 + cqtWhitenedData[i] = cqtData[i] / cqtBinAverages[i]; 360 + 361 + // Check for NaN or Inf 362 + if (!isfinite(cqtWhitenedData[i])) 363 + cqtWhitenedData[i] = 0.0f; 364 + } 365 + 366 + // Apply smoothing to whitened data 367 + for (int i = 0; i < CQT_BINS; i++) 368 + { 369 + cqtSmoothingData[i] = cqtSmoothingData[i] * CQT_SMOOTHING_FACTOR + 370 + cqtWhitenedData[i] * (1.0f - CQT_SMOOTHING_FACTOR); 371 + } 372 + #else 373 + // Apply smoothing to raw data (spectral whitening disabled) 347 374 for (int i = 0; i < CQT_BINS; i++) 348 375 { 349 376 cqtSmoothingData[i] = cqtSmoothingData[i] * CQT_SMOOTHING_FACTOR + 350 377 cqtData[i] * (1.0f - CQT_SMOOTHING_FACTOR); 351 378 } 379 + #endif 352 380 353 381 // Find peak for normalization 354 382 float currentPeak = 0.0f; ··· 395 423 // Apply CQT kernels 396 424 CQT_ApplyKernels(fftReal, fftImag); 397 425 398 - // Apply smoothing 426 + // Apply spectral whitening if enabled 427 + #if CQT_SPECTRAL_WHITENING_ENABLED 428 + for (int i = 0; i < CQT_BINS; i++) 429 + { 430 + // Update running average for this bin 431 + cqtBinAverages[i] = cqtBinAverages[i] * CQT_WHITENING_DECAY + 432 + cqtData[i] * (1.0f - CQT_WHITENING_DECAY); 433 + 434 + // Ensure average doesn't go below floor 435 + if (cqtBinAverages[i] < CQT_WHITENING_FLOOR) 436 + cqtBinAverages[i] = CQT_WHITENING_FLOOR; 437 + 438 + // Apply whitening by dividing by the average 439 + cqtWhitenedData[i] = cqtData[i] / cqtBinAverages[i]; 440 + 441 + // Check for NaN or Inf 442 + if (!isfinite(cqtWhitenedData[i])) 443 + cqtWhitenedData[i] = 0.0f; 444 + } 445 + 446 + // Apply smoothing to whitened data 447 + for (int i = 0; i < CQT_BINS; i++) 448 + { 449 + cqtSmoothingData[i] = cqtSmoothingData[i] * CQT_SMOOTHING_FACTOR + 450 + cqtWhitenedData[i] * (1.0f - CQT_SMOOTHING_FACTOR); 451 + } 452 + #else 453 + // Apply smoothing to raw data (spectral whitening disabled) 399 454 for (int i = 0; i < CQT_BINS; i++) 400 455 { 401 456 cqtSmoothingData[i] = cqtSmoothingData[i] * CQT_SMOOTHING_FACTOR + 402 457 cqtData[i] * (1.0f - CQT_SMOOTHING_FACTOR); 403 458 } 459 + #endif 404 460 405 461 // Find peak for normalization 406 462 float currentPeak = 0.0f;