Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.Clear();
            double[]         real  = new double[512];
            double[]         imag  = new double[512];
            double[]         mag   = new double[512];
            double[]         phase = new double[512];
            Lomont.LomontFFT lom   = new Lomont.LomontFFT();
            double[]         data  = new double[1024];
            lom.A = 1;
            lom.B = -1;

            for (int i = 0; i < 1024; i++)
            {
                data[i] = 30 * Math.Cos(2 * Math.PI * i / 16 + 3 * Math.PI / 2);
            }


            lom.RealFFT(data, true);
            for (int i = 0; i < 512; i++)
            {
                real[i] = data[2 * i];
                imag[i] = data[2 * i + 1];
            }
            for (int i = 0; i < 512; i++)
            {
                mag[i]   = Math.Round(Math.Sqrt(Math.Pow(real[i], 2) + Math.Pow(imag[i], 2)) / 1024);
                phase[i] = Math.Round(Math.Atan2(imag[i], real[i]) * 180 / Math.PI);
            }
            for (int i = 0; i < 512; i++)
            {
                Console.WriteLine(i + ".    " + "mag: " + mag[i] + "   phase: " + phase[i]);
            }
        }
Ejemplo n.º 2
0
Archivo: Fft.cs Proyecto: Faham/emophiz
        public static Complex[] ConvertToFreq(double[] timeData)
        {
            //Get the size of next power of 2
            int length = timeData.Length;
            int root = (int)Math.Log(length, 2);
            root += 1;
            int paddedLength = (int)Math.Pow(2, root);

            //Create an array that is a power of 2 and copy the values over.
            //Pad the end of the array with zeros
            double[] paddedData = new double[paddedLength];
            for(int i=0; i<paddedLength; i++)
            {
                if(i < length)
                    paddedData[i] = timeData[i];
                else
                    paddedData[i] = 0;
            }

            //Do the FFT
            Lomont.LomontFFT lomontFft = new Lomont.LomontFFT();
            lomontFft.RealFFT(paddedData, true);

            //Convert everything to the complex data type
            Complex[] freqData = new Complex[paddedLength / 2];
            for (int i = 0; i < freqData.Length; i++)
            {
                freqData[i] = new Complex(paddedData[i], paddedData[i + 1]);
            }

            return freqData;
        }
        public List <SpectralImage> CreateLogSpectrogram(AudioSamples audioSamples, SpectrogramConfig configuration)
        {
            using (new DebugTimer("CreateLogSpectrogram()"))
            {
                int wdftSize = configuration.WdftSize;
                int width    = (audioSamples.Samples.Length - wdftSize) / configuration.Overlap;
                if (width < 1)
                {
                    return(new List <SpectralImage>());
                }

                float[]  frames = new float[width * configuration.LogBins];
                ushort[] logFrequenciesIndexes = logUtility.GenerateLogFrequenciesRanges(audioSamples.SampleRate, configuration);
                float[]  window  = configuration.Window.GetWindow(wdftSize);
                float[]  samples = audioSamples.Samples;

                // PIN: reverted the following FFT to use lomontFFT with managed code (not the unsafe changed made by the original author due to the issues on my computers)

                // NOTE! When using Parallell.For the result becomes different from time to time
                // when running in Release mode.
                // Therefore make sure to use for loop instead
                for (int index = 0; index < width; index++)
                // Parallel.For(0, width, index =>
                {
                    var fftArray = CopyAndWindow(samples, index * configuration.Overlap, window);

                    lomontFFT.RealFFT(fftArray, true);

                    // after the lomont realfft the fft input array will contain the FFT values
                    // r0, r(n/2), r1, i1, r2, i2 ...
                    // since the extract log bins method only uses lowBound index above 2 we can ignore the fact
                    // that the first and second values are "special":  r0, r(n/2)
                    // see https://github.com/perivar/FindSimilar/blob/6b658b1c54d1504136e25e933f39b7c303da5d9e/Mirage/Fft.cs
                    ExtractLogBins(fftArray, logFrequenciesIndexes, configuration.LogBins, wdftSize, frames, index);
                }
                // );

                if (configuration.Verbosity == Verbosity.Verbose)
                {
                    var imageService = new FindSimilarImageService();
                    using (Image image = imageService.GetSpectrogramImage(frames, width, configuration.LogBins, width, configuration.LogBins))
                    {
                        var fileName = Path.Combine(SoundFingerprinter.DEBUG_DIRECTORY_PATH, (Path.GetFileNameWithoutExtension(audioSamples.Origin) + "_spectrogram.png"));
                        if (fileName != null)
                        {
                            image.Save(fileName, ImageFormat.Png);
                        }
                    }

                    WriteOutputUtils.WriteCSV(frames, Path.Combine(SoundFingerprinter.DEBUG_DIRECTORY_PATH, (Path.GetFileNameWithoutExtension(audioSamples.Origin) + "_frames.csv")));
                }

                var spectralImages = CutLogarithmizedSpectrum(frames, audioSamples.SampleRate, configuration);

                if (configuration.Verbosity == Verbosity.Verbose)
                {
                    if (spectralImages.Count > 0)
                    {
                        var spectralImageList = new List <float[]>();
                        foreach (var spectralImage in spectralImages)
                        {
                            spectralImageList.Add(spectralImage.Image);
                        }
                        var spectralImageArray = spectralImageList.ToArray();
                        WriteOutputUtils.WriteCSV(spectralImageArray, Path.Combine(SoundFingerprinter.DEBUG_DIRECTORY_PATH, (Path.GetFileNameWithoutExtension(audioSamples.Origin) + "_spectral_images.csv")), ";");
                    }
                }

                ScaleFullSpectrum(spectralImages, configuration);
                return(spectralImages);
            }
        }
Ejemplo n.º 4
0
        public static void FindPhaseByFFT(IEnumerable<double> inputSignal, double sampelingFrequency, double baseFrequency)
        {
            Lomont.LomontFFT x = new Lomont.LomontFFT();
            double[] pow2array = new double[1024];
            for (double i = 0; i < 1024; i++)
            {
                pow2array[(int) i] = Math.Cos(i * 250e3 / (2 * Math.PI));
            }
            double a1, a2;
            a1 = pow2array[10];
            x.RealFFT(pow2array, true);
            a2 = pow2array[10];
            //x.FFTPhaseFinder()

            a2 = pow2array[10];
        }