public void TestSpectrograms() { var recordingPath = PathHelper.ResolveAsset("Recordings", "SM304264_0+1_20160421_004539_47-48min.wav"); // "SM304264_0+1_20160421_094539_37-38min.wav" var resultDir = PathHelper.ResolveAssetPath("SpectrogramTestResults"); var outputAmpSpecImagePath = Path.Combine(resultDir, "AmplitudeSpectrogram.bmp"); var outputDecibelSpecImagePath = Path.Combine(resultDir, "DecibelSpectrogram.bmp"); var outputEnergySpecImagePath = Path.Combine(resultDir, "EnergySpectrogram.bmp"); var outputLogEnergySpecImagePath = Path.Combine(resultDir, "LogEnergySpectrogram.bmp"); var outputLinScaImagePath = Path.Combine(resultDir, "LinearScaleSpectrogram.bmp"); var outputMelScaImagePath = Path.Combine(resultDir, "MelScaleSpectrogram.bmp"); var outputNormalizedImagePath = Path.Combine(resultDir, "NormalizedSpectrogram.bmp"); var outputNoiseReducedImagePath = Path.Combine(resultDir, "NoiseReducedSpectrogram.bmp"); var outputLogPsdImagePath = Path.Combine(resultDir, "Psd.bmp"); var recording = new AudioRecording(recordingPath); int nyquist = recording.Nyquist; // 11025; int frameSize = 1024; int finalBinCount = 512; //256; //128; // 100; // 40; // 200; // int hertzInterval = 1000; //FreqScaleType scaleType = FreqScaleType.Linear; var scaleType = FreqScaleType.Mel; //var freqScale = new FrequencyScale(scaleType, nyquist, frameSize, finalBinCount, hertzInterval); //var fst = freqScale.ScaleType; var settings = new SpectrogramSettings() { WindowSize = frameSize, WindowOverlap = 0.1028, DoMelScale = (scaleType == FreqScaleType.Mel) ? true : false, MelBinCount = 256, //(scaleType == FreqScaleType.Mel) ? finalBinCount : frameSize / 2, NoiseReductionType = NoiseReductionType.None, //NoiseReductionType = NoiseReductionType.Median, }; //settings.NoiseReductionParameter = 0.0; // backgroundNeighbourhood noise reduction in dB settings.SourceFileName = recording.BaseName; //var sonogram = new SpectrogramStandard(sonoConfig, recording.WavReader); var sonogram = new EnergySpectrogram(settings, recording.WavReader); sonogram.Data = MatrixTools.Matrix2LogValues(sonogram.Data); var attributes = new SpectrogramAttributes() { NyquistFrequency = sonogram.Attributes.NyquistFrequency, Duration = sonogram.Attributes.Duration, }; Image image = DecibelSpectrogram.DrawSpectrogramAnnotated(sonogram.Data, settings, attributes); //image.Save(outputLogEnergySpecImagePath, ImageFormat.Bmp); //var logSonogramData = MatrixTools.Matrix2LogValues(sonogram.Data); //var dbSpectrogram = new DecibelSpectrogram(settings, recording.WavReader); //dbSpectrogram.DrawSpectrogram(outputMelScaImagePath); //var energySpectro = new EnergySpectrogram(settings, recording.WavReader); //var image = SpectrogramTools.GetImage(sonogram.Data, nyquist, settings.DoMelScale); //var specImage = SpectrogramTools.GetImageFullyAnnotated(image, "MELSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations, settings.Duration); //var logSonogramData = MatrixTools.Matrix2LogValues(sonogram.Data); //var image = SpectrogramTools.GetImage(logSonogramData, nyquist, settings.DoMelScale); //var specImage = SpectrogramTools.GetImageFullyAnnotated(image, "MELSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations, sonogram.Attributes.Duration); //specImage.Save(outputMelScaImagePath); //specImage.Save(outputAmpSpecImagePath); // DO RMS NORMALIZATION //sonogram.Data = SNR.RmsNormalization(sonogram.Data); //energySpectro.Data = SNR.RmsNormalization(energySpectro.Data); //dbSpectrogram.DrawSpectrogram(outputNormalizedImagePath); //var image2 = SpectrogramTools.GetImage(dbSpectrogram.Data, nyquist, settings.DoMelScale); //var normImage = SpectrogramTools.GetImageFullyAnnotated(image2, "NORMALIZEDSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations, sonogram.Attributes.Duration); //normImage.Save(outputNormalizedImagePath); // DO NOISE REDUCTION sonogram.Data = PcaWhitening.NoiseReduction(sonogram.Data); //dbSpectrogram.DrawSpectrogram(outputNoiseReducedImagePath); //var image3 = SpectrogramTools.GetImage(dbSpectrogram.Data, nyquist, settings.DoMelScale); //var noiseReducedImage = SpectrogramTools.GetImageFullyAnnotated(image3, "NOISEREDUCEDSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations, sonogram.Attributes.Duration); //noiseReducedImage.Save(outputNoiseReducedImagePath); Image image2 = DecibelSpectrogram.DrawSpectrogramAnnotated(sonogram.Data, settings, attributes); //image2.Save(outputNoiseReducedImagePath, ImageFormat.Bmp); //energySpectro.DrawLogPsd(outputLogPsdImagePath); /* * var fst = FreqScaleType.Linear; * var freqScale = new FrequencyScale(fst); * var recording = new AudioRecording(recordingPath); * * var sonoConfig = new SonogramConfig * { * WindowSize = freqScale.FinalBinCount * 2, * WindowOverlap = 0.2, * SourceFName = recording.BaseName, * NoiseReductionType = NoiseReductionType.None, * NoiseReductionParameter = 0.0, * }; * * // GENERATE AMPLITUDE SPECTROGRAM * var amplitudeSpectrogram = new AmplitudeSonogram(sonoConfig, recording.WavReader); * amplitudeSpectrogram.Configuration.WindowSize = freqScale.WindowSize; * * var image = amplitudeSpectrogram.GetImageFullyAnnotated(amplitudeSpectrogram.GetImage(), "AmplitudeSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations); * image.Save(outputAmpSpecImagePath); * * // DO RMS NORMALIZATION * amplitudeSpectrogram.Data = SNR.RmsNormalization(amplitudeSpectrogram.Data); * var normImage = amplitudeSpectrogram.GetImageFullyAnnotated(amplitudeSpectrogram.GetImage(), "NORMAmplitudeSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations); * normImage.Save(outputNormAmpImagePath); * * // CONVERT NORMALIZED AMPLITUDE SPECTROGRAM TO dB SPECTROGRAM * var sonogram = new SpectrogramStandard(amplitudeSpectrogram); * var standImage = sonogram.GetImageFullyAnnotated(sonogram.GetImage(), "LinearScaleSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations); * standImage.Save(outputLinScaImagePath); * * // DO NOISE REDUCTION * sonogram.Data = PcaWhitening.NoiseReduction(sonogram.Data); * //SNR.NoiseReduce_Standard(sonogram.Data); * var noiseReducedImage = sonogram.GetImageFullyAnnotated(sonogram.GetImage(), "NOISEREDUCEDSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations); * noiseReducedImage.Save(outputNoiseReducedImagePath); */ }
public static void GenerateSpectrograms() { var recordingDir = @"M:\Liz\SupervisedPatchSamplingSet\Recordings\"; var resultDir = @"M:\Liz\SupervisedPatchSamplingSet\"; // check whether there is any file in the folder/subfolders if (Directory.GetFiles(recordingDir, "*", SearchOption.AllDirectories).Length == 0) { throw new ArgumentException("The folder of recordings is empty..."); } int frameSize = 1024; int finalBinCount = 256; FreqScaleType scaleType = FreqScaleType.Mel; var settings = new SpectrogramSettings() { WindowSize = frameSize, // the duration of each frame (according to the default value (i.e., 1024) of frame size) is 0.04644 seconds // The question is how many single-frames (i.e., patch height is equal to 1) should be selected to form one second // The "WindowOverlap" is calculated to answer this question // each 24 single-frames duration is equal to 1 second // note that the "WindowOverlap" value should be recalculated if frame size is changed // this has not yet been considered in the Config file! WindowOverlap = 0.10725204, DoMelScale = (scaleType == FreqScaleType.Mel) ? true : false, MelBinCount = (scaleType == FreqScaleType.Mel) ? finalBinCount : frameSize / 2, NoiseReductionType = NoiseReductionType.None, NoiseReductionParameter = 0.0, }; foreach (string filePath in Directory.GetFiles(recordingDir, "*.wav")) { FileInfo fileInfo = filePath.ToFileInfo(); // process the wav file if it is not empty if (fileInfo.Length != 0) { var recording = new AudioRecording(filePath); settings.SourceFileName = recording.BaseName; var amplitudeSpectrogram = new AmplitudeSpectrogram(settings, recording.WavReader); var decibelSpectrogram = new DecibelSpectrogram(amplitudeSpectrogram); // DO NOISE REDUCTION decibelSpectrogram.Data = PcaWhitening.NoiseReduction(decibelSpectrogram.Data); // draw the spectrogram var attributes = new SpectrogramAttributes() { NyquistFrequency = decibelSpectrogram.Attributes.NyquistFrequency, Duration = decibelSpectrogram.Attributes.Duration, }; Image image = DecibelSpectrogram.DrawSpectrogramAnnotated(decibelSpectrogram.Data, settings, attributes); string pathToSpectrogramFiles = Path.Combine(resultDir, "Spectrograms", settings.SourceFileName + ".bmp"); image.Save(pathToSpectrogramFiles); // write the matrix to a csv file string pathToMatrixFiles = Path.Combine(resultDir, "Matrices", settings.SourceFileName + ".csv"); Csv.WriteMatrixToCsv(pathToMatrixFiles.ToFileInfo(), decibelSpectrogram.Data); } } }
public void PowerSpectrumDensityTest() { var inputPath = @"C:\Users\kholghim\Mahnoosh\Liz\TrainSet\"; var resultPsdPath = @"C:\Users\kholghim\Mahnoosh\Liz\PowerSpectrumDensity\train_LogPSD.bmp"; var resultNoiseReducedPsdPath = @"C:\Users\kholghim\Mahnoosh\Liz\PowerSpectrumDensity\train_LogPSD_NoiseReduced.bmp"; //var inputPath =Path.Combine(inputDir, "TrainSet"); // directory of the one-min recordings of one day (21 and 23 Apr - Black Rail Data) // check whether there is any file in the folder/subfolders if (Directory.GetFiles(inputPath, "*", SearchOption.AllDirectories).Length == 0) { throw new ArgumentException("The folder of recordings is empty..."); } // get the nyquist value from the first wav file in the folder of recordings int nq = new AudioRecording(Directory.GetFiles(inputPath, "*.wav")[0]).Nyquist; int nyquist = nq; // 11025; int frameSize = 1024; int finalBinCount = 512; //256; // int hertzInterval = 1000; FreqScaleType scaleType = FreqScaleType.Linear; //var freqScale = new FrequencyScale(scaleType, nyquist, frameSize, finalBinCount, hertzInterval); //var fst = freqScale.ScaleType; //var fst = FreqScaleType.Linear; //var freqScale = new FrequencyScale(fst); var settings = new SpectrogramSettings() { WindowSize = frameSize, WindowOverlap = 0.1028, //DoMelScale = (scaleType == FreqScaleType.Mel) ? true : false, //MelBinCount = (scaleType == FreqScaleType.Mel) ? finalBinCount : frameSize / 2, //DoMelScale = false, MelBinCount = 256, DoMelScale = (scaleType == FreqScaleType.Mel) ? true : false, //MelBinCount = (scaleType == FreqScaleType.Mel) ? finalBinCount : frameSize / 2, NoiseReductionType = NoiseReductionType.None, NoiseReductionParameter = 0.0, }; var attributes = new SpectrogramAttributes() { NyquistFrequency = nyquist, Duration = TimeSpan.FromMinutes(1440), }; List <double[]> psd = new List <double[]>(); foreach (string filePath in Directory.GetFiles(inputPath, "*.wav")) { FileInfo fileInfo = filePath.ToFileInfo(); // process the wav file if it is not empty if (fileInfo.Length != 0) { var recording = new AudioRecording(filePath); //var sonogram = new SpectrogramStandard(sonoConfig, recording.WavReader); //var amplitudeSpectrogram = new AmplitudeSonogram(sonoConfig, recording.WavReader); // save the matrix // skip normalisation // skip mel settings.SourceFileName = recording.BaseName; var spectrogram = new EnergySpectrogram(settings, recording.WavReader); //var sonogram = new AmplitudeSpectrogram(settings, recording.WavReader); //var energySpectrogram = new EnergySpectrogram(sonoConfig, amplitudeSpectrogram.Data); //var energySpectrogram = new EnergySpectrogram(sonoConfig, recording.WavReader); //var energySpectrogram = new EnergySpectrogram(settings, recording.WavReader); // square the FFT coefficients to get an energy spectrogram // double[,] energySpectrogram = PowerSpectrumDensity.GetEnergyValues(amplitudeSpectrogram.Data); // RMS NORMALIZATION //double[,] normalizedValues = SNR.RmsNormalization(energySpectro.Data); //energySpectro.Data = SNR.RmsNormalization(energySpectro.Data); // Median Noise Reduction //spectrogram.Data = PcaWhitening.NoiseReduction(spectrogram.Data); //spectrogram.Data = SNR.NoiseReduce_Standard(spectrogram.Data); //double[] psd = PowerSpectralDensity.GetPowerSpectrum(noiseReducedValues); //psd.Add(energySpectro.GetLogPsd()); psd.Add(MatrixTools.GetColumnAverages(spectrogram.Data)); //psd.Add(SpectrogramTools.CalculateAvgSpectrumFromEnergySpectrogram(normalizedValues)); //psd.Add(PowerSpectralDensity.GetPowerSpectrum(normalizedValues)); } } // writing psd matrix to csv file //Csv.WriteMatrixToCsv(new FileInfo(@"C:\Users\kholghim\Mahnoosh\Liz\PowerSpectrumDensity\psd.csv"), psd.ToArray().ToMatrix()); //Image imagePsd = DecibelSpectrogram.DrawSpectrogramAnnotated(psd.ToArray().ToMatrix(), settings, attributes); //imagePsd.Save(resultPsdPath, ImageFormat.Bmp); var psdMatrix = psd.ToArray().ToMatrix(); // calculate the log of matrix var logPsd = MatrixTools.Matrix2LogValues(psdMatrix); Csv.WriteMatrixToCsv(new FileInfo(@"C:\Users\kholghim\Mahnoosh\Liz\PowerSpectrumDensity\logPsd.csv"), logPsd); var image = DecibelSpectrogram.DrawSpectrogramAnnotated(logPsd, settings, attributes); image.Save(resultPsdPath); var noiseReducedLogPsd = PcaWhitening.NoiseReduction(logPsd); //SNR.NoiseReduce_Standard(logPsd); //SNR.NoiseReduce_Mean(logPsd, 0.0);//SNR.NoiseReduce_Median(logPsd, 0.0); // Csv.WriteMatrixToCsv(new FileInfo(@"C:\Users\kholghim\Mahnoosh\Liz\PowerSpectrumDensity\logPsd_NoiseReduced.csv"), logPsd); var image2 = DecibelSpectrogram.DrawSpectrogramAnnotated(noiseReducedLogPsd, settings, attributes); image2.Save(resultNoiseReducedPsdPath); //ImageTools.DrawMatrix(psd.ToArray().ToMatrix(), resultPath); //ImageTools.DrawReversedMatrix(psd.ToArray().ToMatrix(), resultPath); //var data = MatrixTools.Matrix2LogValues(psd.ToArray().ToMatrix()); //Image image = ImageTools.DrawReversedMatrixWithoutNormalisation(data); //Image image = ImageTools.DrawReversedMatrixWithoutNormalisation(logPsd); }