|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace Spectrogram |
| 8 | +{ |
| 9 | + public static class Tools |
| 10 | + { |
| 11 | + public static double[] FFT(double[] values, bool useHammingWindow = true) |
| 12 | + { |
| 13 | + int fftSize = 0; |
| 14 | + for (int i = 4; i < 20; i++) |
| 15 | + { |
| 16 | + int potentialFftSize = (int)Math.Pow(2, i); |
| 17 | + if (potentialFftSize <= values.Length) |
| 18 | + fftSize = potentialFftSize; |
| 19 | + else |
| 20 | + break; |
| 21 | + } |
| 22 | + |
| 23 | + Console.WriteLine($"Processing FFT (size: {fftSize}) of values (size: {values.Length})"); |
| 24 | + |
| 25 | + NAudio.Dsp.Complex[] fft_buffer = new NAudio.Dsp.Complex[fftSize]; |
| 26 | + for (int i = 0; i < fftSize; i++) |
| 27 | + { |
| 28 | + fft_buffer[i].X = (float)values[i]; |
| 29 | + fft_buffer[i].Y = 0; |
| 30 | + |
| 31 | + if (useHammingWindow) |
| 32 | + fft_buffer[i].X *= (float)NAudio.Dsp.FastFourierTransform.HammingWindow(i, fftSize); |
| 33 | + } |
| 34 | + |
| 35 | + // perform the FFT |
| 36 | + NAudio.Dsp.FastFourierTransform.FFT(true, (int)Math.Log(fftSize, 2.0), fft_buffer); |
| 37 | + |
| 38 | + // a list with FFT values |
| 39 | + double[] fft = new double[fftSize / 2]; |
| 40 | + |
| 41 | + for (int i = 0; i < fft.Length; i++) |
| 42 | + { |
| 43 | + // should this be sqrt(X^2+Y^2)? log10? |
| 44 | + |
| 45 | + var fftPointLeft = fft_buffer[i]; |
| 46 | + var fftPointRight = fft_buffer[fft_buffer.Length - i - 1]; |
| 47 | + |
| 48 | + fft[i] = 0; |
| 49 | + fft[i] += (double)fftPointLeft.X + (double)fftPointLeft.Y; |
| 50 | + fft[i] += (double)fftPointRight.X + (double)fftPointRight.Y; |
| 51 | + fft[i] /= 2; |
| 52 | + |
| 53 | + fft[i] = Math.Abs(fft[i]); |
| 54 | + } |
| 55 | + |
| 56 | + return fft; |
| 57 | + } |
| 58 | + |
| 59 | + public static void plotValues(double[] values, string saveFilePath = "values.png", int sampleRateHz = 8000) |
| 60 | + { |
| 61 | + var plt = new ScottPlot.Plot(); |
| 62 | + plt.PlotSignal(values, sampleRateHz, markerSize: 0, lineWidth: 2); |
| 63 | + plt.Title("Signal"); |
| 64 | + plt.YLabel("Value"); |
| 65 | + plt.XLabel("Time (sec)"); |
| 66 | + plt.AxisAuto(0); |
| 67 | + plt.SaveFig(saveFilePath); |
| 68 | + Console.WriteLine($"Saved: {System.IO.Path.GetFullPath(saveFilePath)}"); |
| 69 | + } |
| 70 | + |
| 71 | + public static void plotFFT(double[] fft, string saveFilePath = "fft.png", int sampleRateHz = 8000) |
| 72 | + { |
| 73 | + var plt = new ScottPlot.Plot(); |
| 74 | + double fftSampleRate = (double)fft.Length / sampleRateHz * 2; |
| 75 | + plt.PlotSignal(fft, fftSampleRate, markerSize: 0, lineWidth: 2); |
| 76 | + plt.Title("FFT"); |
| 77 | + plt.YLabel("Power"); |
| 78 | + plt.XLabel("Frequency (Hz)"); |
| 79 | + plt.AxisAuto(0); |
| 80 | + plt.SaveFig(saveFilePath); |
| 81 | + Console.WriteLine($"Saved: {System.IO.Path.GetFullPath(saveFilePath)}"); |
| 82 | + } |
| 83 | + |
| 84 | + public static double[] generateFakeSignal(double durationSeconds = 1, int sampleRateHz = 8000, double signalFrequencyHz = 2000) |
| 85 | + { |
| 86 | + int pointCount = (int)(durationSeconds * sampleRateHz); |
| 87 | + double[] values = new double[pointCount]; |
| 88 | + |
| 89 | + // create a sine wave |
| 90 | + double oscillations = signalFrequencyHz * durationSeconds; |
| 91 | + for (int i = 0; i < values.Length; i++) |
| 92 | + values[i] = Math.Sin(((double)i / values.Length) * 2 * Math.PI * oscillations); |
| 93 | + |
| 94 | + // add noise |
| 95 | + Random rand = new Random(); |
| 96 | + for (int i = 0; i < values.Length; i++) |
| 97 | + values[i] += (rand.NextDouble() - .5) * .1; |
| 98 | + |
| 99 | + return values; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments