Title: Scaling of `mlab.magnitude_spectrum()` is inconsistent · Issue #8417 · matplotlib/matplotlib · GitHub
Open Graph Title: Scaling of `mlab.magnitude_spectrum()` is inconsistent · Issue #8417 · matplotlib/matplotlib
X Title: Scaling of `mlab.magnitude_spectrum()` is inconsistent · Issue #8417 · matplotlib/matplotlib
Description: Compared to signal.welch() and mlab.psd(), the scaling of mlab.magnitude_spectrum() is not consistent. The normalization of the window seems to be missing, as the following example shows: import numpy as np import matplotlib.pyplot as pl...
Open Graph Description: Compared to signal.welch() and mlab.psd(), the scaling of mlab.magnitude_spectrum() is not consistent. The normalization of the window seems to be missing, as the following example shows: import nu...
X Description: Compared to signal.welch() and mlab.psd(), the scaling of mlab.magnitude_spectrum() is not consistent. The normalization of the window seems to be missing, as the following example shows: import nu...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/8417
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Scaling of `mlab.magnitude_spectrum()` is inconsistent","articleBody":"Compared to `signal.welch()` and `mlab.psd()`, the scaling of `mlab.magnitude_spectrum()` is not consistent. The normalization of the window seems to be missing, as the following example shows:\r\n\r\n\r\n\r\n import numpy as np\r\n import matplotlib.pyplot as plt\r\n from matplotlib import mlab\r\n from scipy import signal\r\n \r\n tau, n = 10, 2**10 # 10 second signal with 1024 points\r\n T = tau/n # sampling interval\r\n \r\n t = np.arange(n)*T\r\n x = 4*np.sin(40*np.pi*t) # 20 Hz sine\r\n w = signal.hanning(n, False)\r\n \r\n f = np.fft.fftfreq(n, T) # frequency slots\r\n df = f[1] - f[0]\r\n X = np.fft.fft(x*w)/w.sum() # Windowed FFT scaled to amplitude\r\n \r\n f_Pxx, Pxx = signal.welch(x, fs=1/T, window='hanning', nperseg=n, noverlap=0,\r\n detrend=False, return_onesided=False,\r\n scaling='spectrum')\r\n P_psd, f_psd = mlab.psd(x, NFFT=n, Fs=1/T, window=w,\r\n sides='twosided', scale_by_freq=False)\r\n \r\n # shift for plotting:\r\n f, X, f_Pxx, Pxx = (np.fft.fftshift(zz) for zz in (f, X, f_Pxx, Pxx))\r\n \r\n fg0, axx0 = plt.subplots(4, 1, sharex=True, num=1, figsize=(7, 7), clear=True)\r\n \r\n axx0[0].set_title(r\"FFT Magnitude Spectrum of $x(t)=4\\sin(40\\pi t)$\")\r\n axx0[0].plot(f, np.abs(X), 'C0.-')\r\n \r\n axx0[1].set_title(\"Scipy's Welch Algorithm\")\r\n axx0[1].plot(f_Pxx, np.sqrt(Pxx), \"C1.-\")\r\n \r\n axx0[2].set_title(\"psd()\")\r\n axx0[2].plot(f_psd, np.sqrt(P_psd), \"C2.-\")\r\n \r\n axx0[3].set_title(\"magnitude_spectrum()\")\r\n axx0[3].magnitude_spectrum(x, Fs=1/T, sides='twosided', color=\"C3\", marker='.',\r\n scale='linear')\r\n # Correct result:\r\n # axx0[3].magnitude_spectrum(x, Fs=1/T, sides='twosided', color=\"C3\", marker='.',\r\n # scale='linear', window=w/w.sum())\r\n \r\n axx0[-1].set_xlabel(r\"Frequency $f$\")\r\n axx0[0].set_xlim(19, 21)\r\n for ax in axx0:\r\n ax.grid(True)\r\n ax.set_ylabel(r\"$|X(f)|$\")\r\n fg0.tight_layout()\r\n plt.show()\r\n\r\nI tried fixing it, but I could find the time to fix `mlab._spectral_helper()` to perform consistently with `mlab.magnitude_spectrum()` and `mlab.spegram()`.\r\n\r\nPS: Tested on Matplotlib 2.0.0 (Anaconda Python 3.6 on Debian).","author":{"url":"https://github.com/DietBru","@type":"Person","name":"DietBru"},"datePublished":"2017-04-01T15:51:12.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/8417/matplotlib/issues/8417"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7949b818-2f96-e3f4-e7f3-ebb4d0a4b460 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8508:23E45A:BB5227:F76B84:6A518E57 |
| html-safe-nonce | d9be503fd965e44ba1710f2ff72913bbd72690c64f930ac94b3c9f72aa1c5b4d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NTA4OjIzRTQ1QTpCQjUyMjc6Rjc2Qjg0OjZBNTE4RTU3IiwidmlzaXRvcl9pZCI6IjIzMzc3NTQ5MDU3Njk1MTI1MzUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3f31e1e3cefea2e826f1cb26d801d36d7cd7ebf71a307196ec4303315d48964d |
| hovercard-subject-tag | issue:218704314 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/8417/issue_layout |
| twitter:image | https://opengraph.githubassets.com/11fb4f38994ec14b5ca56d838b28c9f287a62b1abec7b053e7e2042afdcfd123/matplotlib/matplotlib/issues/8417 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/11fb4f38994ec14b5ca56d838b28c9f287a62b1abec7b053e7e2042afdcfd123/matplotlib/matplotlib/issues/8417 |
| og:image:alt | Compared to signal.welch() and mlab.psd(), the scaling of mlab.magnitude_spectrum() is not consistent. The normalization of the window seems to be missing, as the following example shows: import nu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DietBru |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width