/// <summary> /// Method implements simple phaser effect /// </summary> /// <param name="signal"></param> /// <param name="filteringOptions"></param> /// <returns></returns> public DiscreteSignal ApplyTo(DiscreteSignal signal, FilteringOptions filteringOptions = FilteringOptions.Auto) { var x = signal.Samples; var samplingRateInverted = 1.0 / signal.SamplingRate; var lfo = new TriangleWaveBuilder() .SetParameter("lo", MinFrequency) .SetParameter("hi", MaxFrequency) .SetParameter("freq", LfoFrequency) .OfLength(signal.Length) .SampledAt(signal.SamplingRate) .Build(); var y = new float[x.Length]; for (var i = 2; i < signal.Length; i++) { var filter = new NotchFilter((float)(lfo[i] * samplingRateInverted), Q); var b = filter.Tf.Numerator; var a = filter.Tf.Denominator; y[i] = (float)(b[0] * x[i] + b[1] * x[i - 1] + b[2] * x[i - 2] - (a[1] * y[i - 1] + a[2] * y[i - 2])); } return(new DiscreteSignal(signal.SamplingRate, y)); }
/// <summary> /// Method implements simple wah-wah effect /// </summary> /// <param name="signal"></param> /// <param name="filteringOptions"></param> /// <returns></returns> public DiscreteSignal ApplyTo(DiscreteSignal signal, FilteringOptions filteringOptions = FilteringOptions.Auto) { var x = signal.Samples; var samplingRateInverted = 2 * Math.PI / signal.SamplingRate; var lfo = new TriangleWaveBuilder() .SetParameter("lo", MinFrequency) .SetParameter("hi", MaxFrequency) .SetParameter("freq", LfoFrequency) .OfLength(signal.Length) .SampledAt(signal.SamplingRate) .Build(); var f = 2 * Math.Sin(lfo[0] * samplingRateInverted); var yh = new float[x.Length]; var yb = new float[x.Length]; var yl = new float[x.Length]; yh[0] = x[0]; yb[0] = (float)(f * yh[0]); yl[0] = (float)(f * yb[0]); for (var i = 1; i < signal.Length; i++) { yh[i] = x[i] - yl[i - 1] - Q * yb[i - 1]; yb[i] = (float)(f * yh[i] + yb[i - 1]); yl[i] = (float)(f * yb[i] + yl[i - 1]); f = 2 * Math.Sin(lfo[i] * samplingRateInverted); } var maxYb = yb.Max(y => Math.Abs(y)); return(new DiscreteSignal(signal.SamplingRate, yb.Select(y => y / maxYb))); }
private void generateSignalButton_Click(object sender, EventArgs e) { var sampleCount = int.Parse(durationTextBox.Text); var samplingRate = _signal1?.SamplingRate ?? 16000; SignalBuilder signalBuilder; switch (builderComboBox.Text) { case "Sinusoid": signalBuilder = new SineBuilder(); _signal2 = signalBuilder .SetParameter("low", -0.4f) .SetParameter("high", 0.4f) .SetParameter("freq", 233 /*Hz*/) .OfLength(sampleCount) .SampledAt(samplingRate) .Build(); break; case "Sawtooth": signalBuilder = new SawtoothBuilder(); _signal2 = signalBuilder .SetParameter("low", -0.3f) .SetParameter("high", 0.3f) .SetParameter("freq", 233 /*Hz*/) .OfLength(sampleCount) .SampledAt(samplingRate) .Build(); break; case "Triangle Wave": signalBuilder = new TriangleWaveBuilder(); _signal2 = signalBuilder .SetParameter("low", -0.3f) .SetParameter("high", 0.3f) .SetParameter("freq", 233 /*Hz*/) .OfLength(sampleCount) .SampledAt(samplingRate) .Build(); break; case "Square Wave": signalBuilder = new SquareWaveBuilder(); _signal2 = signalBuilder .SetParameter("low", -0.25f) .SetParameter("high", 0.25f) .SetParameter("freq", 233 /*Hz*/) .OfLength(sampleCount) .SampledAt(samplingRate) .Build(); break; case "Pulse Wave": signalBuilder = new PulseWaveBuilder(); _signal2 = signalBuilder .SetParameter("min", 0) .SetParameter("max", 0.5f) .SetParameter("pulse", 0.007f /*sec*/) .SetParameter("period", 0.020f /*sec*/) .OfLength(sampleCount) .DelayedBy(50) .SampledAt(samplingRate) .Build(); break; case "Chirp": signalBuilder = new ChirpBuilder(); _signal2 = signalBuilder .SetParameter("min", -0.3f) .SetParameter("max", 0.3f) .OfLength(sampleCount) .RepeatedTimes(3) .SampledAt(samplingRate) .Build(); break; case "Sinc": signalBuilder = new SincBuilder(); _signal2 = signalBuilder .SetParameter("min", 0) .SetParameter("max", 0.5f) .SetParameter("freq", 700 /*Hz*/) .OfLength(sampleCount) .SampledAt(samplingRate) .Build(); break; case "Ramp": signalBuilder = new RampBuilder(); _signal2 = signalBuilder .SetParameter("slope", 0.0007f) .SetParameter("intercept", -0.5f) .OfLength(sampleCount) .SampledAt(samplingRate) .Build(); break; case "AWGN": signalBuilder = new AwgnBuilder(); _signal2 = signalBuilder .SetParameter("sigma", 0.25f) .OfLength(sampleCount) .SampledAt(samplingRate) .Build(); break; case "Pink Noise": signalBuilder = new PinkNoiseBuilder(); _signal2 = signalBuilder .SetParameter("min", -0.5f) .SetParameter("max", 0.5f) .OfLength(sampleCount) .SampledAt(samplingRate) .Build(); break; case "Red Noise": signalBuilder = new RedNoiseBuilder(); _signal2 = signalBuilder .SetParameter("min", -0.5f) .SetParameter("max", 0.5f) .OfLength(sampleCount) .SampledAt(samplingRate) .Build(); break; case "Perlin Noise": signalBuilder = new PerlinNoiseBuilder(); _signal2 = signalBuilder .SetParameter("min", -0.3f) .SetParameter("max", 0.7f) .OfLength(sampleCount) .SampledAt(samplingRate) .Build(); break; default: signalBuilder = new WhiteNoiseBuilder(); _signal2 = signalBuilder .SetParameter("min", -0.5f) .SetParameter("max", 0.5f) .OfLength(sampleCount) .SampledAt(samplingRate) .Build(); break; } builderParametersListBox.Items.Clear(); builderParametersListBox.Items.AddRange(signalBuilder.GetParametersInfo()); builderParametersListBox.Items.Add(""); builderParametersListBox.Items.Add($"min: {_signal2.Samples.Min():F2}"); builderParametersListBox.Items.Add($"max: {_signal2.Samples.Max():F2}"); builderParametersListBox.Items.Add($"avg: {_signal2.Samples.Average():F4}"); if (_signal1 != null) { //_signal3 = _signal1 + _signal2; var positions = Enumerable.Range(0, 3).Select(pos => pos * (_signal2.Length + 2000)).ToArray(); _signal3 = _signal1.SuperimposeMany(_signal2, positions); superimposedSignalPanel.Signal = _signal3; } generatedSignalPanel.Stride = 1; generatedSignalPanel.Signal = _signal2; var spectrum = _fft.PowerSpectrum(_signal2.First(512)); spectrumPanel.Line = spectrum.Samples; spectrumPanel.ToDecibel(); }