Module_SignalProcessing_Features - vorcyc/Vorcyc.Mathematics GitHub Wiki

Location: Home / Signal processing / Features

Signal Processing Module

Features

The Vorcyc.Mathematics.SignalProcessing.Features namespace contains Harmonic, Perceptual, Pitch, and Spectral for harmonic, perceptual, pitch estimation, and spectral audio features.

All types below are in Vorcyc.Mathematics.SignalProcessing.Features namespace。


:ledger: Contents


Vorcyc.Mathematics.SignalProcessing.Features.Harmonic class

Vorcyc.Mathematics.SignalProcessing.Features.Harmonic is a static class providing harmonic spectral feature computation.

Methods

1. Peaks

  • public static void Peaks(float[] spectrum, int[] peaks, float[] peakFrequencies, int samplingRate, float pitch = -1)
    • Finds harmonic peaks (peak indices and frequencies) in the spectrum.
    • Parameters:
      • spectrum: Spectrum.
      • peaks: Array to store computed peak positions.
      • peakFrequencies: Array to store computed peak frequencies.
      • samplingRate: Sampling rate.
      • pitch: Pitch in Hz; negative if unknown.

2. Centroid

  • public static float Centroid(float[] spectrum, int[] peaks, float[] peakFrequencies)
    • Computes harmonic centroid.
    • Parameters:
      • spectrum: Spectrum.
      • peaks: Peak positions (indices in spectrum).
      • peakFrequencies: Peak frequencies.
    • Returns: Harmonic centroid.

3. Spread

  • public static float Spread(float[] spectrum, int[] peaks, float[] peakFrequencies)
    • Computes harmonic spread.
    • Parameters:
      • spectrum: Spectrum.
      • peaks: Peak positions (indices in spectrum).
      • peakFrequencies: Peak frequencies.
    • Returns: Harmonic spread.

4. Inharmonicity

  • public static float Inharmonicity(float[] spectrum, int[] peaks, float[] peakFrequencies)
    • Computes inharmonicity.
    • Parameters:
      • spectrum: Spectrum.
      • peaks: Peak positions (indices in spectrum).
      • peakFrequencies: Peak frequencies.
    • Returns: Inharmonicity.

5. OddToEvenRatio

  • public static float OddToEvenRatio(float[] spectrum, int[] peaks)
    • Computes odd-to-even ratio.
    • Parameters:
      • spectrum: Spectrum.
      • peaks: Peak positions (indices in spectrum).
    • Returns: Odd-to-even ratio.

6. Tristimulus

  • public static float Tristimulus(float[] spectrum, int[] peaks, int n)
    • Computes tristimulus (component n).
    • Parameters:
      • spectrum: Spectrum.
      • peaks: Peak positions (indices in spectrum).
      • n: Tristimulus component index: 1, 2, or 3.
    • Returns: Tristimulus component.

Code example

The following example uses Harmonic with comments:

using System;
using Vorcyc.Mathematics.SignalProcessing.Features;

public class HarmonicExample
{
    public static void Main()
    {
        // Define spectrum
        float[] spectrum = { 0.5f, 0.6f, 0.55f, 0.7f, 0.65f, 0.8f, 0.75f, 0.9f, 0.85f, 1.0f }; int[] peaks = new int[5];
        float[] peakFrequencies = new float[5]; int samplingRate = 44100;
        // Find harmonic peaks
        Harmonic.Peaks(spectrum, peaks, peakFrequencies, samplingRate);

        // Compute harmonic centroid
        float centroid = Harmonic.Centroid(spectrum, peaks, peakFrequencies);
        Console.WriteLine($"Harmonic Centroid: {centroid}");

        // Compute harmonic spread
        float spread = Harmonic.Spread(spectrum, peaks, peakFrequencies);
        Console.WriteLine($"Harmonic Spread: {spread}");

        // Compute inharmonicity
        float inharmonicity = Harmonic.Inharmonicity(spectrum, peaks, peakFrequencies);
        Console.WriteLine($"Inharmonicity: {inharmonicity}");

        // Compute odd-to-even ratio
        float oddToEvenRatio = Harmonic.OddToEvenRatio(spectrum, peaks);
        Console.WriteLine($"Odd to Even Ratio: {oddToEvenRatio}");

        // Compute tristimulus components
        float tristimulus1 = Harmonic.Tristimulus(spectrum, peaks, 1);
        float tristimulus2 = Harmonic.Tristimulus(spectrum, peaks, 2);
        float tristimulus3 = Harmonic.Tristimulus(spectrum, peaks, 3);
        Console.WriteLine($"Tristimulus 1: {tristimulus1}");
        Console.WriteLine($"Tristimulus 2: {tristimulus2}");
        Console.WriteLine($"Tristimulus 3: {tristimulus3}");
    }
}

Vorcyc.Mathematics.SignalProcessing.Features.Perceptual class

Vorcyc.Mathematics.SignalProcessing.Features.Perceptual is a static class providing perceptual audio feature computation.

Methods

1. Loudness

  • public static float Loudness(float[] spectralBands)
    • Computes perceptual loudness (sum of specific loudness: N'(z) = E(z)^0.23).
    • Parameters:
      • spectralBands: Energy array in the given bands.
    • Returns: Perceptual loudness.

2. Sharpness

  • public static float Sharpness(float[] spectralBands)
    • Computes perceptual sharpness (spectral centroid equivalent).
    • Parameters:
      • spectralBands: Energy array in the given bands.
    • Returns: Perceptual sharpness.

Code example

The following example uses Perceptual with comments:

using System;
using Vorcyc.Mathematics.SignalProcessing.Features;

public class PerceptualExample
{
    public static void Main()
    {
        // Define band energies
        float[] spectralBands = { 0.5f, 0.6f, 0.55f, 0.7f, 0.65f };
        // Compute perceptual loudness
        float loudness = Perceptual.Loudness(spectralBands);
        Console.WriteLine($"Loudness: {loudness}");

        // Compute perceptual sharpness
        float sharpness = Perceptual.Sharpness(spectralBands);
        Console.WriteLine($"Sharpness: {sharpness}");
    }
}

Vorcyc.Mathematics.SignalProcessing.Features.Pitch class

Vorcyc.Mathematics.SignalProcessing.Features.Pitch is a static class providing pitch estimation and tracking.

Methods

1. FromAutoCorrelation

  • public static float FromAutoCorrelation(float[] samples, int samplingRate, int startPos = 0, int endPos = -1, float low = 80, float high = 400)
    • Estimates pitch from samples using autocorrelation.
    • Parameters:
      • samples: Sample array.
      • samplingRate: Sampling rate.
      • startPos: Index of first sample to process in the array.
      • endPos: Index of last sample to process in the array (exclusive).
      • low: Lower bound of expected pitch range.
      • high: Upper bound of expected pitch range.
    • Returns: Estimated pitch.

2. FromAutoCorrelation

  • public static float FromAutoCorrelation(Signal signal, int startPos = 0, int endPos = -1, float low = 80, float high = 400)
    • Estimates pitch from signal using autocorrelation.
    • Parameters:
      • signal: Signal.
      • startPos: Index of first sample to process in the signal.
      • endPos: Index of last sample to process in the signal (exclusive).
      • low: Lower bound of expected pitch range.
      • high: Upper bound of expected pitch range.
    • Returns: Estimated pitch.

3. FromZeroCrossingsSchmitt

  • public static float FromZeroCrossingsSchmitt(float[] samples, int samplingRate, int startPos = 0, int endPos = -1, float lowSchmittThreshold = -1e10f, float highSchmittThreshold = 1e10f)
    • Estimates pitch from samples using zero-crossings and Schmitt trigger.
    • Parameters:
      • samples: Sample array.
      • samplingRate: Sampling rate.
      • startPos: Index of first sample to process in the array.
      • endPos: Index of last sample to process in the array (exclusive).
      • lowSchmittThreshold: Schmitt trigger lower threshold.
      • highSchmittThreshold: Schmitt trigger upper threshold.
    • Returns: Estimated pitch.

4. FromZeroCrossingsSchmitt

  • public static float FromZeroCrossingsSchmitt(Signal signal, int startPos = 0, int endPos = -1, float lowSchmittThreshold = -1e10f, float highSchmittThreshold = 1e10f)
    • Estimates pitch from signal using zero-crossings and Schmitt trigger.
    • Parameters:
      • signal: Signal.
      • startPos: Index of first sample to process in the signal.
      • endPos: Index of last sample to process in the signal (exclusive).
      • lowSchmittThreshold: Schmitt trigger lower threshold.
      • highSchmittThreshold: Schmitt trigger upper threshold.
    • Returns: Estimated pitch.

5. FromYin

  • public static float FromYin(float[] samples, int samplingRate, int startPos = 0, int endPos = -1, float low = 80, float high = 400, float cmdfThreshold = 0.2f)
    • Estimates pitch from samples using YIN.
    • Parameters:
      • samples: Sample array.
      • samplingRate: Sampling rate.
      • startPos: Index of first sample to process in the array.
      • endPos: Index of last sample to process in the array (exclusive).
      • low: Lower bound of expected pitch range.
      • high: Upper bound of expected pitch range.
      • cmdfThreshold: CMDF threshold.
    • Returns: Estimated pitch.

6. FromYin

  • public static float FromYin(Signal signal, int startPos = 0, int endPos = -1, float low = 80, float high = 400, float cmdfThreshold = 0.2f)
    • Estimates pitch from signal using YIN.
    • Parameters:
      • signal: Signal.
      • startPos: Index of first sample to process in the signal.
      • endPos: Index of last sample to process in the signal (exclusive).
      • low: Lower bound of expected pitch range.
      • high: Upper bound of expected pitch range.
      • cmdfThreshold: CMDF threshold.
    • Returns: Estimated pitch.

7. FromHss

  • public static float FromHss(Signal signal, int startPos = 0, int endPos = -1, float low = 80, float high = 400, int fftSize = 0)
    • Estimates pitch from signal using HSS.
    • Parameters:
      • signal: Signal.
      • startPos: Index of first sample to process in the signal.
      • endPos: Index of last sample to process in the signal (exclusive).
      • low: Lower bound of expected pitch range.
      • high: Upper bound of expected pitch range.
      • fftSize: FFT size.
    • Returns: Estimated pitch.

8. FromHss

  • public static float FromHss(float[] spectrum, int samplingRate, float low = 80, float high = 400)
    • Estimates pitch from spectrum using HSS.
    • Parameters:
      • spectrum: Spectrum.
      • samplingRate: Sampling rate.
      • low: Lower bound of expected pitch range.
      • high: Upper bound of expected pitch range.
    • Returns: Estimated pitch.

9. FromHps

  • public static float FromHps(Signal signal, int startPos = 0, int endPos = -1, float low = 80, float high = 400, int fftSize = 0)
    • Estimates pitch from signal using HPS.
    • Parameters:
      • signal: Signal.
      • startPos: Index of first sample to process in the signal.
      • endPos: Index of last sample to process in the signal (exclusive).
      • low: Lower bound of expected pitch range.
      • high: Upper bound of expected pitch range.
      • fftSize: FFT size.
    • Returns: Estimated pitch.

10. FromHps

  • public static float FromHps(float[] spectrum, int samplingRate, float low = 80, float high = 400)
    • Estimates pitch from spectrum using HPS.
    • Parameters:
      • spectrum: Spectrum.
      • samplingRate: Sampling rate.
      • low: Lower bound of expected pitch range.
      • high: Upper bound of expected pitch range.
    • Returns: Estimated pitch.

11. FromSpectralPeaks

  • public static float FromSpectralPeaks(Signal signal, int startPos = 0, int endPos = -1, float low = 80, float high = 400, int fftSize = 0)
    • Estimates pitch from signal using spectral peaks.
    • Parameters:
      • signal: Signal.
      • startPos: Index of first sample to process in the signal.
      • endPos: Index of last sample to process in the signal (exclusive).
      • low: Lower bound of expected pitch range.
      • high: Upper bound of expected pitch range.
      • fftSize: FFT size.
    • Returns: Estimated pitch.

12. FromSpectralPeaks

  • public static float FromSpectralPeaks(float[] spectrum, int samplingRate, float low = 80, float high = 400)
    • Estimates pitch from spectrum using spectral peaks.
    • Parameters:
      • spectrum: Spectrum.
      • samplingRate: Sampling rate.
      • low: Lower bound of expected pitch range.
      • high: Upper bound of expected pitch range.
    • Returns: Estimated pitch.

13. FromCepstrum

  • public static float FromCepstrum(Signal signal, int startPos = 0, int endPos = -1, float low = 80, float high = 400, int cepstrumSize = 256, int fftSize = 1024)
    • Estimates pitch from signal using cepstrum.
    • Parameters:
      • signal: Signal.
      • startPos: Index of first sample to process in the signal.
      • endPos: Index of last sample to process in the signal (exclusive).
      • low: Lower bound of expected pitch range.
      • high: Upper bound of expected pitch range.
      • cepstrumSize: Cepstrum size.
      • fftSize: FFT size.
    • Returns: Estimated pitch.

Code example

The following example uses Pitch with comments:

using System;
using Vorcyc.Mathematics.SignalProcessing;
using Vorcyc.Mathematics.SignalProcessing.Features;
using Vorcyc.Mathematics.SignalProcessing.Signals;

public class PitchExample
{
    public static void Main()
    {
        // Define input signal
        float[] samples = { 0.5f, 0.6f, 0.55f, 0.7f, 0.65f }; var signal = Signal.FromCopy(samples, 44100f);
        // Estimate pitch using autocorrelation
        float pitchAutoCorrelation = Pitch.FromAutoCorrelation(signal);
        Console.WriteLine($"Pitch (AutoCorrelation): {pitchAutoCorrelation} Hz");

        // Estimate pitch using zero-crossings and Schmitt trigger
        float pitchZeroCrossingsSchmitt = Pitch.FromZeroCrossingsSchmitt(signal);
        Console.WriteLine($"Pitch (ZeroCrossingsSchmitt): {pitchZeroCrossingsSchmitt} Hz");

        // Estimate pitch using YIN
        float pitchYin = Pitch.FromYin(signal);
        Console.WriteLine($"Pitch (Yin): {pitchYin} Hz");

        // Estimate pitch using HSS
        float pitchHss = Pitch.FromHss(signal);
        Console.WriteLine($"Pitch (Hss): {pitchHss} Hz");

        // Estimate pitch using HPS
        float pitchHps = Pitch.FromHps(signal);
        Console.WriteLine($"Pitch (Hps): {pitchHps} Hz");

        // Estimate pitch from spectral peaks
        float pitchSpectralPeaks = Pitch.FromSpectralPeaks(signal);
        Console.WriteLine($"Pitch (SpectralPeaks): {pitchSpectralPeaks} Hz");

        // Estimate pitch from cepstrum
        float pitchCepstrum = Pitch.FromCepstrum(signal);
        Console.WriteLine($"Pitch (Cepstrum): {pitchCepstrum} Hz");
    }
}

Vorcyc.Mathematics.SignalProcessing.Features.Spectral class

Vorcyc.Mathematics.SignalProcessing.Features.Spectral is a static class providing spectral feature computation.

Methods

1. Centroid

  • public static float Centroid(float[] spectrum, float[] frequencies)
    • Computes spectral centroid.
    • Parameters:
      • spectrum: Spectrum.
      • frequencies: Center frequencies.
    • Returns: Spectral centroid.

2. Spread

  • public static float Spread(float[] spectrum, float[] frequencies)
    • Computes spectral spread.
    • Parameters:
      • spectrum: Spectrum.
      • frequencies: Center frequencies.
    • Returns: Spectral spread.

3. Decrease

  • public static float Decrease(float[] spectrum)
    • Computes spectral decrease.
    • Parameters:
      • spectrum: Spectrum.
    • Returns: Spectral decrease.

4. Flatness

  • public static float Flatness(float[] spectrum, float minLevel = 1e-10f)
    • Computes spectral flatness.
    • Parameters:
      • spectrum: Spectrum.
      • minLevel: Amplitude threshold.
    • Returns: Spectral flatness.

5. Noiseness

  • public static float Noiseness(float[] spectrum, float[] frequencies, float noiseFrequency = 3000)
    • Computes spectral noisiness.
    • Parameters:
      • spectrum: Spectrum.
      • frequencies: Center frequencies.
      • noiseFrequency: Lower frequency bound for noise.
    • Returns: Spectral noisiness.

6. Rolloff

  • public static float Rolloff(float[] spectrum, float[] frequencies, float rolloffPercent = 0.85f)
    • Computes spectral rolloff frequency.
    • Parameters:
      • spectrum: Spectrum.
      • frequencies: Center frequencies.
      • rolloffPercent: Rolloff percentage.
    • Returns: Spectral rolloff frequency.

7. Crest

  • public static float Crest(float[] spectrum)
    • Computes spectral crest.
    • Parameters:
      • spectrum: Spectrum.
    • Returns: Spectral crest.

8. Contrast

  • public static float[] Contrast(float[] spectrum, float[] frequencies, float minFrequency = 200, int bandCount = 6)
    • Computes spectral contrast array across bands.
    • Parameters:
      • spectrum: Spectrum.
      • frequencies: Center frequencies.
      • minFrequency: Start frequency.
      • bandCount: Number of spectral bands.
    • Returns: Spectral contrast array.

9. Contrast

  • public static float Contrast(float[] spectrum, float[] frequencies, int bandNo, float minFrequency = 200)
    • Computes spectral contrast for band index bandNo.
    • Parameters:
      • spectrum: Spectrum.
      • frequencies: Center frequencies.
      • bandNo: Spectral band index.
      • minFrequency: Start frequency.
    • Returns: Spectral contrast.

10. Entropy

  • public static float Entropy(float[] spectrum)
    • Computes Shannon entropy of spectrum (treated as PDF).
    • Parameters:
      • spectrum: Spectrum.
    • Returns: Shannon entropy of spectrum.

Code example

The following example uses Spectral with comments:

using System;
using Vorcyc.Mathematics.SignalProcessing.Features;

public class SpectralExample
{
    public static void Main()
    {
        // Define spectrum and center frequencies
        float[] spectrum = { 0.5f, 0.6f, 0.55f, 0.7f, 0.65f, 0.8f, 0.75f, 0.9f, 0.85f, 1.0f };
        float[] frequencies = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };

        // Compute spectral centroid
        float centroid = Spectral.Centroid(spectrum, frequencies);
        Console.WriteLine($"Spectral Centroid: {centroid}");

        // Compute spectral spread
        float spread = Spectral.Spread(spectrum, frequencies);
        Console.WriteLine($"Spectral Spread: {spread}");

        // Compute spectral decrease
        float decrease = Spectral.Decrease(spectrum);
        Console.WriteLine($"Spectral Decrease: {decrease}");

        // Compute spectral flatness
        float flatness = Spectral.Flatness(spectrum);
        Console.WriteLine($"Spectral Flatness: {flatness}");

        // Compute spectral noisiness
        float noiseness = Spectral.Noiseness(spectrum, frequencies);
        Console.WriteLine($"Spectral Noiseness: {noiseness}");

        // Compute spectral rolloff
        float rolloff = Spectral.Rolloff(spectrum, frequencies);
        Console.WriteLine($"Spectral Rolloff: {rolloff}");

        // Compute spectral crest
        float crest = Spectral.Crest(spectrum);
        Console.WriteLine($"Spectral Crest: {crest}");

        // Compute spectral contrast array
        float[] contrast = Spectral.Contrast(spectrum, frequencies);
        Console.WriteLine("Spectral Contrast:");
        foreach (var value in contrast)
        {
            Console.WriteLine(value);
        }

        // Compute spectral entropy
        float entropy = Spectral.Entropy(spectrum);
        Console.WriteLine($"Spectral Entropy: {entropy}");
    }
}