/// <summary> /// Evaluate harmonic and percussive signals from given signal /// </summary> /// <param name="signal"></param> /// <returns>Harmonic signal and percussive signal</returns> public (DiscreteSignal, DiscreteSignal) EvaluateSignals(DiscreteSignal signal) { var(harmonicSpectrogram, percussiveSpectrogram) = EvaluateSpectrograms(signal); // reconstruct harmonic part: var harmonic = new DiscreteSignal(signal.SamplingRate, _stft.ReconstructMagnitudePhase(harmonicSpectrogram)); // reconstruct percussive part: var percussive = new DiscreteSignal(signal.SamplingRate, _stft.ReconstructMagnitudePhase(percussiveSpectrogram)); return(harmonic, percussive); }
/// <summary> /// Evaluate harmonic and percussive signals from given signal /// </summary> /// <param name="signal"></param> /// <returns>Harmonic signal and percussive signal</returns> public Tuple <DiscreteSignal, DiscreteSignal> EvaluateSignals(DiscreteSignal signal) { var spectrograms = EvaluateSpectrograms(signal); // reconstruct harmonic part: var harmonic = new DiscreteSignal(signal.SamplingRate, _stft.ReconstructMagnitudePhase(spectrograms.Item1)); // reconstruct percussive part: var percussive = new DiscreteSignal(signal.SamplingRate, _stft.ReconstructMagnitudePhase(spectrograms.Item2)); return(new Tuple <DiscreteSignal, DiscreteSignal>(harmonic, percussive)); }
/// <summary> /// One iteration of reconstruction /// </summary> /// <param name="signal">Signal reconstructed at previous iteration</param> /// <returns>Reconstructed signal</returns> public float[] Iterate(float[] signal = null) { var magPhase = new MagnitudePhaseList() { Magnitudes = _magnitudes }; if (signal == null) { var spectrumSize = _magnitudes[0].Length; var r = new Random(); var randomPhases = new List <float[]>(); for (var i = 0; i < _magnitudes.Count; i++) { randomPhases.Add(Enumerable.Range(0, spectrumSize) .Select(s => (float)(2 * Math.PI * r.NextDouble())) .ToArray()); } magPhase.Phases = randomPhases; } else { magPhase.Phases = _stft.MagnitudePhaseSpectrogram(signal).Phases; } return(_stft.ReconstructMagnitudePhase(magPhase)); }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { var ofd = new OpenFileDialog(); if (ofd.ShowDialog() != DialogResult.OK) { return; } _waveFileName = ofd.FileName; using (var stream = new FileStream(_waveFileName, FileMode.Open)) { var waveFile = new WaveFile(stream); _bitDepth = waveFile.WaveFmt.BitsPerSample; _signal = waveFile[Channels.Average]; } _stft = new Stft(512, 64, _windowType); _spectrogram = _stft.Spectrogram(_signal); //var processed = _stft.Inverse(_stft.Direct(_signal)); //_processedSignal = new DiscreteSignal(_signal.SamplingRate, processed); // 1) check also this: var mp = _stft.MagnitudePhaseSpectrogram(_signal); var processed = _stft.ReconstructMagnitudePhase(mp); _processedSignal = new DiscreteSignal(_signal.SamplingRate, processed); // 2) or check this: //var processed = new GriffinLimReconstructor(_spectrogram, _stft).Reconstruct(); //_processedSignal = new DiscreteSignal(_signal.SamplingRate, processed); signalPanel.Gain = 120; signalPanel.Signal = _signal; processedSignalPanel.Gain = 120; processedSignalPanel.Signal = _processedSignal; spectrogramPanel.Spectrogram = _spectrogram; //// StftC - has complex FFT //// RealFFT-based Stft is 30% faster! //var sr = new Stft(2048, 256); //var sc = new StftC(2048, 256); //var sw = new Stopwatch(); //sw.Start(); //for (var i = 0; i < 10; i++) //{ // var processed1 = sr.Inverse(sr.Direct(_signal)); // _processedSignal = new DiscreteSignal(_signal.SamplingRate, processed1); //} //sw.Stop(); //var t1 = sw.Elapsed; //sw.Reset(); //sw.Start(); //for (var i = 0; i < 10; i++) //{ // var processed1 = sc.Inverse(sc.Direct(_signal)); // _processedSignal = new DiscreteSignal(_signal.SamplingRate, processed1); //} //sw.Stop(); //var t2 = sw.Elapsed; //MessageBox.Show(t1 + " " + t2); }