Beispiel #1
0
    void ProcessData()
    {
        double[]      powerSpectrum;
        ComplexSignal cs = Signal.FromArray(dataBuffer, SAMPLE_RATE).ToComplex();

        cs.ForwardFourierTransform();
        powerSpectrum = Tools.GetPowerSpectrum(cs.GetChannel(0));

        //Convert to dB
        powerSpectrum = powerSpectrum.Select((double arg, int index) => (10 * Math.Log10(arg / baselinePowerSpectrum[index]))).ToArray();

        // Normalize values
        double minPowerSpectrum = (powerSpectrum.Min());
        double maxPowerSpectrum = (powerSpectrum.Max());

        powerSpectrum = powerSpectrum.Select((double arg, int index) => (arg - minPowerSpectrum) / (maxPowerSpectrum - minPowerSpectrum)).ToArray();

        //Pick the color scheme according to the peak frequency - Blue-Green if peak below alpha,
        //Yellow-Red otherwise
        int highestFrequency = Array.IndexOf(powerSpectrum, powerSpectrum.Max());

        currentScheme = highestFrequency >= alphaCutoff ? (highestFrequency >= gammaCutoff ? COLOUR_SCHEME.YELLOW_RED : COLOUR_SCHEME.PURPLE_RED) : COLOUR_SCHEME.BLUE_GREEN;

        Color[] spectrogramColours;

        if (currentScheme == COLOUR_SCHEME.BLUE_GREEN)
        {
            spectrogramColours = powerSpectrum.Select(((double arg) => ColourUtility.HSL2BlueGreen(arg, 0.75, 0.5))).ToArray();
        }
        else if (currentScheme == COLOUR_SCHEME.PURPLE_RED)
        {
            spectrogramColours = powerSpectrum.Select(((double arg) => ColourUtility.HSL2PurpleRed(arg, 0.75, 0.5))).ToArray();
        }
        else
        {
            spectrogramColours = powerSpectrum.Select(((double arg) => ColourUtility.HSL2YellowRed(arg, 0.75, 0.5))).ToArray();
        }
        //Set the texture

        for (int i = 0; i < numFreqs; i++)
        {
            texture.SetPixel(i, (currentWindow % maxTextureWidth), spectrogramColours[i]);
        }

        currentWindow++;
        texture.Apply();

        //Set the normal map

        float xLeft;
        float xRight;
        float yUp;
        float yDown;
        float yDelta;
        float xDelta;

        for (int i = 0; i < numFreqs; i++)
        {
            int y = (currentWindow % maxTextureWidth);
            yUp   = normalMap.GetPixel(y - 1, i).grayscale;
            yDown = normalMap.GetPixel(y + 1, i).grayscale;
            int tempI = (int)Mathf.Clamp(i, 1, numFreqs - 2);
            xLeft  = (float)powerSpectrum[tempI - 1];
            xRight = (float)powerSpectrum[tempI + 1];
            xDelta = ((xLeft - xRight) + 1) * 0.5f;
            yDelta = ((yUp - yDown) + 1) * 0.5f;
            normalMap.SetPixel(i, y, new Color(xDelta, yDelta, 1.0f, yDelta));
        }

        normalMap.Apply();
    }