如何在Python中“手动”复制ifft2

问题描述 投票:0回答:0

我正在尝试找到一组 fft2 正弦曲线,其总和等于我的原始二维数据(完全实数)。我发现这个例子,对 fft 做同样的事情,非常有帮助(感谢@mark snyder)。

我注意到,如果我删除第二个维度,那么我的第一列总是匹配,这告诉我我离得不太远?最后,为什么我必须从 dim1 的输入中减去 1 来对齐相位?

这是我尝试过的:

`

# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
from scipy.fftpack import fftfreq, fft2
import cmath
df=pd.DataFrame(    { 'Ang':[  0.091, -0.141, -0.114], 
                      'Con':[ -0.139, -0.259, -0.573],
                      'EAC':[  0.016, -0.106, -0.044]   } ,
                       columns=['Ang', 'Con', 'EAC']      ,
                       index=[2011,2012,2013]               )

my_fft = fft2(df)
T      , B      =len(df)       , len(df.columns)
freqsT , freqsB = fftfreq(T,1) , fftfreq(B,1)
def ifft2Manually(option):
    recombine      = np.zeros((T,B)) #recombineine   
    for t in list(range(T)):   
        for b in list(range(B)):    
            coef  = abs(my_fft[t][b])/(T*B)
            tfreq , bfreq = freqsT[t] , freqsB[b]
            ps    = cmath.phase(my_fft[t][b])
            dim1 =  coef*np.cos(tfreq*2*np.pi*( np.array([df.index]).T - 1  )   + ps  )   
            dim2 =  coef*np.cos(bfreq*2*np.pi*( np.array(list(range(B)))    )   + ps  )
            if option==1:
                sinusoid = dim1 + dim2
            else: 
                sinusoid=  dim1 #exclude dim2 here 
            recombine=sinusoid +recombine
            #print('\nt={}\nb={}\ndim1={}\ndim2={}\nsinusoid={}'.format(t,b,dim1,dim2,sinusoid))
    if option==1:
        print('\n nothing matches when both dimensions used') 
    else: 
        print('\n first column matches when ONLY the first dimension (TIME) is used') 
    print('\n original data:\n{} \n\n my attempt to match:\n{}'.format(df, recombine )) 

ifft2Manually(option=1)
ifft2Manually(option=2)
`

输出:

`


 nothing matches when both dimensions used

 original data:
        Ang    Con    EAC
2011  0.091 -0.139  0.016
2012 -0.141 -0.259 -0.106
2013 -0.114 -0.573 -0.044 

 my attempt to match:
[[ 0.182 -0.048  0.107]
 [-0.05  -0.28  -0.125]
 [-0.023 -0.253 -0.098]]

 first column matches when ONLY the first dimension is used

 original data:
        Ang    Con    EAC
2011  0.091 -0.139  0.016
2012 -0.141 -0.259 -0.106
2013 -0.114 -0.573 -0.044 

 my attempt to match:
[[ 0.091  0.091  0.091]
 [-0.141 -0.141 -0.141]
 [-0.114 -0.114 -0.114]]

`

signal-processing fft
© www.soinside.com 2019 - 2024. All rights reserved.