Blame view

Pods/Surge/Source/Convolution.swift 4.83 KB
f2cf74c7   yangbin   1.0.20(4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  // Copyright (c) 20142015 Mattt Thompson (http://mattt.me)
  // Copyright (c) 2015-2016 Remy Prechelt
  //
  // Permission is hereby granted, free of charge, to any person obtaining a copy
  // of this software and associated documentation files (the "Software"), to deal
  // in the Software without restriction, including without limitation the rights
  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  // copies of the Software, and to permit persons to whom the Software is
  // furnished to do so, subject to the following conditions:
  //
  // The above copyright notice and this permission notice shall be included in
  // all copies or substantial portions of the Software.
  //
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  // THE SOFTWARE.
  
  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: [Float], _ k: [Float]) -> [Float] {
      precondition(x.count >= k.count, "Input vector [x] must have at least as many elements as the kernel,  [k]")
      
      let resultSize = x.count + k.count - 1
      var result = [Float](repeating: 0, count: resultSize)
      let kEnd = UnsafePointer<Float>(k).advanced(by: k.count - 1)
      let xPad = repeatElement(Float(0.0), count: k.count-1)
      let xPadded = xPad + x + xPad
      vDSP_conv(xPadded, 1, kEnd, -1, &result, 1, vDSP_Length(resultSize), vDSP_Length(k.count))
  
      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: [Double], _ k: [Double]) -> [Double] {
      precondition(x.count >= k.count, "Input vector [x] must have at least as many elements as the kernel,  [k]")
      
      let resultSize = x.count + k.count - 1
      var result = [Double](repeating: 0, count: resultSize)
      let kEnd = UnsafePointer<Double>(k).advanced(by: k.count - 1)
      let xPad = repeatElement(Double(0.0), count: k.count-1)
      let xPadded = xPad + x + xPad
      vDSP_convD(xPadded, 1, kEnd, -1, &result, 1, vDSP_Length(resultSize), vDSP_Length(k.count))
      
      return result
  }
  
  // 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: [Float], _ y: [Float]) -> [Float] {
      precondition(x.count >= y.count, "Input vector [x] must have at least as many elements as [y]")
      var yPadded = y
      if x.count > y.count {
          let padding = repeatElement(Float(0.0), count: x.count - y.count)
          yPadded = y + padding
      }
      
      let resultSize = x.count + yPadded.count - 1
      var result = [Float](repeating: 0, count: resultSize)
      let xPad = repeatElement(Float(0.0), count: yPadded.count-1)
      let xPadded = xPad + x + xPad
      vDSP_conv(xPadded, 1, yPadded, 1, &result, 1, vDSP_Length(resultSize), vDSP_Length(yPadded.count))
      
      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: [Double], _ y: [Double]) -> [Double] {
      precondition(x.count >= y.count, "Input vector [x] must have at least as many elements as [y]")
      var yPadded = y
      if x.count > y.count {
          let padding = repeatElement(Double(0.0), count: x.count - y.count)
          yPadded = y + padding
      }
      
      let resultSize = x.count + yPadded.count - 1
      var result = [Double](repeating: 0, count: resultSize)
      let xPad = repeatElement(Double(0.0), count: yPadded.count-1)
      let xPadded = xPad + x + xPad
      vDSP_convD(xPadded, 1, yPadded, 1, &result, 1, vDSP_Length(resultSize), vDSP_Length(yPadded.count))
      
      return result
  }
  
  // MARK: Auto-correlation
  
  // Auto-correlation of a signal [x]
  public func xcorr(_ x: [Float]) -> [Float] {
      let resultSize = 2*x.count - 1
      var result = [Float](repeating: 0, count: resultSize)
      let xPad = repeatElement(Float(0.0), count: x.count-1)
      let xPadded = xPad + x + xPad
      vDSP_conv(xPadded, 1, x, 1, &result, 1, vDSP_Length(resultSize), vDSP_Length(x.count))
      
      return result
  }
  
  // Auto-correlation of a signal [x]
  public func xcorr(_ x: [Double]) -> [Double] {
      let resultSize = 2*x.count - 1
      var result = [Double](repeating: 0, count: resultSize)
      let xPad = repeatElement(Double(0.0), count: x.count-1)
      let xPadded = xPad + x + xPad
      vDSP_convD(xPadded, 1, x, 1, &result, 1, vDSP_Length(resultSize), vDSP_Length(x.count))
      
      return result
  }