This commit is contained in:
2024-10-29 13:21:34 -05:00
parent 4dda811a1c
commit 16eaad818e
4 changed files with 97 additions and 39 deletions

View File

@@ -63,10 +63,25 @@ func stdev(_ data: [Double]) -> Double {
return sqrt(data.lazy.map { ($0 - mean) * ($0 - mean) }.reduce(0.0, +) / (n - 1))
}
let fftlib = SetupCache<Double>()
struct RRProcess : ~Copyable {
let nrr: Int
func rrprocess(params: Parameters, nrr: Int) -> [Double] {
let w1 = 2.0 * .pi * params.flo
let input: Buffer<Double>
let output: Buffer<Double>
let work: Buffer<Double>
let fft: FFTDouble
init(nrr: Int) {
self.nrr = nrr
self.input = Buffer<Double>(capacity: nrr + 2)
self.output = Buffer<Double>(capacity: nrr)
self.work = Buffer<Double>(capacity: nrr)
self.fft = try! FFTDouble.setup(for: nrr, type: .real)
}
func generate(params: Parameters) -> [Double] {
let w1 = 2.0 * .pi * params.flo
let w2 = 2.0 * .pi * params.fhi
let c1 = 2.0 * .pi * params.flostd
let c2 = 2.0 * .pi * params.fhistd
@@ -80,10 +95,6 @@ func rrprocess(params: Parameters, nrr: Int) -> [Double] {
let sf = Double(params.sfInternal)
let df = sf / Double(nrr)
let input = Buffer<Double>(capacity: (nrr + 2))
let output = Buffer<Double>(capacity: nrr)
input.withUnsafeMutableBufferPointer { swc in
for i in 0 ..< nrr / 2 + 1 {
let w = df * Double(i) * 2.0 * .pi
@@ -103,7 +114,7 @@ func rrprocess(params: Parameters, nrr: Int) -> [Double] {
swc[1] = swc[nrr]
}
try! fftlib.get(for: nrr, type: .real).fft(input: input, output: output, work: nil, sign: .backward)
self.fft.forward(input: input, output: output, work: nil, sign: .backward)
var rr = output.withUnsafeMutableBufferPointer { outptr in
return outptr.map { $0 * 1.0 / Double(nrr) }
@@ -116,6 +127,8 @@ func rrprocess(params: Parameters, nrr: Int) -> [Double] {
rr[i] = rr[i] * ratio + rrmean
}
return rr
}
}