Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DFT matrix in python

Tags:

What's the easiest way to get the DFT matrix for 2-d DFT in python? I could not find such function in numpy.fft. Thanks!

like image 265
Uri Cohen Avatar asked Nov 02 '13 06:11

Uri Cohen


People also ask

How do you code DFT?

Then the basic DFT is given by the following formula: X(k)=n−1∑t=0x(t)e−2πitk/n. The interpretation is that the vector x represents the signal level at various points in time, and the vector X represents the signal level at various frequencies.

How do you calculate 2D DFT?

Length=P Length=Q Length=P+Q-1 For the convolution property to hold, M must be greater than or equal to P+Q-1. As in the 1D case, 2D-DFT, though a self-consistent transform, can be considered as a mean of calculating the transform of a 2D sampled signal defined over a discrete grid.


1 Answers

The easiest and most likely the fastest method would be using fft from SciPy.

import scipy as sp

def dftmtx(N):
    return sp.fft(sp.eye(N))

If you know even faster way (might be more complicated) I'd appreciate your input.

Just to make it more relevant to the main question - you can also do it with numpy:

import numpy as np

dftmtx = np.fft.fft(np.eye(N))

When I had benchmarked both of them I have an impression scipy one was marginally faster but I have not done it thoroughly and it was sometime ago so don't take my word for it.

Here's pretty good source on FFT implementations in python: http://nbviewer.ipython.org/url/jakevdp.github.io/downloads/notebooks/UnderstandingTheFFT.ipynb It's rather from speed perspective, but in this case we can actually see that sometimes it comes with simplicity too.

like image 82
pank Avatar answered Sep 23 '22 14:09

pank