コード例 #1
0
ファイル: EQGenerator.cs プロジェクト: VoidXH/Cavern
 /// <summary>
 /// Gets a zero-delay convolution filter with minimally sacrificed phase that results in this EQ when applied.
 /// </summary>
 /// <param name="eq">Source <see cref="Equalizer"/></param>
 /// <param name="sampleRate">Sample rate of the target system the convolution filter could be used on</param>
 /// <param name="length">Length of the convolution filter in samples, must be a power of 2</param>
 /// <param name="gain">Signal voltage multiplier</param>
 /// <param name="initial">Custom initial spectrum to apply the EQ on - phases will be corrected, this is not convolved,
 /// and has to be twice the size of <paramref name="length"/></param>
 public static float[] GetConvolution(this Equalizer eq, int sampleRate, int length = 1024, float gain = 1,
                                      Complex[] initial = null)
 {
     length <<= 1;
     Complex[] filter = new Complex[length];
     if (initial == null)
     {
         for (int i = 0; i < length; ++i)
         {
             filter[i].Real = gain; // FFT of DiracDelta(x)
         }
     }
     else
     {
         for (int i = 0; i < length; ++i)
         {
             filter[i].Real = initial[i].Magnitude * gain;
         }
     }
     eq.Apply(filter, sampleRate);
     using (FFTCache cache = new FFTCache(length)) {
         Measurements.MinimumPhaseSpectrum(filter, cache);
         filter.InPlaceIFFT(cache);
     }
     return(Measurements.GetRealPartHalf(filter));
 }