DSBuffer

public class DSBuffer

Fixed-length buffer for windowed signal processing

  • Initializer

    Tips:

    • If you do not need to perform FFT on the buffer, set fftIsSupperted to be false could save 50% memory.
    • If you need to perform FFT, set buffer size to power of 2 could accelerate more.

    Declaration

    Swift

    init(_ size: Int, fftIsSupported: Bool = true)

    Parameters

    size

    Buffer length. If you set fftIsSupperted to be true, the size should be number

    fftIsSupported

    Whether FFT will be performed on the buffer

    Return Value

    DSBuffer object

  • Push new value to buffer (and the foremost will be dropped)

    Declaration

    Swift

    func push(_ value: Float)

    Parameters

    value

    New value to be added

  • Get data by index

    Declaration

    Swift

    func dataAt(_ index: Int) -> Float
  • Get Buffer size

    Declaration

    Swift

    var bufferSize: Int
  • Dump buffer as array

    Declaration

    Swift

    var data: [Float]
  • Reset buffer to be zero filled

    Declaration

    Swift

    func clear()
  • Print buffer

    Declaration

    Swift

    func printBuffer(dataFormat: String)
  • Add value to each buffer data

    Declaration

    Swift

    func add(value: Float) -> [Float]
  • Multiply each buffer data with value

    Declaration

    Swift

    func multiply(value: Float) -> [Float]
  • Modulus by value of each buffer data

    Declaration

    Swift

    func mod(value: Float) -> [Float]
  • Remove mean value

    Declaration

    Swift

    var centralized: [Float]
  • Normalize vector to have unit length

    Declaration

    Swift

    func normalizedToUnitLength(centralized: Bool) -> [Float]

    Parameters

    centralized

    Should remove mean?

  • Normalize vector to have unit variance

    Declaration

    Swift

    func normalizedToUnitVariance(centralized: Bool) -> [Float]

    Parameters

    centralized

    Should remove mean?

  • Perform dot production with array

    Declaration

    Swift

    func dotProduct(with: [Float]) -> Float
  • Mean value

    Declaration

    Swift

    var mean: Float
  • sum

    Mean value

    Declaration

    Swift

    var sum: Float
  • Vector length

    Declaration

    Swift

    var length: Float
  • Square of length

    Declaration

    Swift

    var energy: Float
  • max

    Max value

    Declaration

    Swift

    var max: Float
  • min

    Min value

    Declaration

    Swift

    var min: Float
  • Variance

    Declaration

    Swift

    var variance: Float
  • std

    Standard deviation

    Declaration

    Swift

    var std: Float
  • Perform FFT

    Note for FFT related methods:

    • Set fftIsSupported to true when creating the buffer.
    • Buffer size should be even. If you pass odd size when creating the buffer, it is automatically increased by 1.
    • Only results in nfft/2+1 complex frequency bins from DC to Nyquist are returned.

    Declaration

    Swift

    func fft() -> (real: [Float], imaginary: [Float])
  • FFT sample frequencies

    Declaration

    Swift

    func fftFrequencies(fs: Float) -> [Float]

    Return Value

    array of size nfft/2+1

  • FFT magnitudes, i.e. abs(fft())

    Declaration

    Swift

    func fftMagnitudes() -> [Float]

    Return Value

    array of size nfft/2+1

  • Square of FFT magnitudes, i.e. (abs(fft()))^2

    Declaration

    Swift

    func squaredPowerSpectrum() -> [Float]

    Return Value

    array of size nfft/2+1

  • Mean-squared power spectrum, i.e. (abs(fft()))^2 / N

    Declaration

    Swift

    func meanSquaredPowerSpectrum() -> [Float]

    Return Value

    array of size nfft/2+1

  • Power spectral density (PSD), i.e. (abs(fft()))^2 / (fs*N)

    Declaration

    Swift

    func powerSpectralDensity(fs: Float) -> [Float]

    Return Value

    array of size nfft/2+1

  • Average power over specific frequency band, i.e. mean(abs(fft(from…to))^2)

    Declaration

    Swift

    func averageBandPower(fromFreq: Float = 0, toFreq: Float, fs: Float) -> Float
  • Undocumented

    Declaration

    Swift

    public class DSBuffer
  • Get latest FIR output

    Declaration

    Swift

    func latestFIROutput() -> Float
  • FIR filtered buffer

    Declaration

    Swift

    func FIRFiltered() -> [Float]