0% found this document useful (0 votes)
31 views

Py DTFT Num Sim2

This document contains Python code to perform discrete Fourier transforms. It defines functions for inner product of vectors and matrix multiplication of a vector by a matrix. It then generates sample time and frequency domain data, calculates the discrete time Fourier transform (DTFT) analytically, and uses the defined functions to calculate it numerically via a fast Fourier transform (FFT). Plots of the amplitude spectra from both methods are displayed for comparison.

Uploaded by

anish25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Py DTFT Num Sim2

This document contains Python code to perform discrete Fourier transforms. It defines functions for inner product of vectors and matrix multiplication of a vector by a matrix. It then generates sample time and frequency domain data, calculates the discrete time Fourier transform (DTFT) analytically, and uses the defined functions to calculate it numerically via a fast Fourier transform (FFT). Plots of the amplitude spectra from both methods are displayed for comparison.

Uploaded by

anish25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

# -*- coding: utf-8 -*"""

Created on Fri Dec 12 21:28:50 2014


@author: Anish
"""
import os
os.system('cls')
import numpy as np
def inner_prod(v1, v2):
'inner production of two vectors.'
sum = 0
for i in xrange(len(v2)):
sum += v1[i] * v2[i]
return sum
def matmult3(v, m):
'matrix multiply vector by inner production.'
return [inner_prod(v, r) for r in m]

M = 60
N = 500
nvec = np.linspace(0, N-1, num=N)
a = 0.5
x_n = (a)**nvec
plot(nvec,x_n,label='Function x[n]')
plt.xlabel('Time Index')
plt.ylabel('Function')
legend(bbox_to_anchor=(.5, .9), loc=1, borderaxespad=0.)
# DTFT
w = np.linspace(0,2*pi,num = M+1)
x_w = (1-a**(N+1)*exp(-1j*w*(N+1)))/(1-a*exp(-1j*w))
figure(2)
plot(w,np.abs(x_w),'o',label='Amplitude Spectrum 1')
#plot(w,np.angle(x_w),label='Phase Spectrum')
plt.xlabel('Digital Frequency (radians)')
#x_num = [0]*
kvec = np.linspace(0,M,M+1)
#ix = 0;
#for wx in w:
W = (exp(-1j*np.pi*2/M))**(np.outer(nvec,kvec))
#X_bar = np.transpose(x_n)*W
X_bar = matmult3(x_n,transpose(W))
#ix = ix +1;
mag_x = np.abs(X_bar)
plot(w,mag_x,label='Amplitude Spectrum 2')
legend(bbox_to_anchor=(.5, .9), loc=1, borderaxespad=0.)

You might also like