We learned in the previous Time for action section how to detrend a signal. This detrended signal could have a cyclical component. Let's try to visualize this. Some of the steps are a repetition of steps in the previous Time for action section, such as downloading the data and setting up matplotlib
objects. These steps are omitted here.
amps = np.abs(fftpack.fftshift(fftpack.rfft(y)))
10
percent of the strongest component, throw it out:amps[amps < 0.1 * amps.max()] = 0
plt.plot(dates, y, 'o', label="detrended") plt.plot(dates, -fftpack.irfft(fftpack.ifftshift(amps)), label="filtered")
fig.autofmt_xdate() plt.legend(prop={'size':'x-large'})
ax2 = fig.add_subplot(212) N = len(qqq) plt.plot(np.linspace(-N/2, N/2, N), amps, label="transformed")
plt.legend(prop={'size':'x-large'}) plt.show()
The following plots are of the signal and frequency spectrum:
We detrended a signal and applied a simple filter on it using the scipy.fftpack
module (see frequencies.py
):
from matplotlib.finance import quotes_historical_yahoo from datetime import date import numpy as np from scipy import signal import matplotlib.pyplot as plt from scipy import fftpack from matplotlib.dates import DateFormatter from matplotlib.dates import DayLocator from matplotlib.dates import MonthLocator today = date.today() start = (today.year - 1, today.month, today.day) quotes = quotes_historical_yahoo("QQQ", start, today) quotes = np.array(quotes) dates = quotes.T[0] qqq = quotes.T[4] y = signal.detrend(qqq) alldays = DayLocator() months = MonthLocator() month_formatter = DateFormatter("%b %Y") fig = plt.figure() fig.subplots_adjust(hspace=.3) ax = fig.add_subplot(211) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_locator(months) ax.xaxis.set_major_formatter(month_formatter) # make font size bigger ax.tick_params(axis='both', which='major', labelsize='x-large') amps = np.abs(fftpack.fftshift(fftpack.rfft(y))) amps[amps < 0.1 * amps.max()] = 0 plt.title('Detrended and filtered signal') plt.plot(dates, y, 'o', label="detrended") plt.plot(dates, -fftpack.irfft(fftpack.ifftshift(amps)), label="filtered") fig.autofmt_xdate() plt.legend(prop={'size':'x-large'}) plt.grid() ax2 = fig.add_subplot(212) plt.title('Transformed signal') ax2.tick_params(axis='both', which='major', labelsize='x-large') N = len(qqq) plt.plot(np.linspace(-N/2, N/2, N), amps, label="transformed") plt.legend(prop={'size':'x-large'}) plt.grid() plt.tight_layout() plt.show()