kfr

Fast, modern C++ DSP framework, FFT, Sample Rate Conversion, FIR/IIR/Biquad Filters (SSE, AVX, AVX-512, ARM NEON)
Log | Files | Refs | README

normalize.md (1232B)


      1 # How to normalize audio
      2 
      3 ## One channel
      4 
      5 Input/Output data: [See how to pass data to KFR](basics.md)
      6 ```c++
      7 univector<float> audio;
      8 ```
      9 
     10 Code
     11 ```c++
     12 audio /= absmaxof(audio);
     13 ```
     14 
     15 1. `absmaxof` function calculates the absolute maximum of the given array (`max(abs(x[i])...)`)
     16 1. `operator/=` performs inplace division for array
     17     
     18 Equivalent pure C++ code
     19 ```c++
     20 float absmax = 0;
     21 for (size_t i = 0; i < audio.size(); ++i)
     22 {
     23     absmax = std::max(absmax, std::abs(audio[i]));
     24 }
     25 for (size_t i = 0; i < audio.size(); ++i)
     26 {
     27     audio[i] = audio[i] / absmax;
     28 }
     29 ```
     30 
     31 ## Stereo audio
     32 
     33 Input/Output data:
     34 ```c++
     35 univector<univector<float>, 2> stereo;
     36 ```
     37 
     38 Code
     39 ```c++
     40 const float peak = absmaxof(concatenate(stereo[0], stereo[1]));
     41 unpack(stereo[0], stereo[1]) = pack(stereo[0], stereo[1]) / pack(peak, peak);
     42 ```
     43 
     44 1. `concatenate` function concatenates two arrays as if they were written sequentially in memory (but without any actual memory copies)
     45 1. `absmaxof` function calculates the absolute maximum of the concatenated arrays
     46 1. `pack` function combines two arrays as if it were a single array containing pairs of values
     47 1. `operator/` divides pairs by the peak value
     48 1. `unpack` function unpacks the pairs to two target arrays