From 5cb86b9e3fc74f31c56029f5e89779de8fde4e28 Mon Sep 17 00:00:00 2001 From: Vincent Esche Date: Sat, 5 Oct 2019 15:08:41 +0200 Subject: [PATCH] Unified generic parameter names --- Sources/Surge/Arithmetic/Arithmetic.swift | 32 ++-- .../Surge/Auxiliary Functions/Auxiliary.swift | 168 +++++++++--------- .../Convolution.swift | 96 +++++----- 3 files changed, 148 insertions(+), 148 deletions(-) diff --git a/Sources/Surge/Arithmetic/Arithmetic.swift b/Sources/Surge/Arithmetic/Arithmetic.swift index 06632cf..58e8ce9 100644 --- a/Sources/Surge/Arithmetic/Arithmetic.swift +++ b/Sources/Surge/Arithmetic/Arithmetic.swift @@ -526,19 +526,19 @@ func remainderInPlace(_ lhs: X) -> [Float] where X.Element == Float { +public func exp(_ lhs: L) -> [Float] where L.Element == Float { return withArray(from: lhs) { expInPlace(&$0) } } /// - Warning: does not support memory stride (assumes stride is 1). -public func exp(_ lhs: X) -> [Double] where X.Element == Double { +public func exp(_ lhs: L) -> [Double] where L.Element == Double { return withArray(from: lhs) { expInPlace(&$0) } } // MARK: - Exponential: In Place /// - Warning: does not support memory stride (assumes stride is 1). -func expInPlace(_ lhs: inout X) where X.Element == Float { +func expInPlace(_ lhs: inout L) where L.Element == Float { var elementCount: Int32 = numericCast(lhs.count) withUnsafeMutableMemory(&lhs) { lm in precondition(lm.stride == 1, "\(#function) does not support strided memory access") @@ -547,7 +547,7 @@ func expInPlace(_ lhs: inout X) where X.Elemen } /// - Warning: does not support memory stride (assumes stride is 1). -func expInPlace(_ lhs: inout X) where X.Element == Double { +func expInPlace(_ lhs: inout L) where L.Element == Double { var elementCount: Int32 = numericCast(lhs.count) withUnsafeMutableMemory(&lhs) { lm in precondition(lm.stride == 1, "\(#function) does not support strided memory access") @@ -558,19 +558,19 @@ func expInPlace(_ lhs: inout X) where X.Elemen // MARK: - Square Exponentiation /// - Warning: does not support memory stride (assumes stride is 1). -public func exp2(_ lhs: X) -> [Float] where X.Element == Float { +public func exp2(_ lhs: L) -> [Float] where L.Element == Float { return withArray(from: lhs) { exp2InPlace(&$0) } } /// - Warning: does not support memory stride (assumes stride is 1). -public func exp2(_ lhs: X) -> [Double] where X.Element == Double { +public func exp2(_ lhs: L) -> [Double] where L.Element == Double { return withArray(from: lhs) { exp2InPlace(&$0) } } // MARK: - Square Exponentiation: In Place /// - Warning: does not support memory stride (assumes stride is 1). -func exp2InPlace(_ lhs: inout X) where X.Element == Float { +func exp2InPlace(_ lhs: inout L) where L.Element == Float { var elementCount: Int32 = numericCast(lhs.count) withUnsafeMutableMemory(&lhs) { lm in precondition(lm.stride == 1, "\(#function) does not support strided memory access") @@ -579,7 +579,7 @@ func exp2InPlace(_ lhs: inout X) where X.Eleme } /// - Warning: does not support memory stride (assumes stride is 1). -func exp2InPlace(_ lhs: inout X) where X.Element == Double { +func exp2InPlace(_ lhs: inout L) where L.Element == Double { var elementCount: Int32 = numericCast(lhs.count) withUnsafeMutableMemory(&lhs) { lm in precondition(lm.stride == 1, "\(#function) does not support strided memory access") @@ -590,29 +590,29 @@ func exp2InPlace(_ lhs: inout X) where X.Eleme // MARK: - Power /// - Warning: does not support memory stride (assumes stride is 1). -public func pow(_ lhs: X, _ rhs: Y) -> [Float] where X.Element == Float, Y.Element == Float { +public func pow(_ lhs: L, _ rhs: R) -> [Float] where L.Element == Float, R.Element == Float { return withArray(from: lhs) { powInPlace(&$0, rhs) } } /// - Warning: does not support memory stride (assumes stride is 1). -public func pow(_ lhs: X, _ rhs: Y) -> [Double] where X.Element == Double, Y.Element == Double { +public func pow(_ lhs: L, _ rhs: R) -> [Double] where L.Element == Double, R.Element == Double { return withArray(from: lhs) { powInPlace(&$0, rhs) } } /// - Warning: Allocates a temporary array from `rhs` via `Array(repeating: rhs, count: lhs.count)`. -public func pow(_ lhs: X, _ rhs: Float) -> [Float] where X.Element == Float { +public func pow(_ lhs: L, _ rhs: Float) -> [Float] where L.Element == Float { return withArray(from: lhs) { powInPlace(&$0, rhs) } } /// - Warning: Allocates a temporary array from `rhs` via `Array(repeating: rhs, count: lhs.count)`. -public func pow(_ lhs: X, _ rhs: Double) -> [Double] where X.Element == Double { +public func pow(_ lhs: L, _ rhs: Double) -> [Double] where L.Element == Double { return withArray(from: lhs) { powInPlace(&$0, rhs) } } // MARK: - Power: In Place /// - Warning: does not support memory stride (assumes stride is 1). -func powInPlace(_ lhs: inout X, _ rhs: Y) where X.Element == Float, Y.Element == Float { +func powInPlace(_ lhs: inout L, _ rhs: R) where L.Element == Float, R.Element == Float { precondition(lhs.count == rhs.count, "Collections must have the same size") var elementCount: Int32 = numericCast(lhs.count) withUnsafeMutableMemory(&lhs) { lm in @@ -625,7 +625,7 @@ func powInPlace(_ l } /// - Warning: does not support memory stride (assumes stride is 1). -func powInPlace(_ lhs: inout X, _ rhs: Y) where X.Element == Double, Y.Element == Double { +func powInPlace(_ lhs: inout L, _ rhs: R) where L.Element == Double, R.Element == Double { precondition(lhs.count == rhs.count, "Collections must have the same size") var elementCount: Int32 = numericCast(lhs.count) withUnsafeMutableMemory(&lhs) { lm in @@ -638,13 +638,13 @@ func powInPlace(_ l } /// - Warning: Allocates a temporary array from `rhs` via `Array(repeating: rhs, count: lhs.count)`. -func powInPlace(_ lhs: inout X, _ rhs: Float) where X.Element == Float { +func powInPlace(_ lhs: inout L, _ rhs: Float) where L.Element == Float { let rhs = Array(repeating: rhs, count: lhs.count) return powInPlace(&lhs, rhs) } /// - Warning: Allocates a temporary array from `rhs` via `Array(repeating: rhs, count: lhs.count)`. -func powInPlace(_ lhs: inout X, _ rhs: Double) where X.Element == Double { +func powInPlace(_ lhs: inout L, _ rhs: Double) where L.Element == Double { let rhs = Array(repeating: rhs, count: lhs.count) return powInPlace(&lhs, rhs) } diff --git a/Sources/Surge/Auxiliary Functions/Auxiliary.swift b/Sources/Surge/Auxiliary Functions/Auxiliary.swift index 90246a0..040b2aa 100644 --- a/Sources/Surge/Auxiliary Functions/Auxiliary.swift +++ b/Sources/Surge/Auxiliary Functions/Auxiliary.swift @@ -25,12 +25,12 @@ import Accelerate /// Elemen-wise absolute value. /// /// - Warning: does not support memory stride (assumes stride is 1). -public func abs(_ x: C) -> [Double] where C.Element == Double { - var results = [Double](repeating: 0.0, count: numericCast(x.count)) +public func abs(_ lhs: L) -> [Double] where L.Element == Double { + var results = [Double](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - precondition(xm.stride == 1, "\(#function) does not support strided memory access") - vvfabs(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)]) + lhs.withUnsafeMemory { lm in + precondition(lm.stride == 1, "\(#function) does not support strided memory access") + vvfabs(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)]) } } return results @@ -39,12 +39,12 @@ public func abs(_ x: C) -> [Double] where C.Element = /// Elemen-wise absolute value. /// /// - Warning: does not support memory stride (assumes stride is 1). -public func abs(_ x: C) -> [Float] where C.Element == Float { - var results = [Float](repeating: 0.0, count: numericCast(x.count)) +public func abs(_ lhs: L) -> [Float] where L.Element == Float { + var results = [Float](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - precondition(xm.stride == 1, "\(#function) does not support strided memory access") - vvfabsf(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)]) + lhs.withUnsafeMemory { lm in + precondition(lm.stride == 1, "\(#function) does not support strided memory access") + vvfabsf(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)]) } } return results @@ -55,12 +55,12 @@ public func abs(_ x: C) -> [Float] where C.Element == /// Elemen-wise ceiling. /// /// - Warning: does not support memory stride (assumes stride is 1). -public func ceil(_ x: C) -> [Float] where C.Element == Float { - var results = [Float](repeating: 0.0, count: numericCast(x.count)) +public func ceil(_ lhs: L) -> [Float] where L.Element == Float { + var results = [Float](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - precondition(xm.stride == 1, "\(#function) does not support strided memory access") - vvceilf(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)]) + lhs.withUnsafeMemory { lm in + precondition(lm.stride == 1, "\(#function) does not support strided memory access") + vvceilf(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)]) } } return results @@ -69,12 +69,12 @@ public func ceil(_ x: C) -> [Float] where C.Element = /// Elemen-wise ceiling. /// /// - Warning: does not support memory stride (assumes stride is 1). -public func ceil(_ x: C) -> [Double] where C.Element == Double { - var results = [Double](repeating: 0.0, count: numericCast(x.count)) +public func ceil(_ lhs: L) -> [Double] where L.Element == Double { + var results = [Double](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - precondition(xm.stride == 1, "\(#function) does not support strided memory access") - vvceil(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)]) + lhs.withUnsafeMemory { lm in + precondition(lm.stride == 1, "\(#function) does not support strided memory access") + vvceil(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)]) } } return results @@ -82,28 +82,28 @@ public func ceil(_ x: C) -> [Double] where C.Element // MARK: - Clip -public func clip(_ x: C, low: Float, high: Float) -> [Float] where C.Element == Float { - var results = [Float](repeating: 0.0, count: numericCast(x.count)) +public func clip(_ lhs: L, low: Float, high: Float) -> [Float] where L.Element == Float { + var results = [Float](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in + lhs.withUnsafeMemory { lm in var y = low var z = high withUnsafePointers(&y, &z) { y, z in - vDSP_vclip(xm.pointer, numericCast(xm.stride), y, z, rbp.baseAddress!, 1, numericCast(xm.count)) + vDSP_vclip(lm.pointer, numericCast(lm.stride), y, z, rbp.baseAddress!, 1, numericCast(lm.count)) } } } return results } -public func clip(_ x: C, low: Double, high: Double) -> [Double] where C.Element == Double { - var results = [Double](repeating: 0.0, count: numericCast(x.count)) +public func clip(_ lhs: L, low: Double, high: Double) -> [Double] where L.Element == Double { + var results = [Double](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in + lhs.withUnsafeMemory { lm in var y = low var z = high withUnsafePointers(&y, &z) { y, z in - vDSP_vclipD(xm.pointer, numericCast(xm.stride), y, z, rbp.baseAddress!, 1, numericCast(xm.count)) + vDSP_vclipD(lm.pointer, numericCast(lm.stride), y, z, rbp.baseAddress!, 1, numericCast(lm.count)) } } } @@ -141,12 +141,12 @@ public func copysign(sign: /// Elemen-wise floor. /// /// - Warning: does not support memory stride (assumes stride is 1). -public func floor(_ x: C) -> [Float] where C.Element == Float { - var results = [Float](repeating: 0.0, count: numericCast(x.count)) +public func floor(_ lhs: L) -> [Float] where L.Element == Float { + var results = [Float](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - precondition(xm.stride == 1, "\(#function) does not support strided memory access") - vvfloorf(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)]) + lhs.withUnsafeMemory { lm in + precondition(lm.stride == 1, "\(#function) does not support strided memory access") + vvfloorf(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)]) } } return results @@ -155,12 +155,12 @@ public func floor(_ x: C) -> [Float] where C.Element /// Elemen-wise floor. /// /// - Warning: does not support memory stride (assumes stride is 1). -public func floor(_ x: C) -> [Double] where C.Element == Double { - var results = [Double](repeating: 0.0, count: numericCast(x.count)) +public func floor(_ lhs: L) -> [Double] where L.Element == Double { + var results = [Double](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - precondition(xm.stride == 1, "\(#function) does not support strided memory access") - vvfloor(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)]) + lhs.withUnsafeMemory { lm in + precondition(lm.stride == 1, "\(#function) does not support strided memory access") + vvfloor(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)]) } } return results @@ -168,21 +168,21 @@ public func floor(_ x: C) -> [Double] where C.Element // MARK: - Negate -public func neg(_ x: C) -> [Float] where C.Element == Float { - var results = [Float](repeating: 0.0, count: numericCast(x.count)) +public func neg(_ lhs: L) -> [Float] where L.Element == Float { + var results = [Float](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - vDSP_vneg(xm.pointer, numericCast(xm.stride), rbp.baseAddress!, 1, numericCast(xm.count)) + lhs.withUnsafeMemory { lm in + vDSP_vneg(lm.pointer, numericCast(lm.stride), rbp.baseAddress!, 1, numericCast(lm.count)) } } return results } -public func neg(_ x: C) -> [Double] where C.Element == Double { - var results = [Double](repeating: 0.0, count: numericCast(x.count)) +public func neg(_ lhs: L) -> [Double] where L.Element == Double { + var results = [Double](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - vDSP_vnegD(xm.pointer, numericCast(xm.stride), rbp.baseAddress!, 1, numericCast(xm.count)) + lhs.withUnsafeMemory { lm in + vDSP_vnegD(lm.pointer, numericCast(lm.stride), rbp.baseAddress!, 1, numericCast(lm.count)) } } return results @@ -191,24 +191,24 @@ public func neg(_ x: C) -> [Double] where C.Element = // MARK: - Reciprocal /// - Warning: does not support memory stride (assumes stride is 1). -public func rec(_ x: C) -> [Float] where C.Element == Float { - var results = [Float](repeating: 0.0, count: numericCast(x.count)) +public func rec(_ lhs: L) -> [Float] where L.Element == Float { + var results = [Float](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - precondition(xm.stride == 1, "\(#function) does not support strided memory access") - vvrecf(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)]) + lhs.withUnsafeMemory { lm in + precondition(lm.stride == 1, "\(#function) does not support strided memory access") + vvrecf(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)]) } } return results } /// - Warning: does not support memory stride (assumes stride is 1). -public func rec(_ x: C) -> [Double] where C.Element == Double { - var results = [Double](repeating: 0.0, count: numericCast(x.count)) +public func rec(_ lhs: L) -> [Double] where L.Element == Double { + var results = [Double](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - precondition(xm.stride == 1, "\(#function) does not support strided memory access") - vvrec(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)]) + lhs.withUnsafeMemory { lm in + precondition(lm.stride == 1, "\(#function) does not support strided memory access") + vvrec(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)]) } } return results @@ -217,24 +217,24 @@ public func rec(_ x: C) -> [Double] where C.Element = // MARK: - Round /// - Warning: does not support memory stride (assumes stride is 1). -public func round(_ x: C) -> [Float] where C.Element == Float { - var results = [Float](repeating: 0.0, count: numericCast(x.count)) +public func round(_ lhs: L) -> [Float] where L.Element == Float { + var results = [Float](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - precondition(xm.stride == 1, "\(#function) does not support strided memory access") - vvnintf(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)]) + lhs.withUnsafeMemory { lm in + precondition(lm.stride == 1, "\(#function) does not support strided memory access") + vvnintf(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)]) } } return results } /// - Warning: does not support memory stride (assumes stride is 1). -public func round(_ x: C) -> [Double] where C.Element == Double { - var results = [Double](repeating: 0.0, count: numericCast(x.count)) +public func round(_ lhs: L) -> [Double] where L.Element == Double { + var results = [Double](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - precondition(xm.stride == 1, "\(#function) does not support strided memory access") - vvnint(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)]) + lhs.withUnsafeMemory { lm in + precondition(lm.stride == 1, "\(#function) does not support strided memory access") + vvnint(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)]) } } return results @@ -242,26 +242,26 @@ public func round(_ x: C) -> [Double] where C.Element // MARK: - Threshold -public func threshold(_ x: C, low: Float) -> [Float] where C.Element == Float { - var results = [Float](repeating: 0.0, count: numericCast(x.count)) +public func threshold(_ lhs: L, low: Float) -> [Float] where L.Element == Float { + var results = [Float](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in + lhs.withUnsafeMemory { lm in var y = low withUnsafePointer(to: &y) { y in - vDSP_vthr(xm.pointer, numericCast(xm.stride), y, rbp.baseAddress!, 1, numericCast(xm.count)) + vDSP_vthr(lm.pointer, numericCast(lm.stride), y, rbp.baseAddress!, 1, numericCast(lm.count)) } } } return results } -public func threshold(_ x: C, low: Double) -> [Double] where C.Element == Double { - var results = [Double](repeating: 0.0, count: numericCast(x.count)) +public func threshold(_ lhs: L, low: Double) -> [Double] where L.Element == Double { + var results = [Double](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in + lhs.withUnsafeMemory { lm in var y = low withUnsafePointer(to: &y) { y in - vDSP_vthrD(xm.pointer, numericCast(xm.stride), y, rbp.baseAddress!, 1, numericCast(xm.count)) + vDSP_vthrD(lm.pointer, numericCast(lm.stride), y, rbp.baseAddress!, 1, numericCast(lm.count)) } } } @@ -271,24 +271,24 @@ public func threshold(_ x: C, low: Double) -> [Double // MARK: - Truncate /// - Warning: does not support memory stride (assumes stride is 1). -public func trunc(_ x: C) -> [Float] where C.Element == Float { - var results = [Float](repeating: 0.0, count: numericCast(x.count)) +public func trunc(_ lhs: L) -> [Float] where L.Element == Float { + var results = [Float](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - precondition(xm.stride == 1, "\(#function) does not support strided memory access") - vvintf(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)]) + lhs.withUnsafeMemory { lm in + precondition(lm.stride == 1, "\(#function) does not support strided memory access") + vvintf(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)]) } } return results } /// - Warning: does not support memory stride (assumes stride is 1). -public func trunc(_ x: C) -> [Double] where C.Element == Double { - var results = [Double](repeating: 0.0, count: numericCast(x.count)) +public func trunc(_ lhs: L) -> [Double] where L.Element == Double { + var results = [Double](repeating: 0.0, count: numericCast(lhs.count)) results.withUnsafeMutableBufferPointer { rbp in - x.withUnsafeMemory { xm in - precondition(xm.stride == 1, "\(#function) does not support strided memory access") - vvint(rbp.baseAddress!, xm.pointer, [numericCast(xm.count)]) + lhs.withUnsafeMemory { lm in + precondition(lm.stride == 1, "\(#function) does not support strided memory access") + vvint(rbp.baseAddress!, lm.pointer, [numericCast(lm.count)]) } } return results diff --git a/Sources/Surge/Digital Signal Processing/Convolution.swift b/Sources/Surge/Digital Signal Processing/Convolution.swift index 45489ab..1144d34 100644 --- a/Sources/Surge/Digital Signal Processing/Convolution.swift +++ b/Sources/Surge/Digital Signal Processing/Convolution.swift @@ -22,11 +22,11 @@ import Accelerate // MARK: - Convolution -/// Convolution of a signal [x], with a kernel [k]. The signal must be at least as long as the kernel. -public func conv(_ x: X, _ k: K) -> [Float] where X.Element == Float, K.Element == Float { - precondition(x.count >= k.count, "Input vector [x] must have at least as many elements as the kernel, [k]") +/// Convolution of a signal [lhs], with a kernel [k]. The signal must be at least as long as the kernel. +public func conv(_ lhs: L, _ k: K) -> [Float] where L.Element == Float, K.Element == Float { + precondition(lhs.count >= k.count, "Input vector [lhs] must have at least as many elements as the kernel, [k]") - let resultSize = numericCast(x.count) + numericCast(k.count) - 1 + let resultSize = numericCast(lhs.count) + numericCast(k.count) - 1 var result = [Float](repeating: 0, count: resultSize) result.withUnsafeMutableBufferPointer { rbp in k.withUnsafeMemory { km in @@ -34,9 +34,9 @@ public func conv(_ x: X, _ let xPad = repeatElement(0 as Float, count: km.count - 1) var xPadded = [Float]() - xPadded.reserveCapacity(xPad.count + numericCast(x.count) + xPad.count) + xPadded.reserveCapacity(xPad.count + numericCast(lhs.count) + xPad.count) xPadded.append(contentsOf: xPad) - xPadded.append(contentsOf: x) + xPadded.append(contentsOf: lhs) xPadded.append(contentsOf: xPad) vDSP_conv(xPadded, 1, kEnd, -numericCast(km.stride), rbp.baseAddress!, 1, numericCast(resultSize), numericCast(km.count)) @@ -45,11 +45,11 @@ public func conv(_ x: X, _ return result } -/// Convolution of a signal [x], with a kernel [k]. The signal must be at least as long as the kernel. -public func conv(_ x: X, _ k: K) -> [Double] where X.Element == Double, K.Element == Double { - precondition(x.count >= k.count, "Input vector [x] must have at least as many elements as the kernel, [k]") +/// Convolution of a signal [lhs], with a kernel [k]. The signal must be at least as long as the kernel. +public func conv(_ lhs: L, _ k: K) -> [Double] where L.Element == Double, K.Element == Double { + precondition(lhs.count >= k.count, "Input vector [lhs] must have at least as many elements as the kernel, [k]") - let resultSize = numericCast(x.count) + numericCast(k.count) - 1 + let resultSize = numericCast(lhs.count) + numericCast(k.count) - 1 var result = [Double](repeating: 0, count: resultSize) result.withUnsafeMutableBufferPointer { rbp in k.withUnsafeMemory { km in @@ -57,9 +57,9 @@ public func conv(_ x: X, _ let xPad = repeatElement(0 as Double, count: km.count - 1) var xPadded = [Double]() - xPadded.reserveCapacity(xPad.count + numericCast(x.count) + xPad.count) + xPadded.reserveCapacity(xPad.count + numericCast(lhs.count) + xPad.count) xPadded.append(contentsOf: xPad) - xPadded.append(contentsOf: x) + xPadded.append(contentsOf: lhs) xPadded.append(contentsOf: xPad) vDSP_convD(xPadded, 1, kEnd, -numericCast(km.stride), rbp.baseAddress!, 1, numericCast(resultSize), numericCast(km.count)) @@ -70,24 +70,24 @@ public func conv(_ x: X, _ // MARK: - Cross-Correlation -/// Cross-correlation of a signal [x], with another signal [y]. The signal [y] -/// is padded so that it is the same length as [x]. -public func xcorr(_ x: X, _ y: Y) -> [Float] where X.Element == Float, Y.Element == Float { - precondition(x.count >= y.count, "Input vector [x] must have at least as many elements as [y]") - var yPadded = [Float](y) - if x.count > y.count { - let padding = repeatElement(0 as Float, count: numericCast(x.count) - numericCast(y.count)) +/// Cross-correlation of a signal [lhs], with another signal [rhs]. The signal [rhs] +/// is padded so that it is the same length as [lhs]. +public func xcorr(_ lhs: L, _ rhs: Y) -> [Float] where L.Element == Float, Y.Element == Float { + precondition(lhs.count >= rhs.count, "Input vector [lhs] must have at least as many elements as [rhs]") + var yPadded = [Float](rhs) + if lhs.count > rhs.count { + let padding = repeatElement(0 as Float, count: numericCast(lhs.count) - numericCast(rhs.count)) yPadded.append(contentsOf: padding) } - let resultSize = numericCast(x.count) + yPadded.count - 1 + let resultSize = numericCast(lhs.count) + yPadded.count - 1 var result = [Float](repeating: 0, count: resultSize) let xPad = repeatElement(0 as Float, count: yPadded.count-1) var xPadded = [Float]() - xPadded.reserveCapacity(xPad.count + numericCast(x.count) + xPad.count) + xPadded.reserveCapacity(xPad.count + numericCast(lhs.count) + xPad.count) xPadded.append(contentsOf: xPad) - xPadded.append(contentsOf: x) + xPadded.append(contentsOf: lhs) xPadded.append(contentsOf: xPad) result.withUnsafeMutableBufferPointer { rbp in @@ -97,24 +97,24 @@ public func xcorr(_ x: X, return result } -/// Cross-correlation of a signal [x], with another signal [y]. The signal [y] -/// is padded so that it is the same length as [x]. -public func xcorr(_ x: X, _ y: Y) -> [Double] where X.Element == Double, Y.Element == Double { - precondition(x.count >= y.count, "Input vector [x] must have at least as many elements as [y]") - var yPadded = [Double](y) - if x.count > y.count { - let padding = repeatElement(0 as Double, count: numericCast(x.count) - numericCast(y.count)) +/// Cross-correlation of a signal [lhs], with another signal [rhs]. The signal [rhs] +/// is padded so that it is the same length as [lhs]. +public func xcorr(_ lhs: L, _ rhs: Y) -> [Double] where L.Element == Double, Y.Element == Double { + precondition(lhs.count >= rhs.count, "Input vector [lhs] must have at least as many elements as [rhs]") + var yPadded = [Double](rhs) + if lhs.count > rhs.count { + let padding = repeatElement(0 as Double, count: numericCast(lhs.count) - numericCast(rhs.count)) yPadded.append(contentsOf: padding) } - let resultSize = numericCast(x.count) + yPadded.count - 1 + let resultSize = numericCast(lhs.count) + yPadded.count - 1 var result = [Double](repeating: 0, count: resultSize) let xPad = repeatElement(0 as Double, count: yPadded.count-1) var xPadded = [Double]() - xPadded.reserveCapacity(xPad.count + numericCast(x.count) + xPad.count) + xPadded.reserveCapacity(xPad.count + numericCast(lhs.count) + xPad.count) xPadded.append(contentsOf: xPad) - xPadded.append(contentsOf: x) + xPadded.append(contentsOf: lhs) xPadded.append(contentsOf: xPad) result.withUnsafeMutableBufferPointer { rbp in @@ -126,42 +126,42 @@ public func xcorr(_ x: X, // MARK: - Auto-correlation -/// Auto-correlation of a signal [x] -public func xcorr(_ x: X) -> [Float] where X.Element == Float { - let resultSize = 2*numericCast(x.count) - 1 +/// Auto-correlation of a signal [lhs] +public func xcorr(_ lhs: L) -> [Float] where L.Element == Float { + let resultSize = 2*numericCast(lhs.count) - 1 var result = [Float](repeating: 0, count: resultSize) - let xPad = repeatElement(0 as Float, count: numericCast(x.count) - 1) + let xPad = repeatElement(0 as Float, count: numericCast(lhs.count) - 1) var xPadded = [Float]() - xPadded.reserveCapacity(xPad.count + numericCast(x.count) + xPad.count) + xPadded.reserveCapacity(xPad.count + numericCast(lhs.count) + xPad.count) xPadded.append(contentsOf: xPad) - xPadded.append(contentsOf: x) + xPadded.append(contentsOf: lhs) xPadded.append(contentsOf: xPad) - x.withUnsafeMemory { xm in + lhs.withUnsafeMemory { lm in result.withUnsafeMutableBufferPointer { rbp in - vDSP_conv(xPadded, 1, xm.pointer, numericCast(xm.stride), rbp.baseAddress!, 1, numericCast(resultSize), numericCast(xm.count)) + vDSP_conv(xPadded, 1, lm.pointer, numericCast(lm.stride), rbp.baseAddress!, 1, numericCast(resultSize), numericCast(lm.count)) } } return result } -/// Auto-correlation of a signal [x] -public func xcorr(_ x: X) -> [Double] where X.Element == Double { - let resultSize = 2*numericCast(x.count) - 1 +/// Auto-correlation of a signal [lhs] +public func xcorr(_ lhs: L) -> [Double] where L.Element == Double { + let resultSize = 2*numericCast(lhs.count) - 1 var result = [Double](repeating: 0, count: resultSize) - let xPad = repeatElement(0 as Double, count: numericCast(x.count) - 1) + let xPad = repeatElement(0 as Double, count: numericCast(lhs.count) - 1) var xPadded = [Double]() - xPadded.reserveCapacity(xPad.count + numericCast(x.count) + xPad.count) + xPadded.reserveCapacity(xPad.count + numericCast(lhs.count) + xPad.count) xPadded.append(contentsOf: xPad) - xPadded.append(contentsOf: x) + xPadded.append(contentsOf: lhs) xPadded.append(contentsOf: xPad) - x.withUnsafeMemory { xm in + lhs.withUnsafeMemory { lm in result.withUnsafeMutableBufferPointer { rbp in - vDSP_convD(xPadded, 1, xm.pointer, numericCast(xm.stride), rbp.baseAddress!, 1, numericCast(resultSize), numericCast(xm.count)) + vDSP_convD(xPadded, 1, lm.pointer, numericCast(lm.stride), rbp.baseAddress!, 1, numericCast(resultSize), numericCast(lm.count)) } }