计算离散傅立叶变换


9

对任意长度的序列实施离散傅立叶变换(DFT)。这可以实现为函数或程序,并且序列可以作为参数或使用标准输入给出。

该算法将基于标准DFT向前计算结果。输入序列的长度为N,由组成[x(0), x(1), ..., x(N-1)]。输出序列将具有相同的长度和组成[X(0), X(1), ..., X(N-1)],其中每个X(k)由下面的关系定义。

DFT

规则

  • 这是 因此最短的解决方案将获胜。
  • 不允许在向前或向后(也称为反向)方向上计算DFT的内建函数。
  • 浮点数错误将不算在您身上。

测试用例

DFT([1, 1, 1, 1]) = [4, 0, 0, 0]
DFT([1, 0, 2, 0, 3, 0, 4, 0]) = [10, -2+2j, -2, -2-2j, 10, -2+2j, -2, -2-2j]
DFT([1, 2, 3, 4, 5]) = [15, -2.5+3.44j, -2.5+0.81j, -2.5-0.81j, -2.5-3.44j]
DFT([5-3.28571j, -0.816474-0.837162j, 0.523306-0.303902j, 0.806172-3.69346j, -4.41953+2.59494j, -0.360252+2.59411j, 1.26678+2.93119j] = [2, -3j, 5, -7j, 11, -13j, 17]

帮帮我

对于长度等于2的幂的序列,使用FFT算法查找DFT 一直是一个挑战。请记住,这一挑战不会限制您任何复杂性,还需要您的解决方案才能适用于任何长度的序列。

Answers:


2

果冻16 15 字节

LR’µ×'÷L-*²³÷S€

在线尝试!

怎么运行的

LR’µ×'÷L-*²³÷S€  Main link. Argument [x(0), ..., x(N-1)].

L                Length; yield N.
 R               Range; yield [1, ..., N].
  ’              Decrement; yield [0, ..., N-1].
   µ             Begin a new, monadic chain. Argument: [0, ..., N-1]
    ×'           Spawned multiply [0, ..., N-1] with itself, yielding the matrix
                 of all possible products k×n.
      ÷L         Divide each product by N.
        -*       Compute (-1)**(kn÷L) for each kn.
          ²      Square each result, computing (-1)**(2kn÷L).
           ³÷    Divide [x(0), ..., x(N-1)] by the results.
             S€  Compute the sum for each row, i.e., each X(k).


5

Python 3,77个字节

lambda x,e=enumerate:[sum(t/1j**(4*k*n/len(x))for n,t in e(x))for k,_ in e(x)]

Ideone上进行测试

该代码使用等效公式

式


哇,数字巨大。很高兴看到可以缩短代码的等效公式。
英里

4

J,30 20字节

3字节感谢@miles

利用事实e^ipi = -1

公式变为X_k = sum(x_n / ((-1)^(2nk/N)))

+/@:%_1^2**/~@i.@#%#

用法

>> DCT =: +/@:%_1^2**/~@i.@#%#
>> DCT 1 2 3 4 5
<< 15 _2.5j3.44095 _2.5j0.812299 _2.5j_0.812299 _2.5j_3.44095

>>STDIN和<<STDOUT 在哪里。

“浮点数错误将不算在您身上。”


3

MATL20 16字节

-1yn:qt!Gn/E*^/s

输入是列向量,即用分号替换逗号:

[1; 1; 1; 1]
[1; 0; 2; 0; 3; 0; 4; 0]
[1; 2; 3; 4; 5]
[5-3.28571j; -0.816474-0.837162j; 0.523306-0.303902j; 0.806172-3.69346j; -4.41953+2.59494j; -0.360252+2.59411j; 1.26678+2.93119j] 

这基于Leaky Nun的答案中的公式,基于exp()= -1 的事实,并且具有非整数指数的MATL幂运算产生(如大多数编程语言一样)主要分支结果。

在线尝试!

由于具有复数的Octave的怪异间距,实部和虚部之间都用空格隔开,结果矢量的不同条目也是如此。如果看起来太丑陋,请!在末尾添加一个(17个字节),以使输出的每个条目位于不同的行中。

说明

-1      % Push -1
y       % Get input implicitly. Push a copy below and one on top of -1
n:q     % Row vector [0 1 ... N-1] where N is implicit input length
t!      % Duplicate and transpose: column vector
Gn      % Push input length
/       % Divide
E       % Multiply by 2
*       % Multiply, element-wise with broadcast. Gives the exponent as a matrix
^       % Power (base is -1), element-wise. Gives a matrix
/       % Divide matrix by input (column vector), element-wise with broadcast
s       % Sum

2

珀斯,30岁

ms.e*b^.n1****c_2lQk.n0d.j0)QU

测试套件

很幼稚的方法,只是一个公式的实现。值遇到各种较小的浮点问题,这些值应该是加法反加,导致值略小于零。

奇怪的是.j,似乎没有参数就无法工作,但是我不确定是否正确使用了它。


1
恭喜10k !!
路易斯·门多



2

Python 2,78个字节

l=input();p=1
for _ in l:print reduce(lambda a,b:a*p+b,l)*p;p*=1j**(4./len(l))

针对的每个幂p对多项式求值1j**(4./len(l))

该表达式通过霍纳方法对由值reduce(lambda a,b:a*p+b,l)给出的多项式求lp

reduce(lambda a,b:a*10+b,[1,2,3,4,5])
=> 12345

除外,输入列表相反,结尾为常数。我们可以将其取反,但是因为p**len(l)==1对于傅立叶系数,我们可以使用p将整个结果乘以的方法p

一些相等长度的尝试:

l=input();i=0
for _ in l:print reduce(lambda a,b:(a+b)*1j**i,l,0);i+=4./len(l)

l=input();i=0
for _ in l:print reduce(lambda a,b:a*1j**i+b,l+[0]);i+=4./len(l)

作为增加1个字节的函数(79):

lambda l:[reduce(lambda a,b:a*1j**(i*4./len(l))+b,l+[0])for i in range(len(l))]

递归尝试(80):

f=lambda l,i=0:l[i:]and[reduce(lambda a,b:(a+b)*1j**(i*4./len(l)),l,0)]+f(l,i+1)

迭代模拟reduce(80):

l=input();p=1;N=len(l)
exec"s=0\nfor x in l:s=s*p+x\nprint s*p;p*=1j**(4./N);"*N


1

Python 2,89个字节

利用事实e^ipi = -1

公式变为X_k = sum(x_n / ((-1)^(2nk/N)))

lambda a:[sum(a[x]/(-1+0j)**(x*y*2./len(a))for x in range(len(a)))for y in range(len(a))]

伊迪恩!


将其发布为单独的答案!
Leaky Nun

好吧,如果你这么说。
丹尼斯


1

公理,81字节

g(x)==(#x<2=>x;[reduce(+,[x.j/%i^(4*k*(j-1)/#x)for j in 1..#x])for k in 0..#x-1])

使用有人在这里发布的公式。结果

(6) -> g([1,1,1,1])
   (6)  [4,0,0,0]
                                    Type: List Expression Complex Integer
(7) -> g([1,2,3,4])
   (7)  [10,- 2 + 2%i,- 2,- 2 - 2%i]
                                    Type: List Expression Complex Integer
(8) -> g([1,0,2,0,3,0,4,0])
   (8)  [10,- 2 + 2%i,- 2,- 2 - 2%i,10,- 2 + 2%i,- 2,- 2 - 2%i]
                                    Type: List Expression Complex Integer
(11) -> g([1,2,3,4,5])
   (11)
        5+--+4       5+--+3    5+--+2      5+--+
        \|%i   + 5%i \|%i   - 4\|%i   - 3%i\|%i  + 2
   [15, --------------------------------------------,
                           5+--+4
                           \|%i
    5+--+4       5+--+3    5+--+2      5+--+
    \|%i   + 3%i \|%i   - 5\|%i   - 2%i\|%i  + 4
    --------------------------------------------,
                       5+--+4
                       \|%i
    5+--+4       5+--+3    5+--+2      5+--+
    \|%i   + 4%i \|%i   - 2\|%i   - 5%i\|%i  + 3
    --------------------------------------------,
                       5+--+4
                       \|%i
    5+--+4       5+--+3    5+--+2      5+--+
    \|%i   + 2%i \|%i   - 3\|%i   - 4%i\|%i  + 5
    --------------------------------------------]
                       5+--+4
                       \|%i
                                    Type: List Expression Complex Integer
(12) -> g([1,2,3,4,5.])
   (12)
   [15.0, - 2.5 + 3.4409548011 779338455 %i, - 2.5 + 0.8122992405 822658154 %i,
    - 2.5 - 0.8122992405 822658154 %i, - 2.5 - 3.4409548011 779338455 %i]
                                      Type: List Expression Complex Float

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.