Imports BasicDSP Public Class dsp3ex1 Dim wv As New Signal Dim fx As Waveform Dim gr As Graph Const WINTIME As Double = 0.03 Const STPTIME As Double = 0.01 Public Function FindMaxIndex(ByRef c As Spectrum, ByVal first As Integer, ByVal last As Integer) As Integer Dim max As Double = 0 Dim idx As Integer = 0 For i As Integer = first To last If c(i).Mag > max Then max = c(i).Mag idx = i End If Next Return idx End Function Public Sub ProcessSignal() Dim wsize As Integer = WINTIME / wv.Period Dim ssize As Integer = STPTIME / wv.Period fx = New Waveform(0, 1 / STPTIME) For pos As Integer = 1 To wv.Count - wsize Step ssize Dim win As ComplexWaveform = wv.Cut(pos, wsize).Float.Complex Dim s As Spectrum = DFT.ComplexDFT(Window.Hamming(win)) ' copy the log spectrum into a waveform Dim cy As New ComplexWaveform(s.Count, s.Rate) For i As Integer = 0 To s.Count - 1 cy(i + 1) = Math.Log10(s(i).Mag) Next ' calculate the cepstrum Dim c As Spectrum = DFT.ComplexDFT(cy) ' find index of maximum magnitude Dim ms2 As Integer = 0.002 / c.Period Dim ms20 As Integer = 0.02 / c.Period Dim idx As Integer = FindMaxIndex(c, ms2, ms20) ' add fundamental to end of Fx graph fx.Add(1.0 / (idx * c.Period)) Next End Sub Public Sub DisplayGraphs(ByVal fname As String) gr.PlotClear(1) gr.PlotSignal(1, wv, "Input from " & fname) gr.PlotClear(2) gr.PlotWaveDouble(2, fx, "Fx Track", "Freq (Hz)", "Time (s)") zgc.Refresh() End Sub Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then If (wv.LoadWaveFile(OpenFileDialog1.FileName)) Then ProcessSignal() DisplayGraphs(OpenFileDialog1.FileName) End If End If End Sub Private Sub dsp3ex1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load gr = New Graph(Me.CreateGraphics, zgc, 2, 1) End Sub End Class