Time for action – filtering a detrended signal

We learned in how to detrend a signal in the Time for action – detecting a trend in QQQ section. 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 tutorial, such as downloading the data and setting up Matplotlib objects. These steps are omitted here.

  1. Apply Fourier transforms, which will give us the frequency spectrum.
    amps = np.abs(fftpack.fftshift(fftpack.rfft(y)))
  2. Filter out the noise. Let’s say if the magnitude of a frequency component is below 10 percent of the strongest component, throw it out.
    amps[amps < 0.1 * amps.max()] = 0
  3. Transform the filtered signal back to the original domain and plot it together with the detrended signal.
    plt.plot(dates, y, ‘o’, label=”detrended”)
    plt.plot(dates, -fftpack.irfft(fftpack.ifftshift(amps)), label=”filtered”)
  4. Format the x-axis labels as dates and add a legend with extra large size.
    fig.autofmt_xdate()
    plt.legend(prop={‘size’:’x-large’})
  5. Add a second subplot and plot the frequency spectrum after filtering.
    ax2 = fig.add_subplot(212)
    N = len(qqq)
    plt.plot(np.linspace(-N/2, N/2, N), amps, label=”transformed”)
  6. Display the legend and plot.
    plt.legend(prop={‘size’:’x-large’})
    
    plt.show()

    The following plots are of the signal and frequency spectrum:

    Time for action – filtering a detrended signal

What just happened?

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.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)
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.show()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset