Imports BasicDSP Public Class dsp2ex3 ' constant FFT size Const FFTSIZE As Integer = 1024 ' low-pass cut off frequency (Hz) Const LOWPASS As Double = 1000 ' global input signal Dim wv As New Signal ' global filtered signal Dim fwv As Waveform Public Sub ProcessSignal() ' calculate indices of frequencies in spectrum Dim lo = 1024 * LOWPASS / wv.Rate Dim hi = 1024 - lo ' create output waveform fwv = New Waveform(wv.Count, wv.Rate) ' for each input window For i As Integer = 1 To wv.Count - FFTSIZE Step FFTSIZE / 2 ' get section of signal, window and calculate FFT Dim cwv As ComplexWaveform = wv.Cut(i, FFTSIZE).Float.Complex Dim s As Spectrum = DFT.ComplexFFT(Window.Hamming(cwv)) ' clear frequencies above cut-off up to aliased cut-off For j As Integer = lo To hi s(j) = 0 Next ' inverse FFT cwv = DFT.ComplexIFFT(s) ' reassemble signal with overlap-add For j As Integer = 1 To FFTSIZE fwv(i + j - 1) += cwv(j).Real Next Next End Sub Public Sub DisplayGraph(ByVal filename As String) Dim gr As New Graph(Me.CreateGraphics, zgc, 2, 1) gr.PlotSignal(1, wv, "Input: " & filename) gr.PlotWaveform(2, fwv, "Low-pass filtered at " & LOWPASS & "Hz") 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() DisplayGraph(OpenFileDialog1.FileName) End If End If End Sub Private Sub InputToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputToolStripMenuItem.Click wv.Replay() End Sub Private Sub OutputToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OutputToolStripMenuItem.Click fwv.Quantise().Replay() End Sub End Class