示例#1
0
def idcst2(b):
    """ Takes iDST along y, then iDCT along x
    IN: b, the input 2D numpy array
    OUT: f, the 2D inverse-transformed array """
    M = b.shape[0]  # Number of rows
    N = b.shape[1]  # Number of columns
    a = np.zeros((M, N))  # Intermediate array
    f = np.zeros((M, N))  # Final array
    # Take inverse transform along y
    for i in range(M):
        a[i, :] = idst(b[i, :])
    # Take inverse transform along x
    for j in range(N):
        f[:, j] = idct(a[:, j])
    return f
示例#2
0
#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')
plt.legend(fontsize = 'x-small')
plt.savefig('dow2_dct.png',bbox_inches = 'tight')
plt.show()
示例#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()
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()
示例#5
0
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')
plt.plot(days, dow_filtered_2, label='Filtered (DCT)')
plt.legend(loc='upper left')
plt.title('DOW closing value with 2% filter')
plt.xlabel('Business days since start of the data stream')
plt.ylabel('DOW closing value (in points)')
plt.savefig('../images/1c_dct.png')
# 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")



示例#7
0
# 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)')
plt.ylabel('Closing value')
plt.plot(dow, label='Original data', linewidth=2)
plt.plot(dow_new, 'r', label='Smoothed data', linewidth=2)
plt.xlim(0, len(dow))
plt.legend(loc='best')
plt.show()
示例#8
0
# 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)')
plt.ylabel('Closing value')
plt.plot(dow,label = 'Original data',linewidth = 2)
plt.plot(dow_new,'r',label = 'Smoothed data',linewidth = 2)
plt.xlim(0,len(dow))
plt.legend(loc = 'best')
plt.show()