/// <summary>
 /// Compute a 1D real-symmetric fast fourier transform.
 /// </summary>
 /// <param name="data"></param>
 /// <param name="direction"></param>
 public static void RFFT(float[] data, FourierDirection direction)
 {
     if (data == null)
     {
         throw new ArgumentNullException("data");
     }
     Fourier.RFFT(data, data.Length, direction);
 }
        public void scramble()
        {
            //Create necessary variables
            List <short>   rawWavData    = backend.WAVdata;                           //get the data dump of wav file
            int            sampleRateWav = backend.sampleRate;                        //get samplerate of wav file
            List <float[]> dataPerNote   = convertRawData(rawWavData, sampleRateWav); //convert dump into split arrays

            //foreach sample do: a Forward FFT, Flip the values, Backwards FFT
            foreach (float[] noteSamples in dataPerNote)
            {
                //Forward FFT
                Fourier.RFFT(noteSamples, FourierDirection.Forward);

                //Flip the values, (in pairs Real and Imaginary)
                float[] temporary = new float[noteSamples.Length];
                for (int i = 0; i < noteSamples.Length; i += 2)
                {
                    temporary[i]     = noteSamples[noteSamples.Length - 2 - i];
                    temporary[i + 1] = noteSamples[noteSamples.Length - 1 - i];
                }
                Array.Copy(temporary, noteSamples, noteSamples.Length);

                //Forward FFT and Compensate for distortion due to Fourier Transformation
                Fourier.RFFT(noteSamples, FourierDirection.Backward);
                for (int i = 0; i < noteSamples.Length; i++)
                {
                    noteSamples[i] /= noteSamples.Length / 2;
                    noteSamples[i]  = (float)Math.Round(noteSamples[i]);
                }
            }

            //Pass on new Wav file Data
            List <short> newWavData = convertNewData(dataPerNote); //convert split arrays back to dump

            backend.newWavData(newWavData, rawWavData);            //Send to backend
        }
Example #3
0
        public string convert(int ms, bool roundIt, bool returnUnfound, bool seperatorIsComma)
        {
            //read csv file for tones Dictionary
            backend.setSeperator(seperatorIsComma);
            Dictionary <char, double> tonesDictionary = backend.csvDictionary;

            //Read the wav file for usefull values and comunicate with data layer
            List <short> rawWavData    = backend.WAVdata;
            int          sampleRateWav = backend.sampleRate;
            int          notes         = ((rawWavData.Count * 1000) / sampleRateWav) / ms; //aantal tonen in Wav

            //converteer de rawData naar floats per toon
            List <float[]> dataPerNote = convertRawData(rawWavData, sampleRateWav, notes);

            //RFFT toepassen en dominante frequencie zoeken
            List <double> frequencyList = new List <double>();

            foreach (float[] noteSamples in dataPerNote)
            {
                //fourier
                Fourier.RFFT(noteSamples, FourierDirection.Forward);

                //zoek freq
                double maxAmplitude = 0;
                int    positie      = 0;

                for (int i = 0; i < noteSamples.Length; i += 2)//max en pos bepalen
                {
                    double vermogen = Math.Sqrt((Math.Pow(noteSamples[i], 2)) + Math.Pow(noteSamples[i + 1], 2));
                    if (vermogen > maxAmplitude)
                    {
                        positie      = i;
                        maxAmplitude = vermogen;
                    }
                }

                double frequency = ((positie * sampleRateWav) / (double)noteSamples.Length) / 2;//what is the frequency

                //(afronden) en toevoegen
                if (roundIt)
                {
                    frequencyList.Add((Math.Round(frequency * roundPrecision) / roundPrecision));
                }
                else
                {
                    frequencyList.Add(frequency);
                }
            }

            //create return String
            string returnable = "";

            foreach (double frequency in frequencyList)
            {
                //als nog niet afgerond
                double _frequency = frequency;
                if (!roundIt)
                {
                    _frequency = Math.Round(frequency, roundDecimals);
                }

                if (tonesDictionary.FirstOrDefault(x => x.Value == _frequency).Key == 0)//default waarde = ongevonden
                {
                    if (!returnUnfound)
                    {
                        returnable += "? ";
                    }
                    else
                    {
                        returnable += String.Format("?\"{0}Hz\" ", _frequency);
                    }
                }
                else
                {
                    returnable += tonesDictionary.FirstOrDefault(x => x.Value == _frequency).Key + " ";
                }
            }

            return(returnable);
        }