Пример #1
0
        /// <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"
            });
        }
Пример #2
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="FftSpectrum" /> class.
 /// </summary>
 /// <param name="timeSignal">The finite time domain signal.</param>
 /// <param name="sampleRate">The sample rate.</param>
 /// <param name="start">The start.</param>
 public FftSpectrum(IReadOnlyList <double> timeSignal, double sampleRate, int start = 0)
     : this(new FftSeries(sampleRate, timeSignal.Count), Fft.RealFft(timeSignal.CircularShift(start)))
 {
     this.TimeDomainSignal = timeSignal.CircularShift(start).ToReadOnlyList();
 }
Пример #3
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="FftSpectrum" /> class.
 /// </summary>
 /// <param name="timeSignal">The time domain signal.</param>
 /// <param name="fftLength">Length of the FFT.</param>
 /// <param name="sampleRate">The sample rate.</param>
 /// <param name="start">The start sample time of the signal.</param>
 public FftSpectrum(IEnumerable <double> timeSignal, int fftLength, double sampleRate, int start = 0)
     : this(new FftSeries(sampleRate, fftLength), Fft.RealFft(timeSignal.CircularShift(start), fftLength))
 {
 }