mirror of
https://github.com/jkl1337/SwiftPFFFT.git
synced 2026-01-02 11:44:36 -06:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a0628a5725 | |||
| ab36848004 | |||
| 157b187116 | |||
| 01c48650e9 | |||
| f52a4147b8 |
30
.github/workflows/swift.yml
vendored
Normal file
30
.github/workflows/swift.yml
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
name: Swift CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
pull_request:
|
||||
branches: [master]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Run Unit Tests on macOS and Linux
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [macos-latest, ubuntu-latest]
|
||||
swift: ["5.9", "5.10", "6.0"]
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Set up Swift ${{ matrix.swift }}
|
||||
uses: NeedleInAJayStack/setup-swift@feat/swift-6
|
||||
with:
|
||||
swift-version: ${{ matrix.swift }}
|
||||
|
||||
- name: Build and Run Tests
|
||||
run: |
|
||||
swift build --enable-test-discovery
|
||||
swift test
|
||||
35
LICENSE.txt
35
LICENSE.txt
@@ -1,37 +1,18 @@
|
||||
Copyright (c) 2024 John K. Luebs
|
||||
Copyright (c) 2020 Dario Mambro ( dario.mambro@gmail.com )
|
||||
Copyright (c) 2019 Hayati Ayguen ( h_ayguen@web.de )
|
||||
Copyright (c) 2013 Julien Pommier ( pommier@modartt.com )
|
||||
|
||||
Copyright (c) 2020 Dario Mambro (dario.mambro@gmail.com)
|
||||
Copyright (c) 2019 Hayati Ayguen (h_ayguen@web.de)
|
||||
Copyright (c) 2013 Julien Pommier (pommier@modartt.com)
|
||||
Copyright (c) 2004 the University Corporation for Atmospheric
|
||||
Research ("UCAR"). All rights reserved. Developed by NCAR's
|
||||
Computational and Information Systems Laboratory, UCAR,
|
||||
www.cisl.ucar.edu.
|
||||
|
||||
Redistribution and use of the Software in source and binary forms,
|
||||
with or without modification, is permitted provided that the
|
||||
following conditions are met:
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
- Neither the names of NCAR's Computational and Information Systems
|
||||
Laboratory, the University Corporation for Atmospheric Research,
|
||||
nor the names of its sponsors or contributors may be used to
|
||||
endorse or promote products derived from this Software without
|
||||
specific prior written permission.
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
- Redistributions of source code must retain the above copyright
|
||||
notices, this list of conditions, and the disclaimer below.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
- Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions, and the disclaimer below in the
|
||||
documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS 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 CONTRIBUTORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
EXEMPLARY, OR CONSEQUENTIAL 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 WITH THE
|
||||
SOFTWARE.
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
{
|
||||
"originHash" : "d50b9049eb671b1ad14bc8ba592c78735f9e357a6b59835343af6e20e8be4701",
|
||||
"pins" : [
|
||||
{
|
||||
"identity" : "swift-numerics",
|
||||
@@ -11,5 +10,5 @@
|
||||
}
|
||||
}
|
||||
],
|
||||
"version" : 3
|
||||
"version" : 2
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// swift-tools-version: 6.0
|
||||
// swift-tools-version: 5.9
|
||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||
|
||||
import PackageDescription
|
||||
@@ -20,14 +20,15 @@ let package = Package(
|
||||
publicHeadersPath: "include",
|
||||
cSettings: [
|
||||
.define("PFFFT_SCALVEC_ENABLED", to: "1"),
|
||||
.define("PFFFT_ENABLE_NEON"),
|
||||
.define("_USE_MATH_DEFINES"),
|
||||
.define("NDEBUG"),
|
||||
.unsafeFlags(["-O3"]),
|
||||
]
|
||||
),
|
||||
.target(
|
||||
name: "PFFFT",
|
||||
dependencies: ["PFFFTLib", .product(name: "Numerics", package: "swift-numerics")]
|
||||
dependencies: ["PFFFTLib", .product(name: "Numerics", package: "swift-numerics")],
|
||||
swiftSettings: [.enableExperimentalFeature("AccessLevelOnImport")]
|
||||
),
|
||||
.testTarget(
|
||||
name: "PFFFTTests",
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
[](https://github.com/jkl1337/SwiftPFFFT/actions/workflows/swift.yml)
|
||||
|
||||
# SwiftPFFFT
|
||||
|
||||
Swift package providing a PFFFT (Pretty Fast, Fast Fourier Transform) library with wrapper.
|
||||
@@ -21,7 +23,7 @@ performance with much simpler usage and a permissive 3 clause BSD license.
|
||||
let fft = try FFT<Complex<Float>>(n: 16)
|
||||
let signal = fft.makeSignalBuffer()
|
||||
|
||||
signal.mutateEach { (i, v) in
|
||||
signal.mapInPlace { (i, v) in
|
||||
v = Complex(Float(i) + 1.0, Float(i) - 2.0)
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ public struct Buffer<T>: ~Copyable {
|
||||
try buffer.map(transform)
|
||||
}
|
||||
|
||||
@inlinable public func mutateEach(_ body: (Int, inout T) throws -> Void) rethrows {
|
||||
@inlinable public func mapInPlace(_ body: (Int, inout T) throws -> Void) rethrows {
|
||||
for i in 0 ..< buffer.count {
|
||||
try body(i, &buffer[i])
|
||||
}
|
||||
@@ -52,7 +52,7 @@ public protocol ComplexType {
|
||||
extension Complex: ComplexType {}
|
||||
|
||||
public extension Buffer where T: ComplexType {
|
||||
@inlinable public func mutateEachSwapLast(_ body: (Int, inout T) throws -> Void) rethrows {
|
||||
@inlinable func mapInPlaceSwapLast(_ body: (Int, inout T) throws -> Void) rethrows {
|
||||
for i in 0 ..< buffer.count {
|
||||
try body(i, &buffer[i])
|
||||
}
|
||||
|
||||
@@ -159,14 +159,20 @@ public struct FFT<T: FFTElement>: ~Copyable {
|
||||
|
||||
let ptr: OpaquePointer
|
||||
let n: Int
|
||||
let work: Buffer<ScalarType>?
|
||||
let work: Buffer<ScalarType>
|
||||
let setup: Setup
|
||||
|
||||
public init(setup: Setup) {
|
||||
self.setup = setup
|
||||
ptr = setup.ptr
|
||||
n = setup.n
|
||||
work = n > 4096 ? Buffer<ScalarType>(capacity: T.self == ComplexType.self ? 2 * n : n) : nil
|
||||
|
||||
let workCapacity = if n > 4096 {
|
||||
T.self == ComplexType.self ? 2 * n : n
|
||||
} else {
|
||||
0
|
||||
}
|
||||
work = Buffer(capacity: workCapacity)
|
||||
}
|
||||
|
||||
/// Initialize the FFT implementation with the given size and type.
|
||||
@@ -202,10 +208,11 @@ public struct FFT<T: FFTElement>: ~Copyable {
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
func toAddress(_ work: borrowing Buffer<ScalarType>?) -> UnsafeMutablePointer<ScalarType>? {
|
||||
switch work {
|
||||
case let .some(b): return b.baseAddress
|
||||
case .none: return nil
|
||||
var workPtr: UnsafeMutablePointer<ScalarType>? {
|
||||
if work.count > 0 {
|
||||
return work.baseAddress
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,12 +278,12 @@ public struct FFT<T: FFTElement>: ~Copyable {
|
||||
/// - sign: The direction of the FFT.
|
||||
public func forward(signal: borrowing Buffer<T>, spectrum: borrowing Buffer<ComplexType>) {
|
||||
checkFftBufferCounts(signal: signal, spectrum: spectrum)
|
||||
ScalarType.pffftTransformOrdered(ptr, rebind(signal), rebind(spectrum), toAddress(work), .forward)
|
||||
ScalarType.pffftTransformOrdered(ptr, rebind(signal), rebind(spectrum), workPtr, .forward)
|
||||
}
|
||||
|
||||
public func inverse(spectrum: borrowing Buffer<ComplexType>, signal: borrowing Buffer<T>) {
|
||||
checkFftBufferCounts(signal: signal, spectrum: spectrum)
|
||||
ScalarType.pffftTransformOrdered(ptr, rebind(spectrum), rebind(signal), toAddress(work), .backward)
|
||||
ScalarType.pffftTransformOrdered(ptr, rebind(spectrum), rebind(signal), workPtr, .backward)
|
||||
}
|
||||
|
||||
/// Perform a forward FFT on the input buffer, with implementation defined order.
|
||||
@@ -290,12 +297,12 @@ public struct FFT<T: FFTElement>: ~Copyable {
|
||||
/// - sign: The direction of the FFT.
|
||||
public func forwardToInternalLayout(signal: borrowing Buffer<T>, spectrum: borrowing Buffer<ScalarType>) {
|
||||
checkFftInternalLayoutBufferCounts(signal: signal, spectrum: spectrum)
|
||||
ScalarType.pffftTransform(ptr, rebind(signal), spectrum.baseAddress, toAddress(work), .forward)
|
||||
ScalarType.pffftTransform(ptr, rebind(signal), spectrum.baseAddress, workPtr, .forward)
|
||||
}
|
||||
|
||||
public func inverseFromInternalLayout(spectrum: borrowing Buffer<ScalarType>, signal: borrowing Buffer<T>) {
|
||||
checkFftInternalLayoutBufferCounts(signal: signal, spectrum: spectrum)
|
||||
ScalarType.pffftTransform(ptr, spectrum.baseAddress, rebind(signal), toAddress(work), .backward)
|
||||
ScalarType.pffftTransform(ptr, spectrum.baseAddress, rebind(signal), workPtr, .backward)
|
||||
}
|
||||
|
||||
public func reorder(spectrum: borrowing Buffer<ScalarType>, output: borrowing Buffer<ComplexType>) {
|
||||
|
||||
@@ -1,50 +1,51 @@
|
||||
import Testing
|
||||
import ComplexModule
|
||||
@testable import PFFFT
|
||||
import XCTest
|
||||
|
||||
@Test func fftFloat() async throws {
|
||||
final class FFTTests: XCTestCase {
|
||||
func testFftFloat() throws {
|
||||
let fft = try FFT<Complex<Float>>(n: 16)
|
||||
let signal = fft.makeSignalBuffer()
|
||||
let spectrum = fft.makeSpectrumBuffer()
|
||||
|
||||
let fft = try FFT<Complex<Float>>(n: 16)
|
||||
let signal = fft.makeSignalBuffer()
|
||||
let spectrum = fft.makeSpectrumBuffer()
|
||||
signal.mapInPlace { i, v in
|
||||
v = Complex(Float(i) + 1.0, Float(i) - 2.0)
|
||||
}
|
||||
|
||||
signal.mutateEach { (i, v) in
|
||||
v = Complex(Float(i) + 1.0, Float(i) - 2.0)
|
||||
}
|
||||
fft.forward(signal: signal, spectrum: spectrum)
|
||||
|
||||
fft.forward(signal: signal, spectrum: spectrum)
|
||||
let result = spectrum.map { $0 }
|
||||
let expected: [Complex<Float>] = [
|
||||
.init(136.0, 88.0),
|
||||
.init(-48.218716, 32.218716),
|
||||
.init(-27.31371, 11.313708),
|
||||
.init(-19.972847, 3.972846),
|
||||
.init(-16.0, 0.0),
|
||||
.init(-13.345428, -2.6545706),
|
||||
.init(-11.313709, -4.6862917),
|
||||
.init(-9.591298, -6.408703),
|
||||
.init(-8.0, -8.0),
|
||||
.init(-6.408703, -9.591298),
|
||||
.init(-4.6862917, -11.313708),
|
||||
.init(-2.6545706, -13.345429),
|
||||
.init(0.0, -16.0),
|
||||
.init(3.972845, -19.972847),
|
||||
.init(11.313707, -27.31371),
|
||||
.init(32.218716, -48.218716),
|
||||
]
|
||||
for (r, e) in zip(result, expected) {
|
||||
XCTAssert(r.isApproximatelyEqual(to: e))
|
||||
}
|
||||
|
||||
let result = spectrum.map { $0 }
|
||||
let expected: [Complex<Float>] = [
|
||||
.init(136.0, 88.0),
|
||||
.init(-48.218716, 32.218716),
|
||||
.init(-27.31371, 11.313708),
|
||||
.init(-19.972847, 3.972846),
|
||||
.init(-16.0, 0.0),
|
||||
.init(-13.345428, -2.6545706),
|
||||
.init(-11.313709, -4.6862917),
|
||||
.init(-9.591298, -6.408703),
|
||||
.init(-8.0, -8.0),
|
||||
.init(-6.408703, -9.591298),
|
||||
.init(-4.6862917, -11.313708),
|
||||
.init(-2.6545706, -13.345429),
|
||||
.init(0.0, -16.0),
|
||||
.init(3.972845, -19.972847),
|
||||
.init(11.313707, -27.31371),
|
||||
.init(32.218716, -48.218716),
|
||||
]
|
||||
zip(result, expected).forEach { r, e in
|
||||
#expect(r.isApproximatelyEqual(to: e))
|
||||
}
|
||||
fft.inverse(spectrum: spectrum, signal: signal)
|
||||
|
||||
fft.inverse(spectrum: spectrum, signal: signal)
|
||||
let signalResult = signal.map { $0 }
|
||||
let signalExpected = (0 ..< 16).map { i in
|
||||
Complex(Float(i) + 1.0, Float(i) - 2.0) * 16
|
||||
}
|
||||
|
||||
let signalResult = signal.map { $0 }
|
||||
let signalExpected = (0..<16).map { i in
|
||||
Complex(Float(i) + 1.0, Float(i) - 2.0) * 16
|
||||
}
|
||||
|
||||
zip(signalResult, signalExpected).forEach { r, e in
|
||||
#expect(r.isApproximatelyEqual(to: e))
|
||||
for (r, e) in zip(signalResult, signalExpected) {
|
||||
XCTAssert(r.isApproximatelyEqual(to: e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user