Sunday, October 26, 2014

Audio Signal Processing for Music Applications / Week 2: Discrete Fourier transform

DFT
X[k]=N-1n=0x[n]e-j2πkn/Nk=0,...,N-1
n: discrete time index (normalized time, T=1)
k: discrete frequency index
ωk=2πk/N : frequency in radians
fk=fsk/N: frequency in Hz ( fs - sampling rate)



Complex DFT
X[k]=N-1n=0ej2πf0n+ϕe-j2πkn/Nk=0,...,N-1
ejϕ1-e-j2π(k/N-f0)N1-e-j2π(k/N-f0)

def DFT(x):
    """
    Input:
        x (numpy array) = input sequence of length N
    Output:
        The function should return a numpy array of length N
        X (numpy array) = The N point DFT of the input sequence x
    """
    ## Your code here
    N = len(x)
    r = -1j*2*np.pi/N
    iterable = (sum((x[n]*np.exp( r*n*k) for n in range(N) ), 0.0) for k in range(N))
    return np.fromiter( iterable, np.complex)

IDFT
x[n]=1NN-1k=0X[k]ej2πkn/Nn=0,...,N-1

def DFT(x):
    """
    Input:
        x (numpy array) = input sequence of length N
    Output:
        The function should return a numpy array of length N
        X (numpy array) = The N point DFT of the input sequence x
    """
    ## Your code here
    N = len(x)
    r = -1j*2*np.pi/N
    iterable = (sum((x[n]*np.exp( r*n*k) for n in range(N) ), 0.0) for k in range(N))
    return np.fromiter( iterable, np.complex)

IDFT for real signals
X[k]=|X[k]|ej<X[k] and X[-k]=|X[k]|e-j<X[k] for k=0,1,...N/2-1


def IDFT(X):
    """
    Input:
        X (numpy array) = frequency spectrum (length N)
    Output:
        The function should return a numpy array of length N 
        x (numpy array) = The N point IDFT of the frequency spectrum X
    """
    ## Your code here
    N = len(X)
    r = 1j*2*np.pi/N
    iterable = (sum((X[k]*np.exp( r*n*k) for k in range(N) ), 0.0)/N for n in range(N))
    return np.fromiter( iterable, np.complex)


Upd. https://github.com/MTG/sms-tools

No comments:

Post a Comment