什么是numpy.fft.rfft和numpy.fft.irfft及其在MATLAB中的等效代码 [英] What is numpy.fft.rfft and numpy.fft.irfft and its equivalent code in MATLAB

查看:1150
本文介绍了什么是numpy.fft.rfft和numpy.fft.irfft及其在MATLAB中的等效代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将python代码转换为MATLAB,其中一个代码使用numpy rfft.在numpy的文档中,它表示实际输入.

I am converting a python code into MATLAB and one of the code uses numpy rfft. In the documentation of numpy, it says real input.

计算一维离散傅里叶变换以进行实际输入.

Compute the one-dimensional discrete Fourier Transform for real input.

所以我在MATLAB中所做的是使用abs,但结果却不同.

So what I did in MATLAB is using abs but the results are different.

Python代码

ffta = np.fft.rfft(a) 

MATLAB代码

ffta = abs(fft(a));

我误解了什么?

推荐答案

numpy中的实数FFT使用以下事实:实数函数的傅立叶变换可以说是偏斜对称的",即频率上的值kk=1..N-1在频率N-k处的值的复共轭(正确的术语是 Hermitian ).因此,rfft仅返回结果中与非正频率相对应的部分.

The real FFT in numpy uses the fact that the fourier transform of a real valued function is so to say "skew-symmetric", that is the value at frequency k is the complex conjugate of the value at frequency N-k for k=1..N-1 (the correct term is Hermitian). Therefore rfft returns only the part of the result that corresponds to nonpositive frequences.

对于大小为N的输入,rfft函数返回FFT输出的与N/2或以下的频率相对应的部分.因此,如果N是偶数(所有频率从0N/2),rfft的输出大小是N/2+1;如果N是奇数(从0到(N+1)/2的大小>).观察到函数floor(n/2+1)返回偶数和奇数输入大小的正确输出大小.

For an input of size N the rfft function returns the part of the FFT output corresponding to frequences at or below N/2. Therefore the output of rfft is of size N/2+1 if N is even (all frequences from 0 to N/2), or (N+1)/2 if N is odd (all frequences from 0 to (N-1)/2). Observe that the function floor(n/2+1) returns the correct output size for both even and odd input sizes.

因此要在Matlab中重现rfft

So to reproduce rfft in matlab

function rfft = rfft(a)
     ffta = fft(a);
     rfft = ffta(1:(floor(length(ffta)/2)+1));
end

例如

a = [1,1,1,1,-1,-1,-1,-1];
rffta = rfft(a)

会产生

rffta =

 Columns 1 through 3:

   0.00000 + 0.00000i   2.00000 - 4.82843i   0.00000 + 0.00000i   

 Columns 4 through 5:

   2.00000 - 0.82843i   0.00000 + 0.00000i

现在将其与python进行比较

Now compare that with python

>>> np.fft.rfft(a)
array([ 0.+0.j        ,  2.-4.82842712j,  0.-0.j        ,  
        2.-0.82842712j,  0.+0.j        ])

重现罪魁祸首

要重现irfft的基本功能,您需要从rfft输出中恢复丢失的频率.如果所需的输出长度是偶数,则可以从输入长度计算出输出长度,如2 (m - 1).否则应为2 (m - 1) + 1.

Reproducing irfft

To reproduce basic functionality of irfft you need to recover the missing frequences from rfft output. If the desired output length is even, the output length can be computed from the input length as 2 (m - 1). Otherwise it should be 2 (m - 1) + 1.

以下代码将起作用.

function irfft = irfft(x,even=true)
     n = 0; % the output length
     s = 0; % the variable that will hold the index of the highest
            % frequency below N/2, s = floor((n+1)/2)
     if (even)
        n = 2 * (length(x) - 1 );
        s = length(x) - 1;
     else
        n = 2 * (length(x) - 1 )+1;
        s = length(x);
     endif
     xn = zeros(1,n);
     xn(1:length(x)) = x;
     xn(length(x)+1:n) = conj(x(s:-1:2));
     irfft  = ifft(xn);
end

现在您应该拥有

>> irfft(rfft(a))
ans =

   1.00000   1.00000   1.00000   1.00000  -1.00000  -1.00000  -1.00000  -1.00000

还有

abs( irfft(rfft(a)) - a ) < 1e-15

对于奇数输出长度,您会得到

For odd output length you get

>> irfft(rfft(a(1:7)),even=false)
ans =

   1.0000   1.0000   1.0000   1.0000  -1.0000  -1.0000  -1.0000

这篇关于什么是numpy.fft.rfft和numpy.fft.irfft及其在MATLAB中的等效代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆