/// <summary> /// Convolves the specified finite signal with an infinite signal. /// </summary> /// <param name="s1">The finite signal.</param> /// <param name="s2">The infinite signal.</param> /// <returns></returns> /// <exception cref="SamplerateMismatchException"></exception> public static ISignal Convolve(this IFiniteSignal s1, ISignal s2) { if (s1.SampleRate != s2.SampleRate) { throw new SamplerateMismatchException(); } return(new InfiniteSignal( (start, length) => { var l = s1.Length + Math.Max(s1.Length, length) - 1; var n = Fft.NextPowerOfTwo(l); var spectrum1 = Fft.RealFft(s1.Signal, n); var signal2A = s2.GetWindowedSignal(start - s1.Length - s1.Start, s1.Length); var spectrum2A = Fft.RealFft(signal2A, n); var signal2B = s2.GetWindowedSignal(start - s1.Start, length); var spectrum2B = Fft.RealFft(signal2B, n); var spectrumA = spectrum1.Multiply(spectrum2A); var spectrumB = spectrum1.Multiply(spectrum2B); var signalA = Fft.RealIfft(spectrumA).Skip(s1.Length).Take(Math.Min(length, s1.Length - 1)); var signalB = Fft.RealIfft(spectrumB).Take(length); var signal = signalA.AddFull(signalB); return signal; }, s1.SampleRate) { DisplayName = "convolution result" }); }