Skip to content

Commit d0fb574

Browse files
committed
Add chapter 10 code
1 parent de1e531 commit d0fb574

File tree

3 files changed

+503
-0
lines changed

3 files changed

+503
-0
lines changed

chapter10_signal/01_fourier.ipynb

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Analyzing the frequency components of a signal with a Fast Fourier Transform"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import datetime\n",
17+
"import numpy as np\n",
18+
"import scipy as sp\n",
19+
"import scipy.fftpack\n",
20+
"import pandas as pd\n",
21+
"import matplotlib.pyplot as plt\n",
22+
"%matplotlib inline"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 2,
28+
"metadata": {},
29+
"outputs": [],
30+
"source": [
31+
"df0 = pd.read_csv('https://github.com/ipython-books/'\n",
32+
" 'cookbook-2nd-data/blob/master/'\n",
33+
" 'weather.csv?raw=true',\n",
34+
" na_values=(-9999),\n",
35+
" parse_dates=['DATE'])"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 3,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"df = df0[df0['DATE'] >= '19940101']"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 4,
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"df.head()"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 5,
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"df_avg = df.dropna().groupby('DATE').mean()"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 6,
68+
"metadata": {},
69+
"outputs": [],
70+
"source": [
71+
"df_avg.head()"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 7,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"date = df_avg.index.to_datetime()\n",
81+
"temp = (df_avg['TMAX'] + df_avg['TMIN']) / 20.\n",
82+
"N = len(temp)"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 8,
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"fig, ax = plt.subplots(1, 1, figsize=(6, 3))\n",
92+
"temp.plot(ax=ax, lw=.5)\n",
93+
"ax.set_ylim(-10, 40)\n",
94+
"ax.set_xlabel('Date')\n",
95+
"ax.set_ylabel('Mean temperature')"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 9,
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"temp_fft = sp.fftpack.fft(temp)"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": 10,
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"temp_psd = np.abs(temp_fft) ** 2"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 11,
119+
"metadata": {},
120+
"outputs": [],
121+
"source": [
122+
"fftfreq = sp.fftpack.fftfreq(len(temp_psd), 1. / 365)"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 12,
128+
"metadata": {},
129+
"outputs": [],
130+
"source": [
131+
"i = fftfreq > 0"
132+
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"execution_count": 13,
137+
"metadata": {},
138+
"outputs": [],
139+
"source": [
140+
"fig, ax = plt.subplots(1, 1, figsize=(8, 4))\n",
141+
"ax.plot(fftfreq[i], 10 * np.log10(temp_psd[i]))\n",
142+
"ax.set_xlim(0, 5)\n",
143+
"ax.set_xlabel('Frequency (1/year)')\n",
144+
"ax.set_ylabel('PSD (dB)')"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 14,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": [
153+
"temp_fft_bis = temp_fft.copy()\n",
154+
"temp_fft_bis[np.abs(fftfreq) > 1.1] = 0"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": 15,
160+
"metadata": {},
161+
"outputs": [],
162+
"source": [
163+
"temp_slow = np.real(sp.fftpack.ifft(temp_fft_bis))"
164+
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": 16,
169+
"metadata": {},
170+
"outputs": [],
171+
"source": [
172+
"fig, ax = plt.subplots(1, 1, figsize=(6, 3))\n",
173+
"temp.plot(ax=ax, lw=.5)\n",
174+
"ax.plot_date(date, temp_slow, '-')\n",
175+
"ax.set_xlim(datetime.date(1994, 1, 1),\n",
176+
" datetime.date(2000, 1, 1))\n",
177+
"ax.set_ylim(-10, 40)\n",
178+
"ax.set_xlabel('Date')\n",
179+
"ax.set_ylabel('Mean temperature')"
180+
]
181+
}
182+
],
183+
"metadata": {},
184+
"nbformat": 4,
185+
"nbformat_minor": 2
186+
}

chapter10_signal/02_filter.ipynb

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Applying a linear filter to a digital signal"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import numpy as np\n",
17+
"import scipy as sp\n",
18+
"import scipy.signal as sg\n",
19+
"import pandas as pd\n",
20+
"import matplotlib.pyplot as plt\n",
21+
"%matplotlib inline"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 2,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"nasdaq_df = pd.read_csv(\n",
31+
" 'https://github.com/ipython-books/'\n",
32+
" 'cookbook-2nd-data/blob/master/'\n",
33+
" 'nasdaq.csv?raw=true', \n",
34+
" index_col='Date',\n",
35+
" parse_dates=['Date'])"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 3,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"nasdaq_df.head()"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 4,
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"date = nasdaq_df.index\n",
54+
"nasdaq = nasdaq_df['Close']"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 5,
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"fig, ax = plt.subplots(1, 1, figsize=(6, 4))\n",
64+
"nasdaq.plot(ax=ax, lw=1)"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 6,
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"# We get a triangular window with 60 samples.\n",
74+
"h = sg.get_window('triang', 60)\n",
75+
"# We convolve the signal with this window.\n",
76+
"fil = sg.convolve(nasdaq, h / h.sum())"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 7,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"fig, ax = plt.subplots(1, 1, figsize=(6, 4))\n",
86+
"# We plot the original signal...\n",
87+
"nasdaq.plot(ax=ax, lw=3)\n",
88+
"# ... and the filtered signal.\n",
89+
"ax.plot_date(date, fil[:len(nasdaq)],\n",
90+
" '-w', lw=2)"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": 8,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"fig, ax = plt.subplots(1, 1, figsize=(6, 4))\n",
100+
"nasdaq.plot(ax=ax, lw=3)\n",
101+
"# We create a 4-th order Butterworth low-pass filter.\n",
102+
"b, a = sg.butter(4, 2. / 365)\n",
103+
"# We apply this filter to the signal.\n",
104+
"ax.plot_date(date, sg.filtfilt(b, a, nasdaq),\n",
105+
" '-w', lw=2)"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 9,
111+
"metadata": {},
112+
"outputs": [],
113+
"source": [
114+
"fig, ax = plt.subplots(1, 1, figsize=(6, 4))\n",
115+
"nasdaq.plot(ax=ax, lw=1)\n",
116+
"b, a = sg.butter(4, 2 * 5. / 365, btype='high')\n",
117+
"ax.plot_date(date, sg.filtfilt(b, a, nasdaq),\n",
118+
" '-', lw=1)"
119+
]
120+
}
121+
],
122+
"metadata": {},
123+
"nbformat": 4,
124+
"nbformat_minor": 2
125+
}

0 commit comments

Comments
 (0)