/// <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" }); }
protected override Series CreateGraph(ISignal signal) { var ret = new ImpulseResponseGraph(); ret.Points.AddRange( signal.GetWindowedSignal(this.XMin, this.XMax - this.XMin + 1) .Zip(Enumerable.Range(this.XMin, this.XMax - this.XMin + 1), (m, t) => new DataPoint(t, m))); return(ret); //var fsignal = signal as IFiniteSignal; //if (fsignal != null) //{ // ret.Points.AddRange(fsignal.Signal.Zip(Enumerable.Range(fsignal.Start, fsignal.Length), (m, t) => new DataPoint(t, m))); // return ret; //} //var esignal = signal as IEnumerableSignal; //if (esignal != null) //{ // var wsignal = esignal.Multiply(this.CausalWindow); // return this.CreateGraph(wsignal); //} //var iwsignal = signal.Multiply(this.SymmetricWindow); //return this.CreateGraph(iwsignal); }
/// <summary> /// Negates the specified signal. /// </summary> /// <param name="s">The signal.</param> /// <returns></returns> public static ISignal Negate(this ISignal s) { return(new InfiniteSignal((start, length) => s.GetWindowedSignal(start, length).Negate(), s.SampleRate) { DisplayName = "negation result" }); }
/// <summary> /// Adds the specified signals. /// </summary> /// <param name="s1">The first signal.</param> /// <param name="s2">The second signal.</param> /// <returns></returns> /// <exception cref="SamplerateMismatchException"></exception> public static ISignal Add(this ISignal s1, ISignal s2) { if (s1.SampleRate != s2.SampleRate) { throw new SamplerateMismatchException(); } return(new InfiniteSignal((start, length) => s1.GetWindowedSignal(start, length).Add(s2.GetWindowedSignal(start, length)), s1.SampleRate) { DisplayName = "addition result" }); }
/// <summary> /// Multiplies the specified finite signal with a signal. /// </summary> /// <param name="s1">The finite signal.</param> /// <param name="s2">The second signal.</param> /// <returns></returns> /// <exception cref="SamplerateMismatchException"></exception> public static IFiniteSignal Multiply(this IFiniteSignal s1, ISignal s2) { if (s1.SampleRate != s2.SampleRate) { throw new SamplerateMismatchException(); } return(new FiniteSignal(s1.Signal.Multiply(s2.GetWindowedSignal(s1.Start, s1.Length)).ToReadOnlyList(), s1.SampleRate, s1.Start) { DisplayName = "multiplication result" }); }
/// <summary> /// Reverses the specified infinite signal. /// </summary> /// <param name="signal">The signal.</param> /// <returns></returns> public static ISignal Reverse(this ISignal signal) { return(new InfiniteSignal((start, length) => signal.GetWindowedSignal(-start - length + 1, length).Reverse(), signal.SampleRate)); }