예제 #1
0
        /// <summary>
        /// Calculate filtering gain so that frequency response is normalized onto [0, 1] range.
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="fftSize"></param>
        /// <returns>Gain for filtering operations</returns>
        public static float EstimateGain(this IOnlineFilter filter, int fftSize = 512)
        {
            var unit = DiscreteSignal.Unit(fftSize);

            // get impulse response

            var response = unit.Samples.Select(s => filter.Process(s)).ToArray();

            // get frequency response

            var spectrum = new float[fftSize / 2 + 1];
            var fft      = new RealFft(fftSize);

            fft.MagnitudeSpectrum(response, spectrum);

            return(1 / spectrum.Max(s => Math.Abs(s)));
        }