Module_SignalProcessing_Others - vorcyc/Vorcyc.Mathematics GitHub Wiki

Location: Home / Signal processing / Others

Signal Processing Module

Others

The Vorcyc.Mathematics.SignalProcessing namespace includes FractionalDelayLine, Lpc, and Scale. FractionalDelayLine implements fractional delay with write, read, and reset. Lpc provides LPC coefficients, LPC cepstrum, and line spectral frequency (LSF) conversions. Scale converts between decibels, MIDI pitch, mel, bark, and ERB frequency scales.

All types below are in namespace:Vorcyc.Mathematics.SignalProcessing


:ledger: Contents


Vorcyc.Mathematics.SignalProcessing.FractionalDelayLine class

Vorcyc.Mathematics.SignalProcessing.FractionalDelayLine represents a fractional delay line for fractional delay in signal processing.

Properties

1. InterpolationMode

  • public InterpolationMode InterpolationMode { get; set; }
    • Gets or setsInterpolation mode.

2. Size

  • public int Size { get; }
    • Gets Delay line size (number of samples).

Methods

1. FractionalDelayLine constructor

  • public FractionalDelayLine(int size, InterpolationMode interpolationMode = InterpolationMode.Linear)

    • Constructs a FractionalDelayLine instance reserving the given size for samples.
    • Parameters:
      • size: Delay line size (number of samples).
      • interpolationMode: Interpolation mode.
  • public FractionalDelayLine(int samplingRate, double maxDelay, InterpolationMode interpolationMode = InterpolationMode.Linear)

    • Constructs a FractionalDelayLine instance reserving size corresponding to the given maximum delay time.
    • Parameters:
      • samplingRate: Sampling rate.
      • maxDelay: Maximum delay time (seconds).
      • interpolationMode: Interpolation mode.

2. Write

  • public void Write(float sample)
    • Writes a sample to the delay line.
    • Parameters:
      • sample: Sample.

3. Read

  • public float Read(double delay)
    • Reads sample from delay line for given delay (in samples).
    • Parameters:
      • delay: Delay (sample offset, not seconds).
    • Returns: Sample value.

4. Reset

  • public void Reset()
    • Resets the delay line.

5. Ensure

  • public void Ensure(int size)

    • Resizes the delay line to the new size. No operation if the new size does not exceed the current size.
    • Parameters:
      • size: New size.
  • public void Ensure(int samplingRate, double maxDelay)

    • Resizes the delay line to match the new size for the given maximum delay. No operation if the new size does not exceed the current size.
    • Parameters:
      • samplingRate: Sampling rate.
      • maxDelay: Maximum delay time (seconds).
  • public void Ensure(int samplingRate, float maxDelay)

    • Resizes the delay line to match the new size for the given maximum delay. No operation if the new size does not exceed the current size.
    • Parameters:
      • samplingRate: Sampling rate.
      • maxDelay: Maximum delay time (seconds).

Code example

The following example uses FractionalDelayLine with comments:

using System;
using System.Drawing;
using Vorcyc.Mathematics.SignalProcessing;

public class FractionalDelayLineExample
{
    public static void Main()
    {
        // Define delay line size and interpolation mode
        int size = 10;
        var interpolationMode = InterpolationMode.Linear;

        // Create FractionalDelayLine instance
        var delayLine = new FractionalDelayLine(size, interpolationMode);

        // Write samples to delay line
        for (int i = 0; i < size; i++)
        {
            delayLine.Write(i);
        }

        // Read sample from delay line (delay is a sample offset, not seconds)
        double delay = 2.5;
        var sample = delayLine.Read(delay);
        Console.WriteLine($"Sample at delay {delay}: {sample}");

        // Reset delay line
        delayLine.Reset();

        // Ensure new delay line size
        int newSize = 20;
        delayLine.Ensure(newSize);
        Console.WriteLine($"New size of delay line: {delayLine.Size}");

        // Ensure new delay line size (based on sampling rate and max delay)
        int samplingRate = 44100;
        double maxDelay = 0.5;
        delayLine.Ensure(samplingRate, maxDelay);
        Console.WriteLine($"New size of delay line (based on sampling rate and max delay): {delayLine.Size}");
    }
}

Vorcyc.Mathematics.SignalProcessing.Lpc class

Vorcyc.Mathematics.SignalProcessing.Lpc provides functions related to linear predictive coding (LPC).

Methods

1. LevinsonDurbin

  • public static float LevinsonDurbin(float[] input, float[] a, int order, int offset = 0)
    • Evaluates LP coefficients using the Levinson-Durbin algorithm and returns prediction error.
    • Parameters:
      • input: Autocorrelation vector.
      • a: LP coefficients.
      • order: LPC order.
      • offset: Optional offset into the autocorrelation vector.
    • Returns: Prediction error.

2. ToCepstrum

  • public static void ToCepstrum(float[] lpc, float gain, float[] lpcc)
    • Converts LPC coefficients to LPC cepstrum (LPCC).
    • Parameters:
      • lpc: LPC vector.
      • gain: Gain.
      • lpcc: LPC cepstrum.

3. FromCepstrum

  • public static float FromCepstrum(float[] lpcc, float[] lpc)
    • Converts LPC cepstrum to LPC coefficients and returns gain.
    • Parameters:
      • lpcc: LPC cepstrum.
      • lpc: LPC vector.
    • Returns: Gain.

4. EstimateOrder

  • public static int EstimateOrder(int samplingRate)
    • Estimates LPC order for given sampling rate per best practices.
    • Parameters:
      • samplingRate: Sampling rate.
    • Returns: LPC order.

5. ToLsf

  • public static void ToLsf(float[] lpc, float[] lsf)
    • Converts LPC coefficients to line spectral frequencies (LSF). lsf length must equal lpc length; last element will be PI.
    • Parameters:
      • lpc: LPC vector.
      • lsf: Line spectral frequencies.

6. FromLsf

  • public static void FromLsf(float[] lsf, float[] lpc)
    • Converts line spectral frequencies (LSF) to LPC coefficients. lsf length must equal lpc length; last element must be PI.
    • Parameters:
      • lsf: Line spectral frequencies.
      • lpc: LPC vector.

Code example

The following example uses Lpc with comments:

using System;
using Vorcyc.Mathematics.SignalProcessing;

public class LpcExample
{
    public static void Main()
    {
        // Define autocorrelation vector and LPC order
        var input = new float[] { 1.0f, 0.9f, 0.7f, 0.5f, 0.3f }; var order = 4;

        // Compute LPC coefficients
        var a = new float[order + 1];
        var error = Lpc.LevinsonDurbin(input, a, order);
        Console.WriteLine("LPC Coefficients:");

        foreach (var coeff in a)
        {
            Console.WriteLine(coeff);
        }
        Console.WriteLine($"Prediction Error: {error}");

        // Convert LPC coefficients to LPC cepstrum
        var lpcc = new float[order + 1];
        var gain = 1.0f;
        Lpc.ToCepstrum(a, gain, lpcc);
        Console.WriteLine("LPC Cepstrum:");

        foreach (var coeff in lpcc)
        {
            Console.WriteLine(coeff);
        }

        // Convert LPC cepstrum back to LPC coefficients
        var lpcFromCepstrum = new float[order + 1];
        var gainFromCepstrum = Lpc.FromCepstrum(lpcc, lpcFromCepstrum);
        Console.WriteLine("LPC Coefficients from Cepstrum:");

        foreach (var coeff in lpcFromCepstrum)
        {
            Console.WriteLine(coeff);
        }
        Console.WriteLine($"Gain from Cepstrum: {gainFromCepstrum}");

        // Estimate LPC order for sampling rate
        var samplingRate = 44100;
        var estimatedOrder = Lpc.EstimateOrder(samplingRate);
        Console.WriteLine($"Estimated LPC Order for sampling rate {samplingRate}: {estimatedOrder}");

        // Convert LPC coefficients to LSF
        var lsf = new float[order + 1];
        Lpc.ToLsf(a, lsf);
        Console.WriteLine("Line Spectral Frequencies (LSF):");

        foreach (var freq in lsf)
        {
            Console.WriteLine(freq);
        }

        // Convert LSF back to LPC coefficients
        var lpcFromLsf = new float[order + 1];
        Lpc.FromLsf(lsf, lpcFromLsf);
        Console.WriteLine("LPC Coefficients from LSF:");

        foreach (var coeff in lpcFromLsf)
        {
            Console.WriteLine(coeff);
        }
    }
}

Vorcyc.Mathematics.SignalProcessing.Scale class

Vorcyc.Mathematics.SignalProcessing.Scale provides conversions between scales including decibels, MIDI pitch, mel frequency, bark frequency, and ERB frequency.

Methods

1. ToDecibel

  • public static double ToDecibel(double value, double valueReference)

    • Converts amplitude value to decibel level.
    • Parameters:
      • value: Amplitude value.
      • valueReference: Reference amplitude value.
    • Returns: Decibel level.
  • public static double ToDecibel(double value)

    • Converts amplitude value to decibel level (simplified).
    • Parameters:
      • value: Amplitude value.
    • Returns: Decibel level.
  • public static float ToDecibel(float value)

    • Converts amplitude value to decibel level (simplified).
    • Parameters:
      • value: Amplitude value.
    • Returns: Decibel level.

2. ToDecibelPower

  • public static double ToDecibelPower(double value, double valueReference = 1.0)

    • Converts power value to decibel level.
    • Parameters:
      • value: Power value.
      • valueReference: Reference power value.
    • Returns: Decibel level.
  • public static float ToDecibelPower(float value, float valueReference = 1.0f)

    • Converts power value to decibel level.
    • Parameters:
      • value: Power value.
      • valueReference: Reference power value.
    • Returns: Decibel level.

3. FromDecibel

  • public static double FromDecibel(double level, double valueReference)

    • Converts decibel level to amplitude value.
    • Parameters:
      • level: Decibel level.
      • valueReference: Reference amplitude value.
    • Returns: Amplitude value.
  • public static float FromDecibel(float level, float valueReference)

    • Converts decibel level to amplitude value.
    • Parameters:
      • level: Decibel level.
      • valueReference: Reference amplitude value.
    • Returns: Amplitude value.
  • public static double FromDecibel(double level)

    • Converts decibel level to amplitude value (simplified).
    • Parameters:
      • level: Decibel level.
    • Returns: Amplitude value.
  • public static float FromDecibel(float level)

    • Converts decibel level to amplitude value (simplified).
    • Parameters:
      • level: Decibel level.
    • Returns: Amplitude value.

4. FromDecibelPower

  • public static double FromDecibelPower(double level, double valueReference = 1.0)
    • Converts decibel level to power value.
    • Parameters:
      • level: Decibel level.
      • valueReference: Reference power value.
    • Returns: Power value.

5. PitchToFreq

  • public static double PitchToFreq(int pitch)
    • Converts MIDI pitch to frequency (Hz).
    • Parameters:
      • pitch: Pitch.
    • Returns: Frequency (Hz).

6. FreqToPitch

  • public static int FreqToPitch(double freq)
    • Converts frequency to MIDI pitch.
    • Parameters:
      • freq: Frequency (Hz).
    • Returns: Pitch.

7. NoteToFreq

  • public static double NoteToFreq(string note, int octave)
    • Converts musical note (e.g. "G", 3) to frequency (Hz).
    • Parameters:
      • note: Note (A–G#).
      • octave: Octave (0–8).
    • Returns: Frequency (Hz).

8. FreqToNote

  • public static (string note, int octave) FreqToNote(double freq)
    • Converts frequency (Hz) to note (e.g. "G", 3).
    • Parameters:
      • freq: Frequency (Hz).
    • Returns: Note and octave.

9. HerzToMel

  • public static double HerzToMel(double herz)

    • Converts Hz frequency to mel frequency.
    • Parameters:
      • herz: Hz frequency.
    • Returns: Mel frequency.
  • public static float HerzToMel(float herz)

    • Converts Hz frequency to mel frequency.
    • Parameters:
      • herz: Hz frequency.
    • Returns: Mel frequency.

10. MelToHerz

  • public static double MelToHerz(double mel)

    • Converts mel frequency to Hz frequency.
    • Parameters:
      • mel: Mel frequency.
    • Returns: Hz frequency.
  • public static float MelToHerz(float mel)

    • Converts mel frequency to Hz frequency.
    • Parameters:
      • mel: Mel frequency.
    • Returns: Hz frequency.

11. HerzToMelSlaney

  • public static double HerzToMelSlaney(double herz)

    • Converts Hz frequency to mel frequency (M. Slaney).
    • Parameters:
      • herz: Hz frequency.
    • Returns: Mel frequency.
  • public static float HerzToMelSlaney(float herz)

    • Converts Hz frequency to mel frequency (M. Slaney).
    • Parameters:
      • herz: Hz frequency.
    • Returns: Mel frequency.

12. MelToHerzSlaney

  • public static double MelToHerzSlaney(double mel)

    • Converts mel frequency to Hz frequency (M. Slaney).
    • Parameters:
      • mel: Mel frequency.
    • Returns: Hz frequency.
  • public static float MelToHerzSlaney(float mel)

    • Converts mel frequency to Hz frequency (M. Slaney).
    • Parameters:
      • mel: Mel frequency.
    • Returns: Hz frequency.

13. HerzToBark

  • public static double HerzToBark(double herz)

    • Converts Hz frequency to bark frequency (Traunmüller 1990).
    • Parameters:
      • herz: Hz frequency.
    • Returns: Bark frequency.
  • public static float HerzToBark(float herz)

    • Converts Hz frequency to bark frequency (Traunmüller 1990).
    • Parameters:
      • herz: Hz frequency.
    • Returns: Bark frequency.

14. BarkToHerz

  • public static double BarkToHerz(double bark)

    • Converts bark frequency to Hz frequency (Traunmüller 1990).
    • Parameters:
      • bark: Bark frequency.
    • Returns: Hz frequency.
  • public static float BarkToHerz(float bark)

    • Converts bark frequency to Hz frequency (Traunmüller 1990).
    • Parameters:
      • bark: Bark frequency.
    • Returns: Hz frequency.

15. HerzToBarkSlaney

  • public static double HerzToBarkSlaney(double herz)

    • Converts Hz frequency to bark frequency (Wang 1992).
    • Parameters:
      • herz: Hz frequency.
    • Returns: Bark frequency.
  • public static float HerzToBarkSlaney(float herz)

    • Converts Hz frequency to bark frequency (Wang 1992).
    • Parameters:
      • herz: Hz frequency.
    • Returns: Bark frequency.

16. BarkToHerzSlaney

  • public static double BarkToHerzSlaney(double bark)

    • Converts bark frequency to Hz frequency (Wang 1992).
    • Parameters:
      • bark: Bark frequency.
    • Returns: Hz frequency.
  • public static float BarkToHerzSlaney(float bark)

    • Converts bark frequency to Hz frequency (Wang 1992).
    • Parameters:
      • bark: Bark frequency.
    • Returns: Hz frequency.

17. HerzToErb

  • public static double HerzToErb(double herz)

    • Converts Hz frequency to ERB frequency.
    • Parameters:
      • herz: Hz frequency.
    • Returns: ERB frequency.
  • public static float HerzToErb(float herz)

    • Converts Hz frequency to ERB frequency.
    • Parameters:
      • herz: Hz frequency.
    • Returns: ERB frequency.

18. ErbToHerz

  • public static double ErbToHerz(double erb)

    • Converts ERB frequency to Hz frequency.
    • Parameters:
      • erb: ERB frequency.
    • Returns: Hz frequency.
  • public static float ErbToHerz(float erb)

    • Converts ERB frequency to Hz frequency.
    • Parameters:
      • erb: ERB frequency.
    • Returns: Hz frequency.

19. HerzToOctave

  • public static double HerzToOctave(double herz, double tuning = 0, int binsPerOctave = 12)

    • Converts Hz frequency to octave (for librosa-like Chroma filters).
    • Parameters:
      • herz: Hz frequency.
      • tuning: Tuning.
      • binsPerOctave: Bins per octave.
    • Returns: Octave.
  • public static float HerzToOctave(float herz, float tuning = 0, int binsPerOctave = 12)

    • Converts Hz frequency to octave (for librosa-like Chroma filters).
    • Parameters:
      • herz: Hz frequency.
      • tuning: Tuning.
      • binsPerOctave: Bins per octave.
    • Returns: Octave.

20. LoudnessWeighting

  • public static double LoudnessWeighting(double frequency, string weightingType = "A")

    • Returns perceptual loudness weighting (in decibels).
    • Parameters:
      • frequency: Frequency.
      • weightingType: Weighting type (A, B, C).
    • Returns: Loudness weighting (decibels).
  • public static float LoudnessWeighting(float frequency, string weightingType = "A")

    • Returns perceptual loudness weighting (in decibels).
    • Parameters:
      • frequency: Frequency.
      • weightingType: Weighting type (A, B, C).
    • Returns: Loudness weighting (decibels).

Code example

The following example uses Scale with comments:

using System;
using Vorcyc.Mathematics.SignalProcessing;
using static System.Formats.Asn1.AsnWriter;

public class ScaleExample
{
    public static void Main()
    {
        // Convert amplitude to decibel level
        double magnitude = 0.5;
        double decibel = Scale.ToDecibel(magnitude);
        Console.WriteLine($"Magnitude {magnitude} to Decibel: {decibel}");

        // Convert decibel level to amplitude
        double level = -6.0;
        double magnitudeFromDb = Scale.FromDecibel(level);
        Console.WriteLine($"Decibel {level} to Magnitude: {magnitudeFromDb}");

        // Convert MIDI pitch to frequency
        int pitch = 69;
        double frequency = Scale.PitchToFreq(pitch);
        Console.WriteLine($"Pitch {pitch} to Frequency: {frequency} Hz");

        // Convert frequency to MIDI pitch
        double freq = 440.0;
        int midiPitch = Scale.FreqToPitch(freq);
        Console.WriteLine($"Frequency {freq} Hz to Pitch: {midiPitch}");

        // Convert Hz to mel frequency
        double herz = 1000.0;
        double mel = Scale.HerzToMel(herz);
        Console.WriteLine($"Herz {herz} to Mel: {mel}");

        // Convert mel to Hz frequency
        double herzFromMel = Scale.MelToHerz(mel);
        Console.WriteLine($"Mel {mel} to Herz: {herzFromMel}");
    }
}