public void TestFirstSamples() { // Act var first = _signal.First(3); // Assert Assert.That(first.Samples, Is.EqualTo(new float[] { 0, 1, 2 })); }
public static void ApplyFftToSignal(string filePath) { Console.WriteLine($"Applying FFT to {filePath} file"); using (var afr = new FileStream(filePath, FileMode.Open)) { // create a signal file var waveFile = new WaveFile(afr, false); DiscreteSignal signal = waveFile[Channels.Left]; float[] real = signal.First(1024).Samples; float[] imag = new float[1024]; var fft = new Fft(1024); fft.Direct(real, imag); // in-place FFT Console.WriteLine($"FFT size: {fft.Size}"); } }
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(); }