Beispiel #1
0
    /// <summary>
    ///  This corrutine obtains these steps in order:
    ///  1) gets the data from the audiosource
    ///  2) displays the signal in time domain
    ///  3) displays the signal in FFT
    /// </summary>

    IEnumerator treatmentOfSound()
    {
        //ONE IS THE INPUT VALUES
        float[] samples = new float[windowSize];

        audioS.clip.GetData(samples, 0);


        //this is the window size
        for (int ii = 0; ii < windowSize; ii++)
        {
            X_inputValues[ii] = ii;
            Y_inputValues[ii] = (double)samples[ii];
        }


        //perform complex opterations and set up the arrays
        Complex[] inputSignal_Time  = new Complex[windowSize];
        Complex[] outputSignal_Freq = new Complex[windowSize];


        inputSignal_Time = FastFourierTransform.doubleToComplex(Y_inputValues);


        //result is the iutput values once FFT has been applied
        outputSignal_Freq = FastFourierTransform.FFT(inputSignal_Time, false);


        Y_output = new double[windowSize];
        //get module of complex number
        for (int ii = 0; ii < windowSize; ii++)
        {
            //Debug.Log(ii);
            Y_output[ii] = (double)Complex.Abs(outputSignal_Freq[ii]);
        }



        yield return(null);
    }
Beispiel #2
0
    /// <summary>
    ///  This corrutine obtains these steps in order:
    ///  1) generates a time response using SIN() signals
    ///  2) obtains a window of the signal
    ///  3) applies the FFT
    /// </summary>

    IEnumerator time_to_frequency()
    {
        while (true)
        {
            // add point to the time chart
            double signal = 0;
            for (int jj = 0; jj < Amp.Length; jj++)
            {
                signal += (double)(Amp[jj] * Mathf.Sin(2 * Mathf.PI * (float)freq[jj] * (float)count * 1 / (float)samplingFrequency + (float)phase[jj]));
            }
            count++;

            Y_values.Add(signal);


            if (Y_values.Count >= windowSize)
            {
                //ONE IS THE INPUT VALUES

                //this is the window size
                for (int ii = 0; ii < windowSize; ii++)
                {
                    X_inputValues[ii] = ii;


                    X_inputValues[ii] = ii;
                    Y_inputValues[ii] = (Y_values[ii + Y_values.Count - 1 - windowSize]);
                }



                //perform complex opterations and set up the arrays
                Complex[] inputSignal_Time  = new Complex[windowSize];
                Complex[] outputSignal_Freq = new Complex[windowSize];


                inputSignal_Time = FastFourierTransform.doubleToComplex(Y_inputValues);


                //result is the iutput values once DFT has been applied
                outputSignal_Freq = FastFourierTransform.FFT(inputSignal_Time, false);



                Y_output = new double[windowSize];
                //get module of complex number
                for (int ii = 0; ii < windowSize; ii++)
                {
                    //Debug.Log(ii);
                    Y_output[ii] = (double)Complex.Abs(outputSignal_Freq[ii]);
                }


                // find peak VALUES on the FFT
                double MaxPeak   = -1000;
                int    peakIndex = 0;
                for (int i = 1; i < Y_output.Length / 2 - 1; i++)
                {
                    if (Y_output[i] > MaxPeak)
                    {
                        MaxPeak   = Y_output[i];
                        peakIndex = i;
                    }
                }

                //store results
                maxf_obtained = (double)peakIndex / (double)windowSize / 2 * (double)samplingFrequency;
            }


            yield return(new WaitForFixedUpdate());
        }
    }
Beispiel #3
0
    /// <summary>
    ///  This corrutine obtains these steps in order:
    ///  1) generates a time response using SIN() signals
    ///  2) obtains a window of the signal
    ///  3) applies the FFT
    ///  4) filters the signal
    ///  5) applies FFT of filtered signal
    /// </summary>

    IEnumerator filterSignal()
    {
        while (true)
        {
            // add point to the time chart
            double signal = 0;
            for (int jj = 0; jj < Amp.Length; jj++)
            {
                signal += (double)(Amp[jj] * Mathf.Sin(2 * Mathf.PI * (float)freq[jj] * (float)count * 1 / (float)samplingFrequency + (float)phase[jj]));
            }
            count++;

            Y_values.Add(signal);


            if (Y_values.Count >= windowSize)
            {
                //ONE IS THE INPUT VALUES

                //this is the window size
                for (int ii = 0; ii < windowSize; ii++)
                {
                    X_inputValues[ii] = ii;


                    X_inputValues[ii] = ii;
                    Y_inputValues[ii] = (Y_values[ii + Y_values.Count - 1 - windowSize]);
                }



                //perform complex opterations and set up the arrays
                Complex[] inputSignal_Time  = new Complex[windowSize];
                Complex[] outputSignal_Freq = new Complex[windowSize];


                inputSignal_Time = FastFourierTransform.doubleToComplex(Y_inputValues);


                //result is the iutput values once FFT has been applied
                outputSignal_Freq = FastFourierTransform.FFT(inputSignal_Time, false);


                Y_output = new double[windowSize];
                //get module of complex number
                for (int ii = 0; ii < windowSize; ii++)
                {
                    //Debug.Log(ii);
                    Y_output[ii] = (double)Complex.Abs(outputSignal_Freq[ii]);
                }



                //filter process
                if (filtermode == filterMode.low_pass)
                {
                    Y_inputValues_filtered = Filters.low_pass_band(Y_inputValues, alpha);
                }
                else if (filtermode == filterMode.high_pass)
                {
                    Y_inputValues_filtered = Filters.high_pass_band(Y_inputValues, alpha);
                }



                //recalculate FFT of filtered
                //perform complex opterations and set up the arrays
                inputSignal_Time  = new Complex[windowSize];
                outputSignal_Freq = new Complex[windowSize];

                inputSignal_Time = FastFourierTransform.doubleToComplex(Y_inputValues_filtered);

                //result is the iutput values once FFT has been applied
                outputSignal_Freq = FastFourierTransform.FFT(inputSignal_Time, false);


                Y_output_filtered = new double[windowSize];
                //get module of complex number
                for (int ii = 0; ii < windowSize; ii++)
                {
                    //Debug.Log(ii);
                    Y_output_filtered[ii] = (double)Complex.Abs(outputSignal_Freq[ii]);
                }
            }


            yield return(new WaitForFixedUpdate());
        }
    }