示例#1
0
        public void TestBuilderDelay()
        {
            var sinusoid = new SineBuilder()
                           .SetParameter("freq", 0.05f)
                           .OfLength(20)
                           .DelayedBy(1)
                           .Build();

            Assert.Multiple(() =>
            {
                Assert.That(sinusoid[1], Is.EqualTo(0.0).Within(1e-7));
                Assert.That(sinusoid[6], Is.EqualTo(1.0).Within(1e-7));
                Assert.That(sinusoid[16], Is.EqualTo(-1.0).Within(1e-7));
                Assert.That(sinusoid.Length, Is.EqualTo(21));
            });
        }
示例#2
0
        public void TestBuilderRepeat()
        {
            var sinusoid = new SineBuilder()
                           .SetParameter("freq", 0.05f)
                           .OfLength(20)
                           .RepeatedTimes(3)
                           .Build();

            Assert.Multiple(() =>
            {
                Assert.That(sinusoid[0], Is.EqualTo(0.0).Within(1e-7));
                Assert.That(sinusoid[5], Is.EqualTo(1.0).Within(1e-7));
                Assert.That(sinusoid[25], Is.EqualTo(1.0).Within(1e-7));
                Assert.That(sinusoid.Length, Is.EqualTo(60));
            });
        }
示例#3
0
        public void TestSimpleSinusoidBuilder()
        {
            var sinusoid = new SineBuilder()
                           .SetParameter("freq", 0.05f)
                           .OfLength(20)
                           .Build();

            Assert.Multiple(() =>
            {
                Assert.That(sinusoid[0], Is.EqualTo(0.0).Within(1e-7));
                Assert.That(sinusoid[5], Is.EqualTo(1.0).Within(1e-7));
                Assert.That(sinusoid[10], Is.EqualTo(0.0).Within(1e-7));
                Assert.That(sinusoid[15], Is.EqualTo(-1.0).Within(1e-7));
                Assert.That(sinusoid.Length, Is.EqualTo(20));
            });
        }
示例#4
0
        public void TestBuilderSuperimpose()
        {
            var constants = new DiscreteSignal(1, length: 6, value: 2.0f);

            var sinusoid = new SineBuilder()
                           .SetParameter("freq", 0.05f)
                           .SuperimposedWith(constants)
                           .OfLength(20)
                           .SuperimposedWith(constants)             // twice
                           .Build();

            Assert.Multiple(() =>
            {
                Assert.That(sinusoid[0], Is.EqualTo(4.0).Within(1e-7));
                Assert.That(sinusoid[5], Is.EqualTo(5.0).Within(1e-7));
                Assert.That(sinusoid[10], Is.EqualTo(0.0).Within(1e-7));
                Assert.That(sinusoid[15], Is.EqualTo(-1.0).Within(1e-7));
                Assert.That(sinusoid.Length, Is.EqualTo(20));
            });
        }
示例#5
0
        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();
        }