|
21 | 21 | "import scipy.signal as signal\n", |
22 | 22 | "import dask.array as dsar\n", |
23 | 23 | "import matplotlib.pyplot as plt\n", |
| 24 | + "\n", |
24 | 25 | "%matplotlib inline" |
25 | 26 | ] |
26 | 27 | }, |
|
46 | 47 | "metadata": {}, |
47 | 48 | "outputs": [], |
48 | 49 | "source": [ |
49 | | - "k0 = 1/0.52\n", |
50 | | - "T = 4.\n", |
| 50 | + "k0 = 1 / 0.52\n", |
| 51 | + "T = 4.0\n", |
51 | 52 | "dx = 0.02\n", |
52 | | - "x = np.arange(-2*T,2*T,dx) \n", |
53 | | - "y = np.cos(2*np.pi*k0*x) \n", |
54 | | - "y[np.abs(x)>T/2]=0.\n", |
55 | | - "da = xr.DataArray(y, dims=('x',), coords={'x':x})" |
| 53 | + "x = np.arange(-2 * T, 2 * T, dx)\n", |
| 54 | + "y = np.cos(2 * np.pi * k0 * x)\n", |
| 55 | + "y[np.abs(x) > T / 2] = 0.0\n", |
| 56 | + "da = xr.DataArray(y, dims=(\"x\",), coords={\"x\": x})" |
56 | 57 | ] |
57 | 58 | }, |
58 | 59 | { |
|
74 | 75 | } |
75 | 76 | ], |
76 | 77 | "source": [ |
77 | | - "fig, ax = plt.subplots(figsize=(12,4)) \n", |
| 78 | + "fig, ax = plt.subplots(figsize=(12, 4))\n", |
78 | 79 | "fig.set_tight_layout(True)\n", |
79 | | - "da.plot(ax=ax, marker='+', label='original signal') \n", |
80 | | - "ax.set_xlim([-8,8]);" |
| 80 | + "da.plot(ax=ax, marker=\"+\", label=\"original signal\")\n", |
| 81 | + "ax.set_xlim([-8, 8]);" |
81 | 82 | ] |
82 | 83 | }, |
83 | 84 | { |
|
95 | 96 | "metadata": {}, |
96 | 97 | "outputs": [], |
97 | 98 | "source": [ |
98 | | - "da_dft = xrft.dft(da, true_phase=True, true_amplitude=True) # Fourier Transform w/ consideration of phase\n", |
99 | | - "da_fft = xrft.fft(da) # Fourier Transform w/ numpy.fft-like behavior\n", |
| 99 | + "da_dft = xrft.dft(\n", |
| 100 | + " da, true_phase=True, true_amplitude=True\n", |
| 101 | + ") # Fourier Transform w/ consideration of phase\n", |
| 102 | + "da_fft = xrft.fft(da) # Fourier Transform w/ numpy.fft-like behavior\n", |
100 | 103 | "da_npft = npft.fft(da)" |
101 | 104 | ] |
102 | 105 | }, |
|
106 | 109 | "metadata": {}, |
107 | 110 | "outputs": [], |
108 | 111 | "source": [ |
109 | | - "k = da_dft.freq_x # wavenumber axis\n", |
110 | | - "TF_s = T/2*(np.sinc(T*(k-k0)) + np.sinc(T*(k+k0))) # Theoretical result of the Fourier transform" |
| 112 | + "k = da_dft.freq_x # wavenumber axis\n", |
| 113 | + "TF_s = (\n", |
| 114 | + " T / 2 * (np.sinc(T * (k - k0)) + np.sinc(T * (k + k0)))\n", |
| 115 | + ") # Theoretical result of the Fourier transform" |
111 | 116 | ] |
112 | 117 | }, |
113 | 118 | { |
|
129 | 134 | } |
130 | 135 | ], |
131 | 136 | "source": [ |
132 | | - "fig, (ax1,ax2) = plt.subplots(figsize=(12,8), nrows=2, ncols=1)\n", |
| 137 | + "fig, (ax1, ax2) = plt.subplots(figsize=(12, 8), nrows=2, ncols=1)\n", |
133 | 138 | "fig.set_tight_layout(True)\n", |
134 | 139 | "\n", |
135 | | - "(da_dft.real).plot(ax=ax1, linestyle='-', lw=3, c='k', label='phase preservation') \n", |
136 | | - "((da_fft*dx).real).plot(ax=ax1, linestyle='', marker='+',label='no phase preservation') \n", |
137 | | - "ax1.plot(k, (npft.fftshift(da_npft)*dx).real, linestyle='', marker='x',label='numpy fft')\n", |
138 | | - "ax1.plot(k, TF_s.real, linestyle='--', label='Theory')\n", |
139 | | - "ax1.set_xlim([-10,10])\n", |
140 | | - "ax1.set_ylim([-2,2])\n", |
| 140 | + "(da_dft.real).plot(ax=ax1, linestyle=\"-\", lw=3, c=\"k\", label=\"phase preservation\")\n", |
| 141 | + "((da_fft * dx).real).plot(\n", |
| 142 | + " ax=ax1, linestyle=\"\", marker=\"+\", label=\"no phase preservation\"\n", |
| 143 | + ")\n", |
| 144 | + "ax1.plot(\n", |
| 145 | + " k, (npft.fftshift(da_npft) * dx).real, linestyle=\"\", marker=\"x\", label=\"numpy fft\"\n", |
| 146 | + ")\n", |
| 147 | + "ax1.plot(k, TF_s.real, linestyle=\"--\", label=\"Theory\")\n", |
| 148 | + "ax1.set_xlim([-10, 10])\n", |
| 149 | + "ax1.set_ylim([-2, 2])\n", |
141 | 150 | "ax1.legend()\n", |
142 | | - "ax1.set_title('REAL PART')\n", |
| 151 | + "ax1.set_title(\"REAL PART\")\n", |
143 | 152 | "\n", |
144 | | - "(da_dft.imag).plot(ax=ax2, linestyle='-', lw=3, c='k', label='phase preservation') \n", |
145 | | - "((da_fft*dx).imag).plot(ax=ax2, linestyle='', marker='+', label='no phase preservation') \n", |
146 | | - "ax2.plot(k, (npft.fftshift(da_npft)*dx).imag, linestyle='', marker='x',label='numpy fft')\n", |
147 | | - "ax2.plot(k, TF_s.imag, linestyle='--', label='Theory')\n", |
148 | | - "ax2.set_xlim([-10,10])\n", |
149 | | - "ax2.set_ylim([-2,2])\n", |
| 153 | + "(da_dft.imag).plot(ax=ax2, linestyle=\"-\", lw=3, c=\"k\", label=\"phase preservation\")\n", |
| 154 | + "((da_fft * dx).imag).plot(\n", |
| 155 | + " ax=ax2, linestyle=\"\", marker=\"+\", label=\"no phase preservation\"\n", |
| 156 | + ")\n", |
| 157 | + "ax2.plot(\n", |
| 158 | + " k, (npft.fftshift(da_npft) * dx).imag, linestyle=\"\", marker=\"x\", label=\"numpy fft\"\n", |
| 159 | + ")\n", |
| 160 | + "ax2.plot(k, TF_s.imag, linestyle=\"--\", label=\"Theory\")\n", |
| 161 | + "ax2.set_xlim([-10, 10])\n", |
| 162 | + "ax2.set_ylim([-2, 2])\n", |
150 | 163 | "ax2.legend()\n", |
151 | | - "ax2.set_title('IMAGINARY PART');" |
| 164 | + "ax2.set_title(\"IMAGINARY PART\");" |
152 | 165 | ] |
153 | 166 | }, |
154 | 167 | { |
|
169 | 182 | "metadata": {}, |
170 | 183 | "outputs": [], |
171 | 184 | "source": [ |
172 | | - "ida_dft = xrft.idft(da_dft, true_phase=True, true_amplitude=True) # Signal in direct space \n", |
| 185 | + "ida_dft = xrft.idft(\n", |
| 186 | + " da_dft, true_phase=True, true_amplitude=True\n", |
| 187 | + ") # Signal in direct space\n", |
173 | 188 | "ida_fft = xrft.ifft(da_fft)" |
174 | 189 | ] |
175 | 190 | }, |
|
192 | 207 | } |
193 | 208 | ], |
194 | 209 | "source": [ |
195 | | - "fig, ax = plt.subplots(figsize=(12,4))\n", |
| 210 | + "fig, ax = plt.subplots(figsize=(12, 4))\n", |
196 | 211 | "fig.set_tight_layout(True)\n", |
197 | | - "ida_dft.real.plot(ax=ax, linestyle='-', c='k', lw=4, label='phase preservation')\n", |
198 | | - "ax.plot(x, ida_fft.real, linestyle='', marker='+', label='no phase preservation', alpha=.6) # w/out the phase information, the coordinates are lost\n", |
199 | | - "da.plot(ax=ax, ls='--', lw=3, label='original signal')\n", |
200 | | - "ax.plot(x, npft.ifft(da_npft).real, ls=':', label='inverse of numpy fft')\n", |
201 | | - "ax.set_xlim([-8,8])\n", |
202 | | - "ax.legend(loc='upper left');" |
| 212 | + "ida_dft.real.plot(ax=ax, linestyle=\"-\", c=\"k\", lw=4, label=\"phase preservation\")\n", |
| 213 | + "ax.plot(\n", |
| 214 | + " x, ida_fft.real, linestyle=\"\", marker=\"+\", label=\"no phase preservation\", alpha=0.6\n", |
| 215 | + ") # w/out the phase information, the coordinates are lost\n", |
| 216 | + "da.plot(ax=ax, ls=\"--\", lw=3, label=\"original signal\")\n", |
| 217 | + "ax.plot(x, npft.ifft(da_npft).real, ls=\":\", label=\"inverse of numpy fft\")\n", |
| 218 | + "ax.set_xlim([-8, 8])\n", |
| 219 | + "ax.legend(loc=\"upper left\");" |
203 | 220 | ] |
204 | 221 | }, |
205 | 222 | { |
|
235 | 252 | "metadata": {}, |
236 | 253 | "outputs": [], |
237 | 254 | "source": [ |
238 | | - "nshift = 70 # defining a shift\n", |
239 | | - "x0 = dx*nshift \n", |
240 | | - "nda = da.shift(x=nshift).dropna('x')" |
| 255 | + "nshift = 70 # defining a shift\n", |
| 256 | + "x0 = dx * nshift\n", |
| 257 | + "nda = da.shift(x=nshift).dropna(\"x\")" |
241 | 258 | ] |
242 | 259 | }, |
243 | 260 | { |
|
259 | 276 | } |
260 | 277 | ], |
261 | 278 | "source": [ |
262 | | - "fig, ax = plt.subplots(figsize=(12,4))\n", |
| 279 | + "fig, ax = plt.subplots(figsize=(12, 4))\n", |
263 | 280 | "fig.set_tight_layout(True)\n", |
264 | | - "da.plot(ax=ax, label='original (centered) signal') \n", |
265 | | - "nda.plot(ax=ax, marker='+', label='shifted signal', alpha=.6) \n", |
266 | | - "ax.set_xlim([-8,nda.x.max()])\n", |
| 281 | + "da.plot(ax=ax, label=\"original (centered) signal\")\n", |
| 282 | + "nda.plot(ax=ax, marker=\"+\", label=\"shifted signal\", alpha=0.6)\n", |
| 283 | + "ax.set_xlim([-8, nda.x.max()])\n", |
267 | 284 | "ax.legend();" |
268 | 285 | ] |
269 | 286 | }, |
|
280 | 297 | "metadata": {}, |
281 | 298 | "outputs": [], |
282 | 299 | "source": [ |
283 | | - "nda_dft = xrft.dft(nda, true_phase=True, true_amplitude=True) # Fourier Transform w/ phase preservation \n", |
284 | | - "nda_fft = xrft.fft(nda) # Fourier Transform w/out phase preservation\n", |
| 300 | + "nda_dft = xrft.dft(\n", |
| 301 | + " nda, true_phase=True, true_amplitude=True\n", |
| 302 | + ") # Fourier Transform w/ phase preservation\n", |
| 303 | + "nda_fft = xrft.fft(nda) # Fourier Transform w/out phase preservation\n", |
285 | 304 | "nda_npft = npft.fft(nda)" |
286 | 305 | ] |
287 | 306 | }, |
|
291 | 310 | "metadata": {}, |
292 | 311 | "outputs": [], |
293 | 312 | "source": [ |
294 | | - "nk = nda_dft.freq_x # wavenumber axis\n", |
295 | | - "TF_ns = T/2*(np.sinc(T*(nk-k0)) + np.sinc(T*(nk+k0)))*np.exp(-2j*np.pi*nk*x0) # Theoretical FT (Note the additional phase)" |
| 313 | + "nk = nda_dft.freq_x # wavenumber axis\n", |
| 314 | + "TF_ns = (\n", |
| 315 | + " T\n", |
| 316 | + " / 2\n", |
| 317 | + " * (np.sinc(T * (nk - k0)) + np.sinc(T * (nk + k0)))\n", |
| 318 | + " * np.exp(-2j * np.pi * nk * x0)\n", |
| 319 | + ") # Theoretical FT (Note the additional phase)" |
296 | 320 | ] |
297 | 321 | }, |
298 | 322 | { |
|
314 | 338 | } |
315 | 339 | ], |
316 | 340 | "source": [ |
317 | | - "fig, (ax1,ax2) = plt.subplots(figsize=(12,8), nrows=2, ncols=1)\n", |
| 341 | + "fig, (ax1, ax2) = plt.subplots(figsize=(12, 8), nrows=2, ncols=1)\n", |
318 | 342 | "fig.set_tight_layout(True)\n", |
319 | 343 | "\n", |
320 | | - "(nda_dft.real).plot(ax=ax1, linestyle='-', lw=3, c='k', label='phase preservation') \n", |
321 | | - "((nda_fft*dx).real).plot(ax=ax1, linestyle='', marker='+',label='no phase preservation')\n", |
322 | | - "ax1.plot(nk, (npft.fftshift(nda_npft)*dx).real, linestyle='', marker='x',label='numpy fft')\n", |
323 | | - "ax1.plot(nk, TF_ns.real, linestyle='--', label='Theory')\n", |
324 | | - "ax1.set_xlim([-10,10])\n", |
325 | | - "ax1.set_ylim([-2.,2])\n", |
| 344 | + "(nda_dft.real).plot(ax=ax1, linestyle=\"-\", lw=3, c=\"k\", label=\"phase preservation\")\n", |
| 345 | + "((nda_fft * dx).real).plot(\n", |
| 346 | + " ax=ax1, linestyle=\"\", marker=\"+\", label=\"no phase preservation\"\n", |
| 347 | + ")\n", |
| 348 | + "ax1.plot(\n", |
| 349 | + " nk, (npft.fftshift(nda_npft) * dx).real, linestyle=\"\", marker=\"x\", label=\"numpy fft\"\n", |
| 350 | + ")\n", |
| 351 | + "ax1.plot(nk, TF_ns.real, linestyle=\"--\", label=\"Theory\")\n", |
| 352 | + "ax1.set_xlim([-10, 10])\n", |
| 353 | + "ax1.set_ylim([-2.0, 2])\n", |
326 | 354 | "ax1.legend()\n", |
327 | | - "ax1.set_title('REAL PART')\n", |
| 355 | + "ax1.set_title(\"REAL PART\")\n", |
328 | 356 | "\n", |
329 | | - "(nda_dft.imag).plot(ax=ax2, linestyle='-', lw=3, c='k', label='phase preservation') \n", |
330 | | - "((nda_fft*dx).imag).plot(ax=ax2, linestyle='', marker='+', label='no phase preservation') \n", |
331 | | - "ax2.plot(nk, (npft.fftshift(nda_npft)*dx).imag, linestyle='', marker='x',label='numpy fft')\n", |
332 | | - "ax2.plot(nk, TF_ns.imag, linestyle='--', label='Theory')\n", |
333 | | - "ax2.set_xlim([-10,10])\n", |
334 | | - "ax2.set_ylim([-2.,2.])\n", |
| 357 | + "(nda_dft.imag).plot(ax=ax2, linestyle=\"-\", lw=3, c=\"k\", label=\"phase preservation\")\n", |
| 358 | + "((nda_fft * dx).imag).plot(\n", |
| 359 | + " ax=ax2, linestyle=\"\", marker=\"+\", label=\"no phase preservation\"\n", |
| 360 | + ")\n", |
| 361 | + "ax2.plot(\n", |
| 362 | + " nk, (npft.fftshift(nda_npft) * dx).imag, linestyle=\"\", marker=\"x\", label=\"numpy fft\"\n", |
| 363 | + ")\n", |
| 364 | + "ax2.plot(nk, TF_ns.imag, linestyle=\"--\", label=\"Theory\")\n", |
| 365 | + "ax2.set_xlim([-10, 10])\n", |
| 366 | + "ax2.set_ylim([-2.0, 2.0])\n", |
335 | 367 | "ax2.legend()\n", |
336 | | - "ax2.set_title('IMAGINARY PART');" |
| 368 | + "ax2.set_title(\"IMAGINARY PART\");" |
337 | 369 | ] |
338 | 370 | }, |
339 | 371 | { |
|
356 | 388 | "metadata": {}, |
357 | 389 | "outputs": [], |
358 | 390 | "source": [ |
359 | | - "inda_dft = xrft.idft(nda_dft, true_phase=True, true_amplitude=True) # Signal in direct space \n", |
| 391 | + "inda_dft = xrft.idft(\n", |
| 392 | + " nda_dft, true_phase=True, true_amplitude=True\n", |
| 393 | + ") # Signal in direct space\n", |
360 | 394 | "inda_fft = xrft.ifft(nda_fft)" |
361 | 395 | ] |
362 | 396 | }, |
|
379 | 413 | } |
380 | 414 | ], |
381 | 415 | "source": [ |
382 | | - "fig, ax = plt.subplots(figsize=(12,4))\n", |
| 416 | + "fig, ax = plt.subplots(figsize=(12, 4))\n", |
383 | 417 | "fig.set_tight_layout(True)\n", |
384 | | - "inda_dft.real.plot(ax=ax, linestyle='-', c='k', lw=4, label='phase preservation')\n", |
385 | | - "ax.plot(x[:len(inda_fft.real)], inda_fft.real, linestyle='', marker='o', alpha=.7, \n", |
386 | | - " label='no phase preservation (w/out shifting)')\n", |
387 | | - "ax.plot(x[nshift:], inda_fft.real, linestyle='', marker='+', label='no phase preservation')\n", |
388 | | - "nda.plot(ax=ax, ls='--', lw=3, label='original signal')\n", |
389 | | - "ax.plot(x[nshift:], npft.ifft(nda_npft).real, ls=':', label='inverse of numpy fft')\n", |
390 | | - "ax.set_xlim([nda.x.min(),nda.x.max()])\n", |
391 | | - "ax.legend(loc='upper left');" |
| 418 | + "inda_dft.real.plot(ax=ax, linestyle=\"-\", c=\"k\", lw=4, label=\"phase preservation\")\n", |
| 419 | + "ax.plot(\n", |
| 420 | + " x[: len(inda_fft.real)],\n", |
| 421 | + " inda_fft.real,\n", |
| 422 | + " linestyle=\"\",\n", |
| 423 | + " marker=\"o\",\n", |
| 424 | + " alpha=0.7,\n", |
| 425 | + " label=\"no phase preservation (w/out shifting)\",\n", |
| 426 | + ")\n", |
| 427 | + "ax.plot(\n", |
| 428 | + " x[nshift:], inda_fft.real, linestyle=\"\", marker=\"+\", label=\"no phase preservation\"\n", |
| 429 | + ")\n", |
| 430 | + "nda.plot(ax=ax, ls=\"--\", lw=3, label=\"original signal\")\n", |
| 431 | + "ax.plot(x[nshift:], npft.ifft(nda_npft).real, ls=\":\", label=\"inverse of numpy fft\")\n", |
| 432 | + "ax.set_xlim([nda.x.min(), nda.x.max()])\n", |
| 433 | + "ax.legend(loc=\"upper left\");" |
392 | 434 | ] |
393 | 435 | }, |
394 | 436 | { |
|
1645 | 1687 | } |
1646 | 1688 | ], |
1647 | 1689 | "source": [ |
1648 | | - "fig, (ax1,ax2) = plt.subplots(figsize=(12,4), nrows=1, ncols=2)\n", |
| 1690 | + "fig, (ax1, ax2) = plt.subplots(figsize=(12, 4), nrows=1, ncols=2)\n", |
1649 | 1691 | "da.isel(time=0).plot(ax=ax1)\n", |
1650 | 1692 | "Fda_1.real.plot(ax=ax2)" |
1651 | 1693 | ] |
|
0 commit comments