首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >快速逆FFT (IFFT)的Chirp Z变换(CZT)

快速逆FFT (IFFT)的Chirp Z变换(CZT)
EN

Stack Overflow用户
提问于 2017-02-03 06:19:33
回答 1查看 1.3K关注 0票数 1

对于任意的样本大小(样本不等于2^N),我已经能够通过chirp变换实现快速傅立叶变换,使用iOS加速的快速傅立叶变换函数(该函数只适用于样本等于2^N)。

所得结果与Matlab FFT输出的任意长度序列(信号)相匹配。我粘贴下面的代码。

我的下一个挑战是使用iOS that的快速傅立叶变换函数(它只适用于样本等于2^N)来实现对任意样本大小(样本不等于2^N)的逆FFT。

由于我的CZT现在完成了任意长度的快速傅立叶变换(见下文),我希望逆CZT能够使用iOS的快速傅立叶变换函数(只适用于等于2^N的样本)实现任意长度的IFFT。

有什么建议/指导吗?

代码语言:javascript
复制
// FFT IOS ACCELERATE FRAMEWORK (works only for 2^N samples)
import Accelerate

public func fft(x: [Double], y: [Double], type: String) -> ([Double], [Double]) {

    var real = [Double](x)

    var imaginary = [Double](y)

    var splitComplex = DSPDoubleSplitComplex(realp: &real, imagp: &imaginary)

    let length = vDSP_Length(floor(log2(Float(real.count))))

    let radix = FFTRadix(kFFTRadix2)

    let weights = vDSP_create_fftsetupD(length, radix)

    switch type.lowercased() {

    case ("fft"): // CASE FFT
        vDSP_fft_zipD(weights!, &splitComplex, 1, length, FFTDirection(FFT_FORWARD))
        vDSP_destroy_fftsetup(weights)

    case ("ifft"): // CASE INVERSE FFT
        vDSP_fft_zipD(weights!, &splitComplex, 1, length, FFTDirection(FFT_INVERSE))
        vDSP_destroy_fftsetup(weights)
        real = real.map({ $0 / Double(x.count) }) // Normalize IFFT by sample count
        imaginary = imaginary.map({ $0 / Double(x.count) }) // Normalize IFFT by sample count

    default: // DEFAULT CASE (FFT)
        vDSP_fft_zipD(weights!, &splitComplex, 1, length, FFTDirection(FFT_FORWARD))
        vDSP_destroy_fftsetup(weights)
    }

    return (real, imaginary)
}

// END FFT IOS ACCELERATE FRAMEWORK (works only for 2^N samples)

// DEFINE COMPLEX NUMBERS
struct Complex<T: FloatingPoint> {
    let real: T
    let imaginary: T
    static func +(lhs: Complex<T>, rhs: Complex<T>) -> Complex<T> {
        return Complex(real: lhs.real + rhs.real, imaginary: lhs.imaginary + rhs.imaginary)
    }

    static func -(lhs: Complex<T>, rhs: Complex<T>) -> Complex<T> {
        return Complex(real: lhs.real - rhs.real, imaginary: lhs.imaginary - rhs.imaginary)
    }

    static func *(lhs: Complex<T>, rhs: Complex<T>) -> Complex<T> {
        return Complex(real: lhs.real * rhs.real - lhs.imaginary * rhs.imaginary,
                       imaginary: lhs.imaginary * rhs.real + lhs.real * rhs.imaginary)
    }
}

extension Complex: CustomStringConvertible {
    var description: String {
        switch (real, imaginary) {
        case (_, 0):
            return "\(real)"
        case (0, _):
            return "\(imaginary)i"
        case (_, let b) where b < 0:
            return "\(real) - \(abs(imaginary))i"
        default:
            return "\(real) + \(imaginary)i"
        }
    }
}

// DEFINE COMPLEX NUMBERS

// DFT BASED ON CHIRP Z TRANSFORM (CZT)
public func dft(x: [Double]) -> ([Double], [Double]) {

    let m = x.count // number of samples

    var N: [Double] = Array(stride(from: Double(0), through: Double(m - 1), by: 1.0))

    N = N.map({ $0 + Double(m) })

    var NM: [Double] = Array(stride(from: Double(-(m - 1)), through: Double(m - 1), by: 1.0))

    NM = NM.map({ $0 + Double(m) })

    var M: [Double] = Array(stride(from: Double(0), through: Double(m - 1), by: 1.0))

    M = M.map({ $0 + Double(m) })

    let nfft = Int(pow(2, ceil(log2(Double(m + m - 1))))) // fft pad

    var p1: [Double] = Array(stride(from: Double(-(m - 1)), through: Double(m - 1), by: 1.0))

    p1 = (zip(p1, p1).map(*)).map({ $0 / Double(2) }) // W = WR + j*WI has to be raised to power p1

    var WR = [Double]()
    var WI = [Double]()

    for i in 0 ..< p1.count { // Use De Moivre's formula to raise to power p1
        WR.append(cos(p1[i] * 2.0 * M_PI / Double(m)))
        WI.append(sin(-p1[i] * 2.0 * M_PI / Double(m)))
    }

    var aaR = [Double]()
    var aaI = [Double]()

    for j in 0 ..< N.count {
        aaR.append(WR[Int(N[j] - 1)] * x[j])
        aaI.append(WI[Int(N[j] - 1)] * x[j])
    }

    let la = nfft - aaR.count

    let pad: [Double] = Array(repeating: 0, count: la) // 1st zero padding

    aaR += pad

    aaI += pad

    let (fgr, fgi) = fft(x: aaR, y: aaI, type: "fft") // 1st FFT

    var bbR = [Double]()
    var bbI = [Double]()

    for k in 0 ..< NM.count {
        bbR.append((WR[Int(NM[k] - 1)]) / (((WR[Int(NM[k] - 1)])) * ((WR[Int(NM[k] - 1)])) + ((WI[Int(NM[k] - 1)])) * ((WI[Int(NM[k] - 1)])))) // take reciprocal
        bbI.append(-(WI[Int(NM[k] - 1)]) / (((WR[Int(NM[k] - 1)])) * ((WR[Int(NM[k] - 1)])) + ((WI[Int(NM[k] - 1)])) * ((WI[Int(NM[k] - 1)])))) // take reciprocal
    }

    let lb = nfft - bbR.count

    let pad2: [Double] = Array(repeating: 0, count: lb) // 2nd zero padding

    bbR += pad2

    bbI += pad2

    let (fwr, fwi) = fft(x: bbR, y: bbI, type: "fft") // 2nd FFT

    let fg = zip(fgr, fgi).map { Complex<Double>(real: $0, imaginary: $1) } // complexN 1

    let fw = zip(fwr, fwi).map { Complex<Double>(real: $0, imaginary: $1) } // complexN 2

    let cc = zip(fg, fw).map { $0 * $1 } // multiply above 2 complex numbers fg * fw

    var ccR = cc.map { $0.real } // real part (vector) of complex multiply

    var ccI = cc.map { $0.imaginary } // imag part (vector) of complex multiply

    let lc = nfft - ccR.count

    let pad3: [Double] = Array(repeating: 0, count: lc) // 3rd zero padding

    ccR += pad3

    ccI += pad3

    let (ggr, ggi) = fft(x: ccR, y: ccI, type: "ifft") // 3rd FFT (IFFT)

    var GGr = [Double]()
    var GGi = [Double]()
    var W2r = [Double]()
    var W2i = [Double]()

    for v in 0 ..< M.count {
        GGr.append(ggr[Int(M[v] - 1)])
        GGi.append(ggi[Int(M[v] - 1)])
        W2r.append(WR[Int(M[v] - 1)])
        W2i.append(WI[Int(M[v] - 1)])
    }

    let ggg = zip(GGr, GGi).map { Complex<Double>(real: $0, imaginary: $1) }

    let www = zip(W2r, W2i).map { Complex<Double>(real: $0, imaginary: $1) }

    let y = zip(ggg, www).map { $0 * $1 }

    let yR = y.map { $0.real } // FFT real part (output vector)

    let yI = y.map { $0.imaginary } // FFT imag part (output vector)

    return (yR, yI)
}

// END DFT BASED ON CHIRP Z TRANSFORM (CZT)

// CHIRP DFT (CZT) TEST
let x: [Double] = [1, 2, 3, 4, 5] // arbitrary sample size
let (fftR, fftI) = dft(x: x)
print("DFT Real Part:", fftR)
print(" ")
print("DFT Imag Part:", fftI)

// Matches Matlab FFT Output
// DFT Real Part: [15.0, -2.5000000000000018, -2.5000000000000013, -2.4999999999999991, -2.499999999999996]
// DFT Imag Part: [-1.1102230246251565e-16, 3.4409548011779334, 0.81229924058226477, -0.81229924058226599, -3.4409548011779356]

// END CHIRP DFT (CZT) TEST
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-03 14:21:56

把我的评论作为结束这个问题的答案-

如果您确定要使用ICZT作为IFFT的等价物,那么让您的dft函数接受与fft类似的type: String参数。当typeifft时,只需要在这里翻转标志:

WI.append(sin(-p1[i] * 2.0 * M_PI / Double(m)))

前向FFT为负值,逆FFT为正负。

下面是一些我编写的用于演示CZT:gist.github.com/fasiha/42a21405de92ea46f59e的Octave/Matlab代码。演示展示了如何使用czt2来执行fftczt2 (代码中称为w )的第三个参数是用于FFT的exp(-2j * pi / Nup)。只要把它和exp(+2j * pi / Nup)结合起来就可以得到IFFT了。

这就是在sin in WI中翻转标志的原因。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42017971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档