public double[] test2000A(string filenavn)
        {
            filename = filenavn;
            // FileSystemAccess _fileSystem = new FileSystemAccess();
            //_filePathFig = filename.Substring(filename.Length - 4,filename.Length-1);
            int længde = filename.Length - 4;

            _filePathFig = filename.Substring(0, længde) + ".png";
            _filePathtxt = filename.Substring(0, længde) + ".txt";
            _filePathwav = filename.Substring(0, længde) + ".wav";

            (double[] audio, int sampleRate) = ReadWAV(_filePathwav);
            // int fftSize1 = 16384;
            int targetWidthPx = 300;
            int fftSize1      = (int)MathF.Pow(2, 5);
            int stepSize1     = 80;
            //int stepSize1 = audio.Length / targetWidthPx;
            var sg = new SpectrogramGenerator(sampleRate, fftSize: fftSize1, stepSize: stepSize1, maxFreq: 1000);

            sg.Add(audio);
            double[] testData = TakeSum(sg.GetFFTs());
            sg.SaveImage(_filePathFig, intensity: 8, dB: true, roll: true);
            line = line.Replace(',', '.');
            return(testData);
        }
Beispiel #2
0
        public void Test_SFF_Linear2()
        {
            // test creating SFF file from 16-bit 48kHz mono WAV file

            // read the wav file
            (double[] audio, int sampleRate) = AudioFile.ReadWAV("../../../../../data/03-02-03-01-02-01-19.wav");
            Assert.AreEqual(48000, sampleRate);

            // save the SFF
            int fftSize = 1 << 12;
            var spec    = new SpectrogramGenerator(sampleRate, fftSize, stepSize: 300, maxFreq: 2000);

            spec.Add(audio);
            spec.SaveData("testDoor.sff");

            // load the SFF and verify all the values are the same
            var spec2 = new SFF("testDoor.sff");

            Assert.AreEqual(spec.SampleRate, spec2.SampleRate);
            Assert.AreEqual(spec.StepSize, spec2.StepSize);
            Assert.AreEqual(spec.Width, spec2.Width);
            Assert.AreEqual(spec.FftSize, spec2.FftSize);
            Assert.AreEqual(spec.NextColumnIndex, spec2.FftFirstIndex);
            Assert.AreEqual(spec.Height, spec2.Height);
            Assert.AreEqual(spec.OffsetHz, spec2.OffsetHz);
            Assert.AreEqual("SFF 701x170", spec2.ToString());
        }
Beispiel #3
0
        public void Test_SFF_Mel()
        {
            (double[] audio, int sampleRate) = AudioFile.ReadWAV("../../../../../data/cant-do-that-44100.wav");
            int fftSize = 1 << 12;
            var spec    = new SpectrogramGenerator(sampleRate, fftSize, stepSize: 700);
            var window  = new FftSharp.Windows.Hanning();

            spec.SetWindow(window.Create(fftSize / 3)); // sharper window than typical
            spec.Add(audio);

            Bitmap bmp = spec.GetBitmapMel(250);

            bmp.Save("../../../../../dev/sff/halMel.png", System.Drawing.Imaging.ImageFormat.Png);
            spec.SaveData("../../../../../dev/sff/halMel.sff", melBinCount: 250);

            var spec2 = new SFF("../../../../../dev/sff/halMel.sff");

            Assert.AreEqual(spec.SampleRate, spec2.SampleRate);
            Assert.AreEqual(spec.StepSize, spec2.StepSize);
            Assert.AreEqual(spec.Width, spec2.Width);
            Assert.AreEqual(spec.FftSize, spec2.FftSize);
            Assert.AreEqual(spec.NextColumnIndex, spec2.FftFirstIndex);
            Assert.AreEqual(spec.Height, spec2.Height);
            Assert.AreEqual(spec.OffsetHz, spec2.OffsetHz);
        }
Beispiel #4
0
        public void Test_SaveEmpty_Throws()
        {
            (double[] audio, int sampleRate) = AudioFile.ReadWAV("../../../../../data/cant-do-that-44100.wav");
            int fftSize = 4096;
            var spec    = new SpectrogramGenerator(sampleRate, fftSize, stepSize: 500);

            //spec.Add(audio);
            Assert.Throws <InvalidOperationException>(() => { spec.SaveImage("empty.png"); });
        }
Beispiel #5
0
        public void Test_Quickstart_Hal()
        {
            (double[] audio, int sampleRate) = AudioFile.ReadWAV("../../../../../data/cant-do-that-44100.wav");
            var sg = new SpectrogramGenerator(sampleRate, fftSize: 4096, stepSize: 500, maxFreq: 3000);

            sg.Add(audio);
            sg.SaveImage("../../../../../dev/graphics/hal.png");

            Console.WriteLine(sg);
        }
Beispiel #6
0
        public void Test_Readme_HeaderImage()
        {
            (double[] audio, int sampleRate) = AudioFile.ReadWAV("../../../../../data/cant-do-that-44100.wav");
            int fftSize = 2048;
            var spec    = new SpectrogramGenerator(sampleRate, fftSize, stepSize: 400, maxFreq: 6000);

            spec.Add(audio);
            spec.SaveImage("../../../../../dev/graphics/hal-spectrogram.png", intensity: 10, dB: true, dBScale: .05);

            Console.WriteLine(spec);
        }
Beispiel #7
0
        public void Test_AGC_off()
        {
            string wavFilePath = "../../../../../data/qrss-10min.wav";

            (double[] audio, int sampleRate) = AudioFile.ReadWAV(wavFilePath);

            int fftSize = 8192;
            var spec    = new SpectrogramGenerator(sampleRate, fftSize, stepSize: 2000, maxFreq: 3000);

            spec.Add(audio);
            spec.SaveImage("qrss-agc-off.png", intensity: 3);
        }
Beispiel #8
0
        public void Test_SFF_LinearBigMaxFreq()
        {
            // test creating SFF file from 16-bit 48kHz mono WAV file

            (double[] audio, int sampleRate) = AudioFile.ReadWAV("../../../../../data/03-02-03-01-02-01-19.wav");
            Assert.AreEqual(48000, sampleRate);

            int fftSize = 1 << 12;
            var spec    = new SpectrogramGenerator(sampleRate, fftSize, stepSize: 300, maxFreq: 7999);

            spec.Add(audio);
            spec.SaveData("testDoorBig.sff");
        }
        void test()
        {
            FileSystemAccess _fileSystem = new FileSystemAccess();


            _filePath    = _fileSystem.GetCombinePath("TestH1.wav");
            _filePathFig = _fileSystem.GetCombinePath("TestH1Fig.png");

            (double[] audio, int sampleRate) = ReadWAV(_filePath);
            var sg = new SpectrogramGenerator(sampleRate, fftSize: 4096, stepSize: 500, maxFreq: 3000);

            sg.Add(audio);

            sg.SaveImage(_filePathFig);
        }
Beispiel #10
0
        public void BuildSpectrogram(string _wavPath)
        {
            double[] audio; int sampleRate;
            (audio, sampleRate) = ReadWAV(_wavPath);

            var sg = new SpectrogramGenerator(sampleRate, fftSize: FFTSize, stepSize: FFTStepSize, maxFreq: MaxFrequency);

            //sg.SetFixedWidth(SpectrogramWidth);
            sg.Add(audio);
            sg.SetColormap(ImageColormap);
            //sg.SetFixedWidth(1024);
            var bmp = sg.GetBitmapMel(melBinCount: 103, dB: true, dBScale: 10);

            bmp.Save(ImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
            //sg.SaveImage(ImagePath, dB: true, dBScale: 10);
        }
Beispiel #11
0
        private void StartListening()
        {
            int sampleRate = 6000;
            int fftSize    = 1 << (9 + cbFftSize.SelectedIndex);
            int stepSize   = fftSize / 20;

            pbSpectrogram.Image?.Dispose();
            pbSpectrogram.Image = null;
            listener?.Dispose();
            listener             = new Listener(cbDevice.SelectedIndex, sampleRate);
            spec                 = new SpectrogramGenerator(sampleRate, fftSize, stepSize);
            pbSpectrogram.Height = spec.Height;

            pbScaleVert.Image?.Dispose();
            pbScaleVert.Image  = spec.GetVerticalScale(pbScaleVert.Width);
            pbScaleVert.Height = spec.Height;
        }
Beispiel #12
0
        public void Test_SFF_Linear()
        {
            (double[] audio, int sampleRate) = AudioFile.ReadWAV("../../../../../data/cant-do-that-44100.wav");
            int fftSize = 1 << 12;
            var spec    = new SpectrogramGenerator(sampleRate, fftSize, stepSize: 700, maxFreq: 2000);

            spec.SetWindow(FftSharp.Window.Hanning(fftSize / 3)); // sharper window than typical
            spec.Add(audio);
            spec.SaveData("../../../../../dev/sff/hal.sff");

            var spec2 = new SFF("../../../../../dev/sff/hal.sff");

            Assert.AreEqual(spec.SampleRate, spec2.SampleRate);
            Assert.AreEqual(spec.StepSize, spec2.StepSize);
            Assert.AreEqual(spec.Width, spec2.Width);
            Assert.AreEqual(spec.FftSize, spec2.FftSize);
            Assert.AreEqual(spec.NextColumnIndex, spec2.FftFirstIndex);
            Assert.AreEqual(spec.Height, spec2.Height);
            Assert.AreEqual(spec.OffsetHz, spec2.OffsetHz);
        }
Beispiel #13
0
 private SpectrogramGenerator GetSpectogram(double[] audio, int sampleRate)
 {
     if (sampleRate == 2000)
     {
         int targetWidthPx = 300;
         int fftSize1      = (int)MathF.Pow(2, 5);
         int stepSize1     = 80;
         var sg            = new SpectrogramGenerator(sampleRate, fftSize: fftSize1, stepSize: stepSize1, maxFreq: 1000);
         sg.Add(audio);
         return(sg);
     }
     else /*(sampleRate == 44100)*/
     {
         int fftSize1  = 16384 / 2;
         int stepSize1 = 400;
         var sg        = new SpectrogramGenerator(sampleRate, fftSize: fftSize1, stepSize: stepSize1, maxFreq: 1200);
         sg.Add(audio);
         return(sg);
     }
 }
Beispiel #14
0
        public void Test_MelSpectrogram_MelScale()
        {
            (double[] audio, int sampleRate) = AudioFile.ReadWAV("../../../../../data/cant-do-that-44100.wav");
            int fftSize = 4096;
            var sg      = new SpectrogramGenerator(sampleRate, fftSize, stepSize: 500);

            sg.Add(audio);

            Bitmap bmpMel = sg.GetBitmapMel(250);

            bmpMel.Save("../../../../../dev/graphics/halMel-MelScale.png", ImageFormat.Png);

            Bitmap bmpRaw     = sg.GetBitmap();
            Bitmap bmpCropped = new Bitmap(bmpRaw.Width, bmpMel.Height);

            using (Graphics gfx = Graphics.FromImage(bmpCropped))
            {
                gfx.DrawImage(bmpRaw, 0, bmpMel.Height - bmpRaw.Height);
            }
            bmpCropped.Save("../../../../../dev/graphics/halMel-LinearCropped.png", ImageFormat.Png);
        }
Beispiel #15
0
        public void Test_Make_CommonColormaps()
        {
            (double[] audio, int sampleRate) = AudioFile.ReadWAV("../../../../../data/cant-do-that-44100.wav");
            int fftSize = 1 << 12;
            var spec    = new SpectrogramGenerator(sampleRate, fftSize, stepSize: 700, maxFreq: 2000);

            spec.SetWindow(FftSharp.Window.Hanning(fftSize / 3)); // sharper window than typical
            spec.Add(audio);

            // delete old colormap files
            foreach (var filePath in System.IO.Directory.GetFiles("../../../../../dev/graphics/", "hal-*.png"))
            {
                System.IO.File.Delete(filePath);
            }

            foreach (var cmap in Colormap.GetColormaps())
            {
                spec.SetColormap(cmap);
                spec.SaveImage($"../../../../../dev/graphics/hal-{cmap.Name}.png");
                Debug.WriteLine($"![](dev/graphics/hal-{cmap.Name}.png)");
            }
        }
Beispiel #16
0
        public void Test_Quickstart_Handel()
        {
            double[] audio      = AudioFile.ReadMP3("../../../../../data/Handel - Air and Variations.mp3");
            int      sampleRate = 44100;

            int fftSize       = 16384;
            int targetWidthPx = 3000;
            int stepSize      = audio.Length / targetWidthPx;

            var sg = new SpectrogramGenerator(sampleRate, fftSize, stepSize, maxFreq: 2200);

            sg.Add(audio);
            sg.SaveImage("../../../../../dev/graphics/spectrogram-song.png", intensity: 5, dB: true);

            Console.WriteLine(sg);

            /*
             * Spectrogram (2993, 817)
             * Vertical (817 px): 0 - 2,199 Hz, FFT size: 16,384 samples, 2.69 Hz/px
             * Horizontal (2993 px): 2.96 min, window: 0.37 sec, step: 0.06 sec, overlap: 84%
             */
        }
Beispiel #17
0
        public void Test_AGC_normWindow()
        {
            // strategy here is to create a weighted moving window mean and normalize to that

            string wavFilePath = "../../../../../data/qrss-10min.wav";

            (double[] audio, int sampleRate) = AudioFile.ReadWAV(wavFilePath);

            int fftSize = 8192;
            var spec    = new SpectrogramGenerator(sampleRate, fftSize, stepSize: 2000, maxFreq: 3000);

            spec.Add(audio);

            var ffts = spec.GetFFTs();

            for (int i = 0; i < ffts.Count; i++)
            {
                ffts[i] = SubtractMovingWindowFloor(ffts[i]);
            }

            spec.SaveImage("qrss-agc-norm-window.png", intensity: 3);
        }
Beispiel #18
0
        public void Test_AGC_normToNoiseFloor()
        {
            // strategy here is to normalize to the magnitude of the quietest 20% of frequencies

            string wavFilePath = "../../../../../data/qrss-10min.wav";

            (double[] audio, int sampleRate) = AudioFile.ReadWAV(wavFilePath);

            int fftSize = 8192;
            var spec    = new SpectrogramGenerator(sampleRate, fftSize, stepSize: 2000, maxFreq: 3000);

            spec.Add(audio);

            var    ffts            = spec.GetFFTs();
            double normalIntensity = 2;

            for (int i = 0; i < ffts.Count; i++)
            {
                double[] sorted = new double[ffts[i].Length];
                ffts[i].CopyTo(sorted, 0);
                Array.Sort(sorted);

                double percentile      = 0.25;
                int    percentileIndex = (int)(percentile * ffts[0].Length);
                double floorValue      = sorted[percentileIndex];

                for (int y = 0; y < ffts[i].Length; y++)
                {
                    ffts[i][y] = ffts[i][y] / floorValue * normalIntensity;
                }

                Console.WriteLine(floorValue);
            }

            spec.SaveImage("qrss-agc-norm-floor.png", intensity: 3);
        }
 public static void SaveSgAsPNG(SpectrogramGenerator sg, string filePathFig)
 {
     sg.SaveImage(filePathFig, intensity: 8, dB: false, roll: true);
 }