Ejemplo n.º 1
0
        private Bitmap DrawSpectrogram(int NFFT, double[] samples, Fourier f, Color[] palette)
        {
            int NumSamples = samples.Length;

            int Overlap      = 0;
            int ColIncrement = NFFT * (1 - Overlap);

            int Numcols = NumSamples / ColIncrement;

            // make sure we don't step beyond the end of the recording
            while ((Numcols - 1) * ColIncrement + NFFT > NumSamples)
            {
                Numcols--;
            }

            double[] real      = new double[NFFT];
            double[] imag      = new double[NFFT];
            double[] magnitude = new double[NFFT / 2];
            Bitmap   bmp       = new Bitmap(Numcols, NFFT / 2);

            for (int col = 0; col <= Numcols - 1; col++)
            {
                // read a segment of the recorded signal
                for (int c = 0; c <= NFFT - 1; c++)
                {
                    imag[c] = 0;
                    real[c] = samples[col * ColIncrement + c] * Fourier.Hanning(NFFT, c);
                }

                // transform to the frequency domain
                f.FourierTransform(real, imag);

                // and compute the magnitude spectrum
                f.MagnitudeSpectrum(real, imag, Fourier.W0Hanning, magnitude);

                // Draw
                for (int newY = 0; newY < NFFT / 2 - 1; newY++)
                {
                    int colorIndex = MapToPixelIndex(magnitude[newY], 100, 255);
                    bmp.SetPixel(col, (NFFT / 2 - 1) - newY, palette[colorIndex]);
                }
            }
            return(bmp);
        }
Ejemplo n.º 2
0
        //////////////////////////////////////// SPECTRUM ///////////////////////////////////////////////////////////

        public List <Bitmap> GenerateFourierData(int NFFT, string spectrogramDirectory)
        {
            List <Bitmap> bitmaps = new List <Bitmap>();

            // setup fourier transformation
            Fourier f       = new Fourier(NFFT, true);
            double  divider = 2.0;

            for (int k = 0; k < Header.BitsPerSample - 2; k++)
            {
                divider *= 2;
            }

            // determine how to read sample values
            ReadSampleDataValueDelegate readSampleDataValue = GetSampleDataRerader();

            // set up one column of the spectrogram
            Color[] palette = new Color[NFFT];
            if (Configuration.Settings.VideoControls.SpectrogramAppearance == "Classic")
            {
                for (int colorIndex = 0; colorIndex < NFFT; colorIndex++)
                {
                    palette[colorIndex] = PaletteValue(colorIndex, NFFT);
                }
            }
            else
            {
                var list = SmoothColors(0, 0, 0, Configuration.Settings.VideoControls.WaveFormColor.R,
                                        Configuration.Settings.VideoControls.WaveFormColor.G,
                                        Configuration.Settings.VideoControls.WaveFormColor.B, NFFT);
                for (int i = 0; i < NFFT; i++)
                {
                    palette[i] = list[i];
                }
            }

            // read sample values
            DataMinValue = int.MaxValue;
            DataMaxValue = int.MinValue;
            var  samples      = new List <int>();
            int  index        = 0;
            int  sampleSize   = NFFT * 1024; // 1024 = bitmap width
            int  count        = 0;
            long totalSamples = 0;

            // load data in smaller parts
            _data            = new byte[Header.BytesPerSecond];
            _stream.Position = Header.DataStartPosition;
            int bytesRead = _stream.Read(_data, 0, _data.Length);

            while (bytesRead == Header.BytesPerSecond)
            {
                while (index < Header.BytesPerSecond)
                {
                    int value = 0;
                    for (int channelNumber = 0; channelNumber < Header.NumberOfChannels; channelNumber++)
                    {
                        value += readSampleDataValue.Invoke(ref index);
                    }
                    value = value / Header.NumberOfChannels;
                    if (value < DataMinValue)
                    {
                        DataMinValue = value;
                    }
                    if (value > DataMaxValue)
                    {
                        DataMaxValue = value;
                    }
                    samples.Add(value);
                    totalSamples++;

                    if (samples.Count == sampleSize)
                    {
                        var samplesAsReal = new double[sampleSize];
                        for (int k = 0; k < sampleSize; k++)
                        {
                            samplesAsReal[k] = samples[k] / divider;
                        }
                        Bitmap bmp = DrawSpectrogram(NFFT, samplesAsReal, f, palette);
                        bmp.Save(Path.Combine(spectrogramDirectory, count + ".gif"), System.Drawing.Imaging.ImageFormat.Gif);
                        bitmaps.Add(bmp); // save serialized gif instead????
                        samples = new List <int>();
                        count++;
                    }
                }
                bytesRead = _stream.Read(_data, 0, _data.Length);
                index     = 0;
            }

            if (samples.Count > 0)
            {
                var samplesAsReal = new double[sampleSize];
                for (int k = 0; k < sampleSize && k < samples.Count; k++)
                {
                    samplesAsReal[k] = samples[k] / divider;
                }
                Bitmap bmp = DrawSpectrogram(NFFT, samplesAsReal, f, palette);
                bmp.Save(Path.Combine(spectrogramDirectory, count + ".gif"), System.Drawing.Imaging.ImageFormat.Gif);
                bitmaps.Add(bmp); // save serialized gif instead????
            }

            XmlDocument doc = new XmlDocument();

            doc.LoadXml("<SpectrogramInfo><SampleDuration/><TotalDuration/></SpectrogramInfo>");
            double sampleDuration = Header.LengthInSeconds / (totalSamples / NFFT);
            double totalDuration  = Header.LengthInSeconds;

            doc.DocumentElement.SelectSingleNode("SampleDuration").InnerText = sampleDuration.ToString();
            doc.DocumentElement.SelectSingleNode("TotalDuration").InnerText  = totalDuration.ToString();
            doc.Save(System.IO.Path.Combine(spectrogramDirectory, "Info.xml"));

            return(bitmaps);
        }