Created
February 2, 2015 05:21
-
-
Save stormxuwz/9012ca4c74ac54e7a0a1 to your computer and use it in GitHub Desktop.
This is a Discrete Fourier Transformation calculation using matrix mutiplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def omega(N,k,n): | |
return np.exp(-2*np.pi/N*k*n*1j) | |
def hanWindow(N): | |
diag=np.array([np.sin(np.pi*i/(N-1))**2 for i in range(N)]) | |
H=np.diag(diag) | |
return H | |
def createF(N): | |
F=np.zeros((N,N),dtype=complex) | |
for i in range(N): | |
for j in range(N): | |
F[i,j]=omega(N,j,i) | |
return F | |
def createA(N,input_length,hop): | |
FH=np.dot(createF(N),hanWindow(N)) | |
A=np.zeros(((2*input_length-1)*N,input_length*N),dtype=complex) | |
for i in range(2*input_length-1): | |
A[i*N:(i+1)*N,i*hop:i*hop+N]=FH | |
return A | |
def DFT(N,hop,input_data): | |
''' | |
N: DFT window | |
hop: hop size | |
input_data: signal | |
''' | |
input_length=int(len(input_data)/N) | |
print "Using BIG matrix to do spectrogram analysis" | |
print "The size of input data is:",input_length*N | |
A=createA(N,input_length,hop) | |
plot_matrix(A) | |
DFT_coeff=np.dot(A,input_data[:input_length*N]) | |
spectrogram=[] | |
for i in range(2*input_length-1): | |
spectrogram.append(np.abs(DFT_coeff[i*N:i*N+N/2])) | |
spectrogram=np.array(spectrogram) | |
return spectrogram |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment