public void Calibrate() { double[] envelopeAverage = null; // create the envelope for each signal for (int i = 0; i < m_rawSignals.Count; i++) { Signal signal = Signal.FromArray(m_rawSignals[i], EchoTubeSensor.SampleRate, SampleFormat.Format32BitIeeeFloat); LowPassFilter lowPassFilter = new LowPassFilter(100.0, EchoTubeSensor.SampleRate) { Alpha = 0.05f }; Signal filteredSignal = lowPassFilter.Apply(signal); // this is the filtered signal (and a single baseline) float[] filteredRawSData = filteredSignal.ToFloat(); float[] processedData = new float[filteredRawSData.Length]; // now, shift and rectify the raw data Parallel.For(0, processedData.Length, j => { // shift & rectify processedData[j] = Math.Abs(m_rawSignals[i][j] - filteredRawSData[j]); }); Signal processedSignal = Signal.FromArray(processedData, EchoTubeSensor.SampleRate, SampleFormat.Format32BitIeeeFloat); // now, we can create the calibration envelope EnvelopeFilter filter = new EnvelopeFilter(Sensor.EnvelopeAlpha); Signal envelopedSignal = filter.Apply(processedSignal); float[] envelopedData = envelopedSignal.ToFloat(); if (envelopeAverage == null) { envelopeAverage = new double[envelopedData.Length]; } Parallel.For(0, envelopedData.Length, j => { envelopeAverage[j] += envelopedData[j]; }); } // now, we need to average over the envelope if (m_rawSignals.Count > 0) { Parallel.For(0, envelopeAverage.Length, i => { envelopeAverage[i] /= m_rawSignals.Count; }); m_envelopedAvg = envelopeAverage.Select(x => (float)x).ToArray(); IsValid = true; } }
public void sample_test() { string basePath = NUnit.Framework.TestContext.CurrentContext.TestDirectory; string pathWhereTheDatasetShouldBeStored = Path.Combine(basePath, "mfcc"); #region doc_example1 // Let's say we would like to analyse an audio sample. To give an example that // could be reproduced by anyone without having to give a specific sound file // that would need to have been downloaded by every user trying to run this example, // we will use obtain an example from the Free Spoken Digits Dataset instead: var fsdd = new FreeSpokenDigitsDataset(path: pathWhereTheDatasetShouldBeStored); // Let's obtain one of the audio signals: Signal a = fsdd.GetSignal(0, "jackson", 10); int sampleRate = a.SampleRate; // 8000 // Note: if you would like to load a signal from the // disk, you could use the following method directly: // Signal a = Signal.FromFile(fileName); // Create a low-pass filter to keep only frequencies below 100 Hz var filter = new LowPassFilter(frequency: 100, sampleRate: sampleRate); // Apply the filter to the signal Signal result = filter.Apply(a); // Create a spectrogram for the original var sourceSpectrum = new Spectrogram(a); // Create a spectrogram for the filtered signal: var resultSpectrum = new Spectrogram(result); // Get the count for a high frequency before and after the low-pass filter: double before = sourceSpectrum.GetFrequencyCount(windowIndex: 0, frequency: 1000); // 0.00028203820434203334 double after = resultSpectrum.GetFrequencyCount(windowIndex: 0, frequency: 1000); // 2.9116651158267508E-05 #endregion Assert.AreEqual(0.00028203820434203334, before, 1e-8); Assert.AreEqual(2.9116651158267508E-05, after, 1e-8); }
private void ProviderDataReceived(object sender, ushort[] data) { // check the device state if (DeviceState == DeviceState.Startup) { if (m_detectorType == Detector.Old) { if (m_calibrationSamples == null) { m_calibrationSamples = new double[data.Length]; } Parallel.For(0, data.Length, i => { m_calibrationSamples[i] += data[i]; }); m_calibrationCounter++; if (m_calibrationCounter == NumberOfCalibrationSamples) { Parallel.For(0, data.Length, i => { m_calibrationSamples[i] /= m_calibrationCounter; }); DeviceState = DeviceState.Running; } } else { if (!m_calibration.IsValid) { m_calibration.AddSample(data); m_calibrationCounter++; if (m_calibrationCounter == NumberOfCalibrationSamples) { // now, we can calibrate m_calibration.Calibrate(); if (m_calibration.IsValid) { DeviceState = DeviceState.Running; } else { m_calibration.Reset(); } } } } } else if (DeviceState == DeviceState.Running) { // preprocess the signal (get baseline and shift) float[] rawData = data.Select(value => (float)value).ToArray(); ushort[] calibratedData = new ushort[rawData.Length]; if (m_detectorType == Detector.Old) { // calibrate Parallel.For(0, rawData.Length, i => { calibratedData[i] = (ushort)Math.Abs(rawData[i] - m_calibrationSamples[i]); }); } else if (m_detectorType == Detector.New) { Signal rawSignal = Signal.FromArray(rawData, SampleRate, SampleFormat.Format32BitIeeeFloat); LowPassFilter lowPassFilter = new LowPassFilter(100.0, SampleRate) { Alpha = 0.05f }; rawSignal = lowPassFilter.Apply(rawSignal); float[] baseline = rawSignal.ToFloat(); Parallel.For(0, rawData.Length, i => { rawData[i] = Math.Abs(rawData[i] - baseline[i]); }); } else if (m_detectorType == Detector.Advanced) { } // good, raw data is shifted now SensorDataEventArgs eventArgs = null; if (m_detectorType == Detector.Old) { eventArgs = m_signalProcessor.ProcessData(calibratedData, true); } else { eventArgs = m_signalProcessor.ProcessData(m_calibration, rawData, true); } // notify the receviers about the raw data DataReceived?.Invoke(this, eventArgs); // now, do touch processing ProcessTouches(eventArgs); } }