Exemplo n.º 1
0
 public SampleAggregator(int fftLength)
 {
     if (!IsPowerOfTwo(fftLength))
     {
         throw new ArgumentException("FFT Length must be a power of two");
     }
     m = (int)Math.Log(fftLength, 2.0);
     this.fftLength = fftLength;
     fftBuffer      = new Complex[fftLength];
     fftArgs        = new FftEventArgs(fftBuffer);
 }
Exemplo n.º 2
0
        private void FftCalculated(object sender, FftEventArgs e)
        {
            List <double> fft = new List <double>();

            //Adding values form e.Result to fft List
            for (int i = 0; i < e.Result.Length / 2 - 70; i++)
            {
                fft.Add(Math.Abs(e.Result[i].Y * i / (200 - volumeTrackBar.Value)));
            }

            //set volumeLabel to the current volume
            volumeLabel.Text = "Volume: " + (volumeTrackBar.Value + 1) + " | " + (200 - volumeTrackBar.Value);

            //Check if the previous fft list isn't 0
            if (lastFft.Count != 0)
            {
                //Dampening the fft
                for (int i = 0; i < fft.Count; i++)
                {
                    if (fft[i] > lastFft[i] || bufferValueNumericUpDown.Value == 0)
                    {
                        fft[i] = (fft[i] + lastFft[i]) / 2;
                    }
                    else
                    {
                        fft[i] = lastFft[i] - (double)bufferValueNumericUpDown.Value / 1000;
                    }
                }
            }

            int           barCount  = (int)barCountNumericUpDown.Value;
            List <double> scaledFft = new List <double>();

            //Calculate volume of bars
            if (barCount > 0)
            {
                int count = fft.Count / barCount;
                for (int i = 0; i < barCount; i++)
                {
                    double temp = 0;
                    int    j;
                    for (j = i * count; j < count + i * count; j++)
                    {
                        temp += fft[j];
                    }

                    scaledFft.Add(temp);
                }
            }
            else
            {
                scaledFft.AddRange(fft);
            }

            #region flatten

            int flattenValue = (int)flattenValueNumericUpDown.Value;

            //flatten the values
            for (int i = 0; i < scaledFft.Count; i++)
            {
                if (flattenValue > 0)
                {
                    double temp = 0;
                    for (int j = 0; j < flattenValue; j++)
                    {
                        if (i + j < scaledFft.Count)
                        {
                            temp += scaledFft[i + j];
                        }
                    }
                    scaledFft[i] = temp / flattenValue;
                }
            }

            for (int i = 0; i < scaledFft.Count; i++)
            {
                if (flattenValue > 0)
                {
                    double temp = 0;
                    for (int j = 0; j < flattenValue; j++)
                    {
                        if (i - j >= 0)
                        {
                            temp += scaledFft[i - j];
                        }
                    }
                    scaledFft[i] = temp / flattenValue;
                }
            }

            #endregion flatten

            //Clear and add current data to chart1
            lastFft = fft;
            chart1.Series[0].Points.Clear();

            for (int i = 0; i < scaledFft.Count; i++)
            {
                chart1.Series[0].Points.AddY(scaledFft[i]);
            }
        }