Ejemplo n.º 1
0
def dcst2(f):
    """ Takes DCT along x, then DST along y
    IN: f, the input 2D numpy array
    OUT: b, the 2D transformed array """
    M = f.shape[0]  # Number of rows
    N = f.shape[1]  # Number of columns
    a = np.zeros((M, N))  # Intermediate array
    b = np.zeros((M, N))  # Final array
    # Take transform along x
    for j in range(N):
        a[:, j] = dct(f[:, j])
    # Take transform along y
    for i in range(M):
        b[i, :] = dst(a[i, :])
    return b
Ejemplo n.º 2
0
ft_inverse_2 = np.fft.irfft(fk_dow2)


#PLOTTING THE DATA FILE AS WELL AS THE FOURIER TRANSFORM WHERE LAST 90%  OF COEFFICIENTS IS SET TO ZERO
plt.xlabel('Time')
plt.ylabel(r'$f(t)$')
plt.title('DFT::Original data and last "98%" freq set to zero')
plt.plot(t_dow2,y_dow2,'b', label = 'original data')
plt.plot(t_dow2,ft_inverse_2,'r', label = 'Higher freq set to zero')
plt.legend(fontsize = 'x-small')
plt.savefig('dow2_dft.png',bbox_inches = 'tight')
plt.show()
plt.clf()

#DCT
fk_dct_dow2 = dct(y_dow2)

#SETTING THE LAST 98 % OF THE FOURIER COEFFIECIENTS TO ZERO
for i in range(len(freq_dow2)):
	if i >  (0.02 * len(freq_dow2)):
		fk_dct_dow2[i] = 0

ft_dct_inverse_2 = idct(fk_dct_dow2)


#PLOTTING THE DATA FILE AS WELL AS THE FOURIER TRANSFORM WHERE LAST 90%  OF COEFFICIENTS IS SET TO ZERO
plt.xlabel('Time')
plt.ylabel(r'$f(t)$')
plt.title('DCT::Original data and last "98%" freq set to zero')
plt.plot(t_dow2,y_dow2,'b', label = 'original data')
plt.plot(t_dow2,ft_dct_inverse_2,'r', label = 'Higher freq set to zero')
Ejemplo n.º 3
0
N=len(c)

for i in range(N//50,N):
    c[i]=0

y = np.fft.irfft(c)

plt.grid(True, which="both")
plt.plot(price,color='blue',label='Original data')
plt.plot(y,color='red',label='2% fourier coefficients with FFT')
plt.title('Dow Jones Industrial Average (2% fourier coefficients with FFT)')     
plt.gca().legend()
plt.savefig('dow_fft_2percent.png')
plt.show()

dct = dcst.dct(price)
N=len(price)

for i in range(N//50,N):
    dct[i]=0

y = dcst.idct(dct)

plt.grid(True, which="both")
plt.plot(price,color='blue',label='Original data')
plt.plot(y,color='red',label='2% fourier coefficients with DCT')
plt.title('Dow Jones Industrial Average (2% fourier coefficients with DCT)')     
plt.gca().legend()
plt.savefig('dow_dct_2percent.png')
plt.show()
Ejemplo n.º 4
0
y = np.loadtxt("dow2.txt")

plt.figure(1, figsize=(10, 4))
plt.plot(y, label='raw DJIA')
plt.grid()
plt.xlabel('Time (days)')
plt.ylabel('DJIA index')
plt.autoscale(enable=True, axis='x', tight=True)

fy = np.fft.rfft(y)
N_coeffs = len(fy)
fy_lopass = 1 * fy
fy_lopass[int(0.02 * N_coeffs):] = 0.
y_lopass = np.fft.irfft(fy_lopass)

plt.plot(y_lopass, '--', label='low-pass filtered')

# Q1cb -----------------------------------------------------------------------|
cty = dcst.dct(y)
cty_lopass = 1 * cty
cty_lopass[int(0.02 * N_coeffs):] = 0.
y_lopass_ct = dcst.idct(cty_lopass)

plt.plot(y_lopass_ct, ':', label='low-pass filtered, CT')

plt.legend()
plt.tight_layout()
plt.savefig('dow-nonperiodic.png', dpi=150)

# plt.show()
Ejemplo n.º 5
0
#plot both the original and filtered data
plt.figure(0)
plt.plot(days, dow, label='Original')
plt.plot(days, dow_filtered, label='Filtered (DFT)')
plt.legend(loc='upper left')
plt.title('DOW closing value raw and filtered')
plt.xlabel('Business days since start of the data stream')
plt.ylabel('DOW closing value (in points)')
plt.savefig('../images/1c_dft.png')

#-------------------------------------------------------------------
#part b

#compute dct
a = dct(dow)

#calculate the number of values of a to keep non-zero
keep_length_2 = round(keep_frac*len(a))

#create a new array from c with only the first designated fraction 
#non-zero
a_new = np.zeros(len(a), dtype=complex)
a_new[:keep_length_2] = a[:keep_length_2]

#compute inverse fourier transform of filtered c values
dow_filtered_2 = idct(a_new)

#plot both the original and filtered data
plt.figure(2)
plt.plot(days, dow, label='Original')

# plotting function of time
plt.figure(figsize=(10,5))
plt.plot(dow2, label='Original', alpha=0.7)
plt.plot(dow2_filtered, label='Filtered, 2 \%' )
plt.legend()
plt.xlabel("Time (days)")
plt.ylabel("Price")
plt.title("Daily closing values of the Dow Jones Index from 2004 to 2008")
plt.savefig("Lab05_Q1ci")


# we repeat the process but with the discrete cosine routine in dcst.py

c_dow2_cos = dcst.dct(dow2)
N = len(c_dow2_cos)
c_dow2_cos[int(N* 0.02):] = 0
dow2_cosfiltered = dcst.idct(c_dow2_cos)


plt.figure(figsize=(10,5))
plt.plot(dow2, label='Original', alpha=0.7)
plt.plot(dow2_cosfiltered, label='Filtered DCT, 2 \%' )
plt.legend()
plt.xlabel("Time (days)")
plt.ylabel("Price")
plt.title("Daily closing values of the Dow Jones Index from 2004 to 2008")
plt.savefig("Lab05_Q1cii")

Ejemplo n.º 7
0
import matplotlib.pyplot as plt
from dcst import dct, idct

###############################################################################

# This program finds the discrete cosine transform of a set of data, then discards
# all but the first 2% of coefficients to approximate the data

################################## LOAD DATA ##################################

dow = np.loadtxt('dow2.txt')

################################## MAIN PROGRAM ##################################

# Do discrete cosine transform
cks = dct(dow)

# Choose a percentage of coefficients to discard and find corresponding index
percent_discard = 0.02  # 2%
ind = int(percent_discard * len(cks))

# Create new coefficient array
cks_new = np.concatenate((cks[:ind], np.zeros(len(cks) - ind)))

# Find inverse Fourier transform to determine approximation
dow_new = idct(cks_new)

################################## PLOT ##################################

plt.title('Dow Jones Industrial Average')
plt.xlabel('Day (relative to start day)')
Ejemplo n.º 8
0
import matplotlib.pyplot as plt
from dcst import dct,idct

###############################################################################

# This program finds the discrete cosine transform of a set of data, then discards
# all but the first 2% of coefficients to approximate the data

################################## LOAD DATA ##################################

dow = np.loadtxt('dow2.txt')

################################## MAIN PROGRAM ##################################

# Do discrete cosine transform
cks = dct(dow)

# Choose a percentage of coefficients to discard and find corresponding index
percent_discard = 0.02 # 2%
ind = int(percent_discard*len(cks))

# Create new coefficient array
cks_new = np.concatenate((cks[:ind],np.zeros(len(cks)-ind)))

# Find inverse Fourier transform to determine approximation
dow_new = idct(cks_new)

################################## PLOT ##################################

plt.title('Dow Jones Industrial Average')
plt.xlabel('Day (relative to start day)')